美文网首页iOS/MacOS开发
初级_第三方iOS·Charts图表

初级_第三方iOS·Charts图表

作者: 求长生 | 来源:发表于2018-01-05 16:26 被阅读3294次

可以绘制柱状图、折线图、K线图、饼状图等Charts,且和Android一致的图表界面MPAndroidChart

一、下载Charts框架

下载地址:https://github.com/danielgindi/Charts.
解压后的文件夹里面的内容是这个样子的,如下图:

111.png
下载版本,如下图:
222.png

二、导入Charts.xcodeproj工程

手动集成
1.拖拽 Charts.xcodeproj 文件到你的工程中,Source文件夹拖拽到同文件夹下单不引用进工程。
2.在 TARGETS —> General —> Embedded Binaries 下,点击 + 按钮,选择 Charts.framework (重要*动态库添加,不添加真机运行报错)
3.Clean一下,编译一下
4.设置编译选项,找到Build Settings -> Embedded Content Contains Swift Code 选项,设置为Yes,Build Settings -> Always Embed Swift Standard Libraries 选项,设置为Yes,如下图:


333.png

5.如果需要在 ObjC 的工程中使用 Swift 文件,你需要导入 Bridging Header 文件。通常 Bridging Header 文件被命名为 “YourProject-Swift.h”。
6.在使用图表的文件中要写上的Swift头文件,@import Charts ;

三、添加测试代码

import "MyViewController.h"

@import Charts;
@interface MyViewController
@property (strong, nonatomic)PieChartView * pieChartView;
@end
@implementation MyViewController
-(void)creasePieChartView{
self.pieChartView = [[PieChartView alloc] initWithFrame:CGRectMake(0, 50, _View.frame.size.width- 10, _View.frame.size.height - 50)];
// self.pieChartView.backgroundColor = [UIColor lightGrayColor];
[_View addSubview:self.pieChartView];
[self.pieChartView setExtraOffsetsWithLeft:5 top:10 right:70 bottom:5];//饼状图距离边缘的间隙
self.pieChartView.usePercentValuesEnabled = YES;//是否根据所提供的数据, 将显示数据转换为百分比格式
self.pieChartView.dragDecelerationEnabled = YES;//拖拽饼状图后是否有惯性效果
self.pieChartView.drawSlicesUnderHoleEnabled= NO;//
self.pieChartView.chartDescription.enabled = NO;//饼状图描述
self.pieChartView.drawHoleEnabled = YES;//饼状图是否是空心
self.pieChartView.holeRadiusPercent = 0.5;//空心半径占比
self.pieChartView.holeColor = [UIColor clearColor];//空心颜色
self.pieChartView.transparentCircleRadiusPercent = 0.55;//半透明空心半径占比
self.pieChartView.transparentCircleColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.3];//半透明空心的颜色
if (self.pieChartView.isDrawHoleEnabled == YES) {
self.pieChartView.drawCenterTextEnabled = YES;//是否显示中间文字
//普通文本
// self.pieChartView.centerText = @"饼状图";//中间文字
//富文本
NSMutableAttributedString *centerText = [[NSMutableAttributedString alloc] initWithString:@"饼状图bbb\n饼状图aaa"];
[centerText setAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:16],
NSForegroundColorAttributeName: [UIColor orangeColor]}
range:NSMakeRange(0, centerText.length)];
self.pieChartView.centerAttributedText = centerText;
}
self.pieChartView.legend.maxSizePercent = 1;//图例在饼状图中的大小占比, 这会影响图例的宽高
self.pieChartView.legend.formToTextSpace = 5;//文本间隔
self.pieChartView.legend.font = [UIFont systemFontOfSize:10];//字体大小
self.pieChartView.legend.textColor = [UIColor grayColor];//字体颜色
// self.pieChartView.legend.position = ChartLegendPositionBelowChartCenter;//图例在饼状图中的位置
self.pieChartView.legend.horizontalAlignment = ChartLegendHorizontalAlignmentRight;
self.pieChartView.legend.verticalAlignment = ChartLegendVerticalAlignmentTop;
self.pieChartView.legend.orientation = ChartLegendOrientationVertical;
self.pieChartView.legend.form = ChartLegendFormCircle;//图示样式: 方形、线条、圆形
self.pieChartView.legend.formSize = 8;//图示大小
[self.pieChartView setRotationEnabled:false];//禁止拖拽
[self setData];
}

  • (PieChartData *)setData{
    //每个区块的数据
    //每个区块的名称或描述
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    NSMutableArray *nameArr = [[NSMutableArray alloc]init];
    for (pieChart *model in _pieCharModel.pieChart) {
    [arr addObject:model.totalamt];//@[@"8.54",@"3.26",@"2"];
    [nameArr addObject:model.typename];
    }
    NSMutableArray *values = [[NSMutableArray alloc] init];
    // IMPORTANT: In a PieChart, no values (Entry) should have the same xIndex (even if from different DataSets), since no values can be drawn above each other.
    for (int i = 0; i < arr.count; I++)
    {
    NSString * aaa = arr[i];
    double bb = aaa.doubleValue;
    [values addObject:[[PieChartDataEntry alloc] initWithValue: bb label: nameArr[i]]];
    }
    //dataSet
    PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithValues:values label:@"aaaf"];
    dataSet.drawValuesEnabled = YES;//是否绘制显示数据
    NSMutableArray *colors = [[NSMutableArray alloc] init];
    [colors addObjectsFromArray:ChartColorTemplates.vordiplom];
    [colors addObjectsFromArray:ChartColorTemplates.joyful];
    [colors addObjectsFromArray:ChartColorTemplates.colorful];
    [colors addObjectsFromArray:ChartColorTemplates.liberty];
    [colors addObjectsFromArray:ChartColorTemplates.pastel];
    [colors addObject:[UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f]];
    dataSet.colors = colors;//区块颜色
    dataSet.sliceSpace = 5;//相邻区块之间的间距
    dataSet.selectionShift = 8;//选中区块时, 放大的半径
    dataSet.xValuePosition = PieChartValuePositionInsideSlice;//名称位置
    dataSet.yValuePosition = PieChartValuePositionOutsideSlice;//数据位置
    //数据与区块之间的用于指示的折线样式
    dataSet.valueLinePart1OffsetPercentage = 0.85;//折线中第一段起始位置相对于区块的偏移量, 数值越大, 折线距离区块越远
    dataSet.valueLinePart1Length = 0.5;//折线中第一段长度占比
    dataSet.valueLinePart2Length = 0.4;//折线中第二段长度最大占比
    dataSet.valueLineWidth = 1;//折线的粗细
    dataSet.valueLineColor = [UIColor brownColor];//折线颜色
    //data
    PieChartData *data = [[PieChartData alloc] initWithDataSet:dataSet];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterPercentStyle;
    formatter.maximumFractionDigits = 0;//小数位数
    formatter.multiplier = @1.f;
    [data setValueFormatter:[[ChartDefaultValueFormatter alloc] initWithFormatter:formatter]];//设置显示数据格式
    [data setValueTextColor:[UIColor brownColor]];
    [data setValueFont:[UIFont systemFontOfSize:10]];
    self.pieChartView.data = data;
    [ self.pieChartView setNeedsDisplay];
    return data;
    }
    @end

*问题
dyld: Library not loaded: @rpath/Charts.framework/Charts
Referenced from: /var/containers/Bundle/Application/FFFFESFEF24432FFFRRER342422HHWH/test.app/test
Reason: image not found
动态库未链接到项目内
解决办法:
在 TARGETS —> General —> Embedded Binaries 下,点击 + 按钮,选择 Charts.framework就可以解决问题

相关文章

网友评论

    本文标题:初级_第三方iOS·Charts图表

    本文链接:https://www.haomeiwen.com/subject/nxksnxtx.html