4

我已经四处寻找(甚至找到了)一些关于如何做到这一点的例子,但我似乎无法让它发挥作用。无论我做什么,我都无法让另一个轴显示在图表的右侧。我觉得这可能是我错过的一件愚蠢的事情。

这是用于生成图表的所有代码。我正在绘制一天中几个小时的值。最终每个 Y 轴都会有不同的比例,但目前我只想显示另一个轴。

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];
4

1 回答 1

4

您正在设置orthogonalCoordinateDecimal右侧 y 轴的两次。第二次,它移回到另一个顶部的左侧。

于 2012-12-26T20:37:30.320 回答