2012-12-26 135 views
4

我環顧四周尋找(甚至找到)一些關於如何做到這一點的例子,但我似乎無法得到它的工作。無論我做什麼,我都無法讓另一個軸顯示在圖的右側。我覺得這可能是我錯過的一件愚蠢的事情。左右Y軸的核心繪圖圖表

以下是用於生成圖形的所有代碼。我在一天中的數小時內繪製數值。最終每個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

您正在設置右側y軸的orthogonalCoordinateDecimal兩次。第二次,它移動到另一個頂部的左側。

+0

Doh!不能相信我錯過了這一點。我知道這是愚蠢的... –