本文转载至

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 包中虽然有一些例子,但还是需要一个能让人好理解并获得最快速体验的。比如像这下图中这么一个最简单的曲线图,最基本的代码要素应该有哪些呢?

主要代码就是下面那样:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
//  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 application

Mac   效果图

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)

转载于:https://www.cnblogs.com/Camier-myNiuer/p/3820028.html

iOS 使用 Core Plot 绘制统计图表入门相关推荐

  1. iOS中使用Core Plot绘制统计图入门

    iOS(iPhone/iPad) 下图形组件有两个有名的,s7graphview 和 Core Plot,它们都是在 Google 上托管的代码,听说 Core Plot 比较强,因为前者仅支持曲线图 ...

  2. ios 图像坐标系_[译] iOS 开源图形库 Core Plot 使用教程

    注意 :本篇教程已被 Attila Hegedüs 更新,可适用于 iOS 9 和 Swift 2.2.原始教程出自教程组成员 Steve Baranski. 如果你曾经想在自己的 app 中引入图表 ...

  3. iOS 开源图形库 Core Plot 使用教程

    本文讲的是iOS 开源图形库 Core Plot 使用教程, 注意 :本篇教程已被 Attila Hegedüs 更新,可适用于 iOS 9 和 Swift 2.2.原始教程出自教程组成员 Steve ...

  4. iOS开发:Core Animation编程指南

    关于Core Animation Core Animation是iOS与OS X平台上负责图形渲染与动画的基础设施.Core Animation可以动画视图和其他的可视元素.Core Animatio ...

  5. iOS动画效果、绘制图形

    文章转载自:http://www.cnblogs.com/kenshincui/p/3972100.html 概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥 ...

  6. R语言Logistic回归模型亚组分析森林图(forest plot)绘制

    R语言Logistic回归模型亚组分析森林图(forest plot)绘制 目录 R语言Logistic回归模型亚组分析森林图(forest plot)R语言Logistic回归模型亚组分析森林图

  7. matplotlib.pyplot常用画图方式函数封装(一)——.plot绘制折线图及设置坐标轴箭头完美解决

    matplotlib.pyplot常用画图方式函数封装(一)--.plot绘制折线图及设置坐标轴箭头完美解决 py.plot常见绘图设置函数封装 绘制函数图像(完美解决坐标轴添加箭头) 绘制折线图 p ...

  8. IOS开发基础之绘制饼图、柱状图、自定义进度条

    IOS开发基础之绘制饼图.柱状图.自定义进度条 源码在我的主页里 1.绘制饼图 效果 源码 // LJView.m // 34-绘图饼图 // Created by 鲁军 on 2021/2/23. ...

  9. 《ASP.NET Core项目开发实战入门》送书活动结果公布

    截至2020.09.20 本次送书活动<ASP.NET Core项目开发实战入门>.下面把Top 5的留言截图给大家回顾一下. 以下5位同学将获赠书籍一本: 小林子 鉴 静 红脸先生 阿星 ...

最新文章

  1. SEO优化技巧之Alt标签的使用方法
  2. python新建excel表格-python xlwd对excel(表格)写入详解
  3. 没有找到borlandmm.dll 报错的解决方法
  4. 我所理解cocos2d-x 3.6 lua --使用Cocos Studio
  5. 【MySQL】MHA部署与MasterFailover代码分析
  6. POJ 3259 Wormholes【最短路/SPFA判断负环模板】
  7. 猜数字游戏的提示(UVa340)
  8. 【编程练习】正整数分解为几个连续自然数之和
  9. 简而言之Java.io:22个案例研究
  10. SecureCRT常用的使用技巧
  11. 弃用 Cookie!
  12. 数组元素的查找,添加,修改,删除
  13. linux部署java命令
  14. 手动计算均值,方差,协方差,皮尔逊系数
  15. 石墨烯 量子计算机,《Nature Nanotech.》又是石墨烯,这次对量子计算机下手了!...
  16. 谷歌浏览器打不开百度怎么办
  17. 16s扩增子 qiime2 实战
  18. python中的wx模块
  19. Oracle触发器转写成瀚高触发器
  20. 计算机二级相关快捷键,计算机二级考试Word+Excel必备快捷键!

热门文章

  1. Flutter抖动动画、颤抖动画、Flutter文字抖动效果
  2. flutter 一个用户登录页面
  3. 菜鸟——首个页面——奇葩问题
  4. MapReduce中的排序(附代码)
  5. 2017.5.16AM
  6. javascript的回调函数 同步 异步
  7. pydev导入eclipse
  8. web.py框架入门
  9. 4.Flow Layout Pane
  10. Python异常:IndentationError: unexpected unindent