0

i'm drawing a graph and i want the the ascending lines to be in green and the descending lines in red. i tried the following:

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

CGPoint previousPoint = CGPointMake(0.0, 0.0);

//graph the points
for (int i = 0; i < self.numberOfS; i++) {
    CGPoint currentPoint = CGPointMake(150+(40*[[[self.grades objectAtIndex:i] s] doubleValue]), 59+(40*(i+1)));
    if (previousPoint.x != 0.0f) {
        if (previousPoint.x > currentPoint.x) {
            CGContextMoveToPoint(context, previousPoint.x, previousPoint.y);
            CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
            CGContextAddLineToPoint(context, 150+(40*[[[self.grades objectAtIndex:i] semesterGPA] doubleValue]), 59+(40*(i+1)));
        } else{
            CGContextMoveToPoint(context, previousPoint.x, previousPoint.y);
            CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
            CGContextAddLineToPoint(context, 150+(40*[[[self.grades objectAtIndex:i] semesterGPA] doubleValue]), 59+(40*(i+1)));
        }

    }
    previousPoint.x = currentPoint.x;
    previousPoint.y = currentPoint.y;
} 

but it's not working, everything is in red. how can i fix this ?

HusseinB
  • 1,321
  • 1
  • 17
  • 41
  • Where are you actually **drawing** the path? I only see you creating it. – borrrden Mar 23 '13 at 15:09
  • also, you're checking if the previous x is greater than the current x, which will never happen? Shouldn't you be checking the y? – max k. Mar 23 '13 at 15:10
  • the drawing is all fine but the lines are in red... the CGContextSetStrokeColorWithColor to green isn't working. it's like only taking the first one entered (third line of code). i'm doing all that in the drawRect Method of the UIView. @maxk. i'm drawing the graph as a landscape mode but the view coordinates didn't shift so i continued drawing with the old coordinates such a way that i can view it in landscape. – HusseinB Mar 23 '13 at 15:24
  • Actually it is taking the *last* one entered (i.e. the one that gets entered right before you draw). Core Graphics is a state machine. You can add a path, set the color, etc, but when you call draw it will only use the last set thickness, color, etc. – borrrden Mar 23 '13 at 15:52

1 Answers1

0

Ok solved the problem, i just entered this line of code:

CGContextStrokePath(context);
HusseinB
  • 1,321
  • 1
  • 17
  • 41