美文网首页
iOS超简易折线图(初学)

iOS超简易折线图(初学)

作者: Freedom_fly | 来源:发表于2017-06-28 15:35 被阅读739次

一、背景图(表格):

- (void)drawTableLine {
    NSInteger count = 9;
    
    CGFloat spacing_X = self.frame.size.width/(count+1);
    CGFloat spacing_Y = self.frame.size.height/8;
    
    
    CGContextRef tableContext = UIGraphicsGetCurrentContext();
    CGMutablePathRef verticalPath = CGPathCreateMutable();
    for (int i = 0; i < count + 1; i++) {
        // 垂直线
        CGPathMoveToPoint(verticalPath, NULL, i*spacing_X, 0);
        CGPathAddLineToPoint(verticalPath, NULL, i*spacing_X, self.frame.size.height);
        CGContextStrokePath(tableContext);
        //        [[UIColor blueColor] setStroke];
        //        CGContextAddPath(tableContext, verticalPath);
        //        CGContextDrawPath(tableContext, kCGPathStroke);
        
    }
    for ( int i = 0; i < 8; i++) {
        // 水平线
        CGPathMoveToPoint(verticalPath, NULL, 0, i*spacing_Y);
        CGPathAddLineToPoint(verticalPath, NULL, self.frame.size.width, i*spacing_Y);
        CGContextStrokePath(tableContext);
        [[UIColor orangeColor] setStroke];
        CGContextAddPath(tableContext, verticalPath);
        CGContextDrawPath(tableContext, kCGPathStroke);
    }
    CGPathRelease(verticalPath);
}

效果图:


二、折线图:

- (void)drawGraph {
    NSInteger count = self.valueArray.count;
    NSInteger max = [[self.valueArray valueForKeyPath:@"@max.floatValue"] integerValue]+2;
    NSInteger min = [[self.valueArray valueForKeyPath:@"@min.floatValue"] integerValue]-2;
   
    CGFloat spacing_X = self.frame.size.width/(count+1);
    CGFloat spacing_Y = self.frame.size.height/(max-min);
   
    CGContextRef graphContext = UIGraphicsGetCurrentContext();
    CGMutablePathRef graphPath = CGPathCreateMutable();

    for (int i = 0; i < count-1; i++) {
        NSInteger value = [self.valueArray[i] integerValue];
        NSInteger value1 = [self.valueArray[i+1] integerValue];
   
        CGPathMoveToPoint(graphPath, NULL, (i+1)*spacing_X+PointRadius, (max-value)*spacing_Y);
        CGPathAddLineToPoint(graphPath, NULL, (i+2)*spacing_X-PointRadius, (max-value1)*spacing_Y);
        CGContextSetLineWidth(graphContext, 0.5);
   
        CGContextAddPath(graphContext, graphPath);
        CGContextStrokePath(graphContext);
        [[UIColor blueColor] setStroke];
        CGContextDrawPath(graphContext, kCGPathStroke);
    }
    CGPathRelease(graphPath);
}

效果如图:


三、圆点:
在每个坐标点加一个圆点

//  注:折线图中的点可以使用button/label实现  也可以使用画点实现
- (void)drawPoint {
    NSInteger count = self.valueArray.count;
   
    CGContextRef pointContext = UIGraphicsGetCurrentContext();
    CGMutablePathRef pointPath = CGPathCreateMutable();
   
    NSInteger max = [[self.valueArray valueForKeyPath:@"@max.floatValue"] integerValue]+2;
    NSInteger min = [[self.valueArray valueForKeyPath:@"@min.floatValue"] integerValue]-2;
   
    CGFloat spacing_X = self.frame.size.width/(count+1);
    CGFloat spacing_Y = self.frame.size.height/(max-min);
   
    for (int i = 0; i < count; i++) {
        NSInteger value = [self.valueArray[i] integerValue];
       
        CGRect rect = CGRectMake((i+1)*spacing_X-PointRadius, (max-value)*spacing_Y-PointRadius, 2*PointRadius, 2*PointRadius);
        CGPathAddEllipseInRect(pointPath, NULL, rect);
        CGContextAddPath(pointContext, pointPath);
        CGContextStrokePath(pointContext);
        [[UIColor blueColor] setStroke];
        CGContextDrawPath(pointContext, kCGPathFill);
    }
}

效果图如下:


相关文章

  • iOS超简易折线图(初学)

    一、背景图(表格): 效果图: 二、折线图: 效果如图: 三、圆点:在每个坐标点加一个圆点 效果图如下:

  • 瑜伽爱好者必学的瑜伽坐姿

    1:简易坐 简易坐是初学瑜伽打坐者的首选。因为大部分初学者肢体僵硬,气血滞塞不通,心神散乱不定,所以采用简易坐这种...

  • 简易的iOS导航栏颜色渐变方案

    简易的iOS导航栏颜色渐变方案 简易的iOS导航栏颜色渐变方案

  • Android自定义View练习--折线图

    本练习参考 自定义View练习(二)简易折线图控件,折线图支持设置x轴与y轴的取值范围与递增值,效果如下: 首先自...

  • Android简易折线图的实现思路

    Android简易折线图的实现方案 万事开头难,不说内容如何,关键在于我终于迈出这一步了! 对于折线图来说,git...

  • Swift Programmatically-纯代码初学简易Sw

    Swift Programmatically-纯代码初学简易Swift项目 无需操作任何 StoryBoard 或...

  • iOS—一个简易的网络缓存

    iOS—一个简易的网络缓存

  • IOS学习总结

    iOS初学者面对网上浩瀚的资料往往不知所措,为了让iOS初学者们少走弯路,少花时间在搜索资料上,个人为iOS初学者...

  • iOS学习资源集合

    iOS初学者面对网上浩瀚的资料往往不知所措,为了让iOS初学者们少走弯路,少花时间在搜索资料上,个人为iOS初学者...

  • iOS学习资源集合

    iOS初学者面对网上浩瀚的资料往往不知所措,为了让iOS初学者们少走弯路,少花时间在搜索资料上,个人为iOS初学者...

网友评论

      本文标题:iOS超简易折线图(初学)

      本文链接:https://www.haomeiwen.com/subject/bmpucxtx.html