-1

我想在我的 iphone 应用程序中显示折线图。有人建议我 core plot 是一个很好的开源库。所以我尝试用它显示非常基本的图形,但图形根本没有显示,只有黑屏出现。以下是我正在使用的代码

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 回答