gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:25:54 24 4
gpt4 key购买 nike

我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com