Core Plot iOS 从 CPTPlotSpace 点的数据中获取点

Core Plot iOS Get point from data at CPTPlotSpace point

我在这个问题上卡住了一段时间。我想做的是获取图表
来自 -(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDownEvent:(id)event atPoint:(CGPoint)point 事件的数据。我不希望用户只能触摸 plotSymbol-(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index。所以我想做的是触摸 Scatter Plot 上的任何地方,例如在绘制的线上方,将我的点放在我的线上,该线位于该触摸点的触摸事件下方,然后在屏幕上绘制该点。我看过这个:Using core-plot, how can you convert a touched point to the plot space? 但没有用。

编辑: 这是为了 plotSymbol touch。我想触摸图表上的任何地方并在图表上显示点。我不想触摸绘图符号只是为了显示我当时的价值。我希望能够在图表上绘制的线上方或下方触摸它,它应该能够识别出我的 CGPoint)point 有一个绘图符号。

-(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index{

CPTXYGraph *graph = (self.graphs)[0];

if ( symbolTextAnnotation ) {
    [graph.plotAreaFrame.plotArea removeAnnotation:symbolTextAnnotation];
    symbolTextAnnotation = nil;
}

// Setup a style for the annotation
CPTMutableTextStyle *hitAnnotationTextStyle = [CPTMutableTextStyle textStyle];
hitAnnotationTextStyle.color    = [CPTColor whiteColor];
hitAnnotationTextStyle.fontSize = 16.0;
hitAnnotationTextStyle.fontName = @"Helvetica-Bold";

// Determine point of symbol in plot coordinates
NSDictionary *dataPoint = plotData[index];

NSNumber *x = dataPoint[@(CPTScatterPlotFieldX)];
NSNumber *y = dataPoint[@(CPTScatterPlotFieldY)];

NSArray *anchorPoint = @[x, y];

// Add annotation
// First make a string for the y value
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:2];
NSString *yString = [formatter stringFromNumber:y];

// Now add the annotation to the plot area
CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:yString style:hitAnnotationTextStyle] ;


CPTImage *background = [CPTImage imageForPNGFile:@"check"];
textLayer.fill          = [CPTFill fillWithImage:background];
textLayer.paddingLeft   = 2.0;
textLayer.paddingTop    = 2.0;
textLayer.paddingRight  = 2.0;
textLayer.paddingBottom = 2.0;

symbolTextAnnotation                    = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:graph.defaultPlotSpace anchorPlotPoint:anchorPoint];
symbolTextAnnotation.contentLayer       = textLayer;
symbolTextAnnotation.contentAnchorPoint = CGPointMake(0.5, 0.0);
symbolTextAnnotation.displacement       = CGPointMake(0.0, 10.0);
[graph.plotAreaFrame.plotArea addAnnotation:symbolTextAnnotation];
}

在绘图space委托中,在绘图上使用-dataIndexFromInteractionPoint:方法找到离触摸点最近的数据点的索引。获得数据索引后,使用已有的代码创建并显示注释。