2012-01-27 15 views
0

我試圖在同一頁上繪製兩組數據和時間,因此我需要兩個不同的X和Y軸。以下是我正在使用的示例代碼:在單頁視圖頁上的核心圖中爲2個不同圖創建兩個X軸

-(void)initialisePlot 
{ 
// Start with some simple sanity checks before we kick off 
if ((self.hostingView == nil) || (self.graphData == nil)) { 
    NSLog(@"TUTSimpleScatterPlot: Cannot initialise plot without hosting view or data."); 
    return; 
} 

if (self.graph != nil) { 
    NSLog(@"TUTSimpleScatterPlot: Graph object already exists."); 
    return; 
} 

// Create a graph object which we will use to host just one scatter plot. 
CGRect frame = [self.hostingView bounds]; 
self.graph = [[CPTXYGraph alloc] initWithFrame:frame]; 

// Add some padding to the graph, with more at the bottom for axis labels. 
self.graph.plotAreaFrame.paddingTop = 20.0f; 
self.graph.plotAreaFrame.paddingRight = 20.0f; 
self.graph.plotAreaFrame.paddingBottom = 50.0f; 
self.graph.plotAreaFrame.paddingLeft= 20.0f; 

// Tie the graph we've created with the hosting view. 
self.hostingView.hostedGraph = self.graph; 

// If you want to use one of the default themes - apply that here. 
[self.graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]]; 

// Create a line style that we will apply to the axis and data line. 
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; 
lineStyle.lineColor = [CPTColor redColor]; 
lineStyle.lineWidth = 2.0f; 

// Create a text style that we will use for the axis labels. 
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle]; 
textStyle.fontName = @"Helvetica"; 
textStyle.fontSize = 14; 
textStyle.color = [CPTColor blackColor]; 

// Create the plot symbol we're going to use. 
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol hexagonPlotSymbol]; 
plotSymbol.lineStyle = lineStyle; 
plotSymbol.size = CGSizeMake(8.0, 8.0); 


// Setup some floats that represent the min/max values on our axis. 
float xAxisMin = -10; 
float xAxisMax = 10; 
float yAxisMin = 0; 
float yAxisMax = 100; 

// We modify the graph's plot space to setup the axis' min/max values. 
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace; 
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xAxisMin) length:CPTDecimalFromFloat(xAxisMax - xAxisMin)]; 
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yAxisMin) length:CPTDecimalFromFloat(yAxisMax - yAxisMin)]; 

// Modify the graph's axis with a label, line style, etc. 
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet; 


axisSet.xAxis.title = @"Data X"; 
axisSet.xAxis.titleTextStyle = textStyle; 
axisSet.xAxis.titleOffset = 30.0f; 
axisSet.xAxis.axisLineStyle = lineStyle; 
axisSet.xAxis.majorTickLineStyle = lineStyle; 
axisSet.xAxis.minorTickLineStyle = lineStyle; 
axisSet.xAxis.labelTextStyle = textStyle; 
axisSet.xAxis.labelOffset = 3.0f; 
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(2.0f); 
axisSet.xAxis.minorTicksPerInterval = 1; 
axisSet.xAxis.minorTickLength = 5.0f; 
axisSet.xAxis.majorTickLength = 7.0f; 

axisSet.yAxis.title = @"Data Y"; 
axisSet.yAxis.titleTextStyle = textStyle; 
axisSet.yAxis.titleOffset = 40.0f; 
axisSet.yAxis.axisLineStyle = lineStyle; 
axisSet.yAxis.majorTickLineStyle = lineStyle; 
axisSet.yAxis.minorTickLineStyle = lineStyle; 
axisSet.yAxis.labelTextStyle = textStyle; 
axisSet.yAxis.labelOffset = 3.0f; 
axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(20.0f); 
axisSet.yAxis.minorTicksPerInterval = 1; 
axisSet.yAxis.minorTickLength = 5.0f; 
axisSet.yAxis.majorTickLength = 7.0f; 

// Add a plot to our graph and axis. We give it an identifier so that we 
// could add multiple plots (data lines) to the same graph if necessary. 
CPTScatterPlot *plot = [[CPTScatterPlot alloc] init]; 



plot.dataSource = self; 
plot.identifier = @"mainplot"; 

plot.dataLineStyle = lineStyle; 
plot.plotSymbol = plotSymbol; 
[_graph reloadData]; 
[self.graph addPlot:plot]; 
} 

我應該想出哪些更改?任何想法? 謝謝

回答

1

此行

CGRect frame = [self.hostingView bounds]; 

意味着我們將利用我們的所有hostingView情節。

  1. 什麼用

    -(void)initialisePlot:(UIInteger)indexOfPlot

    代替:

    -(void)initialisePlot

  2. 使用只是hostingView的一部分(變化框架相應indexOfPlot):

    CGRect frame = [self.hostingView bounds]; //change this to:

    只是流程:

    CGRect frame;

    if (indexOfPlot == 0) {frame = ...(set upper part here)} else {frame = ...(lower)}

  3. 然後找到源initialisePlot,改變兩個initialezePlot:0initializePlot:1方法。

看起來很簡單。

+0

你好Kaspartus。我有點迷路。 - (void)initialisePlot:(UIInteger)indexOfPlot我應該在哪裏聲明這個?和CGRect frame = [self.hostingView bounds];改變這個? – 2012-01-27 21:57:13

+1

在這裏,可能在.h文件中。而且,是的,改變'CGRect frame = [self.hostingView bounds];'你必須用indexOfPlot設置相應的幀。例如,第一個情節的上半部分和第二個情節的下半部分。 – kaspartus 2012-01-27 22:34:06

+0

你好Kaspartus,烏爾的建議工作正常,但如果我需要同時使用兩個不同的數據集的情節呢? – 2012-01-31 16:01:31

相關問題