Reputation: 5634
I have a second y
-axis (y2
) in my Core Plot project which I have set up as described in the code below.
How can I get both y
-axes to have the same zero-point? Currently, the zero-point of each axis is at different heights (in the screen shot below, the y
-axis' zero-point equals the y2
-value of 8.8%). How can I get Core Plot to plot the zero-point at the same height for both axes?
My code:
CPTXYAxis *y = axisSet.yAxis;
y.coordinate = CPTCoordinateY;
y.orthogonalCoordinateDecimal = CPTDecimalFromDouble(xAxisStart);
y.axisLineStyle = axisLineStyle;
y.majorIntervalLength = CPTDecimalFromInt(1);
y.majorTickLength = 5;
y.minorTickLength = 0;
y.majorGridLineStyle = axisLineStyle;
CPTXYPlotSpace *relativePlotSpace = [[CPTXYPlotSpace alloc] init];
[graph addPlotSpace:relativePlotSpace];
CPTXYAxis *y2 = [[CPTXYAxis alloc] init];
y2.axisLineStyle = axisLineStyle;
y2.coordinate = CPTCoordinateY;
y2.orthogonalCoordinateDecimal = CPTDecimalFromDouble(xAxisLength);
y2.plotSpace = relativePlotSpace;
y2.separateLayers = NO;
y2.majorTickLineStyle = axisLineStyle;
y2.minorTicksPerInterval = 1;
y2.preferredNumberOfMajorTicks = 8;
y2.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions;
graph.axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil];
Based on Eric's answer below I have added the following code snippet to calculate the range for the y2-axis which should align its zero-point to the y-axis's zero-point:
double yAxisAbsoluteLength =
ABS(plotSpace.yRange.lengthDouble);
double y2AxisAbsoluteLength =
(ABS(relativePlotSpace.yRange.lengthDouble) +
ABS(relativePlotSpace.yRange.locationDouble));
double yAxisZeroPointToLengthRatio =
plotSpace.yRange.locationDouble /
yAxisAbsoluteLength;
double y2AxisLocation = yAxisZeroPointToLengthRatio
*y2AxisAbsoluteLength;
relativePlotSpace.yRange = [CPTPlotRange
plotRangeWithLocation:CPTDecimalFromDouble(y2AxisLocation)
length:CPTDecimalFromDouble(y2AxisAbsoluteLength)];
This seems to work fine if y
-values are > 0
. However, for y
-values < 0
, the y2
location is dragged also below the zero-point, which leads to the problem that the calculated y2AxisAbsoluteLength
value is no longer sufficient to compensate for the dragging of the y2
location below the zero-point (y2
points with a high values are cut off or not plotted in the chart). My solution would be simply to add the absolute value of the y2
location to the y2AxisAbsoluteLength
value, but this would distort the alignment of the two zero points (y
and y2
).
How would I need to change my calculation to account correctly for positive and negative y
-values?
In this situation, I can't use expandRangeByFactor
because the multiplication factor is unknown at the time of coding.
Upvotes: 0
Views: 510
Reputation: 27381
Both axes stretch the full height of the plot area, so you can calculate some ratios to determine a range for the second axis.
Example:
Plot range 1: location -100, length 400
This means the zero-point is at a point 25% (100/400) of the length from the range location. If for the second plot range to have a length of 20, you would give it a location of -5, putting 25% of the length below the zero-point.
Upvotes: 1