2012-05-11 12 views
3

我遇到了在同一視圖中顯示兩個不同CorePlot圖形的問題。我在視圖中設置了兩個託管視圖,並且所有參考插口均正確連接。只有一個圖表正確顯示。在numberForPlot方法中添加快速NSLog顯示僅填充第一個圖(和graphView),而不是第二個(trendtrendView)。代碼如下:無法同時顯示兩個CorePlot圖形

- (void) graphSetup { 
    CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme]; 

    graph = (CPTXYGraph *)[theme newGraph]; 

    graphView.hostedGraph = graph; 
    graphView.allowPinchScaling = YES; 

    graph.paddingLeft = -5; 
    graph.paddingTop = 0; 
    graph.paddingRight = -5; 
    graph.paddingBottom = -5; 
    graph.fill = nil; 
    graph.plotAreaFrame.fill = nil; 
    int a = 0; 
    for (int i = 0; i < [romAveragesArray count]; i++) { 
     if ([romAveragesArray objectAtIndex:i] != NULL) 
      a++; 
    } 

    int localMax = 0; 
    int localMin = 0; 

    for (int a = 0; a < [romAveragesArray count]; a++) { 
     if ([[romAveragesArray objectAtIndex:a] getValue] > localMax) 
      localMax = [[romAveragesArray objectAtIndex:a] getValue]; 
     else if ([[romAveragesArray objectAtIndex:a] getValue] < localMin) 
      localMin = [[romAveragesArray objectAtIndex:a] getValue]; 
    } 

    for (int a = 0; a < [obaAveragesArray count]; a++) { 
     if ([[obaAveragesArray objectAtIndex:a] getValue] > localMax) 
      localMax = [[obaAveragesArray objectAtIndex:a] getValue]; 
     else if ([[obaAveragesArray objectAtIndex:a] getValue] < localMin) 
      localMin = [[obaAveragesArray objectAtIndex:a] getValue]; 

    } 


    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; 
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) 
                length:CPTDecimalFromInt(145)]; 
    NSLog(@"%d",a); 
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) 
                length:CPTDecimalFromInt(localMax+15)]; 




    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; 

    CPTXYAxis *x = axisSet.xAxis; 
    x.majorIntervalLength = CPTDecimalFromFloat(10); 
    x.minorTicksPerInterval = 2; 
    x.borderWidth = 0; 
    x.labelExclusionRanges = [NSArray arrayWithObjects: 
           [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-100) 
                  length:CPTDecimalFromFloat(300)], 
           nil];; 

    CPTXYAxis *y = axisSet.yAxis; 
    y.majorIntervalLength = CPTDecimalFromFloat(10); 
    y.minorTicksPerInterval = 1; 
    y.labelExclusionRanges = [NSArray arrayWithObjects: 
           [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-100) 
                  length:CPTDecimalFromFloat(300)], 
           nil]; 





    CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init]; 
    dataSourceLinePlot.identifier = @"Obama"; 
    dataSourceLinePlot.dataSource = self; 
    CPTMutableLineStyle *style = [CPTMutableLineStyle lineStyle]; 
    style.lineColor = [CPTColor colorWithComponentRed:0.3215686274509804 green:0.6078431372549019 blue:1.0 alpha:1.0]; 
    dataSourceLinePlot.dataLineStyle = style; 
    [graph addPlot:dataSourceLinePlot]; 


    CPTScatterPlot *dataSourceLinePlot1 = [[CPTScatterPlot alloc] init]; 
    dataSourceLinePlot1.identifier = @"Rom"; 
    dataSourceLinePlot1.dataSource = self; 
    CPTMutableLineStyle *style1 = [CPTMutableLineStyle lineStyle]; 
    style1.lineColor = [CPTColor colorWithComponentRed:0.9803921568627451 green:0.47058823529411764 blue:0.47058823529411764 alpha:1.0]; 
    dataSourceLinePlot1.dataLineStyle = style1; 
    [graph addPlot:dataSourceLinePlot1]; 
    [self trendSetup]; 

    [self postStats]; 

} 


- (void) trendSetup { 
    CPTTheme *theme1 = [CPTTheme themeNamed:kCPTPlainWhiteTheme]; 

    trend = (CPTXYGraph *) [theme1 newGraph]; 
    trendView.allowPinchScaling = YES; 
    trendView.hostedGraph = trend; 

    trend.paddingLeft = -5; 
    trend.paddingRight = -5; 
    trend.paddingTop = 0; 
    trend.paddingBottom = -5; 
    trend.fill = nil; 
    trend.plotAreaFrame.fill = nil; 
    int obaMax, localMax; 
    for (int i = 0; i < [obaArray count]; i++) 
     obaMax += [[obaArray objectAtIndex:i] getValue]; 

    int romMax; 
    for (int i = 0; i < [romArray count]; i++) 
     romMax += [[romArray objectAtIndex:i] getValue]; 

    if (romMax > obaMax) 
     localMax = romMax; 
    else 
     localMax = obaMax; 

    CPTXYPlotSpace *trendSpace = (CPTXYPlotSpace *)trend.defaultPlotSpace; 
    trendSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(504)]; 
    trendSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(localMax)]; 

    CPTXYAxisSet *trendSet = (CPTXYAxisSet *)trend.axisSet; 

    CPTXYAxis *trendX = trendSet.xAxis; 
    trendX.majorIntervalLength = CPTDecimalFromInt(10); 
    trendX.minorTicksPerInterval = 2; 
    trendX.borderWidth = 0; 
    trendX.labelExclusionRanges = [NSArray arrayWithObjects:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-100) length:CPTDecimalFromFloat(300)], nil];; 

    CPTXYAxis *trendY = trendSet.xAxis; 
    trendY.majorIntervalLength = CPTDecimalFromInt(10); 
    trendY.minorTicksPerInterval = 2; 
    trendY.borderWidth = 0; 
    trendY.labelExclusionRanges = [NSArray arrayWithObjects:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-100) length:CPTDecimalFromFloat(300)], nil];; 

    CPTScatterPlot *trendSourceLinePlot = [[CPTScatterPlot alloc] init]; 
    trendSourceLinePlot.identifier = @"Obama Trend"; 
    trendSourceLinePlot.dataSource = self; 
    CPTMutableLineStyle *style3 = [CPTMutableLineStyle lineStyle]; 
    style3.lineColor = [CPTColor colorWithComponentRed:0.3215686274509804 green:0.6078431372549019 blue:1.0 alpha:1.0]; 
    trendSourceLinePlot.dataLineStyle = style3; 
    [trend addPlot:trendSourceLinePlot]; 


    CPTScatterPlot *trendSourceLinePlot1 = [[CPTScatterPlot alloc] init]; 
    trendSourceLinePlot1.identifier = @"Rom Trend"; 
    trendSourceLinePlot1.dataSource = self; 
    CPTMutableLineStyle *style4 = [CPTMutableLineStyle lineStyle]; 
    style4.lineColor = [CPTColor colorWithComponentRed:0.9803921568627451 green:0.47058823529411764 blue:0.47058823529411764 alpha:1.0]; 
    trendSourceLinePlot1.dataLineStyle = style4; 
    [trend addPlot:trendSourceLinePlot1]; 

} 
+0

這兩個託管視圖是否可見? –

+0

@EricSkroch是的。 –

+0

你有沒有在趨勢設置功能上放置一個斷點? – CStreel

回答

0

下面是我發現不太對你的代碼,我想這就是Eric問你是否兩個託管視圖都可見的原因。

您正在創建兩個CPTXYGraph s一個稱爲另一個稱爲trend。我不知道如何在Interface Builder中設置視圖,但我不會創建兩個CPTXYGraph s。我會將trendSourceLinePlot & trendSourceLinePlot1添加到trend或。

我認爲,你需要做的是將它們呈現在同一CPTXYGraph。所以,這裏是我的建議...

  1. trentSetup

    CPTTheme *theme1 = [CPTTheme themeNamed:kCPTPlainWhiteTheme]; 
    trend = (CPTXYGraph *) [theme1 newGraph]; 
    trendView.allowPinchScaling = YES; 
    trendView.hostedGraph = trend; 
    trend.paddingLeft = -5; 
    trend.paddingRight = -5; 
    trend.paddingTop = 0; 
    trend.paddingBottom = -5; 
    trend.fill = nil; 
    trend.plotAreaFrame.fill = nil; 
    
  2. 刪除以下行替換ONLY以下行

    [trend addPlot:trendSourceLinePlot]; 
    [trend addPlot:trendSourceLinePlot1]; 
    
    分別與

    [graph addPlot:trendSourceLinePlot]; 
    [graph addPlot:trendSourceLinePlot1]; 
    

它也應該加快加載過程。

0

該圖的Y範圍太長。我刪除了刻度線部分中的部分,並立即更新。