美文网首页
iOS开发折线图

iOS开发折线图

作者: Michael_NO1 | 来源:发表于2017-11-03 17:23 被阅读135次

我是自定义封装了view, 用起来比较方便:
详细代码如下: (包括.h和.m文件)

// .h文件
#import <UIKit/UIKit.h>

@interface LineChartView : UIView

@property (nonatomic, strong) NSArray *titleForYArr;

@property (nonatomic, strong) NSArray *titleForXArr;

@property (nonatomic, strong) NSArray *valueArr;

@property (nonatomic, strong) UIColor *lineColor;

- (instancetype)initWithFrame:(CGRect)frame;

- (void)startDraw;

@end

// .m文件
#import "LineChartView.h"

@interface LineChartView()<CAAnimationDelegate>

@property (nonatomic, strong) CAShapeLayer *lineChartLayer;

@end

@implementation LineChartView {
    CGFloat width;
    CGFloat height;
}

static CGFloat edgeLeft = 30;
static CGFloat edgeRight = 20;
static CGFloat bounceY = 40;

static CGFloat edgeUp = 50;
static CGFloat edgeDown = 30;

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor lightGrayColor];
        
        width = frame.size.width - edgeLeft - edgeRight;
        height = frame.size.height - edgeUp - edgeDown;
    }
    return self;
}
// 画出坐标轴
- (void)drawRect:(CGRect)rect {
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 1.0);
    CGContextSetRGBStrokeColor(context, 0.6, 0.6, 0.6, 1);
    CGContextMoveToPoint(context, edgeLeft, edgeUp);
    CGContextAddLineToPoint(context, edgeLeft, edgeUp + height);
    CGContextAddLineToPoint(context,edgeLeft + width, edgeUp + height);
    CGContextStrokePath(context);
}


#pragma mark 画折线图
- (void)dravLine{

    NSInteger yearNum = self.titleForXArr.count;
    if (yearNum <= 0) {
        return;
    }
    CGFloat widthForX = width / yearNum;
    CGFloat maxValue = [[self.titleForYArr lastObject] floatValue];
    
    if (maxValue <= 0) {
        return;
    }
    
    UIBezierPath *pathLine = [[UIBezierPath alloc] init];
    [pathLine moveToPoint:CGPointMake(edgeLeft + widthForX * 0.5, ((maxValue - [self.valueArr[0] floatValue]) / maxValue) * height + edgeUp)];
    
    // 创建折线点标记
    for (NSInteger i = 1; i < self.valueArr.count; i++) {
        
        CGPoint pointCenter = CGPointMake(edgeLeft + widthForX * (i + 0.5), (maxValue - [self.valueArr[i] floatValue]) / maxValue * height + edgeUp);
        
        UIImageView *pointImg = [[UIImageView alloc] init];
        pointImg.bounds = CGRectMake(0, 0, 5, 5);
        pointImg.center = pointCenter;
        pointImg.backgroundColor = _lineColor;
        [self addSubview:pointImg];
        
        [pathLine addLineToPoint:pointCenter];
    }
    
    CAShapeLayer  *lineChartLayer = [CAShapeLayer layer];
    lineChartLayer.path = pathLine.CGPath;
    lineChartLayer.strokeColor = _lineColor.CGColor;
    lineChartLayer.fillColor = [[UIColor clearColor] CGColor];
    lineChartLayer.lineWidth = 2.0;
    lineChartLayer.lineCap = kCALineCapRound;
    lineChartLayer.lineJoin = kCALineJoinRound;
    
    [self.layer addSublayer:lineChartLayer];
}

#pragma mark 创建x轴的数据
- (void)setTitleForXArr:(NSArray *)titleForXArr {
    _titleForXArr = titleForXArr;
    [self createLabelX];
}
- (void)createLabelX{

    NSInteger yearNum = self.titleForXArr.count;
    if (yearNum <= 0) {
        return;
    }
    CGFloat widthForX = width / yearNum;
    
    for (NSInteger i = 0; i < yearNum; i++) {
        
        UILabel *labelYear = [[UILabel alloc] initWithFrame:CGRectMake(edgeLeft + widthForX * i, height + edgeUp, widthForX, edgeDown)];
        labelYear.tag = 1000 + i;
        labelYear.text = self.titleForXArr[i];
        labelYear.font = [UIFont systemFontOfSize:14];
        labelYear.textAlignment = NSTextAlignmentCenter;
        [self addSubview:labelYear];
    }
}
#pragma mark 创建y轴数据及虚线
- (void)setTitleForYArr:(NSArray *)titleForYArr {
    _titleForYArr = titleForYArr;
    [self createLabelY];
    [self setLineDash];
}
- (void)createLabelY{

    NSInteger numForY = _titleForYArr.count;
    if (numForY <= 1) {
        return;
    }
    CGFloat widthForY = edgeLeft;
    CGFloat heightForY = height / (numForY - 1);
    
    for (NSInteger i = 0; i < numForY; i++) {
        
        UILabel *labelForY = [[UILabel alloc] initWithFrame:CGRectMake(0, edgeUp + (i - 0.5) * heightForY, widthForY, heightForY)];
        
        labelForY.tag = 2000 + i;
        labelForY.text = _titleForYArr[numForY - i - 1];
        labelForY.font = [UIFont systemFontOfSize:12];
        labelForY.textAlignment = NSTextAlignmentCenter;
        [self addSubview:labelForY];
    }
}
#pragma mark - 添加虚线
- (void)setLineDash {
    
    NSInteger numForY = _titleForYArr.count - 1;
    if (numForY <= 0) {
        return;
    }
    CGFloat heightForY = height / numForY;
    
    for (NSInteger i = 0; i < numForY; i++) {
        
        CAShapeLayer *dashLayer = [CAShapeLayer layer];
        dashLayer.strokeColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1].CGColor;
        dashLayer.lineWidth = 1.0;
        
        UIBezierPath *path = [[UIBezierPath alloc] init];
        path.lineWidth = 1.0;
        
        [path moveToPoint:CGPointMake(edgeLeft, edgeUp + i * heightForY)];
        [path addLineToPoint:CGPointMake(edgeLeft + width, edgeUp + i * heightForY)];
        CGFloat dash[] = {10,10};
        [path setLineDash:dash count:2 phase:10];
        [path stroke];
        dashLayer.path = path.CGPath;
        [self.layer addSublayer:dashLayer];
    }
}
#pragma mark - 开始画折线
- (void)startDraw {
    [self dravLine];
}

@end


// 使用方法
- (void)viewDidLoad {
    [super viewDidLoad];
    
    LineChartView *lineView = [[LineChartView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 190)];
    
    lineView.titleForYArr = @[@"0",@"1",@"2",@"3",@"4",@"5"];
    lineView.titleForXArr = @[@"2013年",@"2014年",@"2015年",@"2016年",@"2017年"];
    lineView.lineColor = [UIColor redColor];
    lineView.valueArr = @[@"0.8",@"1.9",@"4.0",@"1.3",@"2.5"];
    
    [self.view addSubview:lineView];
    
    [lineView startDraw];
}

相关文章

  • PNChart源码解析

    一. 框架介绍 PNChart是国内开发者开发的iOS图表框架,现在已经7900多颗star了。它涵盖了折线图,饼...

  • iOS绘图入门--手把手教你画折线图

    适用人群:iOS开发人员。本文内容:使用UIBezierPath和CAShareLayer绘制简单的折线图。 平常...

  • iOS开发折线图

    我是自定义封装了view, 用起来比较方便:详细代码如下: (包括.h和.m文件)

  • iOS开发--简单折线图

    作为一个iOS程序媛,还真是没有发文章的习惯,但是据说如果能够长期整理自己代码的心得,写成文章,那是可以证明实力的...

  • 关于可视化图表那点事

    折线图 1.面积折线图:较为美观,但是让人感觉是数量的积累产生误解; 2.渐变折线图:较为美观,但是开发成本较高;...

  • iOS - ECharts 使用(折线图, 柱状图)

    前言 App中使用折线图, 柱状图有很多方法框架, 但是却很少有公司级的产品供iOS开发者使用, 百度团队有一款 ...

  • iOS开发优秀博客和软件推荐

    iOSBlogAndTools iOS开发优秀博客和软件推荐 iOS开发中文博客 iOS开发工具 iOS开发网站 ...

  • 收录 : iOS支付开发

    iOS 银联支付开发流程iOS 微信支付开发流程iOS 支付宝支付开发流程iOS Apple Pay开发流程App...

  • IOS开发问题索引(四)

    全系列文章索引: IOS开发问题索引(一) IOS开发问题索引(二) IOS开发问题索引(三) IOS开发问题索引...

  • IOS开发问题索引(八)

    全系列文章索引: IOS开发问题索引(一) IOS开发问题索引(二) IOS开发问题索引(三) IOS开发问题索引...

网友评论

      本文标题:iOS开发折线图

      本文链接:https://www.haomeiwen.com/subject/zmotmxtx.html