【问题标题】:Core Plot: should adding a CPTPlotSpaceAnnotation to plot space cause the entire graph to be redrawn?核心情节:是否应该在绘图空间中添加 CPTPlotSpaceAnnotation 导致整个图形被重绘?
【发布时间】:2012-03-19 18:00:12
【问题描述】:

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

下一阶段是在用户选择的点显示价格和日期。雅虎股票应用程序有一个很好的功能,它可以在一个移动的标签中显示价格,就好像它附在可拖动线的顶部一样。我尝试使用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
  ...
}

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

【问题讨论】:

    标签: ios cocoa-touch core-plot


    【解决方案1】:

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

    【讨论】:

    • 谢谢@Eric - 我希望你会在身边!我从这个方法中删除了所有实例化,但仍然看到完整的重绘。我将其跟踪到更新显示值的行。深入研究 Core Plot 框架本身,我发现通过在 CPTTextLayer.m 中的方法 setText: 中注释掉调用 [self sizeToFit],我可以阻止重绘的发生。有没有任何程序化的方式来阻止这个调用?
    • 没有。 CPTTextLayer 旨在调整自身大小,使其仅比文本大几个像素。每次更改文本和/或样式时,它都必须更新该大小。因为大小可以改变,所以它会更新图形布局。它不应该导致整个图表重绘。
    • 我无法弄清楚为什么一切都在重绘,但它是 - 每次标签的大小发生变化时,renderAsVectorInContext: 都会为我的图表的每个组件触发。我实际上根本不希望标签大小发生变化,所以我将 CPTTextLayer 子类化以使其固定宽度 - 除非您知道更好的方法?
    • 嗨@Eric。我设法在 CPTTestApp 中复制了这种行为。我将NSLog(@"Redrawing: %@", [self class]); 插入到CPTLayer drawInContext: 中,这样我就可以看到正在重绘的内容。在主演示窗口中,在散点图上找到具有不同宽度的两个点(例如 1.74 和 1.6),然后依次单击每个点 - 您应该会看到整个图形每次都重新绘制。然后,我在 Controller.m 中更改了scatterPlot:plotSymbolWasSelectedAtRecordIndex:,以便注释和文本层是静态的,而不是每次都重新创建并得到相同的结果。
    • 如果你正在滚动图表,它必须重新绘制图表和轴,因为一切都移动了。由于剪辑的完成方式,它会重绘标签——如果我们在标签穿过绘图区域的边缘时不重绘,你会得到各种糟糕的绘图伪影。何时重绘标签可能更智能,但尚未实施优化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多