Coreplot iphone setting correct axis and crossover point - iphone

I am using CorePlot for the iPhone. I can do a simple graph just fine. But what I'm looking to do is to have the y-axis go from 0-100 and the x-axis go from 1998 - 2007. I'd like to keep this numerical (not custom text based labels) if possible.
The issue, is that when you set up these axis:
// We modify the graph's plot space to setup the axis' min / max values.
float xAxisMin = 1998
float xAxisMax = 2007;
float yAxisMin = 0;
float yAxisMax = 100;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xAxisMin) length:CPTDecimalFromFloat(xAxisMax - xAxisMin)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yAxisMin) length:CPTDecimalFromFloat(yAxisMax - yAxisMin)];
// Modify the graph's axis with a label, line style, etc.
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
axisSet.xAxis.title = #"";
axisSet.xAxis.titleTextStyle = textStyle;
axisSet.xAxis.titleOffset = 10.0f;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.labelTextStyle = textStyle;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(1000.0f);
axisSet.xAxis.labelOffset = 1.0f;
axisSet.xAxis.minorTicksPerInterval = 1;
axisSet.xAxis.minorTickLength = 5.0f;
axisSet.xAxis.majorTickLength = 7.0f;
You get a graph that shows the y-axis at 0 going to 100 then the xaxis starts at about 1998 to the right. i.e. they graph does not cross at X: 1998 y: 0. Is there a way to force it to do so? As far as I can tell I have to do a graph with x-axix range from 0-2007 and y-axis range 0-100. Then can I tell it to cut out the 0-1997 piece and shift so that piece is at 0?
The alternative is to use custom points. Which I tried. But is seems there is a problem where it doesn't show ticks on the graph line. For example if I follow the example in from the source, I can see 1998, 1999, 2000, etc but no | lines cross the x-axis. It's like the tick lines will not draw because I'm using custom lines.
Thanks for any and all help,
-David

Use the orthogonalCoordinateDecimal property:
axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(yAxisMin);
axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(xAxisMin);

Related

Distance between x-axis ticks in core plot

Am breaking my head on this since yesterday (tried various attribute settings, goggled, stackoverflow'ed , et al,... in vain). At last, I decided to post this! I hope am not asking anything dumb! Please pardon me if so! :)
My Project Settings:
Simple iPhone App that uses Core Plot, Xcode 4.2, iOS 5.0,
Testing using iOS Simulator
My Question : I used the wonderful and DatePlot example from Core Plot site and am plotting (Scatter Plot) dates along the x-axis. am having two plots. It's all plotted fine (thanks to the simple example!); But the distance between each plot (along the x-axis) i.e. the distance between the major ticks is too small ... it's like this :
(Notice that the two red-colored ellipse-plots are so close to each other...that's the current distance between the ticks)
My Code Snippet (Relevant)
// Set up the axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) lineChart.axisSet;
CPTXYAxis *x = axisSet.xAxis;
NSTimeInterval oneDay = 24.0 * 60.0 * 60.0;
x.majorIntervalLength = CPTDecimalFromFloat(oneDay);
x.majorTickLength = 1.0;
// Get a set of custom label for the X Axis
x.axisLabels = [self getXLabelSetForLineChart:x];
x.labelRotation = M_PI / 2;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = kCFDateFormatterShortStyle;
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
timeFormatter.referenceDate = today;
x.labelFormatter = timeFormatter;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
Can someone please help!!!
Thanks!
The length of the plot space xRange is too long. This compresses your data points into a smaller area and leaves unused spaces off to the right.

Adding two Plots (Bar and Scatter) using coreplot in iOS w.r.t multiple Y axis

I using coreplot , and I want to plot two plots ,1) Bar Plot w.r.t Y1 axis 2) Scatter Plot w.r.t Y2 axis, as shown in the below figure with different set of values.
I managed to create the Y2 axis on the right side by the code below,by referring to the code in CorePlotTestApp example.
CPTXYAxis *y2 = [[(CPTXYAxis *)[CPTXYAxis alloc] initWithFrame:CGRectZero] autorelease];
//y2.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y2.orthogonalCoordinateDecimal = CPTDecimalFromString(#"10");
y2.minorTicksPerInterval = 1;
y2.preferredNumberOfMajorTicks = 2;
y2.labelOffset = -20.0f;
y2.titleOffset = -23.0f;
y2.coordinate = CPTCoordinateY;
y2.plotSpace = plotSpaceRightYaxis;
y2.axisLineStyle = lineStyle;
y2.majorTickLineStyle = lineStyle;
y2.minorTickLineStyle = lineStyle;
//y2.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) length:CPTDecimalFromInteger(20)];
y2.title = #"Y2Title";
y2.labelTextStyle = textStyle;
y2.titleLocation = CPTDecimalFromInteger(4);
y2.axisConstraints = [CPTConstraints constraintWithUpperOffset:0.0];
graph.axisSet.axes = [NSArray arrayWithObjects:x, y,y2,nil];
I can plot the bar graph and scatter plot but with respect to only Y1 axis on left but I want to plot it with respect to Y2 axis so how should I proceed with this,please help me out, waiting for your reply.
Regards
Ranjit.

How to set limit in x- axis of core plot

If i want to show only 10 points of date in x-axis and its corresponding value y axis in the graph. How can set in core plot scattered graph. Can anyone help.
Thanks in advance
There are two properties called xRange and yRange under CPXYPlotSpace. You can use that for setting limits. For example,
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0.0) length:CPDecimalFromInt(10)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromInt(5) length:CPDecimalFromInt(10)];

How to center the core plot graph on the view controller?

I am working on core-plot. I want to make the graph which drawn in the screen need to be centered. when the application get loaded its showing from the bottom the graph. I want to make the entire view visible from center has shown in below figure.
#Thanks in advance
// Just set the xRange and YRange with initial offset:
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
/* Range which is visible on screen */
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(offset)
length:CPTDecimalFromFloat(X_LENGTH)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(offset_Y)
length:CPTDecimalFromFloat(Y_LENGTH)];
/* Total Range which is visible on screen and can be scrolled up to */
CPTPlotRange *globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromDouble(Y_LENGTH)];
plotSpace.globalYRange = globalYRange;
CPTPlotRange *globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromDouble(xRange)];
plotSpace.globalXRange = globalXRange;
/* To constraint X and Y axis */
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
axisSet.xAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:55.0];
axisSet.yAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];

Add second plot space/axis to core-plot

Here is my code:
CPTXYPlotSpace *barGraphPlotSpace = [[CPTXYPlotSpace alloc] init];
barGraphPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPDecimalFromFloat(100.0)];
CPTXYAxis *rightY = [[CPTXYAxis alloc] init];
rightY.coordinate = CPTCoordinateY;
rightY.orthogonalCoordinateDecimal = CPTDecimalFromFloat(oneDay*7);
rightY.plotSpace = barGraphPlotSpace;
[graph addPlotSpace:barGraphPlotSpace];
This doesn't add another axis to my graph though.
What I'm trying to do is get a second y axis which will go from 0-100 (percent). To do this I'm creating a new plot space and a new y axis adding the new plot space to the y axis and adding the plot space to the graph.
What am I doing wrong?
Thank you.
You need to add the new axis to the graph:
NSMutableArray *newAxes = [graph.axisSet.axes mutableCopy];
[newAxes addObject:rightY];
graph.axisSet.axes = newAxes;
[newAxes release];