// // ZSIncomeChartView.m // ZBProject // // Created by 学丽 on 2019/4/9. // Copyright © 2019 ZB. All rights reserved. // #import "SSWlineIncomeChartView.h" @interface SSWlineIncomeChartView (){ 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 *yesterdayBtn; @property(nonatomic)UIButton *monthBtn; @property(nonatomic)UIButton *TodayBtn; @end @implementation SSWlineIncomeChartView -(instancetype)initWithChartType:(SSWChartsType)type{ self = [super initWithChartType:type]; if (self) { [self setUp]; } 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 = 0; self.yScaleValue = 50; 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.monthBtn]; [self addSubview:self.TodayBtn]; [self addSubview:self.yesterdayBtn]; self.scrollView.userInteractionEnabled=YES; [self addTap]; [self.contentView addSubview:self.bubbleLab]; } -(void)setYValuesArr:(NSMutableArray *)yValuesArr { _yValuesArr = yValuesArr; [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:@"#333333"]; } return _unitLab; } -(UIButton *)TodayBtn { if (!_TodayBtn) { _TodayBtn =[[UIButton alloc]init]; _TodayBtn.titleLabel.font =[UIFont systemFontOfSize:12]; [_TodayBtn setTitle:@"今天" forState:UIControlStateNormal]; [_TodayBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal]; _TodayBtn.tag = 3001; [_TodayBtn addTarget:self action:@selector(changeValues:) forControlEvents:UIControlEventTouchUpInside]; } return _TodayBtn; } -(UIButton *)monthBtn { if (!_monthBtn) { _monthBtn =[[UIButton alloc]init]; _monthBtn.titleLabel.font =[UIFont systemFontOfSize:12]; [_monthBtn setTitle:@"昨天" forState:UIControlStateNormal]; [_monthBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal]; _monthBtn.tag = 1001; [_monthBtn addTarget:self action:@selector(changeValues:) forControlEvents:UIControlEventTouchUpInside]; } return _monthBtn; } -(UIButton *)yesterdayBtn { if (!_yesterdayBtn) { _yesterdayBtn =[[UIButton alloc]init]; _yesterdayBtn.titleLabel.font =[UIFont systemFontOfSize:12]; _yesterdayBtn.tag = 2001; [_yesterdayBtn setTitle:@"本月" forState:UIControlStateNormal]; [_yesterdayBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal]; [_yesterdayBtn addTarget:self action:@selector(changeValues:) forControlEvents:UIControlEventTouchUpInside]; } return _yesterdayBtn; } -(void)layoutSubviews{ self.scrollView.frame =CGRectMake(0, 40, self.width, self.height-50); self.unitLab.frame = CGRectMake(20, -40, 30, 20); self.TodayBtn.frame=CGRectMake(self.bounds.size.width-150, 0, 40, 15); self.yesterdayBtn.frame=CGRectMake(self.bounds.size.width-50, 0, 40, 15); self.monthBtn.frame=CGRectMake(self.bounds.size.width-100, 0, 40, 15); } -(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]; //画x轴的刻度 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