折線圖&柱狀圖

效果圖如下:

效果圖.png

主要代碼如下 :

//用法實(shí)例
- (void)viewDidLoad {
    [super viewDidLoad];
    
    chart = [[ZYChartView alloc]initWithFrame:CGRectMake(20, 174, [UIScreen mainScreen].bounds.size.width-40, 200) delegate:self withType:Type_Line];
    [self.view addSubview:chart];
}

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger, TypeChart) {
    Type_Line,
    Type_Bar,
};

@class ZYChartView;

@protocol ZYChartViewDelegate <NSObject>

@required

- (NSArray<NSString*>*)zyChartViewLables;

@optional

@end

@interface ZYChartView : UIView

- (instancetype)initWithFrame:(CGRect)frame delegate:(id<ZYChartViewDelegate>)deleagte withType:(TypeChart)type;

@end

//
//  ZYChartView.m
//  UIChartTest
//
//  Created by JonkeyChen on 16/11/9.
//  Copyright ? 2016年 oneyd.me. All rights reserved.
//

#import "ZYChartView.h"
#import "UIColor+Extension.h"
#import "ZYShapelayer.h"
#import "ZYScrollView.h"

#define xLabelsWidth   40  //底部文字顯示寬度
#define xLabelsHeight  16   //底部文字高度
#define yLineNumber    5    //水平橫線條數(shù)
#define yColumnWidth   15 //柱狀圖寬度

@interface ZYChartView ()<UIScrollViewDelegate,ZYScrollViewDelegate>

@property (nonatomic,assign) id<ZYChartViewDelegate> deleagte;

@property (nonatomic,assign) TypeChart chartType;

@property (nonatomic,strong) ZYScrollView *scrollView;

@property (nonatomic,strong) NSArray<NSString*> *xLabelsArray;//底部標(biāo)題

@property (nonatomic,strong) NSArray<NSNumber*> *xLabelValues;//柱狀圖數(shù)據(jù)

@property (nonatomic,strong) NSArray<NSArray<NSNumber*>*> *yLabelValues;//折線圖數(shù)據(jù)

@property (nonatomic,strong) NSMutableArray<NSValue*> *touchPoints;//每列豎直范圍數(shù)組

@property (nonatomic,strong) CAShapeLayer *vertricalLine;//點(diǎn)擊事的豎線

@property (nonatomic,assign) CGFloat originY;

@end

@implementation ZYChartView

- (instancetype)initWithFrame:(CGRect)frame delegate:(id<ZYChartViewDelegate>)deleagte withType:(TypeChart)type{
    _deleagte = deleagte ;
    _chartType = type ;
    if (self = [super initWithFrame:frame]) {
        [self setUpConfigure];
    }
    return  self;
}

- (void)setUpConfigure {
    
    if (!_xLabelsArray) {
        
        _touchPoints  = [[NSMutableArray alloc]init];
        
        _xLabelsArray = @[@"1月",@"2月",@"3月",@"4月",
                          @"5月",@"6月",@"7月",@"8月",
                          @"9月",@"10月",@"11月",@"12月"];
    }
    
    if (!_xLabelValues) {
        
        //柱狀圖數(shù)據(jù)
        _xLabelValues = @[@20, @25,@19,@34,
                          @100,@79,@5, @200,
                          @100,@87,@54,@99];
    }
    
    if (!_yLabelValues) {
        
        //折線圖數(shù)據(jù)
        _yLabelValues = @[
                          @[@30, @20,@19,@34,
                            @100,@90,@100, @66,
                            @100,@87,@54,@88],
                          
                          @[@20, @30,@29,@14,
                            @90,@80,@110, @102,
                            @110,@97,@14,@90],
                          ];
    }
    
    if (!_vertricalLine) {
        
        //點(diǎn)擊的豎線
        _vertricalLine = [CAShapeLayer layer];
        _vertricalLine.lineWidth = 1;
        _vertricalLine.fillColor = [UIColor redColor].CGColor;
        _vertricalLine.strokeColor = [UIColor redColor].CGColor;
        [_scrollView.layer addSublayer:_vertricalLine];
    }

    CGFloat maxValueColum = 0 ;
    
    if (!_scrollView) {
        
        CGFloat paddingHeight = 0;
        //計(jì)算柱狀圖最大最小值
        for (int i= 0; i < _xLabelValues.count; i++) {
            if (maxValueColum <= [_xLabelValues[i] intValue]) {
                maxValueColum = [_xLabelValues[i] intValue];
            }
        }
        
        _scrollView = [[ZYScrollView alloc]init];
        _scrollView.frame = CGRectMake(0, 0,CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
        _scrollView.contentSize = CGSizeMake(xLabelsWidth * _xLabelsArray.count, CGRectGetHeight(self.bounds));
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.didDeelgate = self;
        [self addSubview:_scrollView];
        
        //底部標(biāo)簽
        _originY = CGRectGetHeight(_scrollView.frame) - xLabelsHeight;
        for (int i = 0 ; i < _xLabelsArray.count; i++) {
            UILabel *xLabel = [[UILabel alloc]init];
            xLabel.frame = CGRectMake(0 + xLabelsWidth*i, _originY, xLabelsWidth, xLabelsHeight);
            xLabel.font = [UIFont systemFontOfSize:10];
            xLabel.textAlignment = NSTextAlignmentCenter;
            xLabel.text = _xLabelsArray[i];
            xLabel.textColor = [UIColor nomoarlBlack];
            [_scrollView addSubview:xLabel];
            
            [_touchPoints addObject:[NSValue valueWithCGRect:CGRectMake(xLabelsWidth*i, 0, xLabelsWidth, _originY)]];
        }
        
        //豎線--網(wǎng)格
        for (int i = 0; i < _xLabelsArray.count; i++) {
            UIBezierPath *beizerPath = [UIBezierPath bezierPath];
            [beizerPath moveToPoint:CGPointMake(xLabelsWidth/2.0+xLabelsWidth*i, 0)];
            [beizerPath addLineToPoint:CGPointMake(xLabelsWidth/2.0+xLabelsWidth*i, _originY)];
            
            CAShapeLayer *shapeLayer = [CAShapeLayer layer];
            shapeLayer.path = beizerPath.CGPath;
            shapeLayer.fillColor = [UIColor lightGrayColor].CGColor;
            shapeLayer.strokeColor = [UIColor lightGrayColor].CGColor;
            shapeLayer.lineWidth = 0.5;
            [_scrollView.layer addSublayer:shapeLayer];
        }
        
        //橫線--網(wǎng)格
        paddingHeight = _originY/yLineNumber;
        for (int i = 0; i < yLineNumber; i++) {
            UIBezierPath *beizerPath = [UIBezierPath bezierPath];
            [beizerPath moveToPoint:CGPointMake(xLabelsWidth/2.0, i*paddingHeight+paddingHeight)];
            [beizerPath addLineToPoint:CGPointMake(_scrollView.contentSize.width-xLabelsWidth/2.0,i*paddingHeight+paddingHeight)];
            
            CAShapeLayer *shapeLayer = [CAShapeLayer layer];
            shapeLayer.path = beizerPath.CGPath;
            shapeLayer.fillColor = [UIColor lightGrayColor].CGColor;
            shapeLayer.strokeColor = [UIColor lightGrayColor].CGColor;
            shapeLayer.lineWidth = 0.5;
            [_scrollView.layer addSublayer:shapeLayer];
        }
    }
    
    //柱狀圖
    for (int i = 0; i < _xLabelValues.count ; i++) {
        
        //柱子高度
        CGFloat heigth = ([_xLabelValues[i] intValue]/maxValueColum)*_originY;
        
        UIBezierPath *beizerPath = [UIBezierPath bezierPath];
        [beizerPath moveToPoint:CGPointMake(xLabelsWidth/2.0+xLabelsWidth*i, _originY + 0.5)];
        [beizerPath addLineToPoint:CGPointMake(xLabelsWidth/2.0+xLabelsWidth*i,_originY - heigth)];
        
        ZYShapelayer *shapeLayer = [ZYShapelayer layer];
        shapeLayer.path = beizerPath.CGPath;
        shapeLayer.fillColor = [UIColor blueColor].CGColor;
        shapeLayer.strokeColor = [UIColor blueColor].CGColor;
        shapeLayer.lineWidth = yColumnWidth;
        [_scrollView.layer addSublayer:shapeLayer];
        
        CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        pathAnimation.duration = 1.5;
        pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        pathAnimation.fromValue = @0.0;
        pathAnimation.toValue = @1.0;
        pathAnimation.autoreverses = NO;
        [shapeLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
        
        shapeLayer.strokeEnd = 2.0;
    }
    
    //折線圖
    for (int i = 0; i < _yLabelValues.count; i++) {
        
        CGFloat maxVlaueLine  = 0 ;
        //計(jì)算線形圖最大最小值
        for (int j = 0 ; j < _yLabelValues[i].count; j++) {
            if (maxVlaueLine <= [_yLabelValues[i][j] intValue]) {
                maxVlaueLine = [_yLabelValues[i][j] intValue];
            }
        }
        
        UIBezierPath *bezierPath = [UIBezierPath bezierPath];
        for (int j = 0; j < _yLabelValues[i].count; j ++) {
            CGFloat heigth = ([_yLabelValues[i][j] integerValue]/maxVlaueLine)*_originY;
            CGPoint point = CGPointMake(xLabelsWidth/2.0+xLabelsWidth*j,_originY - heigth);
            if (j == 0) {
                [bezierPath moveToPoint:point];
            } else {
                [bezierPath addLineToPoint:point];
            }
        }
        
        ZYShapelayer *shapeLayer = [ZYShapelayer layer];
        shapeLayer.path = bezierPath.CGPath;
        shapeLayer.strokeColor = i==0?[UIColor zyRed].CGColor:[UIColor yellow].CGColor; // 線的顏色
        shapeLayer.fillColor = [UIColor clearColor].CGColor; // 填充色
        shapeLayer.lineWidth = 2;
        [_scrollView.layer addSublayer:shapeLayer];
        
        CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        pathAnimation.duration = 1.5;
        pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        pathAnimation.fromValue = @0.0;
        pathAnimation.toValue = @1.0;
        pathAnimation.autoreverses = NO;
        [shapeLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
        
        shapeLayer.strokeEnd = 2.0;
    }
}

///點(diǎn)擊網(wǎng)格內(nèi)的空白處
- (void)zyScrollView:(ZYScrollView *)scrollView withDidPoint:(CGPoint)point {
    for (int i = 0; i < _touchPoints.count; i++) {
        CGRect value = [_touchPoints[i] CGRectValue];
        if (CGRectContainsPoint(value, point)) {
            [_vertricalLine removeFromSuperlayer];
            UIBezierPath *beizerPath = [UIBezierPath bezierPath];
            [beizerPath moveToPoint:CGPointMake(xLabelsWidth/2.0+xLabelsWidth*i, 0)];
            [beizerPath addLineToPoint:CGPointMake(xLabelsWidth/2.0+xLabelsWidth*i, _originY)];
            _vertricalLine.path = beizerPath.CGPath;
            [_scrollView.layer addSublayer:_vertricalLine];
        }
    }
}

@end

[https://github.com/JonkeyChen/UIBezierPathTest]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末粹胯,一起剝皮案震驚了整個(gè)濱河市月而,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異冀自,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)秒啦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門(mén)熬粗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人余境,你說(shuō)我怎么就攤上這事驻呐。” “怎么了芳来?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵含末,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我即舌,道長(zhǎng)佣盒,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任顽聂,我火速辦了婚禮肥惭,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘芜飘。我一直安慰自己务豺,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開(kāi)白布嗦明。 她就那樣靜靜地躺著,像睡著了一般蚪燕。 火紅的嫁衣襯著肌膚如雪娶牌。 梳的紋絲不亂的頭發(fā)上奔浅,一...
    開(kāi)封第一講書(shū)人閱讀 51,287評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音诗良,去河邊找鬼汹桦。 笑死,一個(gè)胖子當(dāng)著我的面吹牛鉴裹,可吹牛的內(nèi)容都是我干的舞骆。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼径荔,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼督禽!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起总处,我...
    開(kāi)封第一講書(shū)人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤狈惫,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后鹦马,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體胧谈,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年荸频,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了菱肖。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡旭从,死狀恐怖稳强,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情遇绞,我是刑警寧澤键袱,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站摹闽,受9級(jí)特大地震影響蹄咖,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜付鹿,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一澜汤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧舵匾,春花似錦俊抵、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春谎替,著一層夾襖步出監(jiān)牢的瞬間偷溺,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工钱贯, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留挫掏,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓秩命,卻偏偏與公主長(zhǎng)得像尉共,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子弃锐,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • afinalAfinal是一個(gè)android的ioc袄友,orm框架 https://github.com/yangf...
    passiontim閱讀 15,429評(píng)論 2 45
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,099評(píng)論 25 707
  • 今天整理以前寫(xiě)過(guò)的文章,看到這一篇后不由得笑出聲來(lái)拿愧。孩子是上帝送給我們的天使杠河,真的是一眨眼間我們家庭就進(jìn)入了小別離...
    平靜75閱讀 239評(píng)論 0 1
  • 那時(shí)候,車(chē)馬很慢浇辜,郵件也很慢券敌,一生只夠愛(ài)一個(gè)人。 我很想你柳洋,于是對(duì)你說(shuō)“早安午安晚安” 我特別特別的想你待诅,于是寄一...
    OLYmpcs閱讀 186評(píng)論 0 0
  • 昨天讀到琢磨先生公眾號(hào)的一篇關(guān)于賺錢(qián)的文章,結(jié)合我一直以來(lái)關(guān)注的李笑來(lái)的《通往財(cái)富自由之路》總結(jié)幾點(diǎn)自己的想法熊镣。 ...
    Evan0827閱讀 228評(píng)論 0 0