2013-04-17 59 views
0

好吧,我很難過。我試圖在我的iOS應用程序中使用Core Plot 1.2,並試圖將兩個圖添加到一個屏幕上。我的View Controller的代碼如下。還有一個NIB文件,但它是空的 - 只是UIView中的默認視圖。我創建了CPTGraphHostingView類型的兩個子視圖,我將它們添加爲self.view的suvviews,然後創建這些圖形。在下面的代碼中,我甚至提出了創建圖形之一的方法,以查看我是否可以顯示一個圖形,但我得到的只是一個空白的白色屏幕。如何在一個屏幕上添加多個核心情節圖

我正在使用一個簡單的X ** 2函數用於每個圖的測試目的。

我在做什麼錯?是否有更簡單的方法將單獨的XY樣式圖表分配到單個屏幕上?

@interface SensorPlotSecondViewController : UIViewController <CPTPlotDataSource> 
{ 
    CPTGraphHostingView *accelSubview; 
    CPTGraphHostingView *gyroSubview; 
    CPTGraphHostingView *magSubview; 

    CPTXYGraph *accelGraph; 
    CPTXYGraph *gyroGraph; 
    CPTXYGraph *magGraph; 
} 

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot; 

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx; 

@end 


@implementation SensorPlotSecondViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     self.title = NSLocalizedString(@"Second", @"Second"); 
     self.tabBarItem.image = [UIImage imageNamed:@"second"]; 

     CGRect frm1=CGRectMake(0,0,320,150); 
     accelSubview=[[CPTGraphHostingView alloc] initWithFrame:frm1]; 
     [self.view addSubview:accelSubview]; 

     CGRect frm2=CGRectMake(0,150,320,150); 
     gyroSubview=[[CPTGraphHostingView alloc] initWithFrame:frm2]; 
     [self.view addSubview:gyroSubview]; 

    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    [self createAccelGraph]; 

    //[self createGyroGraph]; 
} 

-(void)createAccelGraph 
{ 
    accelGraph = [[CPTXYGraph alloc] initWithFrame: accelSubview.bounds]; 

    //CPTTheme *theme=[CPTTheme themeNamed:kCPTPlainWhiteTheme]; 
    //[accelGraph applyTheme:theme]; 

    accelGraph.plotAreaFrame.borderLineStyle = nil; 

    CPTGraphHostingView *hostingView = (CPTGraphHostingView *)accelSubview; 
    hostingView.hostedGraph=accelGraph; 
    accelGraph.paddingLeft = 0.0; 
    accelGraph.paddingTop = 0.0; 
    accelGraph.paddingRight = 0.0; 
    accelGraph.paddingBottom = 0.0; 

    //accelGraph.fill=nil; 

    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)accelGraph.defaultPlotSpace; 
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) 
                length:CPTDecimalFromFloat(10)]; 
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) 
                length:CPTDecimalFromFloat(30)]; 

    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; 
    lineStyle.lineColor = [CPTColor blackColor]; 
    lineStyle.lineWidth = 2.0f; 

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)accelGraph.axisSet; 
    axisSet.xAxis.majorIntervalLength = CPTDecimalFromString(@"5"); 

    axisSet.xAxis.minorTicksPerInterval = 4; 
    axisSet.xAxis.majorTickLineStyle = lineStyle; 
    axisSet.xAxis.minorTickLineStyle = lineStyle; 
    axisSet.xAxis.axisLineStyle = lineStyle; 
    axisSet.xAxis.minorTickLength = 5.0f; 
    axisSet.xAxis.majorTickLength = 7.0f; 
    axisSet.xAxis.labelOffset = 3.0f; 

    axisSet.xAxis.visibleAxisRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromString(@"0") length:CPTDecimalFromString(@"10")]; 

    axisSet.yAxis.majorIntervalLength = CPTDecimalFromString(@"5"); 
    axisSet.yAxis.minorTicksPerInterval = 4; 
    axisSet.yAxis.majorTickLineStyle = lineStyle; 
    axisSet.yAxis.minorTickLineStyle = lineStyle; 
    axisSet.yAxis.axisLineStyle = lineStyle; 
    axisSet.yAxis.minorTickLength = 5.0f; 
    axisSet.yAxis.majorTickLength = 7.0f; 
    axisSet.yAxis.labelOffset = 3.0f; 

    axisSet.yAxis.visibleAxisRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromString(@"0") length:CPTDecimalFromString(@"25")]; 

    accelGraph.plotAreaFrame.paddingLeft = 40.0; 
    accelGraph.plotAreaFrame.paddingTop = 80.0; 
    accelGraph.plotAreaFrame.paddingRight = 40.0; 
    accelGraph.plotAreaFrame.paddingBottom = 300.0; 

    CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] initWithFrame:accelSubview.bounds] autorelease]; 
    //CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] init] autorelease]; 
    xSquaredPlot.identifier = @"X Squared Plot"; 

    CPTMutableLineStyle *dataLineStyle = [CPTMutableLineStyle lineStyle]; 
    dataLineStyle.lineWidth = 1.0f; 
    dataLineStyle.lineColor = [CPTColor redColor]; 
    xSquaredPlot.dataLineStyle=dataLineStyle; 
    xSquaredPlot.dataSource = self; 

    [accelGraph addPlot:xSquaredPlot]; 

    CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol]; 
    greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]]; 
    greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0); 
    //xSquaredPlot.defaultPlotSymbol = greenCirclePlotSymbol; 
    xSquaredPlot.plotSymbol = greenCirclePlotSymbol; 

    /**** 
    CPTScatterPlot *xInversePlot = [[[CPTScatterPlot alloc] init] autorelease]; 
    xInversePlot.identifier = @"X Inverse Plot"; 

    CPTMutableLineStyle *dataLineStyle2 = [CPTMutableLineStyle lineStyle]; 
    dataLineStyle2.lineWidth = 1.0f; 
    dataLineStyle2.lineColor = [CPTColor blueColor]; 
    xInversePlot.dataLineStyle=dataLineStyle2; 
    xInversePlot.dataSource = self; 
    [accelGraph addPlot:xInversePlot]; 
    ****/ 

} 

-(void)createGyroGraph 
{ 
    gyroGraph = [[CPTXYGraph alloc] initWithFrame: gyroSubview.bounds]; 

    CPTTheme *theme=[CPTTheme themeNamed:kCPTPlainWhiteTheme]; 
    [gyroGraph applyTheme:theme]; 

    gyroGraph.plotAreaFrame.borderLineStyle = nil; 

    CPTGraphHostingView *hostingView = (CPTGraphHostingView *)gyroSubview; 
    hostingView.hostedGraph=gyroGraph; 
    gyroGraph.paddingLeft = 0.0; 
    gyroGraph.paddingTop = 0.0; 
    gyroGraph.paddingRight = 0.0; 
    gyroGraph.paddingBottom = 0.0; 

    gyroGraph.fill=nil; 

    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)gyroGraph.defaultPlotSpace; 
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) 
                length:CPTDecimalFromFloat(10)]; 
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) 
                length:CPTDecimalFromFloat(30)]; 

    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; 
    lineStyle.lineColor = [CPTColor blackColor]; 
    lineStyle.lineWidth = 2.0f; 

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)gyroGraph.axisSet; 
    axisSet.xAxis.majorIntervalLength = CPTDecimalFromString(@"5"); 

    axisSet.xAxis.minorTicksPerInterval = 4; 
    axisSet.xAxis.majorTickLineStyle = lineStyle; 
    axisSet.xAxis.minorTickLineStyle = lineStyle; 
    axisSet.xAxis.axisLineStyle = lineStyle; 
    axisSet.xAxis.minorTickLength = 5.0f; 
    axisSet.xAxis.majorTickLength = 7.0f; 
    axisSet.xAxis.labelOffset = 3.0f; 

    axisSet.xAxis.visibleAxisRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromString(@"0") length:CPTDecimalFromString(@"10")]; 

    axisSet.yAxis.majorIntervalLength = CPTDecimalFromString(@"5"); 
    axisSet.yAxis.minorTicksPerInterval = 4; 
    axisSet.yAxis.majorTickLineStyle = lineStyle; 
    axisSet.yAxis.minorTickLineStyle = lineStyle; 
    axisSet.yAxis.axisLineStyle = lineStyle; 
    axisSet.yAxis.minorTickLength = 5.0f; 
    axisSet.yAxis.majorTickLength = 7.0f; 
    axisSet.yAxis.labelOffset = 3.0f; 

    axisSet.yAxis.visibleAxisRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromString(@"0") length:CPTDecimalFromString(@"25")]; 

    gyroGraph.plotAreaFrame.paddingLeft = 40.0; 
    gyroGraph.plotAreaFrame.paddingTop = 200.0; 
    gyroGraph.plotAreaFrame.paddingRight = 40.0; 
    gyroGraph.plotAreaFrame.paddingBottom = 150.0; 

    CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] initWithFrame:gyroSubview.bounds] autorelease]; 
    //CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] init] autorelease]; 
    xSquaredPlot.identifier = @"X Squared Plot"; 

    CPTMutableLineStyle *dataLineStyle = [CPTMutableLineStyle lineStyle]; 
    dataLineStyle.lineWidth = 1.0f; 
    dataLineStyle.lineColor = [CPTColor redColor]; 
    xSquaredPlot.dataLineStyle=dataLineStyle; 
    xSquaredPlot.dataSource = self; 

    [gyroGraph addPlot:xSquaredPlot]; 

    CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol]; 
    greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]]; 
    greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0); 
    //xSquaredPlot.defaultPlotSymbol = greenCirclePlotSymbol; 
    xSquaredPlot.plotSymbol = greenCirclePlotSymbol; 

    /**** 
    CPTScatterPlot *xInversePlot = [[[CPTScatterPlot alloc] init] autorelease]; 
    xInversePlot.identifier = @"X Inverse Plot"; 

    CPTMutableLineStyle *dataLineStyle2 = [CPTMutableLineStyle lineStyle]; 
    dataLineStyle2.lineWidth = 1.0f; 
    dataLineStyle2.lineColor = [CPTColor blueColor]; 
    xInversePlot.dataLineStyle=dataLineStyle2; 
    xInversePlot.dataSource = self; 
    [accelGraph addPlot:xInversePlot]; 
    ****/ 

} 
+0

對未來的建議:在corePlot演示項目中找到最合適的示例並逐步修改它。 – DanSkeel

回答

0

你的繪圖區框架填充太大。該圖形只有150像素高,但您試圖將繪圖區域插入380像素,沒有空間繪製地塊。