barChartView 图表属性
//1.barChartView 设置
[barChartView setExtraOffsetsWithLeft:10 top:10 right:10 bottom:20];//图表四周边距
barChartView.chartDescription.enabled = NO;//图表描述是否展示
barChartView.drawGridBackgroundEnabled = NO;//是否绘制图表背景网线
barChartView.dragEnabled = NO; //是否可以拖拽
[barChartView setScaleEnabled:NO];//是否开启图表缩放能力
barChartView.pinchZoomEnabled = NO; //XY轴是否同时缩放
barChartView.rightAxis.enabled = NO; //left Y轴隐藏
barChartView.leftAxis.enabled = NO;//right Y轴隐藏
barChartView.doubleTapToZoomEnabled = NO; //双击是否缩放
barChartView.legend.enabled = NO; //不显示图例说明
barChartView.highlightPerTapEnabled = NO;//是否开启高亮点击
barChartView.drawBarShadowEnabled = NO;//是否绘制柱形的阴影背景
//2.设置 Y轴
ChartYAxis * rightAxis = barChartView.rightAxis;
rightAxis.drawLabelsEnabled = NO; //不显示 右边 y 轴数值
rightAxis.drawGridLinesEnabled = NO;//不绘制网格
ChartYAxis * leftAxis = barChartView.leftAxis;
leftAxis.drawLabelsEnabled = NO; //不显示 左边 y 轴数值
leftAxis.drawGridLinesEnabled = NO;//不绘制网格
leftAxis.axisMinimum = 0;//y 轴最小值
leftAxis.drawAxisLineEnabled = NO;
//3.设置 X轴
ChartXAxis * xAxis = barChartView.xAxis;
xAxis.granularity = 1.0;
xAxis.drawGridLinesEnabled = NO;//不绘制网格
xAxis.drawAxisLineEnabled = NO;//不绘制网格线
xAxis.axisLineColor = [UIColor redColor]; //底部 x轴颜色
xAxis.labelPosition = XAxisLabelPositionBottom; //label 显示的位置
xAxis.labelFont = [UIFont systemFontOfSize:10]; //label 显示文字大小
xAxis.labelTextColor = [UIColor colorWithHexString:loginBackColor];//label 显示文字颜色
xAxis.valueFormatter = self; //显示 X 轴标题代理
//限制线
ChartLimitLine *limitLine = [[ChartLimitLine alloc] initWithLimit:1000 label:@"文字描述"];
limitLine.lineWidth = 1;
limitLine.lineColor = [UIColor colorWithHexString:EEEEEEColor];
limitLine.labelPosition = ChartLimitLabelPositionBottomRight;//位置
limitLine.valueTextColor = [UIColor colorWithHexString:loginBackColor];//label文字颜色
limitLine.valueFont = [UIFont systemFontOfSize:9];//label字体
[leftAxis addLimitLine:limitLine];
leftAxis.drawLimitLinesBehindDataEnabled = YES;
BarChartData * data = [[BarChartData alloc] initWithDataSets:dataSets];
data.barWidth = [self optimalBarWidthRatioWithMaxBarWidth:10 minBarSpace:3 xAxisCount:array.count];
//更新图表数据
self.barChartView.data = data;
//图表一页显示的最大数量 最好在更新图表数据后调用,也就是在 self.barChartView.data = data; 这句话之后调用下面两句话
[self.barChartView setVisibleXRangeMaximum:array.count];
[self.barChartView setVisibleXRangeMinimum:array.count];
BarChartView
- 最大值和最小值 缩放比例 1/200 例如:如果最大值为 1000 空数据时 可以设置为 5;
计算柱状图宽度的方法
- (CGFloat)optimalBarWidthRatioWithMaxBarWidth:(CGFloat)maxBarWidth minBarSpace:(CGFloat)minBarSpace xAxisCount:(NSInteger)xAxisCount {
CGFloat width = Device_Width - 20; //图表宽度
CGFloat result = maxBarWidth * xAxisCount / width;
CGFloat maxResult = maxBarWidth / (minBarSpace + maxBarWidth);
if (result >= 1.0 || result >= maxResult) {
result = maxResult;
}
return result;
}
效果图
IMG_0299.PNGIMG_0300.PNG
IMG_0301.PNG
备注
个人笔记
网友评论