1

我在我的 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 而不是我得到正确的点,但标注似乎像这样翻转:

在此处输入图像描述

如何获得正确的绘图点坐标来显示标注?

4

2 回答 2

1

您不应该向宿主视图添加任何子视图——它在 iOS 上使用翻转变换,因此 Core Plot 可以在 iOS 和 Mac 之间共享绘图代码。

使用-convertPoint:toView:or-convertPoint:fromView:方法将touchedPoint宿主视图的坐标系转换为ViewController.view坐标系。

于 2012-09-06T22:34:28.930 回答
0

我已经CPTGraphHostingView对一些图表进行了子类化,因此作为替代方案,我发现您添加到其中的任何子视图都应该执行以下操作:

subview.transform = CGAffineTransform(1.0, -1.0)

这不会改变坐标系为 (0,0)-bottom-left 的事实,但您添加的 UIView 将在渲染时以正确的方式向上绘制。仍然使用Eric's Answer在坐标系之间进行转换,但对于发现自己在这里尝试添加子视图的任何人,CPTGraphHostingView我希望这会有所帮助。

于 2014-05-08T03:50:06.847 回答