1

I have implemented the following code in my CPTScatterPlotDelegate to display a callout bubble:

-(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 is just a custom class holding my data and _annotation is the instance of my callout.

The problem is I cant't get the position of the callout working properly. If I set _view to the ViewController.view I get the right callout but in the wrong place like this:

Clicked point in yellow

If I set _view to the CPTGraphHostingView instead I get the right point but the callout appears to be flipped like this:

enter image description here

How to I get the right plot point co-ordinates to display the callout?

Imran
  • 1,488
  • 1
  • 15
  • 36

2 Answers2

1

You shouldn't add any subviews to the hosting view—it uses a flip transform on iOS so Core Plot can share drawing code between iOS and the Mac.

Use the -convertPoint:toView: or -convertPoint:fromView: method to convert touchedPoint from the hosting view's coordinate system to the ViewController.view coordinate system.

Eric Skroch
  • 27,381
  • 3
  • 29
  • 36
  • Thanks Eric that worked great, I used the method: [graph convertPoint:touchedPoint toLayer:_view.layer] though. Same thing I guess? – Imran Sep 07 '12 at 10:00
  • I'm using this `CGPoint convertedPoint = [plot.graph convertPoint:touchedPoint toLayer:self.view.layer];` And its still off by a little bit, sitting to the left of the point and down... does graph padding affect this? – Sam Jarman Dec 09 '13 at 04:08
0

I have subclassed CPTGraphHostingView for a few charts so as an alternative I found any subview you add to it you should do the following:

subview.transform = CGAffineTransform(1.0, -1.0)

This doesn't change the fact that the coordinate system is (0,0)-bottom-left but the UIView you add will draw the right way up when rendered. Still use Eric's Answer for translating between coordinate systems but for anyone that finds themselves here trying to add a subview to CPTGraphHostingView I hope this helps.

Community
  • 1
  • 1
Josh Peak
  • 5,898
  • 4
  • 40
  • 52