iOS 折線圖的簡單現(xiàn)實

一、畫出X,Y軸

- (void)drawRect:(CGRect)rect {
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.f);//線的寬度
    CGContextSetRGBStrokeColor(context, 0.2, 0.2, 0.2, 1);//線的顏色
    CGContextMoveToPoint(context, bounceX, bounceY);
    CGContextAddLineToPoint(context, bounceX, self.bounds.size.height - bounceY);//Y軸
    CGContextAddLineToPoint(context, self.bounds.size.width - bounceX, self.bounds.size.height - bounceY);//X軸
    CGContextStrokePath(context);
}

二、分別給X坐標(biāo)和Y坐標(biāo)添加代表的屬性值

- (void)createXLable {//畫X軸坐標(biāo)參數(shù)
    
    CGFloat  month = 12;
    for (NSInteger i = 0; i < month; i++) {
        
        UILabel * LabelX = [[UILabel alloc]initWithFrame:CGRectMake((self.frame.size.width - 2*bounceX)/month * i + bounceX, self.frame.size.height - bounceY + bounceY*0.5, (self.frame.size.width - 2*bounceX)/month + 5, bounceY/2)];
        LabelX.tag = 1000 + i;
        LabelX.text = [NSString stringWithFormat:@"%ld月",i+1];
        LabelX.font = [UIFont systemFontOfSize:10];
        LabelX.transform = CGAffineTransformMakeRotation(M_PI * 0.3);//讓其轉(zhuǎn)動一點,顯示得更像X坐標(biāo)
        [self addSubview:LabelX];
    }
}

- (void)createYLable {//畫Y軸坐標(biāo)參數(shù)
    
    NSInteger count = 10;
    for (NSInteger i = 0; i < count; i++) {
        UILabel * LabelY = [[UILabel alloc]initWithFrame:CGRectMake(0, (self.frame.size.height - 2*bounceY)/count * i + bounceY, bounceX-5, bounceY/2)];
        LabelY.tag = 1000 + i;
        LabelY.text = [NSString stringWithFormat:@"%ld",(count - i)*100 + 100];
        LabelY.font = [UIFont systemFontOfSize:10];
        LabelY.textAlignment = NSTextAlignmentRight;
        [self addSubview:LabelY];
    }
}

三、畫折線圖

- (void)dravLine{
    
    // 背景視圖
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(bounceX, bounceY, self.bounds.size.width - bounceX*2, self.bounds.size.height - 2*bounceY)];
    self.gradientBackgroundView = backgroundView;
    [self addSubview:backgroundView];
    
    UILabel * label = (UILabel*)[self viewWithTag:1000];//根據(jù)橫坐標(biāo)上面的label 獲取直線關(guān)鍵點的x 值
    UIBezierPath * path = [[UIBezierPath alloc]init];
    path.lineWidth = 1.0;
    UIColor * color = [UIColor greenColor];
    [color set];
    [path moveToPoint:CGPointMake( label.frame.origin.x - bounceX, (MaxCount -arc4random()%MaxCount) /MaxCount * (self.frame.size.height - bounceY*2 )  )];
    
    //創(chuàng)建折現(xiàn)點標(biāo)記
    for (NSInteger i = 1; i< month; i++) {
        UILabel * label1 = (UILabel*)[self viewWithTag:1000 + i];
        CGFloat  arc = arc4random()%MaxCount;  //折線點目前給的是隨機數(shù),替換成相應(yīng)的數(shù)據(jù)就可以了
        [path addLineToPoint:CGPointMake(label1.frame.origin.x - bounceX,  (MaxCount -arc) /MaxCount * (self.frame.size.height - bounceY*2 ) )];
        UILabel * falglabel = [[UILabel alloc]initWithFrame:CGRectMake(label1.frame.origin.x , (MaxCount -arc) /MaxCount * (self.frame.size.height - bounceY*2 )+10 , 30, 15)];
        
        falglabel.tag = 3000+ i;
        falglabel.text = [NSString stringWithFormat:@"%.1f",arc];
        falglabel.textColor = [UIColor purpleColor];
        falglabel.font = [UIFont systemFontOfSize:7.0];
        [self addSubview:falglabel];
    }
    
    CAShapeLayer *lineChartLayer = [CAShapeLayer layer];
    lineChartLayer.path = path.CGPath;
    lineChartLayer.strokeColor = [UIColor grayColor].CGColor;
    lineChartLayer.fillColor = [[UIColor clearColor] CGColor];
    
    lineChartLayer.lineCap = kCALineCapRound;
    lineChartLayer.lineJoin = kCALineJoinRound;
    
    [self.gradientBackgroundView.layer addSublayer:lineChartLayer];//直接添加導(dǎo)視圖上
    
    lineChartLayer.lineWidth = 1;
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 3;
    pathAnimation.repeatCount = 1;
    pathAnimation.removedOnCompletion = YES;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    // 設(shè)置動畫代理,動畫結(jié)束時添加一個標(biāo)簽,顯示折線終點的信息
    pathAnimation.delegate = self;
    [lineChartLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
    
}

結(jié)果圖

49A555CC-8E38-4278-9070-7EA5C39D6261.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容