0

好吧,我很难过。我正在尝试在我的 iOS 应用程序中使用 Core Plot 1.2,并且正在尝试将两个绘图添加到一个屏幕上。我的视图控制器代码如下。也有一个 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];
     ****/

}
4

1 回答 1

0

您在绘图区域框架上的填充太大。该图只有 150 像素高,但您试图将绘图区域插入 380 像素,没有空间来绘制绘图。

于 2013-04-19T01:42:53.033 回答