开发者

How i access the array of CGPoints to draw the graph?

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 coun开发者_运维百科t]; 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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜