tags:

views:

33

answers:

2

I have some data, let's say the number of sells of the month. I want to create a discrete graph showing the amount of sold items every day.

The only way i'm able to do that is creating some CGRects, and then load subviews with these rects as frame, and color their backgrounds. So the columns of the graph are made by small, colored view.

Do you think this could be the right way? Or do you think there are better approaches?

A: 

Ok, i think the best way is using the CoreGraphics!

Here's the code i wrote, maybe can be useful to somebody...

- (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];

    }

}

}

Abramodj
+2  A: 

Take look at Core Plot: http://code.google.com/p/core-plot/

Eric Skroch
Thanks this is very interesting!
Abramodj