从数据创建图表的最佳方式

时间:2010-08-27 08:23:08

标签: iphone

我有一些数据,比方说一个月的销售数量。我想创建一个离散图表,显示每天销售的商品数量。

我能够做到这一点的唯一方法是创建一些CGRects,然后加载带有这些rects的子视图作为框架,并为其背景着色。因此,图表的列是通过小的彩色视图制作的。

你认为这可能是正确的方法吗?或者您认为有更好的方法吗?

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

好吧,我认为最好的方法是使用CoreGraphics!

这是我写的代码,也许对某人有用......

- (void)drawRect:(CGRect)rect {

// Get the graphics context
CGContextRef ctx = UIGraphicsGetCurrentContext();

int max = arc4random()%5+5;
int unit = floor(155/max);
int h;

for (int i=0; i<24; i++) {

    int numero = arc4random()%max;
    if (numero != 0) {
        h = (max-numero)*unit;

        // Draw the bars
        CGContextSetRGBFillColor(ctx, 130, 0, 180, 1);
        CGContextFillRect(ctx, CGRectMake(4+i*25.6, 9+h, 19, 155-h));
        CGContextFillEllipseInRect(ctx, CGRectMake(4+i*25.6, 2+h, 19, 14));

        // Draw the number
        NSString *newString = [[NSString alloc]initWithFormat:@"%d",numero];
        CGContextSetRGBFillColor(ctx, 255, 255, 255, 0.7);
        CGPoint newPoint = {8+i*25.6, 2+h}; 
        UIFont *font = [UIFont systemFontOfSize:20];
        [newString drawAtPoint:newPoint withFont:font];

    }

}

}

相关问题