-1

iPhoneアプリで折れ線グラフを表示したい。コアプロットは、同じための優れたオープンソースライブラリであることが示唆されました. ということで、ごく基本的なグラフを表示してみましたが、グラフがまったく表示されず、真っ黒な画面しか出ません。以下は私が使用しているコードです

ChartViewController.h

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


@interface ChartViewController : UIViewController<CPTPlotDataSource>

@end

ChartViewController.m

#import "ChartViewController.h"

CPTXYGraph *graph;
CPTGraphHostingView *graphHost;
CPTScatterPlot *dataSourceLinePlot;
NSMutableArray *xdata;
NSMutableArray *ydata;

@implementation ChartViewController

- (id)init{
    self = [super initWithNibName:nil bundle:nil];
    return self;
}

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initializeData];
    NSLog(@"viewdidloaded with number of records= %i",xdata.count);
    graph = [[CPTXYGraph alloc]initWithFrame:self.view.bounds];

    graphHost = [[CPTGraphHostingView alloc]initWithFrame:self.view.bounds];

    dataSourceLinePlot = [[CPTScatterPlot alloc] initWithFrame:graph.bounds];
    dataSourceLinePlot.identifier     = @"Data Source Plot";
    dataSourceLinePlot.dataSource   = self;

    [graph addPlot:dataSourceLinePlot];
    graphHost.hostedGraph = graph;

    self.view = graphHost;
}

- (void)initializeData{
    xdata = [[NSMutableArray alloc]init];
    ydata = [[NSMutableArray alloc]init];
    for(int i=0;i<4;i++){
        [xdata addObject:[NSNumber numberWithInt:i]];
        [ydata addObject:[NSNumber numberWithInt:i*5]];
    }
}

- (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
    NSUInteger count = (NSUInteger)[xdata count];
    NSLog(@"inside number of recordsfor plot");
    return count;
}

- (NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
    NSDecimalNumber *num = nil;
    num = (NSDecimalNumber *)[NSNumber numberWithInteger:index];
    NSLog(@"inside number for plot");
    return num;
}

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

@end

私はこの環境に慣れていないので、誰かがこの問題で私を助けてくれれば本当に感謝しています. ありがとう

4

1 に答える 1

0

ホスティングビュー( )を置き換えるのではなくgraphHost、のサブビューにします。self.view

于 2013-03-02T01:31:31.860 に答える