0

He i'm working on simple pie chart, and i've got problem when i'm drawing it. I want to change color of every piece of pie chart, but when method drawRect: is called my pie chart is in signle color. How make it correctly?

for (int i = 0; i < [values count]; i++) {
    CGContextSetRGBFillColor(ctx, 0.2*i, 0.2*i, 0.2*i, 1);

    [bezierPath moveToPoint:chartCenter];

    startAngle = endAngle;

    endAngle = startAngle + [self degreesToRadians:[values[i] floatValue]*step];
    NSLog(@"%.2f, %.2f", startAngle, endAngle);

    [bezierPath addArcWithCenter:chartCenter radius:radius startAngle:startAngle endAngle:endAngle clockwise:1];
    [bezierPath addLineToPoint:chartCenter];

    [bezierPath fill];
}
Tomasz Szulc
  • 4,217
  • 4
  • 43
  • 79

2 Answers2

1

Im am not 100% sure, but I think you have to use -[UIColor setFill] to set the fill color for a bezier path, e.g.

UIColor *color = [UIColor colorWithWhite:0.2*i alpha:1.0];
[UIColor setFill];

You should probably also start a new bezier path for each chart segment.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0

Ok my code is working correctly. I've got some errors in drawing method and i create circles not arcs. sorry for this.

The problem was in this method:

-(float)degreesToRadians:(float)degrees{ return degrees * M_PI / 180; }

I use M_1_PI instead of M_PI and my arcs were circles.

Tomasz Szulc
  • 4,217
  • 4
  • 43
  • 79