【问题标题】:Coreplot taking forever to renderCoreplot 需要永远渲染
【发布时间】:2014-07-01 21:29:02
【问题描述】:

Coreplot 的渲染时间过长。我在所有方法中都放了日志,它们都运行得很快。问题是让图表实际显示在 graphHost 中。每次少于 400 个数据点几乎需要一分钟。代码 -

    -(void)drawChart{

if ( !self.graph ) {
   self.graph = [[CPTXYGraph alloc] initWithFrame:self.graphHost.frame];
    CPTScatterPlot *priceLinePlot = [[CPTScatterPlot alloc] initWithFrame:self.graph.bounds];
    priceLinePlot.identifier = @"Data Source Plot";
    priceLinePlot.paddingBottom = 0;
    priceLinePlot.paddingTop = 0;
    priceLinePlot.paddingLeft = 0;
    priceLinePlot.paddingRight = 0;
    CPTMutableLineStyle *lineStyle = [priceLinePlot.dataLineStyle mutableCopy];
    lineStyle.lineWidth = 1.0;

    int first = [closeTicks.firstObject integerValue];
    int last = [closeTicks.lastObject integerValue];
    int diff = last - first;

    CPTColor *graphColor = (diff >= 0? (CPTColor *)kColorThemeGreen : (CPTColor *)kColorThemeRed);

    lineStyle.lineColor = graphColor;
    priceLinePlot.dataLineStyle = lineStyle;
    priceLinePlot.dataSource = self;
    priceLinePlot.delegate = self;
    CPTColor *areaColor1 = graphColor;
    CPTGradient *areaGradient1 = [CPTGradient gradientWithBeginningColor:areaColor1 endingColor:[CPTColor clearColor]];
  //  areaGradient1.angle = -180.0f;
    CPTFill *areaGradientFill = [CPTFill fillWithGradient:areaGradient1];
    priceLinePlot.areaFill = areaGradientFill;
    priceLinePlot.areaBaseValue = [[NSDecimalNumber zero] decimalValue];
    [self.graph addPlot:priceLinePlot];
    priceLinePlot.cachePrecision = CPTPlotCachePrecisionDouble;
    [self.graph applyTheme:nil];
}

self.graphHost.hostedGraph = self.graph;
self.graph.paddingBottom = 0;
self.graph.paddingTop = 0;
self.graph.paddingLeft = 0;
self.graph.paddingRight = 0;

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;

NSDecimalNumber *high = [NSDecimalNumber decimalNumberWithDecimal:[[closeTicks valueForKeyPath:@"@max.doubleValue"]decimalValue]];
NSDecimalNumber *low  = [NSDecimalNumber decimalNumberWithDecimal:[[closeTicks valueForKeyPath:@"@min.intValue"]decimalValue]];
NSDecimalNumber *length = [high decimalNumberBySubtracting:low];

NSLog(@"high = %@, low = %@, length = %@", high, low, length);
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromUnsignedInteger(xPoints)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:[low decimalValue] length:[length decimalValue]];

 // Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
CPTMutableLineStyle *lineStyle = [axisSet.xAxis.axisLineStyle mutableCopy];
lineStyle.lineWidth = 0.5;
lineStyle.lineColor = (CPTColor *)[UIColor colorWithRed:.9 green:.9 blue:.9 alpha:0.5];
axisSet.xAxis.axisLineStyle = lineStyle;
x.majorIntervalLength         = CPTDecimalFromDouble(1.0);
x.orthogonalCoordinateDecimal = CPTDecimalFromInteger(0);
x.minorTicksPerInterval       = 0;

CPTXYAxis *y  = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyNone;

y.majorIntervalLength         =CPTDecimalFromDouble(1.0);
y.majorTickLineStyle          = nil;
y.minorTicksPerInterval       = 0;
y.minorTickLineStyle          = nil;
y.orthogonalCoordinateDecimal = CPTDecimalFromInteger(0);
y.hidden = YES;

[self.graph reloadData];

}

    -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
    {
        if ( fieldEnum == CPTScatterPlotFieldX ) {
    return [NSDecimalNumber numberWithInteger:(NSInteger)index-1];
}
else if ( fieldEnum == CPTScatterPlotFieldY ) {

    return [NSDecimalNumber numberWithDouble:[[closeTicks objectAtIndex:index] doubleValue]];
}
    }

    -(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
     {
   return closeTicks.count;
     }  

【问题讨论】:

    标签: ios objective-c charts core-plot


    【解决方案1】:

    在绘图空间上设置绘图范围之前尝试设置轴标签属性。在您关闭它们之前,某些东西可能会触发沿每个轴分开一个单位的默认标签的创建。如果有很多标签(仅 x 轴将有 400 个 - 每个数据点一个),重新标记轴可能需要很长时间。

    【讨论】:

    • 没有变化。我将范围配置移到了底部(就在调用 reloadData 之前),并且图表仍然需要很长时间才能真正显示在视图中。委托方法 didFinishDrawing 在它最终出现时被调用。我们在这里遗漏了什么明显的东西吗??
    • 似乎在调用 renderAsVectorInContext 之前就挂断了
    • Core Animation 调用-drawInContext:,而后者又为每个绘图周期调用-renderAsVectorInContext:。您的应用程序是否在做任何可能阻塞主线程并阻碍绘图周期的事情?是否所有 Core Plot 方法都是从主线程调用的?
    • 是的 coreplot 是从 main 调用的。一些反应式可可和一些 UI 更新似乎是主要关注点。将coreplot推到后台会更好吗?还是我的其他代码?有没有办法在后台设置coreplot,然后在绘制完成后将其添加到视图中?
    • 修复了!与CorePlot 无关!必须将我的 NSURLConnection 从 [NSOperationQueue new] 更改为 [NSOperationQueue mainQueue]。终于!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    • 2021-07-15
    • 2021-03-17
    • 2014-02-24
    • 2014-07-08
    • 2013-02-17
    相关资源
    最近更新 更多