2012-09-06 50 views
1

我已經實現了下面的代碼在我CPTScatterPlotDelegate顯示標註泡沫CorePlot顯示標註:當選擇點是顛倒

-(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我得到正確的標註,但在錯誤的地方是這樣的:

Clicked point in yellow

如果我設置_view到CPTGraphHostingView,而不是我得到正確的點,但標註似乎是翻轉這樣的:

enter image description here

如何我得到正確的情節點座標來顯示標註?

回答

1

您不應該在託管視圖中添加子視圖 - 它在iOS上使用翻轉轉換,因此Core Plot可以在iOS和Mac之間共享繪圖代碼。

使用-convertPoint:toView:-convertPoint:fromView:方法將touchedPoint從託管視圖的座標系轉換爲ViewController.view座標系。

+0

感謝埃裏克是偉大的工作,我所用的方法:[圖convertPoint:touchedPoint toLayer:_view.layer]雖然。我猜想同樣的事情? – Imran

+0

我正在使用這個 'CGPoint convertedPoint = [plot.graph convertPoint:touchedPoint toLayer:self.view.layer];' 它仍然有點點,坐在點的左邊... ...圖形填充會影響這個嗎? –

0

我有子類CPTGraphHostingView幾個圖表,以便作爲一種替代,我發現你增加它的任何子視圖,你應該做到以下幾點:

subview.transform = CGAffineTransform(1.0, -1.0) 

這不會改變一個事實,即座標系( 0,0)漢字左但是呈現在您添加了UIView會得出正確的方式了。仍然使用Eric's Answer爲座標系之間,但任何人在這裏找到自己想要一個子視圖添加到CPTGraphHostingView我希望這可以幫助翻譯。