CorePlot を Xcode 4 で使ってみる。
CorePlot に入っているドキュメントとサンプルを見ればおおよその使い方はわかる。
今回はココのページのグラフを描いてみることにした。ちなみに、ここのページの情報はちょっと古いみたいでそのままじゃ動かない…。
1. Xcode 4 で「View-based Application」の新規プロジェクトを作成。名前は「MyCorePlotTest」とした。
2. プロジェクトに CorePlot のヘッダファイルとライブラリファイルを加える。ドラッグ&ドロップで OK。
3. MyCorePlotTestViewController.xib で、View を CPTGraphHostingView に変更。
今のバージョンの CorePlot は CPT がプレフィクスみたい。
4. MyCorePlotTestViewController.h で
5. MyCorePlotTestViewController.m の viewDidLoad でグラフを描く!
さっきのページのコードをベースに、CorePlot のサンプルを見ながら今のバージョンで動くように修正した。
6. プロジェクトのフレームワークに QuartzCore を加える。
7. プロジェクトの「Build Settings」に「-ObjC -all_load」を加える。
とりあえず、MyCorePlotTestViewController.h と MyCorePlotTestViewController.m の viewDidLoad および CPTPlotDataSource プロトコル関係のコードを残しておきます。
MyCorePlotTestViewController.h
#import <UIKit/UIKit.h> #import "CorePlot-CocoaTouch.h" @interface MyCorePlotTestViewController : UIViewController <CPTPlotDataSource> { @private CPTXYGraph *graph; } @end
MyCorePlotTestViewController.m
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; graph = [[CPTXYGraph alloc] initWithFrame: self.view.bounds]; // Create graph from theme graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero]; CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme]; [graph applyTheme:theme]; CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view; hostingView.collapsesLayers = NO; // Setting to YES reduces GPU memory usage, but can slow drawing/scrolling hostingView.hostedGraph = graph; graph.paddingLeft = 10.0; graph.paddingTop = 10.0; graph.paddingRight = 10.0; graph.paddingBottom = 10.0; // Setup plot space CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; plotSpace.allowsUserInteraction = YES; plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-6) length:CPTDecimalFromFloat(12)]; plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-6) length:CPTDecimalFromFloat(32)]; // Axes CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; lineStyle.lineColor = [CPTColor blackColor]; lineStyle.lineWidth = 2.0f; CPTXYAxis *x = axisSet.xAxis; x.majorIntervalLength = CPTDecimalFromString(@"5"); x.minorTicksPerInterval = 4; x.majorTickLineStyle = lineStyle; x.minorTickLineStyle = lineStyle; x.axisLineStyle = lineStyle; x.minorTickLength = 5.0f; x.majorTickLength = 7.0f; CPTXYAxis *y = axisSet.yAxis; y.majorIntervalLength = CPTDecimalFromString(@"5"); y.minorTicksPerInterval = 4; y.majorTickLineStyle = lineStyle; y.minorTickLineStyle = lineStyle; y.axisLineStyle = lineStyle; y.minorTickLength = 5.0f; y.majorTickLength = 7.0f; // xSquared graph CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] init] autorelease]; CPTMutableLineStyle *xSPLineStyle = [CPTMutableLineStyle lineStyle]; xSPLineStyle.lineWidth = 1.0f; xSPLineStyle.lineColor = [CPTColor redColor]; xSquaredPlot.dataLineStyle = xSPLineStyle; xSquaredPlot.identifier = @"X Squared Plot"; xSquaredPlot.dataSource = self; [graph addPlot:xSquaredPlot]; // Add plot symbols CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol]; greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]]; greenCirclePlotSymbol.size = CGSizeMake(3.0, 3.0); xSquaredPlot.plotSymbol = greenCirclePlotSymbol; // xInverse graph CPTScatterPlot *xInversePlot = [[[CPTScatterPlot alloc] init] autorelease]; CPTMutableLineStyle *xISPLineStyle = [CPTMutableLineStyle lineStyle]; xISPLineStyle.lineWidth = 1.0f; xISPLineStyle.lineColor = [CPTColor yellowColor]; xInversePlot.dataLineStyle = xISPLineStyle; xInversePlot.identifier = @"X Inverse Plot"; xInversePlot.dataSource = self; [graph addPlot:xInversePlot]; } #pragma mark - #pragma mark Plot Data Source Methods -(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot { return 51; } -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index { double val = (index/5.0)-5; if(fieldEnum == CPTScatterPlotFieldX) { return [NSNumber numberWithDouble:val]; } else { if(plot.identifier == @"X Squared Plot") { return [NSNumber numberWithDouble:val*val]; } else { return [NSNumber numberWithDouble:1/val]; } } }
もしも必用ならば AdMob のサンプルのように BitBucket にコードをアップしますので、リクエストしていただければと思います。