使用Charts框架可以實(shí)現(xiàn)柱形圖踊餐、曲線圖、圓型圖等。
畫折線使用 LineChartView類深胳,用法與barChartView類型喊废。其他類圖標(biāo)可參考ChartsDemo里面的案例實(shí)現(xiàn)模燥,下載地址:https://github.com/danielgindi/Charts
一府阀、初始化
柱形圖使用Charts框架中的BarChartView類:
_chartView = [[BarChartView alloc]init];
//設(shè)置代理
_chartView.delegate = self;//代理
[self.view addSubview:_chartView];
二甘耿、設(shè)置圖表格式
1.基本樣式
_chartView.descriptionText = @"";//右下角描述
_chartView.drawBarShadowEnabled = NO;//是否繪制陰影背景
_chartView.drawValueAboveBarEnabled = YES;//數(shù)值顯示是否在條柱上面
_chartView.noDataText = NSLocalizedString(@"加載中...", nil);//沒有數(shù)據(jù)時(shí)的顯示
_chartView.rightAxis.enabled = NO;//不畫右邊坐標(biāo)軸
_chartView.legend.enabled = NO;//不顯示圖例說明
_chartView.dragEnabled = NO;//不啟用拖拽圖表
2.交互設(shè)置
_chartView.doubleTapToZoomEnabled = NO;//雙擊是否縮放
_chartView.scaleXEnabled = NO;//X軸縮放
_chartView.scaleYEnabled = NO;//Y軸縮放
_chartView.pinchZoomEnabled = NO;//XY軸是否同時(shí)縮放
3.X軸樣式設(shè)置
ChartXAxis *xAxis = _chartView.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;//X軸的顯示位置
xAxis.drawGridLinesEnabled = NO;//不繪制網(wǎng)格
xAxis.labelFont = [UIFont systemFontOfSize:10.0f];//x數(shù)值字體大小
xAxis.labelTextColor = [UIColor blackColor];//數(shù)值字體顏色
DateFormatter *valueFormatter = [[DateFormatter alloc] init];//坐標(biāo)數(shù)值樣式
xAxis.valueFormatter = valueFormatter;
xAxis.labelCount = 7;
4.Y軸樣式設(shè)置
NSNumberFormatter *leftAxisFormatter = [[NSNumberFormatter alloc] init];//坐標(biāo)數(shù)值樣式
leftAxisFormatter.maximumFractionDigits = 1;//Y軸坐標(biāo)最多為1位小數(shù)
ChartYAxis *leftAxis = _chartView.leftAxis;
leftAxis.drawZeroLineEnabled = YES;//從0開始繪畫
leftAxis.axisMaximum = 60;//最大值
leftAxis.axisMinimum = 0;//最小值
leftAxis.valueFormatter = [[ChartDefaultAxisValueFormatter alloc] initWithFormatter:leftAxisFormatter];
leftAxis.labelFont = [UIFont systemFontOfSize:10.f];//字體大小
leftAxis.labelPosition = YAxisLabelPositionOutsideChart;//坐標(biāo)數(shù)值的位置
leftAxis.labelCount = 8;//數(shù)值分割個(gè)數(shù)
leftAxis.labelTextColor = [UIColor blackColor];//坐標(biāo)數(shù)值字體顏色
leftAxis.spaceTop = 0.15;//最大值到頂部的范圍比
leftAxis.drawGridLinesEnabled = YES;//是否繪制網(wǎng)格
leftAxis.axisLineWidth = 1;//Y軸線寬
leftAxis.axisLineColor = [UIColor blackColor];//Y軸顏色
ChartYAxis *right = _chartView.rightAxis;
right.drawLabelsEnabled = NO;//是否顯示Y軸坐標(biāo)
right.drawGridLinesEnabled = NO;//不繪制網(wǎng)格
5.marker設(shè)置
點(diǎn)擊條柱時(shí)的數(shù)據(jù)展示扰她,ChartsDemo中包含BalloonMarker類
BalloonMarker *marker = [[BalloonMarker alloc]
initWithColor: [UIColor colorWithWhite:180/255. alpha:1.0]
font: [UIFont systemFontOfSize:12.0]
textColor: UIColor.whiteColor
insets: UIEdgeInsetsMake(4.0, 4.0, 10.0, 4.0)];
marker.chartView = _chartView;
marker.minimumSize = CGSizeMake(40.f, 20.f);
_chartView.marker = marker;
設(shè)置動(dòng)畫
[_chartViewanimateWithYAxisDuration:1.0f];
ChartLimitLine類即可添加限制線兽掰,使用可參考ChartsDemo
三、data設(shè)置
- (void)setDataCount:(int)count {
NSMutableArray *yVals = [[NSMutableArray alloc] init];
for (int i = 0; i < count; i++) {
int val = (double) (arc4random_uniform(60))+2;
[yVals addObject:[[BarChartDataEntry alloc] initWithX:i y:val]];
}
BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithValues:yVals label:@"DataSet"];
[set1 setColor:[UIColor grayColor]];//bar的顏色
[set1 setValueTextColor: [UIColor lightGrayColor]];
// [set1 setDrawValuesEnabled:NO];//是否在bar上顯示數(shù)值
// [set1 setHighlightEnabled:NO];//是否點(diǎn)擊有高亮效果徒役,為NO是不會(huì)顯示marker的效果
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
[data setValueFont:[UIFont systemFontOfSize:10]];
self.chartView.data = data;
[self.chartView notifyDataSetChanged];
}
四孽尽、實(shí)現(xiàn)barChartView的代理方法
1.點(diǎn)擊選中時(shí)的代理
- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight{
NSLog(@"chartValueSelected");
}
2.沒有選中時(shí)的代理
- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView{
NSLog(@"chartValueNothingSelected");
}
3.捏合放大或縮小
- (void)chartScaled:(ChartViewBase * _Nonnull)chartView scaleX:(CGFloat)scaleX scaleY:(CGFloat)scaleY {
NSLog(@"---chartScaled---scaleX:%g, scaleY:%g", scaleX, scaleY);
}
4.拖拽圖表時(shí)的代理方法
- (void)chartTranslated:(ChartViewBase * _Nonnull)chartView dX:(CGFloat)dX dY:(CGFloat)dY {
NSLog(@"---chartTranslated---dX:%g, dY:%g", dX, dY);
}
效果: