我正在使用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;
}
我不确定从哪里开始。我们将非常感谢您提供的任何帮助或指导。
答案 0 :(得分:0)
使用displacement
属性将注释偏离锚点。位移以像素为单位测量,因此不会随绘图空间而变化。