background image

iOS

      

使用

 

    Core

 

    Plot

 

    

绘制统计图表入门

 

  

iOS(iPhone/iPad) 下图形组件有两个有名的,s7graphview 和 Core Plot,它们都是在 Google 
上托管的代码,听说

 Core Plot 比较强,因为前者仅支持曲线图,后者呢曲线图、饼图、柱状

图等通吃,且较活跃。那就专注下

 Core Plot 的使用。它提供了 Mac OS X 和 iOS 下的组件库,

我只用到它的

 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 包中虽然有一些例子,但还是需要一个能让人好理解并获得

最快速体验的。比如像这下图中这么一个最简单的曲线图,最基本的代码要素应该有哪些呢?

本文原始链接:

http://unmi.cc/ios-coreplot-chart-started, 来自:隔叶黄莺 Unmi Blog

主要代码就是下面那样:

//
//  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