標籤:des blog http color 使用 width
本文轉載至
http://blog.csdn.net/zhibudefeng/article/details/7677457
iOS(iPhone/iPad) 形組件有兩個有名的,s7graphview 和 Core Plot,它們都是在 Google 上託管的代碼,聽說 Core Plot 比較強,因為前者僅支援曲線圖,後者呢曲線圖、餅圖、柱狀圖等通吃,且較活躍。那就專註下 Core Plot 的使用。它提供了 Mac OS X 和 iOS 下的組件庫,我只用到它的 iOS 圖表庫。
Core Plot 能畫出來圖表的效果應該多看看:http://code.google.com/p/core-plot/wiki/PlotExamples,相信看過之後絕大多數的 iOS 下的圖表可以用它來滿足你了。
配置其實很簡單的,先從 http://code.google.com/p/core-plot/downloads/list 下載最新版的 Core Plot,比如當前是:CorePlot_0.4.zip,解壓開,然後就兩步:
1. 把目錄 CorePlot_0.4/Binaries/iOS 中的 libCorePlotCocoaTouch.a 和整個子目錄 CorePlotHeaders 從 Finder 中一併拖入到當前項目中,選擇 Copy item into destination group‘s folder (if needed),Add to targets 裡選上相應的 target。此時你可以在項目的 target 中 Build Phases 頁裡 Link Binary With Libraries 中看到有了 libCorePlot-CocoaTouch.a.
2. 再到相應 Target 的 Build Settings 頁裡,Other Linker Flags 項中加上 -ObjC -all_load
[注]我所用的 Xcode 是 4.1 版本的。Xcode 3 的 Target 設定項位置稍有不同。
配置就這麼完成了,使用時只需要 #import "CorePlot-CocoaTouch.h",下面來體驗一個最簡單的例子,下載的 CorePlot 包中雖然有一些例子,但還是需要一個能讓人好理解並獲得最快速體驗的。比如像這中這麼一個最簡單的曲線圖,最基本的代碼要素應該有哪些呢?
主要代碼就是下面那樣:
| 0102030405060708091011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
//// Created by Unmi Qiu on 8/11/11.// Copyright 2011 . All rights reserved.// #import <UIKit/UIKit.h>#import "CorePlot-CocoaTouch.h" @interface TestCorePlotViewController : UIViewController<CPTPlotDataSource> { NSMutableArray *dataArray;}@end @implementation TestCorePlotViewController #pragma mark - View lifecycle - (void) viewDidAppear:(BOOL)animated { //初始化數組,並放入十個 0 - 20 間的隨機數 dataArray = [[NSMutableArray alloc] init]; for(int i=0; i< 10; i++){ [dataArray addObject:[NSNumber numberWithInt:rand()%20]]; } CGRect frame = CGRectMake(10,10, 300,100); //圖形要放在一個 CPTGraphHostingView 中,CPTGraphHostingView 繼承自 UIView CPTGraphHostingView *hostView = [[CPTGraphHostingView alloc] initWithFrame:frame]; //把 CPTGraphHostingView 加到你自己的 View 中 [self.view addSubview:hostView]; hostView.backgroundColor = [UIColor blueColor]; //在 CPTGraph 中畫圖,這裡的 CPTXYGraph 是個曲線圖 //要指定 CPTGraphHostingView 的 hostedGraph 屬性來關聯 CPTXYGraph *graph = [[CPTXYGraph alloc] initWithFrame:hostView.frame]; hostView.hostedGraph = graph; CPTScatterPlot *scatterPlot = [[CPTScatterPlot alloc] initWithFrame:graph.bounds]; [graph addPlot:scatterPlot]; scatterPlot.dataSource = self; //設定資料來源,需應用 CPTPlotDataSource 協議 //設定 PlotSpace,這裡的 xRange 和 yRange 要理解好,它決定了點是否落在圖形的可見地區 //location 值表示座標起始值,一般可以設定元素中的最小值 //length 值表示從起始值上浮多少,一般可以用最大值減去最小值的結果 //其實我倒覺得,CPTPlotRange:(NSRange) range 好理解些,可以表示值從 0 到 20 CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) scatterPlot.plotSpace; plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat([dataArray count]-1)]; plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(20)]; //下面省去了座標與線型及其他圖形風格的代碼 [plotSpace release]; [graph release]; [hostView release];} //詢問有多少個資料,在 CPTPlotDataSource 中聲明的- (NSUInteger) numberOfRecordsForPlot:(CPTPlot *)plot { return [dataArray count];} //詢問一個個資料值,在 CPTPlotDataSource 中聲明的- (NSNumber *) numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index { if(fieldEnum == CPTScatterPlotFieldY){ //詢問 Y 值時 return [dataArray objectAtIndex:index]; }else{ //詢問 X 值時 return [NSNumber numberWithInt:index]; }} - (void) dealloc { [dataArray release]; [super dealloc];} @end |
關於更詳細的 Core Plot 使用,下面還會繼續作介紹的。
參考:1. http://www.e-string.com/content/simple-graph-using-core-plot
2. http://www.e-string.com/content/simple-bar-chart-core-plot
3. http://www.jaysonjc.com/programming/pie-chart-drawing-in-iphone-using-core-plot-library.html
Test applicationMac
iPhone
iPad
DropPlot (Mac)
AAPLot (iPhone)
Function plotting
This is drawn from the following tutorial: "Using Core Plot in an iPhone Application" (Switch On The Code)