1
votes

I am trying to understand how to use CorePlot. Previously, I used to use google charts to plot my graph.

I am trying to set the axis of my chart to the bottom left and start the 0 from the bottom for both x and y axis. However, I am unable to understand how the code works.

Here is the code -

// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(2.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(3.0)];

// Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromString(@"0.5");
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"2");
x.minorTicksPerInterval = 2;
NSArray *exclusionRanges = [NSArray arrayWithObjects:
    [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.99) length:CPTDecimalFromFloat(0.02)], 
    [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.99) length:CPTDecimalFromFloat(0.02)],
    [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(2.99) length:CPTDecimalFromFloat(0.02)],
    nil];
x.labelExclusionRanges = exclusionRanges;

CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromString(@"0.5");
y.minorTicksPerInterval = 5;
y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"2");
exclusionRanges = [NSArray arrayWithObjects:
    [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.99) length:CPTDecimalFromFloat(0.02)], 
    [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.99) length:CPTDecimalFromFloat(0.02)],
    [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(3.99) length:CPTDecimalFromFloat(0.02)],
    nil];
y.labelExclusionRanges = exclusionRanges;

Would someone be able to tell me what I should do?

2

2 Answers

3
votes

To start the graph at (0, 0) you need to change the x and y ranges and intersection points. You can change this by modifying

plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(2.0)];

to

plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(2.0)];

and also change the plotRangeWithLocation to 0 for the yRange.

Also, you need to change this

x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"2");

to

x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");

and do the same for orthogonalCoordinateDecimal of y.

1
votes
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setGeneratesDecimalNumbers:NO];

axisSet = (CPTXYAxisSet *)graph.axisSet;

x_axis = axisSet.xAxis;
x_axis.majorIntervalLength = CPTDecimalFromString(@"1");
x_axis.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
x_axis.minorTicksPerInterval = 0;
x_axis.labelFormatter = numberFormatter;

 axisSet.xAxis.visibleRange =[CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(100)]; // you can set your number instead of 100
 axisSet.xAxis.gridLinesRange = axisSet.xAxis.visibleRange;

y_axis = axisSet.yAxis;
y_axis.majorIntervalLength = CPTDecimalFromString(@"1");
y_axis.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
y_axis.minorTicksPerInterval = 1;
y_axis.labelFormatter = numberFormatter;

axisSet.yAxis.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(100)]; // you can set your number instead of 100
axisSet.yAxis.gridLinesRange = axisSet.yAxis.visibleRange;