I was going to provide a screenshot of my core plot graph for reference but I’m not allowed to yet (new user) so I can email it upon request.
I have a set of data points, with x values of 1 to ~150, and y values of ~300 to ~450.
What I would like to do is plot these points, with a clean grid overlay, zooming in on the
y Axis (graph y axis should start from y min, rather than 0, and go to y max), while still retaining a visible x-axis for referencing purposes.
I’m fairly close to achieving what I want, but I’m still struggling with a few things.
First of all, I would like a label on the y axis at the intersection between the y axis and x axis, and at the top of the y axis, in order to give the user a point of reference.
To achieve this, I’ve started by playing around with major tick intervals, as well as running a few operations on my data set to determine max and mins. However, when I then attempted to zoom in (i.e. focus on the range between y-min and y max), the x axis was no longer visible.
To fix this, after some searching across the net I came across:
axisSet.xAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0];
While this seems to force the x-Axis to appear, it also seems to be throwing my graph off a bit… there are only 3 minor ticks between my first y-axis major tick, and the origin, and no y-Axis label on the origin.
Does anyone know how to achieve this offset, while forcing the origin to be the first major tick, and display labels for the y-Axis at both the origin and y-Max?
As a side question, does anyone know how to display the x-Axis labels as ints rather than floats (looking to cut off the decimal)?
I’ll throw my source code for what I’ve achieved so far below.
Thanks,
Brandon
Source Code for initialize plot:
-(void)initialisePlot {
[self setDataMaxAndMins];
// Create a graph object which we will use to host just one scatter plot.
CGRect frame = [self.hostingView bounds];
self.graph = [[CPTXYGraph alloc] initWithFrame:frame];
// Add some padding to the graph, with more at the bottom for axis labels.
self.graph.plotAreaFrame.paddingTop = 50.0f;
self.graph.plotAreaFrame.paddingRight = 50.0f;
self.graph.plotAreaFrame.paddingBottom = 100.0f;
self.graph.plotAreaFrame.paddingLeft = 120.0f;
// Tie the graph we've created with the hosting view.
self.hostingView.hostedGraph = self.graph;
self.hostingView.backgroundColor = [UIColor blackColor];
// Create a line style that we will apply to the axis
CPTMutableLineStyle *axisStyle = [CPTMutableLineStyle lineStyle];
axisStyle.lineColor = [CPTColor grayColor];
axisStyle.lineWidth = 2.0f;
CPTColor * customGray = [CPTColor colorWithComponentRed:0.6f green: 0.6f blue: 0.6f alpha: 1.0f];
CPTMutableLineStyle *gridStyle = [CPTMutableLineStyle lineStyle];
gridStyle.lineColor = customGray;
gridStyle.lineWidth = 2.0f;
//Create plot symbol style
CPTMutableLineStyle * plotStyle = [CPTMutableLineStyle lineStyle];
plotStyle.lineColor = [CPTColor whiteColor];
plotStyle.lineWidth = 2.0f;
// Create a text style that we will use for the axis labels.
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.fontName = @"Helvetica";
textStyle.fontSize = 20;
textStyle.color = [CPTColor whiteColor];
// Create the plot symbol we're going to use.
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.lineStyle = plotStyle;
plotSymbol.size = CGSizeMake(3.0, 3.0);
//plotSymbol.
// Setup some floats that represent the min/max values on our axis.
float xAxisMin = xMin;
float xAxisMax = xMax + 5;
float yAxisMin = yMin;
float yAxisMax = yMax + 5;
// We modify the graph's plot space to setup the axis' min / max values.
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.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 *)self.graph.axisSet;
axisSet.xAxis.title = xAxisTitle;
axisSet.xAxis.titleTextStyle = textStyle;
axisSet.xAxis.titleOffset = 50.0f;
axisSet.xAxis.axisLineStyle = axisStyle;
axisSet.xAxis.majorTickLineStyle = axisStyle;
axisSet.xAxis.minorTickLineStyle = axisStyle;
axisSet.xAxis.labelTextStyle = textStyle;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat((xAxisMax - xAxisMin) / 4);//CPTDecimalFromFloat(25.0f);
axisSet.xAxis.minorTicksPerInterval = 4;
axisSet.xAxis.minorTickLength = 5.0f;
axisSet.xAxis.majorTickLength = 7.0f;
axisSet.yAxis.title = [yAxisTitle stringByAppendingString: @" ($M)"];
axisSet.yAxis.titleTextStyle = textStyle;
axisSet.yAxis.titleOffset = 80.0f;
axisSet.yAxis.axisLineStyle = axisStyle;
axisSet.yAxis.majorTickLineStyle = axisStyle;
axisSet.yAxis.minorTickLineStyle = axisStyle;
axisSet.yAxis.labelTextStyle = textStyle;
axisSet.yAxis.labelOffset = 3.0f;
axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat((yAxisMax - yAxisMin) / 4);
axisSet.yAxis.minorTicksPerInterval = 4;
axisSet.yAxis.minorTickLength = 5.0f;
axisSet.yAxis.majorTickLength = 7.0f;
CPTColor * customLighterBlue = [CPTColor colorWithComponentRed:0.0f green: 0.0f blue: 0.4f alpha: 1.0f];
CPTColor * customDarkBlue = [CPTColor colorWithComponentRed:0.0f green: 0.0f blue: 0.3f alpha: 1.0f];
axisSet.xAxis.alternatingBandFills = [NSArray arrayWithObjects:customLighterBlue, customDarkBlue, nil];
axisSet.xAxis.majorGridLineStyle = gridStyle;
axisSet.yAxis.majorGridLineStyle = gridStyle;
axisSet.xAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0];
// Add a plot to our graph and axis. We give it an identifier so that we
// could add multiple plots (data lines) to the same graph if necessary.
CPTScatterPlot *plot = [[CPTScatterPlot alloc] init];
plot.dataSource = self;
plot.identifier = @"mainplot";
plot.dataLineStyle = nil;
plot.plotSymbol = plotSymbol;
[self.graph addPlot:plot];
//[_graph.defaultPlotSpace scaleToFitPlots:[_graph allPlots]];
}
Change the labeling policy. I think
CPTAxisLabelingPolicyEqualDivisionswill do what you want. There is a demo in the Plot Gallery example app that shows all of the available labeling policies.Set the
labelFormatteron the x-axis. It is a standard NSNumberFormatter.