views:

79

answers:

3

Hi, I have the code in which i want to develop the graph.The code is,

NSArray *coordinate = [[NSArray alloc] initWithObjects: @"42,213", @"75,173", @"108,153", @"141,133", @"174,113", @"207,73", @"240,33", nil];
CGContextSetRGBFillColor(ctx, 255, 0, 0, 1.0);
CGContextSetLineWidth(ctx, 8.0);
for(int intIndex = 0; intIndex < [coordinate count]; fltX1+=33, intIndex++)
{
    CGContextMoveToPoint(ctx, fltX1+37, fltY2+18);
    CGPoint point = [[coordinate objectAtIndex:intIndex] CGPointValue];
    CGContextAddLineToPoint(ctx, point);
    CGContextStrokePath(ctx);
}

I the above code goes in to debugger at the line of CGPoint point = [[coordinate objectAtIndex:intIndex] CGPointValue]. How i drawing the line in the graph using above code????????

Now i changing the above code as fllows,

Hi,glorifiedHacker,I already guessing above sentence. But, now i changing the my code like,

    CGContextSetRGBFillColor(ctx, 255, 0, 0, 1.0);
CGContextSetLineWidth(ctx, 8.0);
NSArray *coordinate1 = [[NSArray alloc] initWithObjects:@"42",@"75",@"108",@"141",@"174",@"207",@"240",nil];
NSLog(@"The points of coordinate1: %@", coordinate1);
NSArray *coordinate2 = [[NSArray alloc] initWithObjects:@"213",@"173",@"153",@"133",@"113",@"73",@"33",nil];
for(int intIndex = 0; intIndex < [coordinate1 count], intIndex < [coordinate2 count]; fltX1+=33, intIndex++)
{
    CGContextMoveToPoint(ctx, fltX1+37, fltY2+18);
    NSString *arrayDataForCoordinate1 = [coordinate1 objectAtIndex:intIndex];
    NSString *arrayDataForCoordinate2 = [coordinate2 objectAtIndex:intIndex];
    CGContextAddLineToPoint(ctx, (float *)arrayDataForCoordinate1, (float *)arrayDataForCoordinate2);
    //NSLog(@"CGPoints of drawing the bar: %@", point);
}
CGContextClosePath(ctx);    
CGContextStrokePath(ctx)

But it still given me error on same line.

+1  A: 

Your graphics context likely doesn't have a path for you to add lines to at this point. Try wrapping your for loop in the appropriate "begin path" and "close path" function calls.

CGContextBeginPath(ctx);
for(int intIndex = 0; intIndex < [coordinate count]; fltX1 += 33, intIndex++) {
    CGContextMoveToPoint(ctx, fltX1+37, fltY2+18);
    CGPoint point = [[coordinate objectAtIndex:intIndex] CGPointValue];
    CGContextAddLineToPoint(ctx, point.x, point.y);
}
CGContextClosePath(ctx);
CGContextStrokePath(ctx);

Also, note that I moved the "stroke path" call outside of the for loop until you have closed the path.

glorifiedHacker
Hi,glorifiedHacker,The given code give me the error near following 2 lines CGPoint point = [[coordinate objectAtIndex:intIndex] CGPointValue]; CGContextAddLineToPoint(ctx, point);
RRB
glorifiedHacker,your code i edited in the my code it given me the error on CGContextAddLineToPoint(ctx, point).The error is incompatible type for arguments 2 of CGContextAddLineToPoint and another is too few arguments to function CGContextAddLineToPoint.
RRB
The error is there because there is a typo in your original code that I must have copied when I typed out my code. CGContextAddLineToPoint takes three arguments: graphics context, float value for x, float value for y.
glorifiedHacker
Have any shortcut idea to plot the graph.
RRB
glorifiedHacker, In your given code the point object of CGPoint contains the structure of points , therefor u given me point.x and point.y??
RRB
I'm not sure what you mean here - CGPoint is a structure (not an object) - and you must provide CGContextAddLineToPoint a graphics context and two CGFloat values (point.x and point.y).
glorifiedHacker
A: 

I the above code goes in to debugger at the line of CGPoint point = [[coordinate objectAtIndex:intIndex] CGPointValue].

Because NSStrings don't respond to CGPointValue messages. You can see this for yourself by looking at the NSString documentation (or the Debugger Console, which will contain an exception message telling you this).

How i drawing the line in the graph using above code????????

Parse the strings yourself using NSScanner, or use the NSPointFromString function, or use a C array (not an NSArray) of NSPoints/CGPoints.

On an unrelated note:

for(int intIndex = 0; intIndex < [coordinate count]; fltX1+=33, intIndex++)
{
    CGContextMoveToPoint(ctx, fltX1+37, fltY2+18);

Don't just drop random numeric literals into your code. You will have no idea what these numbers mean six months from now, and if you ever want to change, say, the horizontal offset of the graph, you will have to replace the number 37 everywhere in your app (except where it isn't the horizontal offset of the graph), not to mention any numbers that are 37 plus some other offset.

Instead, give these numbers names using something like this:

enum {
    MyDistanceBetweenPoints = 33,
    MyGraphOffsetX = 37,
    MyGraphOffsetY = 18,
};

This way, if you want to change the horizontal offset of the graph, you don't need to hunt down every last relevant numeric literal; you need only change the definition of MyGraphOffsetX.

You can also use the current transformation matrix to offset the graph, thereby eliminating the additions from every CGContextMoveToPoint call and eliminating the risk that you'll forget an addition.

Peter Hosey
A: 

Instead of typecasting an (NSString *) to a (float *) try [myString doubleValue]. If you have strings of the form "{x,y}" you can use CGPointFromString to convert directly to a point. If you can, avoid strings altogether and keep the original data as numeric types. If you must use an NSArray, that could be NSNumber pairs or NSValue of CGPoint.

drawnonward