【问题标题】:Changing the position of a CorePlot annotation after zooming(iOS)缩放后更改 CorePlot 注释的位置(iOS)
【发布时间】:2015-05-14 16:33:24
【问题描述】:

我正在使用 CorePlot ScatterPlot 来显示来自外部蓝牙传感器的数据读数。我已经完成了以下教程:http://www.raywenderlich.com/13271/how-to-draw-graphs-with-core-plot-part-2 并且我已经按照我想要的方式进行了所有工作,但有一个例外。当用户点击其中一个数据点时,注释会放置在数据点的顶部。

这是我创建和添加注释的方法:

NSNumber *value = [self numberForPlot:plot field:CPTScatterPlotFieldY recordIndex:idx];
if (!valueAnnotation) {
    NSNumber *x = [NSNumber numberWithInt:0];
    NSNumber *y = [NSNumber numberWithInt:0];
    NSArray *anchorPoint = [NSArray arrayWithObjects:x, y, nil];
    valueAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:plot.plotSpace anchorPlotPoint:anchorPoint];
}

// 5 - Create text layer for annotation
NSString *stringValue = [NSString stringWithFormat:@"%@", value];

CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:stringValue style:style];
valueAnnotation.contentLayer = textLayer;

// 7 - Get the anchor point for annotation
CGFloat x = (CGFloat)idx;
NSNumber *anchorX = [NSNumber numberWithFloat:x];
CGFloat y = [value floatValue]; //edit this line?
NSNumber *anchorY = [NSNumber numberWithFloat:y];
valueAnnotation.anchorPlotPoint = [NSArray arrayWithObjects:anchorX, anchorY, nil];

// 8 - Add the annotation
[plot.graph.plotAreaFrame.plotArea addAnnotation:valueAnnotation];

我尝试向anchorY添加一个小值以将注释移动到绘图点上方:

CGFloat y = [value floatValue] + 5;

虽然起初这很有效,但当用户放大图形时,注释会越来越远离绘图点。缩放时如何将注释保持在绘图点的上方或下方?

我想我可能必须实现 CPTPlotSpaceDelegate 方法 plotSpace: shouldScaleBy:aboutPoint: 方法,这就是我卡住的地方。这就是我所拥有的:

-(BOOL)plotSpace:(CPTPlotSpace *)space shouldScaleBy:(CGFloat)interactionScale aboutPoint:(CGPoint)interactionPoint {
     CPTGraph *graph = self.hostView.hostedGraph;
     NSArray *annotations = graph.plotAreaFrame.plotArea.annotations;

     if (annotations.count > 0) {
         CPTPlotSpaceAnnotation *annotation = [annotations objectAtIndex:0];

         NSNumber *anchorX = /* ? */;
         NSNumber *anchorY = /* ? */;

         annotation.anchorPlotPoint = [NSArray arrayWithObjects:anchorX, anchorY, nil];
     }
     return YES;
 }

我不确定从这里去哪里。非常感谢您提供的任何帮助或指导。

【问题讨论】:

    标签: ios objective-c cocoa-touch annotations core-plot


    【解决方案1】:

    使用displacement 属性将注释从锚点偏移。位移以像素为单位测量,因此不会随绘图空间而变化。

    【讨论】:

    • 完美运行!谢谢。
    • 为我工作。 CorePlot 2.3
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 2016-08-22
    • 2011-09-24
    • 1970-01-01
    相关资源
    最近更新 更多