ios - 核心剧情: should adding a CPTPlotSpaceAnnotation to plot space cause the entire graph to be redrawn?

标签 ios cocoa-touch core-plot

我正在使用 Core Plot 来显示价格的时间序列。当用户触摸图形时,我会在该点显示一条可拖动的垂直线。时间序列和可拖动线都是 CPTXYGraph 中的 CPTScatterPlot 对象。这非常有效 - 在时间序列图上拖动线时的性能是可以接受的。

下一阶段是在用户选择的位置显示价格和日期。 Yahoo Stocks App 有一个很好的功能,它在标签中显示价格,标签会移动,就好像它附在可拖动线的顶部一样。我尝试使用 CPTPlotSpaceAnnotation 中显示的文本来复制它。这可行,但会严重影响性能。经过一番挖掘,我发现 CPTLayer drawInContext: 被调用了多次——看起来每次我重绘文本标签时整个图都在重绘(事实上我的日志暗示它被重绘了两次) .

这是绘制标签的代码(进行中)。它由 plotSpace:shouldHandlePointingDeviceDraggedEvent:atPoint: 调用。

- (void)displayPriceAndDateForIndex:(NSUInteger)index atPoint:(CGPoint)pointInPlotArea
{
  NSNumber * theValue = [[self.graphDataSource.timeSeries objectAtIndex:index] observationValue];

  // if the annotations already exist, remove them
  if ( self.valueTextAnnotation ) {
    [self.graph.plotAreaFrame.plotArea removeAnnotation:self.valueTextAnnotation];
    self.valueTextAnnotation = nil;
  }

  // Setup a style for the annotation
  CPTMutableTextStyle *annotationTextStyle = [CPTMutableTextStyle textStyle];
  annotationTextStyle.color = [CPTColor whiteColor];
  annotationTextStyle.fontSize = 14.0f;
  annotationTextStyle.fontName = @"Helvetica-Bold";

  // Add annotation
  // First make a string for the y value
  NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
  [formatter setMaximumFractionDigits:2];
  NSString *currentValue = [formatter stringFromNumber:theValue];

  NSNumber *x            = [NSNumber numberWithDouble:[theDate timeIntervalSince1970]];
  NSNumber *y            = [NSNumber numberWithFloat:self.graphDataSource.maxValue];
  NSArray *anchorPoint = [NSArray arrayWithObjects:x, y, nil];

  // Then add the value annotation to the plot area
  float valueLayerWidth = 50.0f;
  float valueLayerHeight = 20.0f;
  CPTTextLayer *valueLayer = [[CPTTextLayer alloc] initWithFrame:CGRectMake(0,0,valueLayerWidth,valueLayerHeight)];

  valueLayer.text = currentValue;
  valueLayer.textStyle = annotationTextStyle;
  valueLayer.backgroundColor = [UIColor blueColor].CGColor;

  self.valueTextAnnotation  = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:self.graph.defaultPlotSpace anchorPlotPoint:anchorPoint];

  self.valueTextAnnotation.contentLayer = valueLayer;

  // modify the displacement if we are close to either edge
  float xDisplacement = 0.0;
  ...
  self.valueTextAnnotation.displacement = CGPointMake(xDisplacement, 8.0f);

  [self.graph.plotAreaFrame.plotArea addAnnotation:self.valueTextAnnotation];

  // now do the date field
  ...
}

完全重绘是预期的行为吗?有没有更好的方法来管理注解而不破坏它并在每次调用方法时重新创建它?

最佳答案

您不需要每次都销毁和创建注释。创建后,只需更新 anchorPoint。删除和添加注释可能与不断重绘有关。

关于ios - 核心剧情: should adding a CPTPlotSpaceAnnotation to plot space cause the entire graph to be redrawn?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9775360/

相关文章:

iphone - 从 Objective-C 中的另一个类调用方法

iphone - 如何在 iPhone 上使用核心图实现股票成交量图表?

ios - 使用核心图根据圆半径将散点图缩放到 Y 轴

objective-c - 运行时记录的协议(protocol)名称

iphone - CorePlot 缩放轴独立于多点触控

ios - cocos2d-iphone-2.0-rc2.tar.gz 没有响应

ios - 如何实现多点触摸同时画两条线?

iphone - 以编程方式在 contenteditable UIWebView 中显示键盘

ios - 未调用 IBM MFP onReadyToSubscribe 方法

ios - 在 Today 扩展中安排本地通知