I have a working core-plot, my first one and am currently trying to implement annotation. I have logged the annotation, and the x and y coordinates and they are null. thanks
-(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index
{
//CPTGraph* graph = [graphs objectAtIndex:0];
NSLog(@"add annotation called");
if ( symbolTextAnnotation ) {
[graph.plotAreaFrame.plotArea removeAnnotation:symbolTextAnnotation];
//[graph removeAnnotation:symbolTextAnnotation];
symbolTextAnnotation = nil;
}
// Setup a style for the annotation
CPTMutableTextStyle *hitAnnotationTextStyle = [CPTMutableTextStyle textStyle];
hitAnnotationTextStyle.color = [CPTColor whiteColor];
hitAnnotationTextStyle.fontSize = 16.0f;
hitAnnotationTextStyle.fontName = @"Helvetica-Bold";
// Determine point of symbol in plot coordinates
NSNumber *x = [[plotData objectAtIndex:index] valueForKey:@"x"];
NSNumber *y = [[plotData objectAtIndex:index] valueForKey:@"y"];
NSArray *anchorPoint = [NSArray arrayWithObjects:x, y, nil];
NSLog(@"x %@, y %@",[[plotData objectAtIndex:index] valueForKey:@"x"],y);
// Add annotation
// First make a string for the y value
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setMaximumFractionDigits:2];
NSString *yString = [formatter stringFromNumber:y];
// Now add the annotation to the plot area
CPTTextLayer *textLayer = [[[CPTTextLayer alloc] initWithText:yString style:hitAnnotationTextStyle] autorelease];
symbolTextAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:graph.defaultPlotSpace anchorPlotPoint:anchorPoint];
symbolTextAnnotation.contentLayer = textLayer;
symbolTextAnnotation.displacement = CGPointMake(0.0f, 20.0f);
[graph.plotAreaFrame.plotArea addAnnotation:symbolTextAnnotation];
//[graph addAnnotation:symbolTextAnnotation];
CPTAnnotation *annot = [graph.plotAreaFrame开发者_运维技巧.plotArea.annotations objectAtIndex:0];
NSLog(@"annot: %@", annot);
}
This is the code that determines the anchor point:
NSNumber *x = [[plotData objectAtIndex:index] valueForKey:@"x"];
NSNumber *y = [[plotData objectAtIndex:index] valueForKey:@"y"];
NSArray *anchorPoint = [NSArray arrayWithObjects:x, y, nil];
It assumes your plot data is stored as NSNumber objects in an array of dictionaries with "x" and "y" keys. If your data is stored some other way, you'll have to extract the values differently. The important thing is to package the two numbers in an NSArray with the x-value first and the y-value second.
精彩评论