AlexR
AlexR

Reputation: 5634

Core Plot: How to position the axis title for two y-axes at the same height?

I am using two y-axes (y and y2) in my scatter plot and I would like to position titles for each axis at the same height.

Please refer to my sample chart below, where I have two titles (y: "USD" and y2: "ev/ebit"). Unfortunately, I did not find a way to draw both titles at exactly the same height (y-value).

This is how I currently calculate the y-location for my titles:

double yTitleLocation = plotSpace.yRange.lengthDouble + plotSpace.yRange.locationDouble;
yAxis.titleLocation = CPTDecimalFromDouble(yTitleLocation);
yAxis.title = self.issue.company.contrCurrency;
yAxis.titleRotation = 2 * M_PI;
yAxis.titleOffset = -15;

double y2TitleLocation = valuationPlotSpace.yRange.lengthDouble + valuationPlotSpace.yRange.locationDouble;
y2Axis.titleLocation = CPTDecimalFromDouble(y2TitleLocation);
y2Axis.title = self.valuation.id;
y2Axis.titleRotation = 2 * M_PI;

How would I need to change my code to draw the title at the same y-value for both y-axes?

What would you suggest to left-align my title for the y-axis and right-align for the y2-axis. I am currently using titleOffset for the y-axis but believe there could be a better way to do this.

Thank you! Axis titles

Chart after the bug fix in the Core-Plot framework: (please refer to the answer 1 below)

enter image description here

Edit: This is how I set up my chart:

I have added this code snippet to show how I set up the graph and the two scatter charts (stock price and valuation):

graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
self.hostView.hostedGraph = graph;
[graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];    
graph.frame = self.view.bounds;
graph.plotAreaFrame.masksToBorder = NO;
graph.plotAreaFrame.cornerRadius  = 0.0f;

CPTScatterPlot *sharePricePlot = [[CPTScatterPlot alloc] initWithFrame:graph.bounds];
sharePricePlot.identifier = @"CloseSharePricePlot";
sharePricePlot.dataLineStyle = sharePricePlotLineStyle;
sharePricePlot.dataSource = self;

CPTScatterPlot *valuationPlot = [[CPTScatterPlot alloc]initWithFrame:graph.bounds];
valuationPlot.dataSource = self;

CPTXYPlotSpace *valuationPlotSpace = [[CPTXYPlotSpace alloc] init];
valuationPlotSpace.identifier = ValuationPlotSpaceIdentifier;
[graph addPlotSpace:valuationPlotSpace];

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
     
[plotSpace scaleToFitPlots:[NSArray arrayWithObject:sharePricePlot]];
[valuationPlotSpace scaleToFitPlots:[NSArray arrayWithObject:valuationPlot]];

Upvotes: 1

Views: 3477

Answers (1)

Eric Skroch
Eric Skroch

Reputation: 27381

That was a Core Plot bug, fixed here. You can make that change in your copy of Core Plot for now. It will be part of the next release whenever that happens. I would write the title code like this:

yAxis.titleLocation = plotSpace.yRange.maxLimit;
yAxis.title = self.issue.company.contrCurrency;
yAxis.titleRotation = 0.0;
yAxis.titleOffset = -15;

y2Axis.titleLocation = valuationPlotSpace.yRange.maxLimit;
y2Axis.title = self.valuation.id;
y2Axis.titleRotation = 0.0;
y2Axis.titleOffset = -15;

You can compute an exact title location using the plot space. For example, to place the title 15 pixels above the plot area, try this:

CGRect plotAreaBounds = graph.plotAreaFrame.plotArea.bounds;
CGPoint viewPoint = CGPointMake(CGRectMinX(plotAreaBounds), 
                                CGRectMaxY(plotAreaBounds) + 15.0);

NSDecimal plotPoint[2];
[plotSpace plotPoint:plotPoint forPlotAreaViewPoint:viewPoint];

yAxis.titleLocation = plotPoint[CPTCoordinateY];

Upvotes: 5

Related Questions