如何画曲线图

文章目录

效果:

image

由左下图可以看到两点之间有两个control point,可以取两点之间的中点的x作为两个control point的x,前一点的y作为control point1的y值,后一点的y作为control point2的y值。

image

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context,1,0,0,0.6);
CGContextSetLineWidth(context, 1);

CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -rect.size.height);

NSArray *values = @[@50, @65, @75, @70, @80, @90, @67, @65, @75, @70, @80, @90, @67, @65, @75, @70, @80, @90, @67]; // y值数组
CGFloat space = 10.f; //x坐标间隔
CGFloat startX = 10.f; //x坐标起始点
int i = 0;
for (int x = startX; x < rect.size.width-2*startX; x+=space) {
if (i >= values.count) {
break;
}

CGFloat currentY = [values[i] floatValue];
if (x == startX) {
CGContextMoveToPoint(context, x, currentY);
}else{

CGFloat preY = [values[i-1] floatValue];
CGFloat controlPointX = x-5;
CGContextAddCurveToPoint(context, controlPointX, preY, controlPointX, currentY, x, currentY);
}
i++;
}
CGContextDrawPath(context, kCGPathStroke);
}
分享到 评论