-
-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to implement line chart with multiple colors #5071
Comments
Asking the same thing :D |
- (void)setTestData1 {
ChartLimitLine *maxLine = [[ChartLimitLine alloc] initWithLimit:80 label:@"80"];
maxLine.valueFont = [UIFont systemFontOfSize:8];
maxLine.lineWidth = 1;
maxLine.labelPosition = ChartLimitLabelPositionRightTop;
maxLine.lineColor = [UIColor qmui_colorWithHexString:@"#FFA200"];
[self.chartView.leftAxis addLimitLine:maxLine];
ChartLimitLine *minLine = [[ChartLimitLine alloc] initWithLimit:60 label:@"60"];
minLine.valueFont = [UIFont systemFontOfSize:8];
minLine.lineWidth = 1;
minLine.labelPosition = ChartLimitLabelPositionRightTop;
minLine.lineColor = [UIColor redColor];
[self.chartView.leftAxis addLimitLine:minLine];
NSMutableArray *dataSets = [NSMutableArray array];
// 第一段
{
NSMutableArray *firstValue = [NSMutableArray array];
for (int i = 0; i < 10; i++) {
// value 是 0 - 60 的随机数
int value = [self getRandomNumber:60 to:80];
ChartDataEntry *entry = [[ChartDataEntry alloc] initWithX:i y:value];
[firstValue addObject:entry];
}
ChartDataEntry *entry = [[ChartDataEntry alloc] initWithX:10 y:60];
[firstValue addObject:entry];
LineChartDataSet *dataSet = [[LineChartDataSet alloc] initWithEntries:firstValue];
dataSet.drawFilledEnabled = YES;
dataSet.drawCirclesEnabled = YES;
dataSet.drawCircleHoleEnabled = YES;
dataSet.circleHoleColor = UIColor.whiteColor;
dataSet.circleColors = @[
[UIColor qmui_colorWithHexString:@"#FFA200"],
[UIColor qmui_colorWithHexString:@"#FFA200"],
[UIColor qmui_colorWithHexString:@"#FFA200"],
[UIColor qmui_colorWithHexString:@"#FFA200"],
[UIColor qmui_colorWithHexString:@"#FFA200"],
[UIColor qmui_colorWithHexString:@"#FFA200"],
[UIColor qmui_colorWithHexString:@"#FFA200"],
[UIColor qmui_colorWithHexString:@"#FFA200"],
[UIColor qmui_colorWithHexString:@"#FFA200"],
[UIColor qmui_colorWithHexString:@"#FFA200"],
[UIColor clearColor]
];
dataSet.circleRadius = 3;
dataSet.circleHoleRadius = 1;
dataSet.lineWidth = 1;
dataSet.mode = LineChartModeCubicBezier;
dataSet.drawValuesEnabled = NO;
NSArray *fillColors = @[
(id)[ChartColorTemplates colorFromString:@"#00FFA200"].CGColor,
(id)[ChartColorTemplates colorFromString:@"#FFFFA200"] .CGColor,
];
CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)fillColors, nil);
dataSet.fillAlpha = 1.f;
dataSet.fill = [[ChartLinearGradientFill alloc] initWithGradient:gradient angle:90.0f];
[dataSet setColors:@[
[UIColor qmui_colorWithHexString:@"#FFA200"],
]];
[dataSets addObject:dataSet];
}
// 第二段
{
NSMutableArray *firstValue = [NSMutableArray array];
ChartDataEntry *entry = [[ChartDataEntry alloc] initWithX:10 y:60];
[firstValue addObject:entry];
for (int i = 11; i < 20; i++) {
int value = [self getRandomNumber:00 to:60];
ChartDataEntry *entry = [[ChartDataEntry alloc] initWithX:i y:value];
[firstValue addObject:entry];
}
LineChartDataSet *dataSet = [[LineChartDataSet alloc] initWithEntries:firstValue];
dataSet.drawFilledEnabled = YES;
dataSet.drawCirclesEnabled = YES;
dataSet.drawCircleHoleEnabled = YES;
dataSet.circleHoleColor = UIColor.whiteColor;
dataSet.circleColors = @[
[UIColor clearColor],
[UIColor redColor],
[UIColor redColor],
[UIColor redColor],
[UIColor redColor],
[UIColor redColor],
[UIColor redColor],
[UIColor redColor],
[UIColor redColor],
[UIColor redColor]
];
dataSet.circleRadius = 3;
dataSet.circleHoleRadius = 1;
dataSet.lineWidth = 1;
dataSet.mode = LineChartModeCubicBezier;
dataSet.drawValuesEnabled = NO;
NSArray *fillColors = @[
(id)[ChartColorTemplates colorFromString:@"#00FF0000"].CGColor,
(id)[ChartColorTemplates colorFromString:@"#FFFF0000"] .CGColor,
];
CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)fillColors, nil);
dataSet.fillAlpha = 1.f;
dataSet.fill = [[ChartLinearGradientFill alloc] initWithGradient:gradient angle:90.0f];
[dataSet setColors:@[
[UIColor redColor],
[UIColor orangeColor],
[UIColor yellowColor],
]];
dataSet.valueColors = @[
[UIColor blueColor],
[UIColor purpleColor],
[UIColor systemPinkColor],
];
[dataSets addObject:dataSet];
}
// 第三段
LineChartData *chartData = [[LineChartData alloc] initWithDataSets:dataSets];
self.chartView.data = chartData;
}
|
我用了这样一个思路,效果勉勉强强,不知道能不能给你启发 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: