123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- //
- // SSWLineChartView.m
- // SSWCharts
- //
- // Created by WangShaoShuai on 2018/5/3.
- // Copyright © 2018年 com.sswang.www. All rights reserved.
- //
- #import "SSWLineChartView.h"
- @interface SSWLineChartView (){
- CGFloat _totalWidth;
- CGFloat _totalHeight;
- CAShapeLayer *_AxiasLineLayer;
- CAShapeLayer *_lineLayer;
- }
- @property(nonatomic)UIScrollView *scrollView;
- @property(nonatomic)UIView *contentView;
- @property(nonatomic)NSMutableArray *barsStartPointsArr;
- @property(nonatomic)NSMutableArray *barsEndPointsArr;
- @property(nonatomic)NSMutableArray *linePointPathArr;
- @property(nonatomic)NSMutableArray *linePointLayerArr;
- @property(nonatomic)UILabel *unitLab;
- @property(nonatomic)UIButton *dayBtn;
- @property(nonatomic)UIButton *hourBtn;
- @property(nonatomic)UIView *lineView;
- @property(nonatomic)NSString *changeTime;
- @end
- @implementation SSWLineChartView
- -(instancetype)initWithChartType:(SSWChartsType)type{
- self = [super initWithChartType:type];
- if (self) {
- [self setUp];
- _changeTime = @"1";
- }
- return self;
- }
- -(void)setUp{
- self.xValuesArr = [@[] mutableCopy];
- self.yValuesArr = [@[] mutableCopy];
- self.barsStartPointsArr = [@[] mutableCopy];
- self.barsEndPointsArr = [@[] mutableCopy];
- self.linePointPathArr = [@[] mutableCopy];
- self.linePointLayerArr = [@[] mutableCopy];
- self.barWidth =SCREEN_WIDTH/6-16;
- self.gapWidth = 5;
- self.yScaleValue = 100;
- self.yAxisCount = 5;
- self.showEachYValus=YES;
- self.lineColor = [UIColor colorWithHexString:ThemeColor];
- self.userInteractionEnabled=YES;
- [self addSubview:self.scrollView];
- [self.scrollView addSubview:self.contentView];
- [self.scrollView addSubview:self.unitLab];
- [self addSubview:self.lineView];
- [self addSubview:self.hourBtn];
- [self addSubview:self.dayBtn];
- self.scrollView.userInteractionEnabled=YES;
- [self addTap];
- [self.contentView addSubview:self.bubbleLab];
-
- }
- -(void)setYValuesArr:(NSMutableArray *)yValuesArr
- {
- _yValuesArr = yValuesArr;
- //获取最大的
- NSInteger maxValue = [[yValuesArr valueForKeyPath:@"@max.floatValue"] floatValue];
- NSInteger keduValu =(maxValue+(5-maxValue%5))/5;
- self.yScaleValue = keduValu;
- [self layoutSubviews];
- [self draWUpdate];
- }
- -(void)addTap{
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
- [self.contentView addGestureRecognizer:tap];
- }
- -(void)tap:(UITapGestureRecognizer *)tap{
- CGPoint point = [tap locationInView:self.contentView];
- for (UIBezierPath *path in self.linePointPathArr) {
- if ([path containsPoint:point]) {
- NSInteger index = [self.linePointPathArr indexOfObject:path];
- CGPoint endPoint = [self.barsEndPointsArr[index] CGPointValue];
- // NSLog(@"点击了第%ld个柱形图",index);
- // self.bubbleLab.hidden=NO;
- // self.bubbleLab.text = self.yValuesArr[index];
- // self.bubbleLab.center = CGPointMake(endPoint.x, endPoint.y-10);
- if (self.delegate && [self.delegate respondsToSelector:@selector(SSWChartView:didSelectIndex:)]) {
- [self.delegate SSWChartView:self didSelectIndex:index];
- }
- }
- }
- }
- -(void)setUnit:(NSString *)unit{
- self.unitLab.hidden=NO;
- _unit=unit;
- self.unitLab.text =unit;
- }
- -(UIScrollView *)scrollView{
- if (!_scrollView) {
- _scrollView = [[UIScrollView alloc]init];
- _scrollView.showsVerticalScrollIndicator = NO;
- _scrollView.showsHorizontalScrollIndicator=NO;
- _scrollView.clipsToBounds=NO;
- }
- return _scrollView;
- }
- -(UIView *)contentView{
- if (!_contentView) {
- _contentView = [[UIView alloc]init];
- // _contentView.clipsToBounds = YES;
- }
- return _contentView;
- }
- -(UILabel *)unitLab{
- if (!_unitLab) {
- _unitLab = [[UILabel alloc]init];
- _unitLab.font = [UIFont systemFontOfSize:12];
- _unitLab.textColor=[UIColor colorWithHexString:fontColor];
-
-
- }
- return _unitLab;
- }
- -(UIButton *)hourBtn
- {
- if (!_hourBtn) {
- _hourBtn =[[UIButton alloc]init];
- _hourBtn.titleLabel.font =[UIFont systemFontOfSize:12];
- [_hourBtn setTitle:@"1小时" forState:UIControlStateNormal];
-
- [_hourBtn setTitleColor:[UIColor colorWithHexString:ThemeColor] forState:UIControlStateNormal];
- _hourBtn.tag = 1001;
- [_hourBtn addTarget:self action:@selector(changeValues:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _hourBtn;
- }
- -(UIButton *)dayBtn
- {
- if (!_dayBtn) {
- _dayBtn =[[UIButton alloc]init];
- _dayBtn.titleLabel.font =[UIFont systemFontOfSize:12];
- _dayBtn.tag = 2001;
- [_dayBtn setTitle:@"24小时" forState:UIControlStateNormal];
- [_dayBtn setTitleColor:[UIColor colorWithHexString:@"#bbb9bb"] forState:UIControlStateNormal];
- [_dayBtn addTarget:self action:@selector(changeValues:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _dayBtn;
- }
- -(UIView *)lineView
- {
- if (!_lineView) {
- _lineView =[[UIView alloc]init];
- // _lineView.backgroundColor=[UIColor colorWithHexString:ThemeColor];
-
- }
- return _lineView;
- }
- -(void)layoutSubviews{
- self.scrollView.frame =CGRectMake(0, 40, self.bounds.size.width, self.bounds.size.height-50);
- self.unitLab.frame = CGRectMake(20, -40, 30, 20);
-
- self.dayBtn.frame=CGRectMake(self.bounds.size.width-70, 0, 60, 15);
- self.hourBtn.frame=CGRectMake(self.bounds.size.width-140, 0, 60, 15);
- if (_changeTime.integerValue == 1) {
- self.lineView.frame=CGRectMake(self.bounds.size.width-117, 15, 14, 1);
- }else{
- self.lineView.frame=CGRectMake(self.bounds.size.width-47, 15, 14, 1);
- }
- // [self draWUpdate];
- }
- -(void)draWUpdate
- {
- _totalWidth= self.width-60;
- _totalHeight=self.scrollView.bounds.size.height-30;
- self.scrollView.contentSize = CGSizeMake(30+_totalWidth, 0);
-
- self.contentView.frame = CGRectMake(40,10, _totalWidth,_totalHeight);
- [self drawLineChart];
- }
- //开始绘制图表
- -(void)drawLineChart{
- [self clear];
- [self drawAxis];
- [self addYAxisLabs];
- [self addXAxisLabs];
- [self calculationTheLinePoints];
- [self addLine];
- [self addLinePoints];
- [self showBarChartYValus];
- }
- //触发界面布局之前清除掉之前的绘制
- -(void)clear{
- [self.barsStartPointsArr removeAllObjects];
- [self.barsEndPointsArr removeAllObjects];
- for (CAShapeLayer *layer in self.linePointLayerArr) {
- [layer removeFromSuperlayer];
- }
- [self.linePointLayerArr removeAllObjects];
- [_AxiasLineLayer removeFromSuperlayer];
- [_lineLayer removeFromSuperlayer];
- _AxiasLineLayer=nil;
- _lineLayer=nil;
- for (UIView *view in self.contentView.subviews) {
- if([view isEqual:self.unitLab])continue;
- [view removeFromSuperview];
- }
-
- }
- //画坐标轴
- -(void)drawAxis{
- UIBezierPath *path = [UIBezierPath bezierPath];
-
- // //画左上角的箭头
- // [path moveToPoint:CGPointMake(-5, 5)];
- // [path addLineToPoint:CGPointMake(0, 0)];
- // [path addLineToPoint:CGPointMake(5, 5)];
- //画右上角的箭头
- // [path moveToPoint:CGPointMake(_totalWidth-5, _totalHeight-5)];
- // [path addLineToPoint:CGPointMake(_totalWidth, _totalHeight)];
- // [path addLineToPoint:CGPointMake(_totalWidth-5, _totalHeight+5)];
- //画y轴刻度
- // for (int i = 0; i<self.yAxisCount; i++) {
- // [path moveToPoint:CGPointMake(0, i*(_totalHeight/self.yAxisCount))];
- // [path addLineToPoint:CGPointMake(5, i*(_totalHeight/self.yAxisCount))];
- // }
- //画x轴的刻度
- for (int i = 0; i<self.xValuesArr.count; i++) {
- CGPoint startPoint = CGPointMake(i*(self.barWidth+self.gapWidth)+self.gapWidth+self.barWidth/2, _totalHeight);
- [self.barsStartPointsArr addObject:[NSValue valueWithCGPoint:startPoint]];
- [path moveToPoint:startPoint];
- [path addLineToPoint:CGPointMake(i*(self.barWidth+self.gapWidth)+self.gapWidth+self.barWidth/2, _totalHeight-5)];
- }
- // //先画整体的线
- [path moveToPoint:CGPointMake(0, _totalHeight)];
- [path addLineToPoint:CGPointMake(0, _totalHeight)];
- [path addLineToPoint:CGPointMake(_totalWidth, _totalHeight)];
-
- CAShapeLayer *lineLayer = [CAShapeLayer layer];
- lineLayer.strokeColor = [UIColor colorWithHexString:@"#606060"].CGColor;
- lineLayer.lineWidth = 0.5;
- lineLayer.path = path.CGPath;
- lineLayer.fillColor = [UIColor clearColor].CGColor;
- [self.contentView.layer addSublayer:lineLayer];
-
- _AxiasLineLayer = lineLayer;
- }
- //给y轴添加刻度显示
- -(void)addYAxisLabs{
- for (int i =self.yAxisCount ; i>0; i--) {
- CGFloat yAxis = self.yScaleValue*i;
- UILabel *lab = [[UILabel alloc]init];
- lab.frame = CGRectMake(-5, (self.yAxisCount-i)*(_totalHeight/self.yAxisCount)-10, -25, 20);
- lab.text = [NSString stringWithFormat:@"%.f",yAxis];
- lab.font = [UIFont systemFontOfSize:10];
- lab.textAlignment = NSTextAlignmentRight;
-
- UIView *views =[[UIView alloc]initWithFrame:CGRectMake(20, (self.yAxisCount-i)*(_totalHeight/self.yAxisCount), _totalWidth-30, 0.5)];
- [self drawDashLine:views lineLength:_totalWidth lineSpacing:0.5 lineColor:[UIColor colorWithHexString:@"878585"]];
-
- [self.contentView addSubview:views];
- [self.contentView addSubview:lab];
- }
- }
- /**
- ** lineView: 需要绘制成虚线的view
- ** lineLength: 虚线的宽度
- ** lineSpacing: 虚线的间距
- ** lineColor: 虚线的颜色
- **/
- - (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor
- {
- CAShapeLayer *shapeLayer = [CAShapeLayer layer];
- [shapeLayer setBounds:lineView.bounds];
- [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
- [shapeLayer setFillColor:[UIColor clearColor].CGColor];
-
- // 设置虚线颜色为
- [shapeLayer setStrokeColor:lineColor.CGColor];
-
- // 设置虚线宽度
- [shapeLayer setLineWidth:0.2];
- [shapeLayer setLineJoin:kCALineJoinRound];
-
- // 设置线宽,线间距
- [shapeLayer setLineDashPattern:
-
- [NSArray arrayWithObjects:[NSNumber numberWithInt:3],
-
- [NSNumber numberWithInt:1],nil]];
-
- // 设置路径
- CGMutablePathRef path = CGPathCreateMutable();
- CGPathMoveToPoint(path, NULL, 0, 0);
- CGPathAddLineToPoint(path, NULL, CGRectGetWidth(lineView.frame), 0);
-
- [shapeLayer setPath:path];
- CGPathRelease(path);
-
- // 把绘制好的虚线添加上来
- [lineView.layer addSublayer:shapeLayer];
- }
- //给x轴添加刻度显示
- -(void)addXAxisLabs{
- for (int i = 0 ; i<self.xValuesArr.count; i++) {
- CGPoint point = [self.barsStartPointsArr[i] CGPointValue];
- UILabel *lab = [[UILabel alloc]init];
- lab.bounds = CGRectMake(0, 0, self.barWidth+self.gapWidth*4/5, 20);
- lab.center = CGPointMake(point.x, point.y+lab.bounds.size.height/2);
- lab.text = [NSString stringWithFormat:@"%@",self.xValuesArr[i]];
- lab.textColor=[UIColor colorWithHexString:fontColor];
- lab.font = [UIFont systemFontOfSize:12];
- lab.textAlignment = NSTextAlignmentCenter;
- [self.contentView addSubview:lab];
- }
- }
- //计算折线的点坐标
- -(void)calculationTheLinePoints{
- for (int i = 0 ; i<self.yValuesArr.count; i++) {
- CGFloat Y = [(NSString *)self.yValuesArr[i] floatValue];
- CGFloat barHeight = (Y/(self.yAxisCount*self.yScaleValue))*_totalHeight;
- CGPoint startPoint = [self.barsStartPointsArr[i] CGPointValue];
- CGPoint endPoint = CGPointMake(startPoint.x, startPoint.y-barHeight);
- [self.barsEndPointsArr addObject:[NSValue valueWithCGPoint:endPoint]];
- }
- }
- //添加折线的点
- -(void)addLinePoints{
- for (int i = 0 ; i<self.xValuesArr.count; i++) {
- CGPoint point = [self.barsEndPointsArr[i] CGPointValue];
- UIBezierPath *path = [UIBezierPath bezierPath];
- [path addArcWithCenter:point radius:2 startAngle:0 endAngle:M_PI*2 clockwise:YES];
- // [path closePath];
- [self.linePointPathArr addObject:path];
-
- CAShapeLayer *layer = [CAShapeLayer layer];
- layer.strokeColor = self.lineColor.CGColor;
- layer.fillColor = self.lineColor.CGColor;
- layer.lineWidth=1;
- layer.path = path.CGPath;
- [layer addAnimation:[self animationWithDuration:0.3*i] forKey:nil];
- [self.contentView.layer addSublayer:layer];
- [self.linePointLayerArr addObject:layer];
- }
- }
- //添加折线
- -(void)addLine{
- UIBezierPath *linePath = [UIBezierPath bezierPath];
- if (self.barsStartPointsArr.count > 0) {
- [linePath moveToPoint:[self.barsEndPointsArr[0] CGPointValue]];
- }
-
- for (int i = 1; i<self.barsEndPointsArr.count; i++) {
- [linePath addLineToPoint:[self.barsEndPointsArr[i] CGPointValue]];
- }
-
- CAShapeLayer *lineLayer = [CAShapeLayer layer];
- lineLayer.strokeColor = self.lineColor.CGColor;
- lineLayer.lineWidth = 1;
- lineLayer.fillColor = [UIColor clearColor].CGColor;
- lineLayer.path = linePath.CGPath;
- [lineLayer addAnimation:[self animationWithDuration:0.3*self.barsEndPointsArr.count] forKey:nil];
- [self.contentView.layer addSublayer:lineLayer];
- _lineLayer = lineLayer;
- }
- //添加动画
- -(CABasicAnimation *)animationWithDuration:(CFTimeInterval)duration{
- CABasicAnimation *anmiation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
- anmiation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- anmiation.duration =duration;
- anmiation.fromValue=@(0);
- anmiation.toValue = @(1);
- return anmiation;
- }
- //显示每个柱形图的值
- -(void)showBarChartYValus{
- if(!self.showEachYValus)return;
- for (int i = 0; i<self.xValuesArr.count; i++) {
- CGPoint point = [self.barsEndPointsArr[i] CGPointValue];
- UILabel *lab = [[UILabel alloc]init];
- lab.textColor = self.lineColor;
- lab.font = [UIFont systemFontOfSize:10];
- lab.text = self.yValuesArr[i];
- lab.textAlignment = NSTextAlignmentCenter;
- lab.bounds = CGRectMake(0, 0, self.barWidth+self.gapWidth*4/5, 20);
- lab.center = CGPointMake(point.x, point.y-10);
- [self.contentView addSubview:lab];
- }
- }
- #pragma mark---小时和天切换
- -(void)changeValues:(UIButton *)btn
- {
- if (btn.tag == 1001) {//小时
- self.lineView.frame=CGRectMake(self.bounds.size.width-117, 15, 14, 1);
- [self.hourBtn setTitleColor:[UIColor colorWithHexString:ThemeColor] forState:UIControlStateNormal];
- [self.dayBtn setTitleColor:[UIColor colorWithHexString:@"#bbb9bb"] forState:UIControlStateNormal];
- // [self OrderStatistics:@"1"];
- _changeTime = @"1";
- }else{
- self.lineView.frame=CGRectMake(self.bounds.size.width-47, 15, 14, 1);
- [self.dayBtn setTitleColor:[UIColor colorWithHexString:ThemeColor] forState:UIControlStateNormal];
- [self.hourBtn setTitleColor:[UIColor colorWithHexString:@"#bbb9bb"] forState:UIControlStateNormal];
- // [self OrderStatistics:@"0"];
- _changeTime = @"0";
- }
- if (self.changeBtn) {
- self.changeBtn(btn);
- }
- }
- #pragma mark---订单统计
- -(void)OrderStatistics:(NSString *)type
- {
-
- }
- @end
|