Core Plot never stops asking for data, hangs device
- by Ben Collins
I'm trying to set up a core plot that looks somewhat like the AAPLot example on the core-plot wiki. I have set up my plot like this:
- (void)initGraph:(CPXYGraph*)graph forDays:(NSUInteger)numDays {
self.cplhView.hostedLayer = graph;
graph.paddingLeft = 30.0;
graph.paddingTop = 20.0;
graph.paddingRight = 30.0;
graph.paddingBottom = 20.0;
CPXYPlotSpace *plotSpace = (CPXYPlotSpace*)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0) length:CPDecimalFromFloat(numDays)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0) length:CPDecimalFromFloat(1)];
CPLineStyle *lineStyle = [CPLineStyle lineStyle];
lineStyle.lineColor = [CPColor blackColor];
lineStyle.lineWidth = 2.0f;
// Axes
NSLog(@"Setting up axes");
CPXYAxisSet *xyAxisSet = (id)graph.axisSet;
CPXYAxis *xAxis = xyAxisSet.xAxis;
// xAxis.majorIntervalLength = CPDecimalFromFloat(7);
// xAxis.minorTicksPerInterval = 7;
CPXYAxis *yAxis = xyAxisSet.yAxis;
// yAxis.majorIntervalLength = CPDecimalFromFloat(0.1);
// Line plot with gradient fill
NSLog(@"Setting up line plot");
CPScatterPlot *dataSourceLinePlot = [[[CPScatterPlot alloc] initWithFrame:graph.bounds] autorelease];
dataSourceLinePlot.identifier = @"Data Source Plot";
dataSourceLinePlot.dataLineStyle = nil;
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
CPColor *areaColor = [CPColor colorWithComponentRed:1.0 green:1.0 blue:1.0 alpha:0.6];
CPGradient *areaGradient = [CPGradient gradientWithBeginningColor:areaColor endingColor:[CPColor clearColor]];
areaGradient.angle = -90.0f;
CPFill *areaGradientFill = [CPFill fillWithGradient:areaGradient];
dataSourceLinePlot.areaFill = areaGradientFill;
dataSourceLinePlot.areaBaseValue = CPDecimalFromString(@"320.0");
// OHLC plot
NSLog(@"OHLC Plot");
CPLineStyle *whiteLineStyle = [CPLineStyle lineStyle];
whiteLineStyle.lineColor = [CPColor whiteColor];
whiteLineStyle.lineWidth = 1.0f;
CPTradingRangePlot *ohlcPlot = [[[CPTradingRangePlot alloc] initWithFrame:graph.bounds] autorelease];
ohlcPlot.identifier = @"OHLC";
ohlcPlot.lineStyle = whiteLineStyle;
ohlcPlot.stickLength = 2.0f;
ohlcPlot.plotStyle = CPTradingRangePlotStyleOHLC;
ohlcPlot.dataSource = self;
NSLog(@"Data source set, adding plot");
[graph addPlot:ohlcPlot];
}
And my delegate methods like this:
#pragma mark -
#pragma mark CPPlotdataSource Methods
- (NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot {
NSUInteger maxCount = 0;
NSLog(@"Getting number of records.");
if (self.data1 && [self.data1 count] > maxCount) {
maxCount = [self.data1 count];
}
if (self.data2 && [self.data2 count] > maxCount) {
maxCount = [self.data2 count];
}
if (self.data3 && [self.data3 count] > maxCount) {
maxCount = [self.data3 count];
}
NSLog(@"%u records", maxCount);
return maxCount;
}
- (NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
NSLog(@"Getting record @ idx %u", index);
return [NSNumber numberWithInt:index];
}
All the code above is in the view controller for the view hosting the plot, and when initGraph is called, numDays is 30. I realize of course that this plot, if it even worked, would look nothing like the AAPLot example. The problem I'm having is that the view is never shown. It finished loading because viewDidLoad is the method that calls initGraph above, and the NSLog statements indicate that initGraph finishes. What's strange is that I return a value of 54 from numberOfRecordsForPlot, but the plot asks for more than 54 data points. in fact, it never stops asking. The NSLog statement in numberForPlot:field:recordIndex prints forever, going from 0 to 54 and then looping back around and continuing.
What's going on? Why won't the plot stop asking for data and draw itself?