I am using Core Plot (1.1) to draw a bar chart and I would like to present a popover with further details below the bar which has b
The -plotAreaViewPointForPlotPoint:
method returns a point in the plot area coordinate system. You need to convert that to the graph's coordinate system:
[graph convertPoint:dataPoint fromLayer:graph.plotAreaFrame.plotArea];
The graph and its hosting view share a coordinate system. If self.view
is not the hosting view, you will need to convert the coordinates to its coordinate system from the hosting view.
A more complete code sample:
NSDecimal plotPoint[2];
NSNumber *plotXvalue = [self numberForPlot:plot
field:CPTScatterPlotFieldX
recordIndex:idx];
plotPoint[CPTCoordinateX] = plotXvalue.decimalValue;
NSNumber *plotYvalue = [self numberForPlot:plot
field:CPTScatterPlotFieldY
recordIndex:idx];
plotPoint[CPTCoordinateY] = plotYvalue.decimalValue;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
// convert from data coordinates to plot area coordinates
CGPoint dataPoint = [plotSpace plotAreaViewPointForPlotPoint:plotPoint];
// convert from plot area coordinates to graph (and hosting view) coordinates
dataPoint = [graph convertPoint:dataPoint fromLayer:graph.plotAreaFrame.plotArea];
// convert from hosting view coordinates to self.view coordinates (if needed)
dataPoint = [self.view convertPoint:dataPoint fromView:hostingView];
NSLog(@"barWasSelectedAtRecordIndex x: %@, y: %@", plotXvalue, plotYvalue);
NSLog(@"datapoint coordinates tapped: %@", NSStringFromCGPoint(dataPoint));