iosgraphcore-plotcallouts

Callout from Core Plot doesn't follow scrolling


I'm using a modified version of the code here to display callout popups from points along the plot in a graph. I've got it mostly working, but the problem I have is that when I scroll or zoom the graph, the callout stays in the same place on the screen, not following as the point moves.

In case you'd like some code samples to play around with, here they are. Any ideas on how to make the callout travel with the plot point? Thanks!

-(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)idx {
    if ([plot.identifier isEqual:_plotIdentifier]) {

        NSArray *selectedSymbolItem = [self.data.xyArrays objectAtIndex:idx];
        NSNumber *x = [selectedSymbolItem objectAtIndex:0];
        NSNumber *y = [selectedSymbolItem objectAtIndex:1];
        double doublePrecisionPlotPoint[2];
        doublePrecisionPlotPoint[0] = x.doubleValue;
        doublePrecisionPlotPoint[1] = y.doubleValue;
        CGPoint pointTouched = [plot.graph.defaultPlotSpace plotAreaViewPointForDoublePrecisionPlotPoint:doublePrecisionPlotPoint];

        CGPoint convertedPoint = [plot.graph convertPoint:pointTouched toLayer:self.view.layer];

        _popupView = [[SMCalloutView alloc] init];
        _popupView.title = [NSString stringWithFormat:@"Thing %i", idx];
        [_popupView presentCalloutFromRect:CGRectMake(convertedPoint.x, convertedPoint.y, 10, 10) inLayer:self.view.layer constrainedToLayer:self.hostView.layer permittedArrowDirections:SMCalloutArrowDirectionDown animated:YES];

    }
}

Solution

  • Use a plot space delegate to monitor changes to the plot space and update the callout position as needed.