iOS 曲线图、折线图(ORCharts)的使用

97 篇文章 0 订阅
79 篇文章 2 订阅

相信大家在开发过程中经常会遇到使用折线图、曲线图等,但是我们自己开发就会非常浪费时间了,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的下载地址:

https://github.com/SunriseOYR/ORCharts

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王 哪跑!!!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值