0
votes

I am using core plot to display a users weight in an iPhone app, and with test data populated into the data source array it works perfectly. But when I manually enter data, and there is only one element in the data source array, the graph does not scale to the appropriate place on the graph where the single point is located. My single test data point is 42, and when the graph appears it is zoomed in to the Y-Range of 0-1 and the point is off screen. If I set the single test data point to .5, it is visible. When I add another point (44), the graph scales appropriately and shows the Y-Range of 42-44.

I leaned heavily on the Ray Wenderlich tutorial when creating this app. I think the plotSpace is what determines what range of the graph will be visible (I am still very new to using Core Plot). This code is almost verbatim from the tutorial:

// 1 - Get graph and plot space
graph = self.hostView.hostedGraph;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;

//2 - Create the Weight plot
weightPlot = [[CPTScatterPlot alloc] init];
weightPlot.dataSource = self;
CPTColor *weightColor = [CPTColor greenColor];
[graph addPlot:weightPlot toPlotSpace:plotSpace];

// 3 - Set up plot space
[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:weightPlot, nil]];
CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
[xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
plotSpace.xRange = xRange;
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
[yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)];
plotSpace.yRange = yRange;

Thanks for any advice! I can post the rest of my core plot code is anyone thinks it will help.

1

1 Answers

0
votes

The -scaleToFitPlots: method doesn't have enough information from only one data point to calculate "reasonable" axis ranges. In that case, set the xRange and yRange yourself based on the data you do have.