我想在我的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
如果有人能帮我解决这个问题,我真的很感激,因为我是这个环境的新手。 感谢
答案 0 :(得分:0)
将托管视图(graphHost
)设为self.view
的子视图,而不是替换它。