我在这个问题上停留了一段时间。我想要做的是获取图形
 -(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触摸。我想触摸图形上的任意位置并在图形上显示点。我不想仅仅为了显示我当时的价值而触摸情节符号。我希望能够在图形的绘制线的上方或下方触摸它,并且它应该能够识别出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];
}

最佳答案

在图空间委托中,在图上使用-dataIndexFromInteractionPoint:方法来查找最接近触摸点的数据点的索引。有了数据索引后,请使用已经创建和显示注释的代码。

关于ios - Core Plot iOS从CPTPlotSpace点的数据获取点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35629209/

10-14 20:45
查看更多