相信大家在开发过程中经常会遇到使用折线图、曲线图等,但是我们自己开发就会非常浪费时间了,ORCharts就解决了这个问题,他将大家所需要的折线图和曲线图已经全部封装完毕,大家只要使用即可,使用也是非常简单。
首先、引库
#import "ORLineChartView.h"
接下来就是遵守他的协议,他的协议一共有两个
ORLineChartViewDataSource, ORLineChartViewDelegate
最后就是创建并使用
第一步、创建曲线图或者折线图
ORLineChartView *lineChartView = [[ORLineChartView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 180)];
lineChartView.dataSource = self;
lineChartView.delegate = self;
//设置曲线图或折线图的风格ORLineChartStyleSlider:指示器随拖拽显示(默认)ORLineChartStyleControl:指示器由点击控制
lineChartView.config.style = ORLineChartStyleSlider;
//设置曲线图或者折线图的线的宽度
lineChartView.config.chartLineWidth = 1;
//设置动画时长
lineChartView.config.animateDuration = 1;
//设置展示是曲线图还是折线图,默认曲线图 YES 为折线图
lineChartView.config.isBreakLine = NO;
//设置X轴的Label的宽度
lineChartView.config.bottomLabelWidth = 75;
//设置阴影闲的颜色
lineChartView.config.shadowLineColor = [UIColor clearColor];
//设置滑动展示数据控件的北京颜色
lineChartView.config.indicatorTintColor = [UIColor clearColor];
//设置曲线或者折现距离X轴之间的颜色渐变色
lineChartView.config.gradientColors = @[[UIColor orangeColor],[UIColor whiteColor]];
[self.view addSubview:lineChartView];
第二步、设置Y轴或者X轴的数据源
//设置横向数据源
_xdatas = [[NSMutableArray alloc]init];
//设置纵向数据源
_ydatas = [[NSMutableArray alloc]init];
for ( NSDictionary *dic in array ) {
[_xdatas addObject:dic[@"x"]];
[_ydatas addObject:dic[@"x"]];
}
//刷新数据
[lineChartView reloadData];
第三步、实现ORLineChartView的协议方法、(类似于tableView)
#pragma mark - ORLineChartViewDataSource
//返回Y轴数据源的数量
- (NSInteger)numberOfHorizontalDataOfChartView:(ORLineChartView *)chartView {
return _ydatas.count;
}
//返回Y轴数据源对应的内容
- (CGFloat)chartView:(ORLineChartView *)chartView valueForHorizontalAtIndex:(NSInteger)index {
return [_ydatas[index] doubleValue];
}
//返回X轴数据源对应的内容
- (NSString *)chartView:(ORLineChartView *)chartView titleForHorizontalAtIndex:(NSInteger)index{
return _xdatas[index];
}
//返回Y展示数量范围,默认为5,例如:从20开始到40结束分别为 20、25、30、35、40
- (NSInteger)numberOfVerticalLinesOfChartView:(ORLineChartView *)chartView {
return 6;
}
//设置每次滑动或者点击位置的Y轴对应的值
- (NSAttributedString *)chartView:(ORLineChartView *)chartView attributedStringForIndicaterAtIndex:(NSInteger)index {
NSAttributedString *string = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%g", [_ydatas[index] doubleValue]]];
return string;
}
//设置Y轴展示数据的颜色
- (NSDictionary<NSAttributedStringKey,id> *)labelAttrbutesForVerticalOfChartView:(ORLineChartView *)chartView {
return @{NSFontAttributeName : [UIFont systemFontOfSize:12], NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#999999"]};
}
//设置X轴展示数据的颜色
- (NSDictionary<NSAttributedStringKey,id> *)labelAttrbutesForHorizontalOfChartView:(ORLineChartView *)chartView {
return @{NSFontAttributeName : [UIFont systemFontOfSize:12], NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#999999"]};
}
#pragma mark - ORLineChartViewDelegate
//获取点击位置的数据
- (void)chartView:(ORLineChartView *)chartView didSelectValueAtIndex:(NSInteger)index {
// NSLog(@"did select index %ld and value is %g", (long)index, [_datas[index] doubleValue]);
}
//获取滑动到某个位置的数据
- (void)chartView:(ORLineChartView *)chartView indicatorDidChangeValueAtIndex:(NSInteger)index {
// NSLog(@"indicater did change index %ld and value is %g", (long)index, [_datas[index] doubleValue]);
}
ORCharts的下载地址: