首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >核心图iOS从CPTPlotSpace点的数据中获取点

核心图iOS从CPTPlotSpace点的数据中获取点
EN

Stack Overflow用户
提问于 2016-02-25 13:51:19
回答 1查看 358关注 0票数 0

我在这个问题上被困了一段时间。我想要做的是得到图

来自-(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDownEvent:(id)event atPoint:(CGPoint)point事件的数据。我不希望那个用户只能用-(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index来触摸-(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的情节符号。

代码语言:javascript
复制
-(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];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-27 13:53:25

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35629209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档