Reputation: 14818
I've looked around for (and even found) some examples on how to do this, but I can't seem to get it working. No matter what I do I'm unable to get another axis to show up on the right side of the graph. I feel like it may be a stupid thing I'm missing.
Here is all of the code used to generate the graph. I'm graphing values over hours in a day. Eventually each Y axis will have a different scale, but for the moment I just want the other axis to show up.
CPTXYGraph *graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero xScaleType:CPTScaleTypeDateTime yScaleType:CPTScaleTypeLinear];
CPTTheme *theme = [CPTTheme themeNamed:@"CustomTheme"];
[graph applyTheme:theme];
graph.paddingLeft = 2.0;
graph.paddingTop = 2.0;
graph.paddingRight = 2.0;
graph.paddingBottom = 2.0;
graph.plotAreaFrame.paddingLeft = 30.0;
graph.plotAreaFrame.paddingTop = 20.0;
graph.plotAreaFrame.paddingRight = 15.0;
graph.plotAreaFrame.paddingBottom = 30.0;
UIColor *darkTextColor = [UIColor colorWithRed:0.325 green:0.322 blue:0.333 alpha:1.000];
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.color = [CPTColor colorWithCGColor:[darkTextColor CGColor]];
textStyle.fontSize = 10.0;
graph.titleTextStyle = textStyle;
graph.titleDisplacement = CGPointMake(0.0f, -10.0f);
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = NO;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(24.0 * D_HOUR)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(400.0)];
CPTXYPlotSpace *plotSpace2 = [[CPTXYPlotSpace alloc] init];
plotSpace2.xRange = plotSpace.xRange;
plotSpace2.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(400.0)];
[graph addPlotSpace:plotSpace2];
// Set axis styles
CPTXYAxis *leftY = [(CPTXYAxisSet *)graph.axisSet yAxis];
CPTXYAxis *x = [(CPTXYAxisSet *)graph.axisSet xAxis];
CPTXYAxis *rightY = [(CPTXYAxis *)[CPTXYAxis alloc] initWithFrame:CGRectZero];
rightY.coordinate = CPTCoordinateY;
rightY.plotSpace = plotSpace2;
rightY.orthogonalCoordinateDecimal = CPTDecimalFromFloat(24.0 * D_HOUR);
graph.axisSet.axes = @[x,leftY,rightY];
x.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(24.0 * D_HOUR)];
x.majorIntervalLength = CPTDecimalFromInt(D_HOUR * 2);
x.labelOffset = 3.0f;
x.titleOffset = 25.0f;
NSDateFormatter *rawFormatter = [[NSDateFormatter alloc] init];
[rawFormatter setDateFormat:@"ha"];
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:rawFormatter];
x.labelFormatter = timeFormatter;
leftY.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(400.0)];
leftY.gridLinesRange = x.visibleRange;
leftY.majorIntervalLength = CPTDecimalFromInt(40);
leftY.titleLocation = CPTDecimalFromFloat(20.0);
leftY.titleOffset = 14.0f;
NSNumberFormatter *noDecimalFormatter = [[NSNumberFormatter alloc] init];
[noDecimalFormatter setNumberStyle:NSNumberFormatterNoStyle];
leftY.labelFormatter = noDecimalFormatter;
// Skin right y
rightY.labelingPolicy = CPTAxisLabelingPolicyFixedInterval;
rightY.majorIntervalLength = CPTDecimalFromInt(50);
rightY.minorTicksPerInterval = 2;
rightY.orthogonalCoordinateDecimal = CPTDecimalFromDouble(0.0);
rightY.tickDirection = CPTSignNegative;
rightY.majorTickLineStyle = leftY.majorTickLineStyle;
rightY.minorTickLineStyle = nil;
rightY.axisLineStyle = leftY.axisLineStyle;
rightY.majorTickLength = 2.0;
rightY.minorTickLength = 1.0;
rightY.labelTextStyle = leftY.labelTextStyle;
rightY.titleTextStyle = leftY.titleTextStyle;
rightY.majorGridLineStyle = leftY.majorGridLineStyle;
rightY.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(400.0)];
rightY.gridLinesRange = x.visibleRange;
rightY.majorIntervalLength = CPTDecimalFromInt(40);
rightY.labelFormatter = noDecimalFormatter;
// Create plot
CPTScatterPlot *hourlyPlot = [[CPTScatterPlot alloc] initWithFrame:graph.bounds];
CPTMutableLineStyle *dataStyle = [hourlyPlot.dataLineStyle mutableCopy];
dataStyle.lineWidth = 2.0f;
dataStyle.lineColor = [CPTColor colorWithCGColor:[darkTextColor CGColor]];
hourlyPlot.dataLineStyle = [dataStyle copy];
hourlyPlot.dataSource = self;
[graph addPlot:hourlyPlot];
Upvotes: 4
Views: 2311
Reputation: 27381
You're setting the orthogonalCoordinateDecimal
of the right y-axis twice. The second time, it moves back to the left on top of the other one.
Upvotes: 4