Coreplot永远呈现

时间:2014-07-01 21:29:03

标签: ios objective-c charts core-plot

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;
     }  

1 个答案:

答案 0 :(得分:0)

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