我在这个问题上被困了一段时间。我想做的是获得图形
来自-(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];
}
答案 0 :(得分:1)
在绘图空间委托中,使用绘图上的-dataIndexFromInteractionPoint:
方法查找到触摸点的最近数据点的索引。获得数据索引后,使用已有的代码创建并显示注释。