Charts图表库升级语法变更笔记

目标:

将项目中的Charts库从2.2.3更新至最新的3.1.1。

环境:

旧环境:XCode7.3、swift2;
新环境:XCode9.4、swift4.1。

语法变更:

1.descriptionText属性

该属性是用来设置图表名称的。 现在变成了一个chartDescription的对象属性,通过该属性还可以设置颜色和字号等属性。

_chartView.descriptionText = @"柱状图";
 _chartView.chartDescription.text =@"柱状图"; 
2.noDataTextDescription属性

该属性是用来设置没有数据时图表的占位文字。 现在该属性变成了noDataText。

_chartView.noDataTextDescription = @"You need to provide data for the chart.";
_chartView.noDataText = @"You need to provide data for the chart.";
3. drawSliceTextEnabled属性

该属性是用来设置饼状图每个部分的文字描述是否显示。 现在该属性变成drawEntryLabelsEnabled。

_chartView. drawSliceTextEnabled =YES;
_chartView.drawEntryLabelsEnabled =YES;
4.spaceBetweenLabels属性

该属性是用来设置两个label之间的间隔的。若设置为1,则如果能全部显示,则每个柱形下面都会显示label。 现在该属性变为granularity。

ChartXAxis *xAxis1 = _chartView.xAxis;
xAxis1.spaceBetweenLabels = 1.0;
xAxis1.granularity =1.0;
5. position属性(上面部分的代码是旧环境代码)

该属性是用来设置图例的位置的。 现在该属性由一个枚举属性变成了几个属性的组合。

ChartLegend *l =_ chartView.legend;
l.position = ChartLegendPositionBelowChartLeft;//图例位于图表的左下方且在图表外部
ChartLegend *l =_ chartView.legend;
l.horizontalAlignment = ChartLegendHorizontalAlignmentLeft;//水平方向的左侧
l.verticalAlignment = ChartLegendVerticalAlignmentBottom;//竖直方向的底部
l.orientation = ChartLegendOrientationHorizontal;//图例水平排列
l.drawInside =NO;//图例不可以显示在图表内
6.ChartYAxis的valueFormatter属性

该属性是用来设置Y轴上显示数据和格式的。 现在该属性的所属类由NSNumberFormatter变为3.0版本新增类ChartAxisValueFormatter。

ChartYAxis *leftAxis = _chartView.leftAxis;
leftAxis.valueFormatter = [[NSNumberFormatter alloc] init];
leftAxis.valueFormatter.maximumFractionDigits = 0;
leftAxis.valueFormatter.negativeSuffix = @"";
leftAxis.valueFormatter.positiveSuffix = @"";
ChartYAxis *leftAxis = _chartView.leftAxis;
NSNumberFormatter *leftAxisFormatter = [[NSNumberFormatter alloc] init];
leftAxisFormatter.maximumFractionDigits = 0;//设置小数点位数
leftAxisFormatter.negativeSuffix =@"";//设置Y轴正向的数据后缀
leftAxisFormatter.positiveSuffix =@"";//设置Y轴负向的数据后缀
leftAxis.valueFormatter =[[ChartDefaultAxisValueFormatter alloc] initWithFormatter:leftAxisFormatter];//设置Y轴的显示数据

注:ChartDataSet、ChartData的valueFormatter属性同上也需要包装一层。
7.ChartXAxis的valueFormatter属性

该属性是用来设置X轴上显示数据和格式的。 Chart3.0新增属性,为了统一坐标轴的属性和设置。

NSMutableArray *xVals = [[NSMutableArray alloc] init];
for (int i = 0; i < count; i++)
 {
      [xVals addObject:self.arrayName[i % self.arrayName.count]];
  }
 xAxis1.valueFormatter =[[ChartIndexAxisValueFormatter alloc] initWithValues:xVals];
8.饼状图的数据设置
   NSArray * parties =@[@"Party A",@"Party B",@"Party C",@"Party D",@"Party E"];
   NSArray * values =@[@"10",@"20",@"30",@"25",@"15"];

   NSMutableArray *yVals = [[NSMutableArray alloc] init];
   NSMutableArray *xVals = [[NSMutableArray alloc] init];
 
    NSUInteger count = parties.count;
    if (count >=10) {
        count = 10;//限制个数
    }
    for (int i = 0; i < count; i++)
    {
        NSString * numString = values[i];
        double dd = [numString doubleValue];
        [yVals addObject:[[BarChartDataEntry alloc] initWithValue:dd xIndex:i]];
    }
    for (int i = 0; i < count; i++)
    {
        [xVals addObject:parties[i % parties.count]];
    }
    PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithYVals:yVals label:@""];
    PieChartData *data = [[PieChartData alloc] initWithXVals:xVals dataSet:dataSet];
    _chartView.data = data;
}

   NSUInteger count = parties.count;
    if (count >=10) {
        count = 10;
    }

    for (int i = 0; i < count; i++)
    {
         NSString * numString = values[i];
        double dd = [numString doubleValue];
        [yVals1 addObject:[[PieChartDataEntry alloc] initWithValue:dd label:parties[i % parties.count]]];
    }

    PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithValues:yVals1 label:@""];
    PieChartData *data = [[PieChartData alloc] initWithDataSet:dataSet];
    _chartView.data = data;
   NSArray * parties =@[@"Party A",@"Party B",@"Party C",@"Party D",@"Party E"];
   NSArray * values =@[@"10",@"20",@"30",@"25",@"15"];

   NSMutableArray *yVals = [[NSMutableArray alloc] init];
   NSMutableArray *xVals = [[NSMutableArray alloc] init];
 
    NSUInteger count = parties.count;
    if (count >=10) {
        count = 10;//限制个数
    }

    for (int i = 0; i < count; i++)
    {
        NSString * numString = values[i];
        double dd = [numString doubleValue];
        [yVals  addObject:[[PieChartDataEntry alloc] initWithValue:dd label:parties[i % parties.count]]];
    }

    PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithValues:yVals  label:@""];
    PieChartData *data = [[PieChartData alloc] initWithDataSet:dataSet];
    _chartView.data = data;
9.单一柱状图的数据设置
   NSArray * parties =@[@"Party A",@"Party B",@"Party C",@"Party D",@"Party E"];
   NSArray * values =@[@"10",@"20",@"30",@"25",@"15"];

   NSMutableArray *yVals = [[NSMutableArray alloc] init];
   NSMutableArray *xVals = [[NSMutableArray alloc] init];

   NSUInteger count = parties.count;
    if (count >=10) {
        count = 10;
    }

    for (int i = 0; i < count; i++)
    {
        double dd = [values[i] doubleValue];
        [yVals addObject:[[BarChartDataEntry alloc] initWithValue:dd xIndex:i]];
    }

    for (int i = 0; i < count; i++)
    {
        [xVals addObject: parties[i % parties.count]];
    }

    BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:@""];

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.negativeSuffix = @"k";
    formatter.positiveSuffix = @"k";
    formatter.numberStyle = NSNumberFormatterNoStyle;
    formatter.maximumFractionDigits = 3;//小数位数
    formatter.multiplier = @1.f;
    [set1 setValueFormatter:formatter];//设置显示数据格式

    BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSet:set1];
    data.groupSpace = 0.8;
    _chartView.data = data;
   NSArray * parties =@[@"Party A",@"Party B",@"Party C",@"Party D",@"Party E"];
   NSArray * values =@[@"10",@"20",@"30",@"25",@"15"];

   NSMutableArray *yVals = [[NSMutableArray alloc] init];
   NSMutableArray *xVals = [[NSMutableArray alloc] init];

   NSUInteger count = parties.count;
    if (count >=10) {
        count = 10;
    }

    for (int i = 0; i < count; i++)
    {
        double dd = [values[i] doubleValue];
        [yVals addObject:[[BarChartDataEntry alloc] initWithX:i  y:dd]];
    }

    for (int i = 0; i < count; i++)
    {
        [xVals addObject:parties[i %parties.count]];
    }
    xAxis1.valueFormatter =[[ChartIndexAxisValueFormatter alloc] initWithValues:xVals];

    BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithValues:yVals label:@""];

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.negativeSuffix = @"k";
    formatter.positiveSuffix = @"k";
    formatter.numberStyle = NSNumberFormatterNoStyle;
    formatter.maximumFractionDigits = 3;//小数位数
    formatter.multiplier = @1.f;
    [set1 setValueFormatter:[[ChartDefaultValueFormatter alloc] initWithFormatter:formatter]];//设置显示数据格式
   
    BarChartData *data = [[BarChartData alloc] initWithDataSet:set1];
    chartView2.data = data;
10.多柱状图(簇状图)的数据设置
// (0.35 + 0.1) * 2 + 0.1 = 1.00 -> interval per "group"
    float groupSpace = 0.1f;
    float barSpace = 0.1f;
    float barWidth = 0.35f;
  
   BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
    data.barWidth =barWidth;

    _chartView.xAxis.axisMinimum =0;
    _chartView.xAxis.axisMaximum =[data groupWidthWithGroupSpace:groupSpace barSpace: barSpace] * count;
    
    [data groupBarsFromX: 0.0 groupSpace: groupSpace barSpace: barSpace];
    _chartView.data = data;

可以通过设置X轴的label数量,调整X轴竖线间隔

xAxis1.labelCount =count;//每个组都会有描述,每个组之间都会有竖线隔开

通过设置centerAxisLabelsEnabled属性使每组的描述文字居中

xAxis1.centerAxisLabelsEnabled =YES;

通过设置最大、最小值来设置X轴的起始和最终位置

xAxis.axisMinimum = -1.0;//避免柱状图从X轴上开始显示
xAxis.axisMaximum =count;

推荐阅读更多精彩内容