缩放后更改 CorePlot 注释的位置(iOS)
Changing the position of a CorePlot annotation after zooming(iOS)
我正在使用 CorePlot 散点图来显示来自外部蓝牙传感器的数据读数。我完成了以下教程: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;
}
我不太确定从这里到哪里去。如果您能提供任何帮助或指导,我们将不胜感激。
使用displacement
属性 使注释偏离锚点。位移以像素为单位进行测量,因此它不会随绘图改变 space。
我正在使用 CorePlot 散点图来显示来自外部蓝牙传感器的数据读数。我完成了以下教程: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;
}
我不太确定从这里到哪里去。如果您能提供任何帮助或指导,我们将不胜感激。
使用displacement
属性 使注释偏离锚点。位移以像素为单位进行测量,因此它不会随绘图改变 space。