iOS Core plot - align y axis labels - ios

I want to align the y axis labels of my scatter plot in Core Plot. Currently, the labels are like this:
16
12
8
4
How can I change they alignment to right :
16
12
_8
_4
I tried the following:
y.labelAlignment = CPTAlignmentRight;
It didn't work. How can I do this alignment?
Edit-
Here is my code:
CPTAxis *y = axisSet.yAxis;
y.title = #"Y Label";
y.titleTextStyle = axisTitleStyle;
y.titleOffset = -40.0f;
y.axisLineStyle = axisLineStyle;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelTextStyle = axisTextStyle;
y.labelOffset = 16.0f;
y.majorTickLineStyle = axisLineStyle;
y.majorTickLength = 4.0f;
y.minorTickLength = 2.0f;
y.tickDirection = CPTSignPositive;
y.labelAlignment = CPTAlignmentRight;

You can specify the label direction independently from the tick direction now. To have the tick marks extend to the right of the axis while lining up the labels on the left side, use the following settings:
y.tickDirection = CPTSignPositive;
y.tickLabelDirection = CPTSignNegative;
y.labelOffset = 5.0;

Related

Core Plot Graphs : X axis Label Truncation Issue

As shown in the image, X-axis Labels are being truncated.
I am trying with this
[xRange expandRangeByFactor:CPTDecimalFromDouble(1.05)]
but still I am not able to succeed. Please let me know if any method to avoid this truncation.
This configures graph,
-(void)configureGraph {
CPTGraph *graph = [[CPTXYGraph alloc initWithFrame:self.hostView.bounds];
[graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];
self.hostView.hostedGraph = graph;
graph.paddingLeft = 0;
graph.paddingRight = 0;
graph.paddingTop = 0;
graph.paddingBottom = 0;
CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
borderLineStyle.lineWidth = 1.0;
borderLineStyle.lineColor = [CPTColor colorWithCGColor:[UIColor whiteColor].CGColor];
graph.plotAreaFrame.borderLineStyle = borderLineStyle;
CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor blackColor];
titleStyle.fontName = [UIFont flukeFontBoldName];
titleStyle.fontSize = 16.0f;
graph.titleTextStyle = titleStyle;
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
graph.titleDisplacement = CGPointMake(0.0f, -15.0f);
[graph.plotAreaFrame setPaddingLeft:kPaddingLeft];
[graph.plotAreaFrame setPaddingBottom:kPaddingBottom];
[graph.plotAreaFrame setPaddingTop:kPaddingTop];
[graph.plotAreaFrame setPaddingRight:kPaddingRight];
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
}
-(void)configureAxes
{
CPTGraph *graph = self.hostView.hostedGraph;
// Grid line styles
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.5;
majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.6] colorWithAlphaComponent:0.75];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
x.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0);
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
x.preferredNumberOfMajorTicks = 4;
x.minorTicksPerInterval = 1;
// pin axis to bottom
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
// Y axis
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0);
y.majorIntervalLength = CPTDecimalFromDouble(20.0f);
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.preferredNumberOfMajorTicks = kCorePlotYAxisPreferredTickLocationCount;
y.minorTicksPerInterval = 0;
// Pin axis to left
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
}
You need to do some combination of the following things. Which one(s) you choose depends on the appearance you want to achieve.
Increase the length of the xRange further to move the last data value in from the right edge.
Increase the right padding on the plot area frame to move the edge of the plot area further from the edge of the graph where the text is being clipped.
At xAxis, you can set label alignment to left.

core plot: how to add four axis on left,right,top and bottom side

default there is only two axis on left and bottom, I want to add another tow axis on right and bottom to show the same tick and label.
Thanks in advance.
You can do that with constraints when making your axis,
CPTXYAxis *x = [self _makeDefaultAxis];
x.coordinate = CPTCoordinateX;
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
x.tickDirection = CPTSignPositive;
// ...
CPTXYAxis *y = [self _makeDefaultAxis];
y.coordinate = CPTCoordinateY;
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
y.tickDirection = CPTSignPositive;
// ...
CPTXYAxis *y2 = [self _makeDefaultAxis];
y2.coordinate = CPTCoordinateY;
y2.axisConstraints = [CPTConstraints constraintWithUpperOffset:0.0];
y2.tickDirection = CPTSignNegative;
// ...
CPTXYAxis *x2 = [self _makeDefaultAxis];
x2.coordinate = CPTCoordinateX;
x2.axisConstraints = [CPTConstraints constraintWithUpperOffset:0.0];
x2.tickDirection = CPTSignNegative;
Which gives something like,
So also show the labels you need to adjust the padding etc.

Core Plot graph bottom axis not visible

I've got a core-plot line graph which is displaying as follows (background view coloured red for clarity):
I have 0 padding at the bottom, and I have an x-axis configured, with labels. The graph is squeezed right down against the bottom of the view though. If I increase the bottom padding I get a grey bar at the bottom with the same result.
What do I need to change in order to move the graph up from the bottom of the view and display the axis?
The code is quite lengthy, but here's some excerpts:
self->graph = [[CPTXYGraph alloc] initWithFrame:hostView.bounds];
hostView.hostedGraph = graph;
self->graph.paddingLeft = 0.0f;
self->graph.paddingTop = 10.0f;
self->graph.paddingRight = 0.0f;
self->graph.paddingBottom = 0.0f;
...
[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:p1, p2, p3, nil]];
CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
[xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
plotSpace.xRange = xRange;
CPTMutablePlotRange *yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(500.0f)];
plotSpace.yRange = yRange;
...
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self->graph.axisSet;
CPTAxis *x = axisSet.xAxis;
x.axisLineStyle = axisLineStyle;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.majorTickLineStyle = axisLineStyle;
x.majorTickLength = 4.0f;
x.tickDirection = CPTSignNegative;
...
x.axisLabels = xLabels;
x.majorTickLocations = xLocations;
Try adding bottom padding to graph.plotAreaFrame.

Core Plot: Additional axis not shown

I have added an additional x axis to my plot space in order to show zero values:
CPTMutableLineStyle *boldLineStyle = [xAxis.axisLineStyle mutableCopy];
boldLineStyle.lineWidth = 1;
boldLineStyle.lineColor = [CPTColor darkGrayColor];
boldLineStyle.lineCap = kCGLineCapRound;
CPTXYAxis *xZeroAxis = [[CPTXYAxis alloc] init];
xZeroAxis.coordinate = CPTCoordinateX;
xZeroAxis.plotSpace = plotSpace;
xZeroAxis.axisLineStyle = boldLineStyle;
Interestingly, the labels on this axis are shown, but not the axis itself:
xZeroAxis.axisLabels = [NSSet setWithArray:naLabels];
xZeroAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
xyAxisSet.axes = #[xAxis, xLabelAxis, yAxis, xZeroAxis];
How would I need to change my code to show the x axis?
Thank you!
x.axisLineStyle is set as follows:
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 1.0;
axisLineStyle.lineCap = kCGLineCapRound;
axisLineStyle.lineColor = [CPTColor darkGrayColor];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPPTXYAxis *x = axisSet.xAxis;
x.axisLineStyle = axisLineStyle;
This seems to be a bug in the current Core Plot release. The issue has been reported here: Core Plot Issue 514

Core-Plot - preferredNumberOfMajorTicks do not limit the number of Ticks

I want to draw only 10 Majors Ticks on my Y axis. For that I'm using the methode preferredNumberOfMajorTicks = 10.
but as seen on the screenshot, it doesn't really work ...
Here is my configuration for the y axis :
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyFixedInterval;
//y.majorIntervalLength = CPTDecimalFromDouble(2);
y.preferredNumberOfMajorTicks = 10;
y.minorTicksPerInterval = 5;
y.tickDirection = CPTSignNone;
y.majorTickLineStyle = majorLineStyle;
y.minorTickLineStyle = minorLineStyle;
y.axisLineStyle = majorLineStyle;
y.majorTickLength = 7.0;
y.minorTickLength = 5.0;
y.labelTextStyle = whiteTextStyle;
y.minorTickLabelTextStyle = minorTickBlackTextStyle;
y.titleTextStyle = whiteTextStyle;
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:40.0];
Something should over right the methode preferredNumberOfMajorTicks = 10 but I cannot see what ...
The preferredNumberOfMajorTicks property does not apply to the fixed interval labeling policy. See the Core Plot docs for details. You have two options here:
Keep the fixed interval labeling policy and change the majorIntervalLength to a value that will give the desired number of ticks.
Change the labeling policy to CPTAxisLabelingPolicyAutomatic or CPTAxisLabelingPolicyEqualDivisions.

Resources