// // 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/5-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; i0; 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 0) { [linePath moveToPoint:[self.barsEndPointsArr[0] CGPointValue]]; } for (int i = 1; i