在iPhone上创建简单的折线图

4
我想创建一个简单的线形图,就像下面这个例子一样: enter image description here 我通过谷歌搜索找到了CorePlot,但它看起来太复杂了。有人能建议我如何创建类似下面这样的线形图吗?或者教程也可以。谢谢。

其中一种可能性在这里:http://blogs.remobjects.com/blogs/mh/2010/01/26/p973 - holex
2个回答

1
首先,为什么要称这个图形为简单?您可以沿着X坐标绘制垂直线来制作它。您可能有一个包含顶部点(显示重量进度)的数组,因此您可以从顶部点到基准线绘制一条线,并在循环中调用每个顶部点的代码。以下是绘制线条的代码:
-(void)drawLineFromX:(CGPoint)topPoint{
    UIGraphicsBeginImageContext(instanceToYourImageView.image.size);
    [instanceToYourImageView.image drawInRect:CGRectMake(0, 0, instanceToYourImageView.image.size.width, instanceToYourImageView.image.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), topPoint.x, topPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), topPoint.x, topPoint.y + a_value_to_reach_the_base_line);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    [instanceToYourImageView setImage:UIGraphicsGetImageFromCurrentImageContext()];
    UIGraphicsEndImageContext();
}

这里的CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);是黑色的,但你可以轻松地将其更改为所需的颜色。

干杯!


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接