核心图注释坐标返回 null
我有一个工作核心图,这是我的第一个,目前正在尝试实现注释。我已记录注释以及 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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是确定锚点的代码:
它假设您的绘图数据作为 NSNumber 对象存储在带有“x”和“y”键的字典数组中。如果您的数据以其他方式存储,您将必须以不同的方式提取值。重要的是将两个数字打包在 NSArray 中,首先是 x 值,然后是 y 值。
This is the code that determines the anchor point:
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.