iOS动画图表

效果图

前言

最近做手表手环类app比较多,这种类型的app有个特点就是最喜欢用图表去展示数据。接到公司项目的时候看到图表第一时间就网上找找有没有合适的,这样既省时又高效。但是搜索一圈之后发现都没有找到满意,所以最后决定还是自己动手写一个出来,借鉴了UUChart的写法。

调用方式

YzcChartView *view = [[YzcChartView alloc] initWithFrame:CGRectMake(10, 100,  self.view.frame.size.width, 200)];
    view.style         = YzcLineGrid;
    view.isdrawLine    = YES;  //网格线
    view.isDrawPoint   = YES;  //圆点
    view.isShadow      = YES;  //阴影填充
    view.unit          = @"bpm";  //左边的单位
    view.lineColor     = [UIColor redColor];
    view.pointColor    = [UIColor orangeColor];
    view.yLabels       = @[@"60",@"70",@"140",@"80",@"120",@"140",@"80",@"120",@"140",@"80",@"120",@"80",@"120",@"80",@"120"];
    view.xLabels       = @[@"0min",@"30min",@"60min",@"90min",@"120min",@"60min",@"90min",@"120min",@"60min",@"90min",@"120min",@"120min",@"60min",@"90min",@"120min"];
    [view strokeChart];
    [self.scrollView addSubview:view];

代码实现

绘画表格竖线和X轴刻度

- (void)setXLabels:(NSArray *)xLabels
{

    _xLabels = xLabels;
    CGFloat num = 0;
    if (xLabels.count>=20) {
        num=20.0;
    }else if (xLabels.count<=1){
        num=1.0;
    }else{
        num = xLabels.count;
    }
    _xLabelWidth = (self.myScrollView.frame.size.width - UUYLabelwidth * 0.5)/(self.style ? 23 : 10);
    
    NSInteger count = xLabels.count > 10 ? xLabels.count : 10;
    count = self.style ? 24 : count;
    for (int i=0; i<count; i++) {
        
        if (self.style == YzcLineGrid) {
            
            if (i%2 == 0) {
                
                NSString *labelText = xLabels[i];
                YzcLabel * label = [[YzcLabel alloc] initWithFrame:CGRectMake(i * _xLabelWidth+UUYLabelwidth*0.5-10, self.frame.size.height - UULabelHeight, _xLabelWidth, UULabelHeight)];
                label.text = labelText;
                [self.myScrollView addSubview:label];
            }
        }else{
            if (i%6 == 0) {
                YzcLabel * label = [[YzcLabel alloc] initWithFrame:CGRectMake(i * _xLabelWidth+UUYLabelwidth*0.5, self.frame.size.height - UULabelHeight, _xLabelWidth*2, UULabelHeight)];
                label.text = [NSString stringWithFormat:@"%02d:00",i];
                [self.myScrollView addSubview:label];
            }
        }
        
        
    }
    
    if (self.style == YzcLineNone) {
        //画底部的点
        for (int i=0; i<24; i++) {
        
            [self addPoint:CGPointMake(UUYLabelwidth+i*_xLabelWidth,self.frame.size.height-UULabelHeight-10)];
        }
        
        float max = ((count-1)*_xLabelWidth + chartMargin)+_xLabelWidth;
        self.myScrollView.contentSize = CGSizeMake(max+10, 0);
    }else{
        
        //画竖线
        for (int i=0; i<count; i++) {
            CAShapeLayer *shapeLayer = [CAShapeLayer layer];
            UIBezierPath *path = [UIBezierPath bezierPath];
            [path moveToPoint:CGPointMake(UUYLabelwidth+i*_xLabelWidth,0)];
            [path addLineToPoint:CGPointMake(UUYLabelwidth+i*_xLabelWidth,self.frame.size.height-UULabelHeight-10)];
            [path closePath];
            shapeLayer.path = path.CGPath;
            shapeLayer.strokeColor =  self.yLineColor ? self.yLineColor.CGColor : [[[UIColor blackColor] colorWithAlphaComponent:0.1] CGColor];
            shapeLayer.fillColor = [[UIColor whiteColor] CGColor];
            shapeLayer.lineWidth = 1;
            [self.myScrollView.layer addSublayer:shapeLayer];
        }
        
        float max = ((count-1)*_xLabelWidth + chartMargin)+_xLabelWidth;
        if (self.myScrollView.frame.size.width < max-10) {
            self.myScrollView.contentSize = CGSizeMake(max+10, 0);
        }
    }
    

}

绘画横线和Y轴的刻度

- (void)setYLabels:(NSArray *)yLabels
{
    _yLabels = yLabels;
    _xLabelWidth = (self.myScrollView.frame.size.width - UUYLabelwidth * 0.5)/(self.style ? 23 : 10);
  
    CGFloat _yValueMax = [[self.yLabels valueForKeyPath:@"@max.floatValue"] floatValue];
    CGFloat _yValueMin = [[self.yLabels valueForKeyPath:@"@min.floatValue"] floatValue];
    float level = (_yValueMax-_yValueMin) /3;
    CGFloat chartCavanHeight = self.frame.size.height - UULabelHeight*3;
    CGFloat levelHeight = chartCavanHeight /3;
    
    for (int i=0; i<4; i++) {
        YzcLabel * label = [[YzcLabel alloc] initWithFrame:CGRectMake(0,chartCavanHeight - i * levelHeight, UUYLabelwidth+20, UULabelHeight)];
        label.text = [NSString stringWithFormat:@"%d%@",(int)(level * i+_yValueMin),self.unit ? self.unit : @""];
        [self addSubview:label];
    }
    
    if (self.style == YzcLineGrid) {
        //画横线
        for (int i=0; i<4; i++) {
            CAShapeLayer *shapeLayer = [CAShapeLayer layer];
            UIBezierPath *path = [UIBezierPath bezierPath];
            [path moveToPoint:CGPointMake(20,UULabelHeight+i*levelHeight)];
            [path addLineToPoint:CGPointMake(self.frame.size.width,UULabelHeight+i*levelHeight)];
            [path closePath];
            shapeLayer.path = path.CGPath;
            shapeLayer.strokeColor = self.xlineColor ? self.xlineColor.CGColor :[[[UIColor blackColor] colorWithAlphaComponent:0.1] CGColor];
            shapeLayer.fillColor = [[UIColor whiteColor] CGColor];
            shapeLayer.lineWidth = 1;
            [self.layer insertSublayer:shapeLayer atIndex:0];
        }
        
        float max = (([yLabels count]-1)*_xLabelWidth + chartMargin)+_xLabelWidth;
        if (self.myScrollView.frame.size.width < max-10) {
            self.myScrollView.contentSize = CGSizeMake(max+10, 0);
        }
    }
}

渐变图层的填充方法
渐变图层的颜色设置成折线的颜色一样,然后调颜色透明度即可,这样看起来就会有渐变效果,最后加上动画。

/**
 添加渐变图层
 */
- (void)addGradientLayer:(UIBezierPath *)bezier1
{
    CAShapeLayer *shadeLayer = [CAShapeLayer layer];
    shadeLayer.path = bezier1.CGPath;
    shadeLayer.fillColor = [UIColor greenColor].CGColor; 
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(5, 0, 0, self.myScrollView.bounds.size.height-20);
    gradientLayer.cornerRadius = 5;
    gradientLayer.masksToBounds = YES;
    gradientLayer.colors = @[(__bridge id)[self.lineColor colorWithAlphaComponent:0.4].CGColor,(__bridge id)[self.lineColor colorWithAlphaComponent:0.0].CGColor]; //如果需要调图层颜色,只需要修改这里即可
    gradientLayer.locations = @[@(0.1f),@(1.0f)];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    
    CALayer *baseLayer = [CALayer layer];
    [baseLayer addSublayer:gradientLayer];
    [baseLayer setMask:shadeLayer];
    [self.myScrollView.layer insertSublayer:baseLayer atIndex:0];
    
    CABasicAnimation *anmi1 = [CABasicAnimation animation];
    anmi1.keyPath = @"bounds";
    anmi1.duration = _yLabels.count*0.4;
    anmi1.toValue = [NSValue valueWithCGRect:CGRectMake(5, 0, 2*self.lastPoint.x, self.myScrollView.bounds.size.height-20)];
    anmi1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    anmi1.fillMode = kCAFillModeForwards;
    anmi1.autoreverses = NO;
    anmi1.removedOnCompletion = NO;
    
    [gradientLayer addAnimation:anmi1 forKey:@"bounds"];

}

最后就是绘画数据

- (void)strokeChart
{
    
    BOOL isShowMaxAndMinPoint = YES;
    NSInteger maxValue = [[_yLabels valueForKeyPath:@"@max.intValue"] integerValue];
    NSInteger minValue = [[_yLabels valueForKeyPath:@"@min.intValue"] integerValue];

    //划线
    CAShapeLayer *_chartLine = [CAShapeLayer layer];
    _chartLine.lineCap = kCALineCapRound;   //设置线条拐角帽的样式
    _chartLine.lineJoin = kCALineJoinRound; //设置两条线连结点的样式
    _chartLine.fillColor   = [[UIColor clearColor] CGColor];
    _chartLine.lineWidth   = 2.0;
    _chartLine.strokeEnd   = 0.0;
    [self.myScrollView.layer addSublayer:_chartLine];
    
    //线
    UIBezierPath *progressline = [UIBezierPath bezierPath];
    CGFloat firstValue = [[_yLabels objectAtIndex:0] floatValue];
    CGFloat xPosition = UUYLabelwidth;
    CGFloat chartCavanHeight = self.frame.size.height - UULabelHeight*3;
    
    //第一个点
    float grade = ((float)firstValue-minValue) / ((float)maxValue-minValue);
    if (isnan(grade)) {
        grade = 0;
    }
    CGPoint firstPoint = CGPointMake(xPosition, chartCavanHeight - grade * chartCavanHeight+UULabelHeight);
    [progressline moveToPoint:firstPoint];
    [progressline setLineWidth:2.0];
    [progressline setLineCapStyle:kCGLineCapRound];
    [progressline setLineJoinStyle:kCGLineJoinRound];
    
    //遮罩层形状
    UIBezierPath *bezier1 = [UIBezierPath bezierPath];
    bezier1.lineCapStyle = kCGLineCapRound;
    bezier1.lineJoinStyle = kCGLineJoinMiter;
    [bezier1 moveToPoint:firstPoint];
    self.originPoint = firstPoint;       //记录原点 
    
    NSInteger index = 0;
    for (NSString * valueString in _yLabels) {
        
        float grade =([valueString floatValue] - minValue) / ((float)maxValue-minValue);
        if (isnan(grade)) {
            grade = 0;
        }
        CGPoint point = CGPointMake(xPosition+index*_xLabelWidth, chartCavanHeight - grade * chartCavanHeight+UULabelHeight);
        
        if (index != 0) {
            
            [progressline addCurveToPoint:point controlPoint1:CGPointMake((point.x+self.prePoint.x)/2, self.prePoint.y) controlPoint2:CGPointMake((point.x+self.prePoint.x)/2, point.y)];
             [progressline moveToPoint:point];
            [bezier1 addCurveToPoint:point controlPoint1:CGPointMake((point.x+self.prePoint.x)/2, self.prePoint.y) controlPoint2:CGPointMake((point.x+self.prePoint.x)/2, point.y)];
        }
        
        if (index == _yLabels.count-1) {
            self.lastPoint = point;          //记录最后一个点
        }
        if (self.isDrawPoint) {
            [self addPoint:point
                     index:index
                    isShow:isShowMaxAndMinPoint
                     value:[valueString floatValue]];
        }
        index += 1;
        self.prePoint = point;
    }
    
    if (self.isdrawLine) {
        
        _chartLine.path = progressline.CGPath;
        _chartLine.strokeColor = self.lineColor ? self.lineColor.CGColor : [UIColor greenColor].CGColor;
        
        CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        pathAnimation.duration = _yLabels.count*0.4;
        pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
        pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
        pathAnimation.autoreverses = NO;
        [_chartLine addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
        
        _chartLine.strokeEnd = 1.0;
        
        if (self.isShadow) {
            
            [bezier1 addLineToPoint:CGPointMake(self.lastPoint.x, self.frame.size.height - UULabelHeight*2)];
            [bezier1 addLineToPoint:CGPointMake(self.originPoint.x, self.frame.size.height - UULabelHeight*2)];
            [bezier1 addLineToPoint:self.originPoint];
            
            [self addGradientLayer:bezier1];
        }
        }

}

最后附上完整demo地址:https://github.com/Yzc-jason/YzcChart

简书初笔,请多多指教,欢迎发问。如果有幸你也用上了,记得点个star哈。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,723评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,080评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,604评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,440评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,431评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,499评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,893评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,541评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,751评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,547评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,619评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,320评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,890评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,896评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,137评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,796评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,335评论 2 342

推荐阅读更多精彩内容