我在我的 CPTScatterPlotDelegate 中实现了以下代码来显示标注气泡:
-(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index
{
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)plot.plotSpace;
CPTGraph *graph = plot.graph;
int yIdent = ((NSString*)plot.identifier).intValue;
NSNumber *yVal = [[_dataRange yForIndex:index] objectAtIndex:yIdent-1];
NSNumber *xVal = [_dataRange xForIndex:index];
double doublePrecisionPlotPoint[2];//[x,y]
doublePrecisionPlotPoint[0] = xVal.doubleValue;
doublePrecisionPlotPoint[1] = yVal.doubleValue;
CGPoint touchedPoint = [graph.defaultPlotSpace plotAreaViewPointForDoublePrecisionPlotPoint:doublePrecisionPlotPoint];
if(_annotation)
[_annotation dismissCalloutAnimated:YES];
_annotation = [[SMCalloutView alloc] init];
//todo appropriate units
_annotation.title = [NSString stringWithFormat:@"%.2f kg", yVal.doubleValue];
[_annotation presentCalloutFromRect:CGRectMake(touchedPoint.x, touchedPoint.y, 1, 1) inView:_view constrainedToView:_view permittedArrowDirections:SMCalloutArrowDirectionAny animated:YES];
}
_dataRange 只是一个保存我的数据的自定义类,_annotation 是我标注的实例。
问题是我无法让标注的位置正常工作。如果我将 _view 设置为 ViewController.view 我会得到正确的标注,但在错误的位置,如下所示:
如果我将 _view 设置为 CPTGraphHostingView 而不是我得到正确的点,但标注似乎像这样翻转:
如何获得正确的绘图点坐标来显示标注?