Search code examples
ioscore-plotvisible

CorePlot Visible Range Not Working As Expected


So I've been modifying the sample code to try to have a graph show the y values of 0-20 and to only show the max of 6 x values on the screen at a time. A visible range of 0-6 for x;

Here's a picture of what I'm shooting for

enter image description here

And this is what it looks like enter image description here

So here's the part of the code I think is most important. Here I'll show you how I made the visible Axis Range, plot space ranges and plot space global ranges:

CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];

// I'm not sure what the difference is between the two
// constrict this to 0-20 for y and 0-6 for x
x.visibleRange = nil;
y.visibleRange = nil;
x.visibleAxisRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(6.f)];
y.visibleAxisRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(20.f)];

// expand the X, y axis to be more than we need
[xRange expandRangeByFactor:CPTDecimalFromDouble(1.05)];
[yRange expandRangeByFactor:CPTDecimalFromDouble(1.05)];
plotSpace.xRange = xRange;
plotSpace.yRange = yRange;

// expand it to be even bigger
[xRange expandRangeByFactor:CPTDecimalFromDouble(3.0)];
[yRange expandRangeByFactor:CPTDecimalFromDouble(3.0)];
plotSpace.globalXRange = xRange;
plotSpace.globalYRange = yRange;

So in the picture of what is displayed on the app, seems to shows all my values instead of just the six, but it seems to know that I only want to show 6 since the labels stop at six. I'd like to get the app to scroll left and right to show the other points after I get it to show the proper length of the line, but one thing at a time.

Here's the code. It's pretty sloppy, I was just hacking around to see what was possible before I clean it up and bring it to my real project. All the work is done in the controlChart.m file, I mucked around with some of the Plain White theme file but that should be unimportant.

coreplot-horrible-code


Solution

  • Since the x data points are one unit apart, giving the xRange a length of 6 (plus a bit for the plot symbols) will do what you want. In the code you posted, the xRange is set up with -scaleToFitPlots: before expanding the range. This makes it fit all of the plot data.