Charts 图表控件功能强大,可以通过(pod 'Charts')导入,目前使用该控件画图遇到的问题有:
1、获取图表内容相关属性:
float width = _chart.viewPortHandler.contentWidth ; //获取图表内容宽度
float leftPadding =_chart.viewPortHandler.contentLeft; //获取图表左侧间距
2、添加长按,根据触摸点,十字线显示图表的横纵坐标:
可以通过scrollerView包裹Charts,禁用Charts交互,再添加scrollerView长按事件来实现。
3、X轴(上下)、Y轴(左右)设置:
ChartYAxis *rightAxis = _chart.rightAxis;
rightAxis.drawGridLinesEnabled = NO;
rightAxis.drawLabelsEnabled=NO;
rightAxis.drawAxisLineEnabled = NO;
rightAxis.axisMinimum = 0.0; // this replaces startAtZero = YES
ChartYAxis *leftAxis = _chart.leftAxis;
leftAxis.drawGridLinesEnabled = NO; // 网格绘制
leftAxis.drawAxisLineEnabled = NO; // 是否显示轴线
leftAxis.drawLabelsEnabled = NO;
leftAxis.axisMinimum=0;// 最小值
// leftAxis.labelPosition = YAxisLabelPositionOutsideChart; // 显示位置
// leftAxis.labelFont = [UIFont systemFontOfSize:10]; // 字号
// leftAxis.labelTextColor = [UIColor blackColor]; // 颜色
// leftAxis.axisLineColor = [UIColor blackColor]; // Y 轴颜色
// leftAxis.axisLineWidth = 1.f; // Y 轴线宽
ChartXAxis *xAxis = _chart.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.labelCount=ITEM_COUNT;
xAxis.axisMaxLabels=ITEM_COUNT;
xAxis.axisMinimum=0;
xAxis.axisMaximum=ITEM_COUNT;
xAxis.centerAxisLabelsEnabled = YES;
xAxis.granularityEnabled = YES;
xAxis.granularity=1.0;
xAxis.drawGridLinesEnabled = NO;
xAxis.drawLabelsEnabled = YES;
xAxis.drawAxisLineEnabled = NO;
xAxis.valueFormatter = self;
xAxis.labelFont= [UIFontsystemFontOfSize:10.0];
xAxis.labelTextColor = THIRD_TEXT_COLOR;
xAxis.labelWidth=20.0;
xAxis.gridAntialiasEnabled = YES;
4、柱状图设置:
_chart.leftAxis.axisMaximum = _maxYvalue; //设置柱状图最高值,否则显示异常
_chart.rightAxis.axisMaximum = _maxYvalue; //双柱状图要设置右边柱状图最高值,否则显示异常
BarChartData *data = [[BarChartData alloc] initWithDataSets:@[set]];
[data setBarWidth:0.6]; //设置单个柱状图的柱子占宽的比例
5、X轴文本间隔显示:
使用 _chart.xAxis.granularity=3; 或者通过- (NSString*)stringForValue:(double)value
axis:(ChartAxisBase*)axis 回调显示为空来自定义间隔