123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- //
- // 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;
- //获取最大的
- 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:@"#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.gapWidth+(self.barWidth+self.gapWidth)*self.xValuesArr.count;
- // _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; 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-10, _totalHeight)];
-
- CAShapeLayer *lineLayer = [CAShapeLayer layer];
- lineLayer.strokeColor = [UIColor colorWithHexString:ThemeColor].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);
- if (self.xValuesArr.count<=6) {
- lab.text = [NSString stringWithFormat:@"%@",self.xValuesArr[i]];
- }
- lab.textColor=[UIColor colorWithHexString:@"#333333"];
- 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.monthBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
- [self.yesterdayBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
- [self.TodayBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
- [self OrderStatistics:@"1"];
-
- }else if(btn.tag == 2001){//本月
-
- [self.yesterdayBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
- [self.monthBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
- [self.TodayBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
- [self OrderStatistics:@"2"];
-
- }else{
- [self.TodayBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
- [self.yesterdayBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
- [self.monthBtn setTitleColor:[UIColor colorWithHexString:@"#BBB9BB"] forState:UIControlStateNormal];
- [self OrderStatistics:@"0"];
- }
- if (self.changeMineBtn) {
- self.changeMineBtn(btn);
- }
- }
- #pragma mark---订单统计
- -(void)OrderStatistics:(NSString *)type
- {
-
- }
- @end
|