我有一个有效的核心情节,我的第一个正在尝试实现注释。我记录了注释,x和y坐标都是空的。感谢
-(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);
}
答案 0 :(得分:0)
这是确定锚点的代码:
NSNumber *x = [[plotData objectAtIndex:index] valueForKey:@"x"];
NSNumber *y = [[plotData objectAtIndex:index] valueForKey:@"y"];
NSArray *anchorPoint = [NSArray arrayWithObjects:x, y, nil];
它假设您的绘图数据作为NSNumber对象存储在具有“x”和“y”键的字典数组中。如果您的数据以其他方式存储,则必须以不同方式提取值。重要的是将两个数字打包在NSArray中,首先是x值,第二个是y值。