DaffodilGirl 2013-07-11 08:26 采纳率: 0%
浏览 3744

UIBezierPath充填颜色

我要用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];

得到的结果如下:

CSDN移动问答

如何给图形和x,y坐标之间充填颜色?

  • 写回答

1条回答 默认 最新

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-01-05 14:30
    关注

    你可以将aPath的所有边都设置为从y轴到数据点的垂直线,然后使用closePath方法来将路径闭合。这样就可以使用fill方法来填充路径内部的区域了。


    下面是修改后的代码:

    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];
    
    // 从x轴开始绘制
    [aPath moveToPoint:CGPointMake(10, kGraphBottom)];
    
    for (int i = 0; i < graphData.count; i++) {
        // 从y轴到数据点绘制垂直线
        [aPath addLineToPoint:CGPointMake(10 + 50 * i, kGraphBottom - [[[graphData objectAtIndex:i] objectAtIndex:1] floatValue])];
    }
    
    // 从最后一个数据点到x轴绘制垂直线
    [aPath addLineToPoint:CGPointMake(10 + 50 * graphData.count, kGraphBottom)];
    
    // 闭合路径
    [aPath closePath];
    
    // 填充路径内部的区域
    [aPath fill];
    
    // 绘制路径
    [aPath stroke];
    

    这样就可以在图形和x,y坐标之间充填颜色了。

    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的