折线与单系列柱状图混合图表绘制数据处理赋值
NSMutableArray *lineVals = [NSMutableArray array];
double rightSetMin = 0;
double rightSetMax = 0;
NSArray *annGrowRate = @[@"2.24", @"16.79", @"14.54", @"-2.25", @"<null>"];
for (int i = 0; i < annGrowRate.count; i++)
{
NSString *valStr = [NSString stringWithFormat:@"%@", annGrowRate[i]];
double val = [valStr doubleValue];
rightSetMax = MAX(val, rightSetMax);
rightSetMin = MIN(val, rightSetMin);
ChartDataEntry *entry = [[ChartDataEntry alloc] initWithX:i y:val];
[lineVals addObject:entry];
}
LineChartDataSet *lineSet = [[LineChartDataSet alloc] initWithValues:lineVals label:@"折线图"];
lineSet.lineWidth = 3.0f;
lineSet.drawValuesEnabled = YES;
lineSet.valueColors = @[RGB(92, 178, 240)];
[lineSet setColor:RGB(92, 178, 240)];
lineSet.drawSteppedEnabled = NO;
lineSet.drawCirclesEnabled = NO;
lineSet.circleRadius = 3.0f;
lineSet.drawCircleHoleEnabled = YES;
lineSet.circleHoleRadius = 1.0f;
lineSet.circleHoleColor = RGB(92, 178, 240);
lineSet.highlightEnabled = NO;
lineSet.valueFont = [UIFont systemFontOfSize:12];
lineSet.axisDependency = AxisDependencyRight;
LineChartData *lineData = [[LineChartData alloc] initWithDataSet:lineSet];
// 柱状
double leftSetMin = 0;
double leftSetMax = 0;
NSMutableArray *yVals = [[NSMutableArray alloc] init];
NSArray *annBloodCol = @[@"233303.8", @"272471.46", @"312079.13", @"305052.68", @"281939.85", @""];
for (int i = 0; i < annBloodCol.count ; i++)
{
NSString *valStr = [NSString stringWithFormat:@"%@", annBloodCol[i]];
double val = [valStr doubleValue];
leftSetMax = MAX(val, leftSetMax);
leftSetMin = MIN(val, leftSetMin);
[yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:val]];
}
BarChartDataSet *barSet = [[BarChartDataSet alloc] initWithValues:yVals label:@"柱状图"];
[barSet setColor:RGB(242, 152, 80)];
barSet.highlightEnabled = NO;
BarChartData *barData = [[BarChartData alloc] initWithDataSet:barSet];
[barData setValueFont:[UIFont systemFontOfSize:10.f]];
barData.barWidth = 0.5f;
CombinedChartData *combineData = [[CombinedChartData alloc] init];
double leftDiff = leftSetMax - leftSetMin;
if (leftSetMax == 0 && leftSetMin == 0) {
leftSetMax = 100.0;
leftSetMin = -10.0;
} else {
leftSetMax = (leftSetMax + leftDiff * 0.2);
leftSetMin = (leftSetMin - leftDiff * 0.1);
}
self.comChartView.leftAxis.axisMaximum = leftSetMax;
self.comChartView.leftAxis.axisMinimum = 0;
double rightDiff = rightSetMax - rightSetMin;
if (rightSetMax == 0 && rightSetMin == 0) {
rightSetMax = 100.0;
rightSetMin = -10.0;
} else {
rightSetMax = (rightSetMax + rightDiff * 0.2);
rightSetMin = (rightSetMin - rightDiff * 0.1);
}
self.comChartView.rightAxis.axisMaximum = rightSetMax;
self.comChartView.rightAxis.axisMinimum = rightSetMin;
_comChartView.data = nil;
combineData.lineData = lineData;
combineData.barData = barData;
if (combineData.barData || combineData.lineData) {
_comChartView.data = combineData;
_comChartView.xAxis.axisMinimum = combineData.xMin - 0.8;
_comChartView.xAxis.axisMaximum = 4.8;
}
[self.comChartView animateWithXAxisDuration:0.25f];
效果图如下:
混合图表绘制套路基本和折线、柱状图绘制相同,主要有俩点,在初始化的时候设置混合图表的数据类型,这里有先后顺序的,在combinedData数据赋值时候可以不按顺序,数据类型初始化的时候可以试一下,类型顺序不同,或许就被遮盖了,主要代码:
comChartView.drawOrder = @[ @(CombinedChartDrawOrderBar), @(CombinedChartDrawOrderLine)];
combinedData赋值的时候,需要折线数据就按照LineChartData进行初始化,需要柱状图就按照BarChartData进行处理,最后把处理好的数据赋值给combinedData
combineData.lineData = lineData;
combineData.barData = barData;
其他的注意点基本也都说过了,目前只研究了Charts的折线图,柱状图、折线与柱状图混合图表,相信其他的类型的套路也是一样的,有问题可以一起讨论交流,代码写的有些粗糙别见怪,以后再优化代码和文字样式,希望对大家有帮助~~😝