如何在一个屏幕上添加多个核心绘图图

时间:2013-04-17 16:36:33

标签: ios core-plot

好的,我很难过。我正在尝试在我的iOS应用程序中使用Core Plot 1.2,我试图在一个屏幕上添加两个图。我的View Controller的代码如下。还有一个NIB文件,但它是空的 - 只是一个UIView中的默认视图。我创建了两个CPTGraphHostingView类型的子视图,我将它们添加为self.view的suvviews,然后创建图形。在下面的代码中,我甚至评论了创建其中一个图表的方法,看看我是否能够显示一个图表,但我得到的只是一个空白的白色屏幕。

我正在使用一个简单的X ** 2函数来测试每个图。

我做错了什么?是否有更简单的方法可以将2个或3个单独的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];
     ****/

}

1 个答案:

答案 0 :(得分:0)

你在情节区域框架上的填充太大了。图表只有150像素高,但你试图将绘图区域插入380像素,没有留下绘制图的空间。