3

我想使用 UIBezierPath 在图形内填充颜色。到目前为止,我已经使用了以下代码:

NSMutableArray *graphData=[[NSMutableArray alloc] initWithObjects:[NSArray arrayWithObjects:@"2013-06-26",@"46", nil],[NSArray arrayWithObjects:@"2013-06-27",@"37", nil],[NSArray arrayWithObjects:@"2013-06-28",@"96", nil],[NSArray arrayWithObjects:@"2013-06-29",@"29", nil],[NSArray arrayWithObjects:@"2013-06-30",@"29", nil],[NSArray arrayWithObjects:@"2013-07-01",@"24", nil], nil];
    UIBezierPath *aPath = [UIBezierPath bezierPath];
    [[UIColor blackColor] setStroke];
    [[UIColor redColor] setFill];

    [aPath moveToPoint:CGPointMake(10, kGraphBottom-[[[graphData objectAtIndex:0] objectAtIndex:1]floatValue])];

    for (int i = 1; i < graphData.count; i++)
    {
        [aPath addLineToPoint:CGPointMake(10+50* (i), kGraphBottom-[[[graphData objectAtIndex:i] objectAtIndex:1]floatValue])];

    }

    [aPath moveToPoint:CGPointMake(10+50*graphData.count , kGraphBottom)];
    [aPath moveToPoint:CGPointMake(10 , kGraphBottom)];
    [aPath moveToPoint:CGPointMake(10 , kGraphBottom-[[[graphData objectAtIndex:0] objectAtIndex:1]floatValue])];
     [aPath closePath];
    [aPath fill];
    [aPath stroke];

我得到的输出如下图所示:图形

如何填充图形和 x,y 轴之间的颜色?

4

2 回答 2

2

看起来您没有正确关闭路径,因此第一个点和最后一个点之间的直线。您应该使用addLineToPoint最后三分,而不仅仅是moveToPoint然后我认为您会在那里。

于 2013-07-11T05:53:27.263 回答
2

据我了解,您想要描边该路径,但填充不同的区域(路径和轴之间的区域)。

为此,您必须使用两条路径。您现在正在绘制的一个用于描边,另一个用于填充。同时构建两条路径,但从坐标系的原点开始填充路径,并在图形的开头添加一条线。继续向两条路径添加线,最后在填充路径的 x 轴上添加一条线,关闭填充路径就完成了。

于 2013-07-11T06:03:02.247 回答