设备上的Core Plot Graph存在问题但模拟器上没有问题

时间:2014-03-10 04:28:44

标签: ios iphone core-plot

我正在尝试使用Core Plot在iPad上创建三种投资方案的图表。

我正在使用Xcode 5,iPad 4和iOS 7。

基本前提是用户输入三种不同投资的初始投资和利率,单击按钮后,将显示第二个视图,显示同一绘图空间中每个场景的核心绘图散点图表示。 / p>

我的问题是,当我在iPad(实际设备)上运行应用程序时,生成的图形只有等于y = 0的图形(这就是显示的内容,我无法发布图片,因为堆栈溢出需要更高的声誉)

然而,在Xcode的iPad模拟器中,图表显示正确,我使用从初始数据视图输入的数据设置了方程式。

我有两个带有两个独立视图控制器的UIView。一个用于我的数据,一个用于我的图形显示。 我的代码如下:

DataViewController.h

#import <UIKit/UIKit.h>

@interface DataViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *principalOneText;
@property (weak, nonatomic) IBOutlet UITextField *rateOneText;
@property (weak, nonatomic) IBOutlet UITextField *principalTwoText;
@property (weak, nonatomic) IBOutlet UITextField *rateTwoText;
@property (weak, nonatomic) IBOutlet UITextField *principalThreeText;
@property (weak, nonatomic) IBOutlet UITextField *rateThreeText;





@property (weak, nonatomic) IBOutlet UIButton *ViewGraphButton;



- (IBAction)HideKeyboard:(id)sender;


@end

和DataViewController.m

#import "DataViewController.h"

@interface DataViewController ()

@end

@implementation DataViewController

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)HideKeyboard:(id)sender {
    [self.principalOneText resignFirstResponder];
    [self.rateOneText resignFirstResponder];
    [self.principalTwoText resignFirstResponder];
    [self.rateTwoText resignFirstResponder];
    [self.principalThreeText resignFirstResponder];
    [self.rateThreeText resignFirstResponder];

}
@end

GraphViewController.h

#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
#import "DataViewController.h"
#import <QuartzCore/QuartzCore.h>


@interface GraphViewController : UIViewController <CPTPlotDataSource>

@property (weak, nonatomic) NSString *principalOne;
@property (weak, nonatomic) NSString *rateOne;
@property (weak, nonatomic) NSString *principalTwo;
@property (weak, nonatomic) NSString *rateTwo;
@property (weak, nonatomic) NSString *principalThree;
@property (weak, nonatomic) NSString *rateThree;

- (IBAction)foundSwipe:(id)sender;

@end

和GraphViewController.m

#import "GraphViewController.h"

@interface GraphViewController ()

@end

@implementation GraphViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Transfer values from text boxes in Data Scene to the data holders in Graph Scene
    self.principalOne = ((DataViewController *)self.presentingViewController).principalOneText.text;
    self.rateOne = ((DataViewController *)self.presentingViewController).rateOneText.text;
    self.principalTwo= ((DataViewController *)self.presentingViewController).principalTwoText.text;
    self.rateTwo = ((DataViewController *)self.presentingViewController).rateTwoText.text;
    self.principalThree = ((DataViewController *)self.presentingViewController).principalThreeText.text;
    self.rateThree = ((DataViewController *)self.presentingViewController).rateThreeText.text;

    //create graph upon loading
    //initialise the host-view
    CPTGraphHostingView *hostView = [[CPTGraphHostingView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:hostView];

    //initialize the graph
    CPTXYGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.view.bounds];
    hostView.hostedGraph = graph;
    graph.paddingTop = 25.0f;

    //initialize the plot space
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace*) graph.defaultPlotSpace;
    //[plotSpace setXRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(16)]];
    //[plotSpace setYRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(80) length:CPTDecimalFromFloat(140)]];


    //Initialize the plots (1,2, and 3)
    CPTScatterPlot *plot1 = [[CPTScatterPlot alloc] initWithFrame:CGRectZero];
    plot1.identifier = @"Investment #1";
    CPTColor *plot1Color = [CPTColor redColor];

    CPTScatterPlot *plot2 = [[CPTScatterPlot alloc] initWithFrame:CGRectZero];
    plot2.identifier = @"Investment #2";
    CPTColor *plot2Color = [CPTColor blueColor];

    CPTScatterPlot *plot3 = [[CPTScatterPlot alloc] initWithFrame:CGRectZero];
    plot3.identifier = @"Investment #3";
    CPTColor *plot3Color = [CPTColor greenColor];

    //plot 4 is a way to make sure that the automatic ranges include y=0
    CPTScatterPlot *plot4 = [[CPTScatterPlot alloc] initWithFrame:CGRectZero];
    plot4.identifier  = @"Placeholder";

    //define the datasource
    plot1.dataSource = self;
    plot2.dataSource = self;
    plot3.dataSource = self;
    plot4.dataSource = self;

    //Combine elements with the graph method call
    [graph addPlot:plot1 toPlotSpace:graph.defaultPlotSpace];
    [graph addPlot:plot2 toPlotSpace:graph.defaultPlotSpace];
    [graph addPlot:plot3 toPlotSpace:graph.defaultPlotSpace];
    [graph addPlot:plot4 toPlotSpace:graph.defaultPlotSpace];





    //graph title and theme setup
    //graph.title = @"Future Values";
    //graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
    CPTTheme *selectedTheme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
    [graph applyTheme:selectedTheme];

    //auto-formats the ranges of the x and y coordinates depending on the data used

    [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:plot1, plot2, plot3, plot4, Nil]];
    CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
    [xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
    plotSpace.xRange = xRange;
    CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
    [yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
    plotSpace.yRange = yRange;

    //Styles and symbols
    //plot1
    CPTMutableLineStyle *plot1LineStyle = [plot1.dataLineStyle mutableCopy];
    plot1LineStyle.lineWidth = 2.5;
    plot1LineStyle.lineColor = plot1Color;
    plot1.dataLineStyle = plot1LineStyle;
    CPTMutableLineStyle *plot1SymbolLineStyle = [CPTMutableLineStyle lineStyle];
    plot1LineStyle.lineColor = plot1Color;
    CPTPlotSymbol *plot1Symbol = [CPTPlotSymbol ellipsePlotSymbol];
    plot1Symbol.fill = [CPTFill fillWithColor:plot1Color];
    plot1Symbol.lineStyle = plot1SymbolLineStyle;
    plot1Symbol.size = CGSizeMake(6.0f, 6.0f);
    plot1.plotSymbol = plot1Symbol;

    //plot2 style
    CPTMutableLineStyle *plot2LineStyle = [plot2.dataLineStyle mutableCopy];
    plot2LineStyle.lineWidth = 1.0;
    plot2LineStyle.lineColor = plot2Color;
    plot2.dataLineStyle = plot2LineStyle;
    CPTMutableLineStyle *plot2SymbolLineStyle = [CPTMutableLineStyle lineStyle];
    plot2SymbolLineStyle.lineColor = plot2Color;
    CPTPlotSymbol *plot2Symbol = [CPTPlotSymbol starPlotSymbol];
    plot2Symbol.fill = [CPTFill fillWithColor:plot2Color];
    plot2Symbol.lineStyle = plot2SymbolLineStyle;
    plot2Symbol.size = CGSizeMake(6.0f, 6.0f);
    plot2.plotSymbol = plot2Symbol;

    //plot3 style
    CPTMutableLineStyle *plot3LineStyle = [plot3.dataLineStyle mutableCopy];
    plot3LineStyle.lineWidth = 2.0;
    plot3LineStyle.lineColor = plot3Color;
    plot3.dataLineStyle = plot3LineStyle;
    CPTMutableLineStyle *plot3SymbolLineStyle = [CPTMutableLineStyle lineStyle];
    plot3SymbolLineStyle.lineColor = plot3Color;
    CPTPlotSymbol *plot3Symbol = [CPTPlotSymbol diamondPlotSymbol];
    plot3Symbol.fill = [CPTFill fillWithColor:plot3Color];
    plot3Symbol.lineStyle = plot3SymbolLineStyle;
    plot3Symbol.size = CGSizeMake(6.0f, 6.0f);
    plot3.plotSymbol = plot3Symbol;

    //axis styles and setup
    CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
    axisTitleStyle.color = [CPTColor whiteColor];
    axisTitleStyle.fontName = @"Helvetica-Bold";
    axisTitleStyle.fontSize = 12.0f;
    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
    axisLineStyle.lineWidth = 2.0f;
    axisLineStyle.lineColor = [CPTColor whiteColor];
    CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
    axisTextStyle.color = [CPTColor whiteColor];
    axisTextStyle.fontName = @"Helvetica-Bold";
    axisTextStyle.fontSize = 11.0f;
    CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
    tickLineStyle.lineColor = [CPTColor whiteColor];
    tickLineStyle.lineWidth = 2.0f;
    CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
    tickLineStyle.lineColor = [CPTColor blackColor];
    tickLineStyle.lineWidth = 1.0f;

    //x axis setup
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) hostView.hostedGraph.axisSet;
    CPTAxis *x = axisSet.xAxis;
    x.title = @"Number of Years";
    x.titleTextStyle = axisTitleStyle;
    x.titleOffset = 15.0f;
    x.axisLineStyle = axisLineStyle;
    x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
    x.labelTextStyle = axisTextStyle;
    x.preferredNumberOfMajorTicks = 15;
    x.majorTickLineStyle = axisLineStyle;
    x.majorTickLength = 4.0f;
    x.tickDirection = CPTSignNegative;

    //y axis setup
    CPTAxis *y = axisSet.yAxis;
    y.title = @"Value";
    y.titleTextStyle = axisTitleStyle;
    y.titleOffset = -20.0f;
    y.axisLineStyle = axisLineStyle;
    y.majorGridLineStyle = gridLineStyle;
    y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
    y.labelTextStyle = axisTextStyle;
    y.labelOffset = 16.0f;
    y.majorTickLineStyle = axisLineStyle;
    y.preferredNumberOfMajorTicks = 10;
    y.majorTickLength = 4.0f;
    y.minorTickLength = 2.0f;
    y.tickDirection = CPTSignPositive;

    //create a legend
    graph.legend = [CPTLegend legendWithGraph:graph];
    graph.legendAnchor = CPTRectAnchorTop;
    graph.legend.fill = [CPTFill fillWithColor:[CPTColor lightGrayColor]];
    graph.legendDisplacement = CGPointMake(-200.0f, -50.0f);







}

-(NSUInteger) numberOfRecordsForPlot:(CPTPlot *)plot
{
    return 15;
}
-(NSNumber *) numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx
{
    int x = idx;
    float principal1 = [self.principalOne floatValue];
    float interest1 = [self.rateOne floatValue]/100;
    float power1 = powf((1+(interest1)), x);

    float principal2 = [self.principalTwo floatValue];
    float interest2 = [self.rateTwo floatValue]/100;
    float power2 = powf((1+(interest2)),x);

    float principal3 = [self.principalThree floatValue];
    float interest3 = [self.rateThree floatValue]/100;
    float power3 = powf((1+(interest3)), x);


    if (fieldEnum == CPTScatterPlotFieldX) {
        return [NSNumber numberWithInt:x];
    }
    else if(fieldEnum == CPTScatterPlotFieldY){
        if([plot.identifier isEqual:@"Investment #1"])
        {
            return [NSNumber numberWithFloat:principal1*power1];
        }
        else if([plot.identifier isEqual:@"Investment #2"])
        {
            return [NSNumber numberWithFloat:principal2 * power2];
        }
        else if([plot.identifier isEqual:@"Investment #3"])
        {
            return [NSNumber numberWithFloat:principal3 * power3];
        }
        else if ([plot.identifier isEqual:@"Placeholder"])
        {
            return [NSNumber numberWithFloat:0];
        }
    }
    return [NSNumber numberWithFloat:0];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)foundSwipe:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end

我错过了什么吗?为什么我的Xcode iPad模拟器图表和我实际设备上显示的图表之间存在差异?欢迎任何帮助或建议。

0 个答案:

没有答案