iOS 折线图

2021-11-03  本文已影响0人  安宇辛

一:话不多说先上效果图

折线图
二:实现
1借鉴网上的ZHLineChart可以实现折线图
https://github.com/hi-zhouyn/zhlinechart/
2然后根据自己的需要定制
1
#import "ZHLineChartView.h"
2
@property (nonatomic, strong) ZHLineChartView *lineView;
3
[self.lineView drawLineChart];//调用
4
- (ZHLineChartView *)lineView
{
    if (!_lineView) {
        _lineView = [[ZHLineChartView alloc] initWithFrame:CGRectMake(0, 15,self.width+32*2, 175)];
        _lineView.horizontalDataArr = [self getCurrentDayToLastServeDay];
        self.kdata = @[@0, @20, @16, @38, @28, @32, @47];
       //取数组的最大和最小
        _lineView.lineDataAry = self.kdata;
        NSArray *numbers = self.kdata;
        numbers = [numbers sortedArrayUsingSelector:@selector(compare:)];
        int max = [[numbers lastObject] intValue]/10;
        int min = [numbers[0] intValue]/10;
        if(max>1){
            max = (max+1)*10;
        }else{
            max = 10;
        }
        if(min>0){
            min = min*10;
        }else{
            min = 0;
        }
        _lineView.max = [NSNumber numberWithInt:max];
        _lineView.min = [NSNumber numberWithInt:min];
        _lineView.splitCount = 5;
        _lineView.angle = 0;
        _lineView.dataTextWidth = 100;
        _lineView.bottomOffset = 10;
        _lineView.textColor = [UIColor whiteColor];
        _lineView.horizontalBottomLineColor = [UIColor whiteColor];
        _lineView.dataTextColor = [UIColor colorWithRed:133/255.0 green:45/255.0 blue:224/255.0 alpha:1.0];
        _lineView.lineColor = [UIColor colorWithRed:133/255.0 green:45/255.0 blue:224/255.0 alpha:1.0];
        _lineView.circleStrokeColor = [UIColor colorWithRed:133/255.0 green:45/255.0 blue:224/255.0 alpha:1.0];
        _lineView.circleFillColor = [UIColor colorWithRed:133/255.0 green:45/255.0 blue:224/255.0 alpha:1.0];
        _lineView.toCenter = NO;
        _lineView.showColorGradient = NO;
        _lineView.addCurve = NO;
        _lineView.isShowHeadTail = NO;
        _lineView.edge = UIEdgeInsetsMake(25, 6, 20, 30);
        _lineView.backgroundColor = [UIColor clearColor];
        [self addSubview:_lineView];
    }
    return _lineView;
}

定制

                //画白色竖线
                UIBezierPath *linePath = [UIBezierPath bezierPath];
                   // 起点
                   [linePath moveToPoint:CGPointMake(CGRectGetMaxX(leftLabel.frame) + self.lineToLeftOffset, 15)];
                   // 终点
                   [linePath addLineToPoint:CGPointMake(CGRectGetMaxX(leftLabel.frame) + self.lineToLeftOffset, 140)];
                   
                   CAShapeLayer *lineLayer = [CAShapeLayer layer];
                   
                   lineLayer.lineWidth = self.horizontalBottomLineWidth;
                   lineLayer.strokeColor = self.horizontalBottomLineColor.CGColor;
                   lineLayer.path = linePath.CGPath;
//                   lineLayer.fillColor = nil; // 默认为blackColor
                   
                   [self.layer addSublayer:lineLayer];
hLineLayer.lineDashPattern = @[@(5),@(5)];//虚线显示
//获取当前日期开始的七天日期
-(NSMutableArray *)getCurrentDayToLastServeDay{
    NSMutableArray *weekArr = [[NSMutableArray alloc] init];
    NSDate *nowDate = [NSDate date];
    //计算从当前日期开始的七天日期
    for (int i = 0; i < 7; i ++) {
           //从现在开始的24小时
           NSTimeInterval secondsPerDay = i * 24*60*60;
           NSDate *curDate = [NSDate dateWithTimeInterval:-secondsPerDay sinceDate:nowDate];
           NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
           [dateFormatter setDateFormat:@"M-d"];
           NSString *dateStr = [dateFormatter stringFromDate:curDate];//几月几号
      
      
        [weekArr addObject:dateStr];
    }
    //倒序
    weekArr = (NSMutableArray *)[[weekArr reverseObjectEnumerator] allObjects];
    return weekArr;
}

上一篇 下一篇

猜你喜欢

热点阅读