Charts分析及使用

1.Charts源码分析

1.1 文件组结构
1.2 ChartView类型图表简析

BarLineChartViewBase与PieRadarChartViewBase共同点为均有横坐标xAxis区别在于BarLineChartViewBase有YAlxis纵坐标


Charts分析及使用_第2张图片
image.png
1.3 数据源

ChartDataEntry组成ChartDataSet,多组ChartDataSet构成ChartData。

1.4 渲染绘制过程

设置了chartView的data之后,在set方法里调用dataChanged的通知notifyDataSetChanged(),这个方法的具体实现是在每个子类的ChartView中自己实现,主要做的事情是:

1> 重新计算边界值和offset偏移值
2> 调用setNeedsDisplay(),触发视图界面的刷新
3> 在ChartView的几类和子类的drawRect方法中进行界面渲染:
i>. 获取当前的CGContext
ii>. 绘制横纵坐标轴
iii>. 绘制数据(我们提供的数据源)
iv>. 绘制额外的补充数据
v>. 绘制图例(实际场景中饼图必须,其他图按需)
vi>. 绘制description(描述语)

2.使用场景

使用Charts框架可以实现折线图、柱形图、折线图、散点图、饼图等,较常用的为折线图、柱形图。

3.基本用法 以柱状图为例

3.1 创建ChartView表格
    self.barChartView = [[BarChartView alloc] initWithFrame:CGRectMake(10, 300, self.view.frame.size.width - 20, 158)];
    self.barChartView.delegate = self;//设置代理
    [self.view addSubview:self.barChartView];
3.2 基本样式 (按需设置)
    self.barChartView.backgroundColor = [UIColor colorWithRed:230/255.0f green:253/255.0f blue:253/255.0f alpha:1];
    self.barChartView.noDataText = @"暂无数据";//没有数据时的文字提示
    self.barChartView.drawValueAboveBarEnabled = YES;//数值显示在柱形的上面还是下面
    self.barChartView.drawBarShadowEnabled = NO;//是否绘制柱形的阴影背景
    self.barChartView.rightAxis.enabled = NO;//不绘制右边轴
    self.barChartView.leftAxis.enabled = NO;//不绘制左边轴
    self.barChartView.xAxis.enabled = NO;//不绘制X轴
    self.barChartView.legend.enabled = NO;//不显示图例说明
    self.barChartView.descriptionText = @"";//右下角的description文字样式不显示,就设为空字符串即
  /*
     //X轴样式
     ChartXAxis *xAxis = self.barChartView.xAxis;
     xAxis.axisLineWidth = 1;//设置X轴线宽
     xAxis.labelPosition = XAxisLabelPositionBottom;//X轴的显示位置,默认是显示在上面的
     xAxis.drawGridLinesEnabled = NO;//不绘制网格线
     xAxis.labelTextColor = [UIColor brownColor];//label文字颜色
     
    //左边Y轴样式
    ChartYAxis *leftAxis = self.barChartView.leftAxis;//获取左边Y轴
    leftAxis.labelCount = 5;//Y轴label数量,数值不一定,如果forceLabelsEnabled等于YES, 则强制绘制制定数量的label, 但是可能不平均
    leftAxis.forceLabelsEnabled = NO;//不强制绘制制定数量的label
    leftAxis.axisMaxValue = 105;//设置Y轴的最大值
    leftAxis.inverted = NO;//是否将Y轴进行上下翻转
    leftAxis.axisLineWidth = 0.5;//Y轴线宽
    leftAxis.axisLineColor = [UIColor blackColor];//Y轴颜色
    leftAxis.labelPosition = YAxisLabelPositionOutsideChart;//label位置
    leftAxis.labelTextColor = [UIColor brownColor];//文字颜色
    leftAxis.labelFont = [UIFont systemFontOfSize:10.0f];//文字字体
    //网格线样式
    leftAxis.gridLineDashLengths = @[@3.0f, @3.0f];//设置虚线样式的网格线
    leftAxis.gridColor = [UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1];//网格线颜色
    leftAxis.gridAntialiasEnabled = YES;//开启抗锯齿
    //添加限制线
    ChartLimitLine *limitLine = [[ChartLimitLine alloc] initWithLimit:80 label:@"限制线"];
    limitLine.lineWidth = 2;
    limitLine.lineColor = [UIColor greenColor];
    limitLine.lineDashLengths = @[@5.0f, @5.0f];//虚线样式
    limitLine.labelPosition = ChartLimitLabelPositionRightTop;//位置
    [leftAxis addLimitLine:limitLine];//添加到Y轴上
    leftAxis.drawLimitLinesBehindDataEnabled = YES;//设置限制线绘制在柱形图的后面
    */
3.3 交互设置
    self.barChartView.scaleYEnabled = NO;//取消Y轴缩放
    self.barChartView.doubleTapToZoomEnabled = NO;//取消双击缩放
    self.barChartView.dragEnabled = YES;//启用拖拽图表
    self.barChartView.dragDecelerationEnabled = YES;//拖拽后是否有惯性效果
    self.barChartView.dragDecelerationFrictionCoef = 0.9;//拖拽后惯性效果的摩擦系数(0~1),数值越小,惯性越不明显
    [self.barChartView setVisibleXRangeMinimum:10.f];//设置只滑动 不缩放 (系统内部默认是先缩放后滑动)
    [self.barChartView setVisibleXRangeMaximum:10.f];
3.4 设置动画效果
     [self.barChartView animateWithYAxisDuration:1.5f];//可以设置X轴和Y轴的动画效果
3.5 表格数据设置
 //为柱形图设置数据
- (BarChartData *)setData{
    int xVals_count = 18;//X轴上要显示多少条数据
    //X轴上面需要显示的数据
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < xVals_count; i++) {
        [xVals addObject:[NSString stringWithFormat:@"%d月", i+1]];
    }
    //对应Y轴上面需要显示的数据
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < xVals_count; i++) {
        double val = 20+i>32?32:20+i;
        BarChartDataEntry *entry = [[BarChartDataEntry alloc] initWithX:i y:val];
        [yVals addObject:entry];
    }
    //创建BarChartDataSet对象,其中包含有Y轴数据信息,以及可以设置柱形样式
    BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithValues:yVals label:nil];
    set1.drawValuesEnabled = YES;//是否在柱形图上面显示数值
    set1.highlightEnabled = NO;//点击选中柱形图是否有高亮效果,(双击空白处取消选中)
    [set1 setColors:ChartColorTemplates.material];//设置柱形图颜色
    set1.drawValuesEnabled = YES;//是否填充颜色
    //将BarChartDataSet对象放入数组中
    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    [dataSets addObject:set1];
    
    //创建BarChartData对象, 此对象就是barChartView需要最终数据对象
    BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
    [data setValueTextColor:[UIColor orangeColor]];//文字颜色
    data.barWidth = 0.6;//总共为1 数值越大柱状条的宽度越大
    return data;
}
 //为柱形图提供数据
    self.barChartView.data = [self setData];
3.6 常用代理事件
 #pragma mark - ChartViewDelegate
//点击选中柱形时回调
- (void)chartValueSelected:(ChartViewBase * _Nonnull)chartView entry:(ChartDataEntry * _Nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * _Nonnull)highlight{
}
//没有选中柱形图时回调,当选中一个柱形图后,在空白处双击,就可以取消选择,此时会回调此方法
- (void)chartValueNothingSelected:(ChartViewBase * _Nonnull)chartView{
    NSLog(@"---chartValueNothingSelected---");
}
//放大图表时回调
- (void)chartScaled:(ChartViewBase * _Nonnull)chartView scaleX:(CGFloat)scaleX scaleY:(CGFloat)scaleY{
//    NSLog(@"---chartScaled---scaleX:%g, scaleY:%g", scaleX, scaleY);
}
//拖拽图表时回调
- (void)chartTranslated:(ChartViewBase * _Nonnull)chartView dX:(CGFloat)dX dY:(CGFloat)dY{
//    NSLog(@"---chartTranslated---dX:%g, dY:%g", dX, dY);
}
3.7 数据改变时 刷新数据
   -(void)updateData{
    //数据改变时,刷新数据
    self.data = [self setData];
    self.barChartView.data = self.data;
    [self.barChartView notifyDataSetChanged];
}

4.使用注意事项

1> y轴显示默认为double类型,需要自定义的,比如需要显示为9%,此时需要去自定义一个类遵循的协议来完成
2> 未提供方法设置柱状图的圆角,需要自行修改三方库文件drawDataSet方法,可参照如下https://github.com/danielgindi/Charts/issues/1066

5.参考资料

【Charts开源库链接】 https://github.com/danielgindi/Charts
【Charts源码解读】http://www.jianshu.com/p/b183ff57df01

你可能感兴趣的:(Charts分析及使用)