IOS開發架構:Core Plot開源架構使用

來源:互聯網
上載者:User

IOS開發架構Core Plot開源架構使用是本文要介紹的內容,主要是來學習IOS開發架構的學習。iPhone下的圖形架構並不是很多。其中比較知名的就兩個s7graphview和Core Plot。巧的是兩個都是Google的。前者使用簡單,但功能單一,只能畫曲線圖。後者是開源項目,項目仍在不斷更新中,用起來可就複雜多了,而且各個版本會有差異,包括屬性、方法,甚至是類名都有改變。

關於Core Plot使用的中文網上資料,不是缺乏,而是根本沒有。唯一的一篇介紹得比較詳細的文章是“Using Core Plot in an iPhone Application”, 原文是英文的:http://www.switchonthecode.com/tutorials/using-core-plot-in-an-iphone-application 。但時間真的是太老了,居然是09年5月發表的,原文很多地方已經不再適用。因此我費了好大的勁,才把原文中的代碼跑通了,不敢獨享,與各位共用之。

一、下載安裝Core Plot架構

原文介紹的是“原始碼”版本。首先下載並安裝Mercurial很簡單,在http://www.selenic.com/mercurial/wiki/ 有標準Mac安裝包下載),再使用命令:

 
  1. hg clone http://core-plot.googlecode.com/hg/ core-plot 

即可把Core Plot項目原始碼下載到指定目錄core-plot下。

截至本文發表時止,Core Plot已經提供了Mac標準安裝器 CorePlotInstaller_0.2.2.zip   , 你可以直接用安裝器安裝,可以很方便地把Core Plot直接以SDK的方式安裝到本機。SDK的使用方法見後一篇博文《Core Plot SDK的用法》。

二、如何在項目中使用Core Plot

由於iOS 的限制,Core Plot以靜態庫的形式連結到iPhone應用。在core-plot/framework目錄下存在CorePlot- CocoaTouch.xcodeproj檔案,這就是一個靜態庫項目。關於靜態庫的使用,前一篇博文《封裝自己的控制項陳列庫:iPhone靜態庫的應用》已 經有介紹,使用方法都是一樣的。

1、建立Windows-base Application項目。

2、使用Add->Existing Files…,把CorePlot-CocoaTouch.xcodeproj添加到新項目中。

3、把libCorePlot-CocoaTouch.a最右邊的“add to target”小框勾上。

4、選擇Target “info->General”,添加對項目CorePlot-CocoaTouch的依賴引用)。

5、選擇新項目的“info->Build”,在“Header Search Paths”中添加Core Plot標頭檔搜尋路徑,如: /Users/kmyhy/core-plot/framework。注意要選中“Recursive”小勾英文原文中沒有提這一點)。同時,在Other Linker Flags中要增加兩個選項:-ObjC和-all_load英文原文中遺漏了第2個選項)。

6、建立一個ViewController,如TestViewController。在本例中,我們勾選了“With Xib”選項。在英文原文中,要求在Interface Build中把xib的View對象由UIView改為CPLayerHostingView其實應當是CPGraphHostingView)。但在這裡,其實沒有必要,只需在原始碼中修改就可以了。

7、.h檔案:

 
  1. #import <UIKit/UIKit.h> 
  2. #import "CorePlot-CocoaTouch.h"  
  3. @interface TestViewController : UIViewController <CPPlotDataSource>{  
  4. CPXYGraph * graph ;  
  5. }  
  6. @end 

8、.m檔案:

 
  1. #import "TestViewController.h"  
  2. @implementation TestViewController  
  3. -( NSUInteger )numberOfRecordsForPlot:( CPPlot *)plot {  
  4. return 51 ;  
  5. }  
  6. -( NSNumber *)numberForPlot:( CPPlot *)plot field:( NSUInteger )fieldEnum recordIndex:( NSUInteger )index {  
  7. double val = (index/ 5.0 )- 5 ;  
  8. if (fieldEnum == CPScatterPlotFieldX )  
  9. { return [ NSNumber numberWithDouble :val]; }  
  10. else  
  11. {  
  12. if (plot. identifier == @"X Squared Plot" )  
  13. { return [ NSNumber numberWithDouble :val*val]; }  
  14. else  
  15. { return [ NSNumber numberWithDouble : 1 /val]; }  
  16. }  
  17. }  
  18.  
  19. - ( void )viewDidLoad {  
  20.     //[super viewDidLoad];  
  21. graph = [[ CPXYGraph alloc ] initWithFrame : self . view . bounds ];  
  22.  
  23. // 原來的 CPLayerHostingView 由 CPGraphHostingView 所代替  
  24. self . view = [[ CPGraphHostingView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ];  
  25.  
  26. CPGraphHostingView *hostingView = ( CPGraphHostingView *) self . view ;  
  27. hostingView. hostedGraph = graph ;  
  28. graph . paddingLeft = 20.0 ;  
  29. graph . paddingTop = 20.0 ;  
  30. graph . paddingRight = 20.0 ;  
  31. graph . paddingBottom = 20.0 ;  
  32.  
  33. CPXYPlotSpace *plotSpace = ( CPXYPlotSpace *) graph . defaultPlotSpace ;  
  34. plotSpace. xRange = [ CPPlotRange plotRangeWithLocation : CPDecimalFromFloat (- 6 )  
  35.    length : CPDecimalFromFloat ( 12 )];  
  36. plotSpace. yRange = [ CPPlotRange plotRangeWithLocation : CPDecimalFromFloat (- 5 )  
  37.    length : CPDecimalFromFloat ( 30 )];  
  38. CPLineStyle *lineStyle = [ CPLineStyle lineStyle ];  
  39.  
  40. //CPLineStyle 的 lineColor 和 lineWidth 已經變為唯讀屬性  
  41. // lineStyle.lineColor = [CPColor blackColor];  
  42. // lineStyle.lineWidth = 2.0f;  
  43.  
  44. CPXYAxisSet *axisSet = ( CPXYAxisSet *) graph . axisSet ;  
  45.  
  46. //majorIntervalLength 的類型由 NSDecimalNumber 改變為 NSDecimal  
  47. axisSet. xAxis . majorIntervalLength = [[ NSDecimalNumber decimalNumberWithString : @"5" ] decimalValue ];  
  48. axisSet. xAxis . minorTicksPerInterval = 4 ;  
  49. axisSet. xAxis . majorTickLineStyle = lineStyle;  
  50. axisSet. xAxis . minorTickLineStyle = lineStyle;  
  51. axisSet. xAxis . axisLineStyle = lineStyle;  
  52. axisSet. xAxis . minorTickLength = 5.0f ;  
  53. axisSet. xAxis . majorTickLength = 7.0f ;  
  54.  
  55. //axisLableOffset 屬性由 labelOffset 所代替  
  56. axisSet. xAxis . labelOffset = 3.0f ;  
  57. //      axisSet.xAxis.axisLabelOffset = 3.0f;  
  58.  
  59. axisSet. yAxis . majorIntervalLength = [[ NSDecimalNumber decimalNumberWithString : @"5" ] decimalValue ];  
  60. axisSet. yAxis . minorTicksPerInterval = 4 ;  
  61. axisSet. yAxis . majorTickLineStyle = lineStyle;  
  62. axisSet. yAxis . minorTickLineStyle = lineStyle;  
  63. axisSet. yAxis . axisLineStyle = lineStyle;  
  64. axisSet. yAxis . minorTickLength = 5.0f ;  
  65. axisSet. yAxis . majorTickLength = 7.0f ;  
  66.  
  67. //axisLableOffset 屬性由 labelOffset 所代替  
  68. axisSet. yAxis . labelOffset = 3.0f ;  
  69. //      axisSet.yAxis.axisLabelOffset = 3.0f;  
  70.  
  71. //CPPlotSpace 的 bounds 屬性不再有效  
  72. CPScatterPlot *xSquaredPlot = [[[ CPScatterPlot alloc ]  
  73.    initWithFrame : self . view . bounds ] autorelease ];  
  74. //initWithFrame:graph.defaultPlotSpace.bounds] autorelease];  
  75. xSquaredPlot. identifier = @"X Squared Plot" ;  
  76.  
  77. //CPLineStyle 的 lineColor 和 lineWidth 已經變為唯讀屬性  
  78. // xSquaredPlot.dataLineStyle.lineWidth = 1.0f;  
  79. // xSquaredPlot.dataLineStyle.lineColor = [CPColor redColor];  
  80. xSquaredPlot. dataSource = self ;  
  81. [ graph addPlot :xSquaredPlot];  
  82.  
  83. CPPlotSymbol *greenCirclePlotSymbol = [ CPPlotSymbol ellipsePlotSymbol ];  
  84. greenCirclePlotSymbol. fill = [ CPFill fillWithColor :[ CPColor greenColor ]];  
  85. greenCirclePlotSymbol. size = CGSizeMake ( 2.0 , 2.0 );  
  86. xSquaredPlot. plotSymbol = greenCirclePlotSymbol;   
  87.  
  88. //CPPlotSpace 的 bounds 屬性不再有效  
  89. CPScatterPlot *xInversePlot = [[[ CPScatterPlot alloc ]  
  90.    initWithFrame : self . view . bounds ] autorelease ];  
  91. //initWithFrame:graph.defaultPlotSpace.bounds] autorelease];  
  92. xInversePlot. identifier = @"X Inverse Plot" ;  
  93. //CPLineStyle 的 lineColor 和 lineWidth 已經變為唯讀屬性  
  94. // xInversePlot.dataLineStyle.lineWidth = 1.0f;  
  95. // xInversePlot.dataLineStyle.lineColor = [CPColor blueColor];  
  96. xInversePlot. dataSource = self ;  
  97. [ graph addPlot :xInversePlot];  
  98. }    
  99. - ( void )dealloc {  
  100.     [ super dealloc ];  
  101. }   
  102. @end 

仔細查看代碼,你會發現原文中的代碼被我做了一些修改和調整。

附一張Core Plot架構的類層次圖,便於理解代碼中各個對象的使用:

注意,右邊各個類的顏色和左邊各個層次的顏色是對應的,:

小結:IOS開發架構Core Plot開源架構使用的內容介紹完了,希望通過本文的學習能對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.