2012-06-07 81 views
8

我正在編程一個應用程序,其中我有兩個y軸和一個x軸的圖形。左側y軸的範圍從0到20,因此有20個主要軸。右側y軸的範圍從0到10,所以我希望左y軸標記爲每個替代majorTickAxis。不同軸座標的核心繪圖圖中的兩個y軸

這裏是我的代碼片斷

//code 
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace; 
float xmax=10.0; 
float xmin=0.0; 
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xmin)  length:CPTDecimalFromFloat(xmax-xmin)]; 
float ymax=20.0; 
float ymin=0.0; 
float ymax2=10.0; 
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(ymin) length:CPTDecimalFromFloat(ymax-ymin)]; 



// Grid line styles 
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle]; 
majorGridLineStyle.lineWidth = 0.75; 
majorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.75]; 

CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle]; 
minorGridLineStyle.lineWidth = 0.25; 
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1]; 

CPTMutableLineStyle *redLineStyle = [CPTMutableLineStyle lineStyle]; 
redLineStyle.lineWidth = 2.0; 
redLineStyle.lineColor = [[CPTColor redColor] colorWithAlphaComponent:0.5];       

CPTMutableLineStyle *greenLineStyle = [CPTMutableLineStyle lineStyle]; 
greenLineStyle.lineWidth = 2.0; 
greenLineStyle.lineColor = [[CPTColor greenColor] colorWithAlphaComponent:0.5];  

// Axes 
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet; 
CPTXYAxis *x   = axisSet.xAxis; 
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0"); 
x.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"1"] decimalValue]; 
x.minorTicksPerInterval  = 4; 
x.labelOffset = 3.0f; 
x.title   = @"Time"; 
x.titleOffset = 20.0; 
x.titleLocation = CPTDecimalFromFloat((xmax+xmin)/2); 
x.majorGridLineStyle= majorGridLineStyle; 
x.minorGridLineStyle=minorGridLineStyle; 


CPTXYAxis *y = axisSet.yAxis; 
y.majorIntervalLength   = CPTDecimalFromString(@"1.0"); 
y.majorTickLength=2.0f; 
y.minorTicksPerInterval  = 4; 
y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.0); 
y.labelExclusionRanges = [NSArray arrayWithObjects: 
          [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0.0) 
                 length:CPTDecimalFromInteger(0.0)],Nil]; 
y.majorGridLineStyle= majorGridLineStyle; 
y.minorGridLineStyle=minorGridLineStyle; 


CPTXYAxis *y2 = [[(CPTXYAxis *)[CPTXYAxis alloc] initWithFrame:CGRectZero] autorelease]; 
y2.plotSpace =plotSpace;  
y2.orthogonalCoordinateDecimal = CPTDecimalFromFloat(xmax); 
y2.majorGridLineStyle=majorGridLineStyle; 
y2.minorGridLineStyle=minorGridLineStyle; 
y2.minorTicksPerInterval  = 4; 
y2.majorIntervalLength  = CPTDecimalFromString(@"1.0"); 
y2.labelOffset  = 10.0; 
y2.coordinate   =CPTCoordinateY; 

y2.axisLineStyle  = x.axisLineStyle; 
y2.labelTextStyle    = x.labelTextStyle; 
y2.labelOffset     = -30.0f; 
y2.visibleRange    = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) length:CPTDecimalFromInteger(ymax2)]; 


y2.title      = @"Temperature"; 
y2.titleLocation    = CPTDecimalFromInteger(5.0); 
y2.titleTextStyle    =x.titleTextStyle; 
y2.titleOffset     =-45.0f; 
y2.labelExclusionRanges = [NSArray arrayWithObjects: 
          [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0.0) 
                 length:CPTDecimalFromInteger(0.0)],Nil]; 


self.graph.axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil]; 

// Create a plot that uses the data source method for red graph 
CPTScatterPlot *redPlot = [[[CPTScatterPlot alloc] init] autorelease]; 
redPlot.identifier = @"red Plot";; 

CPTMutableLineStyle *lineStyle = [[redPlot.dataLineStyle mutableCopy] autorelease]; 
lineStyle.miterLimit  = 1.0f; 
redPlot.dataLineStyle = redLineStyle; 
redPlot.dataSource = self; 
redPlot.interpolation = CPTScatterPlotInterpolationStepped; 

[self.graph addPlot:redPlot]; 

// Create a plot that uses the data source method for green graph 
CPTScatterPlot *greenPlot = [[[CPTScatterPlot alloc] init] autorelease]; 
greenPlot.identifier = @"green Plot";; 

CPTMutableLineStyle *greenlineStyle = [[greenPlot.dataLineStyle mutableCopy] autorelease]; 
greenlineStyle.miterLimit  = 1.0f; 
greenPlot.dataLineStyle = greenLineStyle; 
greenPlot.dataSource = self; 
[self.graph addPlot:greenPlot]; 

// Add some data 
NSMutableArray *newData = [NSMutableArray arrayWithCapacity:100]; 
NSUInteger i; 
for (i = 0; i < 45; i++) { 
    id x = [NSNumber numberWithDouble:i * 0.2]; 
    id y = [NSNumber numberWithDouble:i * rand()/((double)RAND_MAX*5.0) ];   
    [newData addObject:[NSDictionary dictionaryWithObjectsAndKeys: 
     x, @"x",y, @"y",nil]]; 
} 
NSMutableArray *newData1 = [NSMutableArray arrayWithCapacity:100]; 
for (i = 0; i < 45; i++) { 
    id x =[NSNumber numberWithDouble:i * rand()/((double)RAND_MAX*5.0) ]; 
    id y2 = [NSNumber numberWithDouble:i * 0.2]; 
    [newData1 addObject:[NSDictionary dictionaryWithObjectsAndKeys: 
         x, @"x",y2, @"y2",nil]]; 
} 
self.plotData = newData; 
self.plotData2=newData1; 
+0

設置'labelExclusionRanges'如果你不需要的話,可以用'nil'。 –

回答

10

如果你希望兩個y軸有不同的尺度,你需要添加另一個情節空間。使用相同的xRange第二個情節空間,但使用不同的yRange,例如,

CPTXYPlotSpace *plotSpace2 = [[[CPTXYPlotSpace alloc] init] autorelease]; 
plotSpace2.xRange = plotSpace.xRange; 
plotSpace2.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(ymin) 
               length:CPTDecimalFromFloat(ymax2 - ymin)]; 
[graph addPlotSpace:plotSpace2]; 
y2.plotSpace = plotSpace2; 

使用majorIntervalLength控制蜱和網格線的位置:

y.majorIntervalLength = CPTDecimalFromFloat((ymax - ymin)/10.0f); 
y2.majorIntervalLength = CPTDecimalFromFloat((ymax2 - ymin)/10.0f); 
+0

嗨埃裏克,非常感謝,你的代碼是非常有幫助的,現在我有不同的尺度爲y軸 – raja

+0

萬一我使用酒吧顯示我的價值觀,我有兩個y軸有兩個情節空間(其中一個是紅色的,是例如綠色),並且它們使用相同的x軸,這將使它們彼此重疊,因此例如我的紅色條代表第一個值將用綠色代表第二個值在同一個x軸位置覆蓋。我實際上會喜歡那些紅色和綠色的條紋,以x軸爲中心,因此它們都是可見的。那可能嗎? – Ewoks

+0

您可以使用'barOffset'屬性將一個或兩個圖中的條移動到遠離正常中心線的位置,以便您可以看到它們兩個。 –