How to show the graph with high data in a given space? - ios

I am using Line Graph of Core Plot in my project. The thing is when I kept the data of my X-axis and Y-axis like: 1,2,3....10, everything worked fine. But now the data is like: X-axis: 0, 150, 500.... and on Y-axis: 0,1000,2000 and so on.
So now my graph is not fully visible in the given area. I know it is because the range on my Y-axis is getting too high and it is not visible. Some code that might possibly the reason for it:
// Setup scatter plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0.0) length:CPTDecimalFromInteger(8)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0.0) length:CPTDecimalFromInteger(5)];
// Restrict y range to a global range
CPTPlotRange *globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-2.5)
length:CPTDecimalFromDouble(5.0)];
plotSpace.globalYRange = globalYRange;
CPTPlotRange *globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-0.2)
length:CPTDecimalFromDouble(17.0)];
plotSpace.globalXRange=globalXRange;

The xRange and yRange are too small to display all of the plot data.
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0)
length:CPTDecimalFromInteger(500)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0)
length:CPTDecimalFromInteger(2000)];

No luck :( It becomes as per its shown above. I dont want to compress the data. The distance between the months should be as same as the figure posted in the question. The Y axis data is still not getting visible completely. My global X and Y range is like this
CPTPlotRange *globalYRange = [CPTPlotRange
plotRangeWithLocation:CPTDecimalFromDouble(-2.5)
length:CPTDecimalFromDouble(5.0)];
plotSpace.globalYRange = globalYRange;
CPTPlotRange *globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-0.2)
length:CPTDecimalFromDouble(17.0)];
plotSpace.globalXRange=globalXRange;

Related

How to set limit for zoom in core plot graph ( tell at-least 2 points visible)

I want to set the maximum zoom till at-least 2 points visible. I have added globalXRange which works good for minimum zoom out.
But i couldnt able to find solution for zoom in.
[plotSpace setYRange: [CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:0] length:[NSNumber numberWithInt:maxY+10]]];
[plotSpace setXRange: [CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:0] length:[NSNumber numberWithInt:maxX]]];
[plotSpace setGlobalYRange:[CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:0] length:[NSNumber numberWithInt:maxY+20]]];
[plotSpace setGlobalXRange:[CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:0] length:[NSNumber numberWithInt:maxX]]];
[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:aaplPlot, nil]];
You'll need to use a plot space delegate for this. Implement the -plotSpace:willChangePlotRangeTo:forCoordinate: method and return the desired plot range. Use the plot range parameter value if it meets your requirements or the min/max range value if the parameter value is outside your desired visible range.

Core Plot Thread Error - EXC_BAD_ACCESS (code=1, address=0x400002122)

My am using Core Plot Framework version 2 in my app. I am facing error following method "plotRangeWithLocation:CPTDecimalFromInt" CPTDecimalFromInt returning null value.
Core Plot space Creation
//Add plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
// coreplot change
plotSpace.delegate = self;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0.0)
length:CPTDecimalFromInt(10)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0)
length:CPTDecimalFromInt([self getMaxYValue])];*/
Getting Crash in Below code
+ (instancetype)plotRangeWithLocation:(NSNumber *)loc length:(NSNumber *)len {
return [[self alloc] initWithLocation:loc length:len];
}
The Core Plot API changed slightly in 2.0. The +plotRangeWithLocation:length: method now takes NSNumber objects as parameters instead of NSDecimal values.
See this wiki page for a summary of the changes between 1.x and 2.0.

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
And this is what it looks like
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
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.

iOS CorePlot numeric formatting of y-axis

I'm working with Core Plot 1.1 to draw a simple scatter plot in iOS6. I am using the following code to properly format my y-axis which then dynamically scales to the plot data.
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.minorTicksPerInterval = 3;
y.preferredNumberOfMajorTicks = 6;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
...
NSNumberFormatter * yformatter = [[NSNumberFormatter alloc] init];
[yformatter setUsesSignificantDigits:YES];
[yformatter setMaximumSignificantDigits:4];
[yformatter setMaximumFractionDigits:1];
[yformatter setRoundingMode:NSNumberFormatterRoundCeiling];
y.labelFormatter = yformatter;
Then I dynamically change the range based on the data to be plotted using maxPlotValue but bound it to a minimum.
plotSpace.xRange = [CPTPlotRange
plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(5)];
plotSpace.yRange = [CPTPlotRange
plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(maxPlotValue)];
This works great in most cases but sometimes I get a strange formatting error like in the below fig 1 where 0.6001 is displayed in stead of 0.6. If I manually change the minimum range to 2 the error disappears.
The reason I'm using 4 significant digits is that I can have numbers up to 8000 and then they are displayed without the fraction. If I change the setMaximumSignificantDigits to 3 I get 0.601 which I guess indicates that the problem is with the CPTAxisLabelingPolicyAutomatic.
Any help on this would be greatly appreciated.
Fig 1, Error in formatting:
https://dl.dropbox.com/u/8083213/fig_1.png
Fig 2, No error in formatting:
https://dl.dropbox.com/u/8083213/fig_2.png
This sounds like a rounding error in the labeling calculations. Please report it on the Core Plot issue tracker.

Force grid lines to update when range changes in Core-Plot scatter plot on iOS

I have a CPTXYGraph with a CPTScatterPlot, which I update with new data using [graph reloadData]. I also set the ranges based on the data. My problem is that though the axes update fine when the range changes, the gridlines do not, so I end up with this http://inky.ws/g/hw (no grid lines at the end of the x-axis).
I set axis.gridLinesRange = nil, and I tried re-setting all the gridline properties when updating ranges, to no avail.
To clarify, the original plot looks fine, but when I update with a bigger data set, the grid lines are missing in the added range.
I've had the same issue and used the following workaround: calculate new bounds of your values and replace the default plot space range with a new one using the plotRangeWithLocation:length: convenience method.
Not very satisfying performance-wise and conceptually (feels a bit like doing the job of the framework) but it worked for me.
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat([valuesMin floatValue]) length:CPTDecimalFromFloat([valuesMax floatValue]-[valuesMin floatValue])];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *y = axisSet.yAxis;
float amplitude = ([valuesMax floatValue]-[valuesMin floatValue]);
NSDecimal intervalLength = CPTDecimalFromFloat(100);
y.majorIntervalLength = intervalLength;

Resources