Core Plot: How to present popover from a bar selected by the user

前端 未结 1 1268
-上瘾入骨i
-上瘾入骨i 2021-01-06 17:19

What I would like to accomplish

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

相关标签:
1条回答
  • 2021-01-06 17:38

    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));
    
    0 讨论(0)
提交回复
热议问题