iOS中使用Core Plot绘制统计图入门
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?包中雖然有一些例子,但還是需要一個能讓人好理解并獲得最快速體驗的。比如像這下圖中這么一個最簡單的曲線圖,最基本的代碼要素應該有哪些呢?
主要代碼就是下面那樣:
// //? Created by Unmi Qiu on 8/11/11. //? Copyright 2011 . All rights reserved. // #import <UIKit/UIKit.h> #import "CorePlot-CocoaTouch.h" @interfaceTestCorePlotViewController : UIViewController<CPTPlotDataSource> { ????NSMutableArray*dataArray; } @end @implementationTestCorePlotViewController #pragma mark - View lifecycle - (void) viewDidAppear:(BOOL)animated { ????? ????//初始化數組,并放入十個 0 - 20 間的隨機數 ????dataArray = [[NSMutableArrayalloc] init]; ????for(inti=0; i< 10; i++){ ????????[dataArray addObject:[NSNumbernumberWithInt: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 = [UIColorblueColor]; ????? ????//在 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[NSNumbernumberWithInt:index]; ????} } - (void) dealloc { ????[dataArray release]; ????[superdealloc]; } @end
原文地址:http://unmi.cc/ios-coreplot-chart-started
總結
以上是生活随笔為你收集整理的iOS中使用Core Plot绘制统计图入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 离线下载,腾讯插入迅雷的利刃
- 下一篇: 花名册信息填写另一个exceL简历表格里