将折现表封装到一个view里,暴露给使用者的只有一个传入数据的方法。

//
// ChartLine.h
// BoxingChampion
//功能:根据传入的数组,绘制折线图 注意 其frame的宽 不能小于20,高不能小于50
// Created by on 14-12-29.
// Copyright (c) 2014年 . All rights reserved.
// #import <UIKit/UIKit.h> @interface ChartLine : UIView /*
功能:绘制折线图
参数:折线图的数据,如@[@1,@2]
返回值:无
*/
-(void)setData:(NSArray *)array_data; @end

在.m文件中,重写-(id)initWithFrame:(CGRect)frame
方法。

一个折线表,我们可以将它分为三个图层来表示:1、坐标轴图层。2、背景色图层。3、数据折线图层。其中坐标图层应设定贝塞尔曲线,根据路径绘制坐标轴。

        // 添加坐标轴曲线路径
UIBezierPath *path_coordinate=[[UIBezierPath alloc] init];
[path_coordinate moveToPoint:CGPointMake(8, 12)]; //带有箭头状的坐标轴
[path_coordinate addLineToPoint:CGPointMake(10, 10)];
[path_coordinate addLineToPoint:CGPointMake(12, 12)];
[path_coordinate addLineToPoint:CGPointMake(10, 10)];
[path_coordinate addLineToPoint:CGPointMake(10, self.frame.size.height-10)];
[path_coordinate addLineToPoint:CGPointMake(self.frame.size.width-10, self.frame.size.height-10)];
[path_coordinate addLineToPoint:CGPointMake(self.frame.size.width-12, self.frame.size.height-12)];
[path_coordinate addLineToPoint:CGPointMake(self.frame.size.width-10, self.frame.size.height-10)];
[path_coordinate addLineToPoint:CGPointMake(self.frame.size.width-12, self.frame.size.height-8)]; //坐标轴路径图层
CAShapeLayer *layer_coordinate=[CAShapeLayer layer];
layer_coordinate.frame=
CGRectMake(0, 0, self.layer.frame.size.width, self.layer.frame.size.height);
layer_coordinate.lineCap=kCALineCapButt;
layer_coordinate.lineWidth=2;
layer_coordinate.fillColor=[[UIColor clearColor] CGColor];
layer_coordinate.strokeColor=[[UIColor orangeColor] CGColor];
layer_coordinate.strokeEnd=1;
layer_coordinate.path=[path_coordinate CGPath];

背景色图层应有渐变色效果,且背景色都在坐标轴所包围的象限内

 //淡色背景图层
CAGradientLayer *layer_background=[CAGradientLayer layer];
layer_background.frame=CGRectMake(11, 13, self.layer.frame.size.width-22,self.layer.frame.size.height-24);//保证在第一象限
[layer_background setColors:[NSArray arrayWithObjects:
(id)[[UIColor whiteColor] CGColor],
[[UIColor colorWithRed:1.000 green:0.827 blue:0.000 alpha:1.000] CGColor] ,nil]];
layer_background.locations=@[@0.1];//从0.1处开始渐变 location 坐标是左上角(0,0),在右下角(1,1) 使用时应注意
layer_background.startPoint=CGPointMake(0,0);
layer_background.endPoint=CGPointMake(0, 1);

将两个图层添加到视图图层上

      //添加图层
[self.layer addSublayer:layer_coordinate];
[self.layer addSublayer:layer_background];

最后当用户调用setData:方法时,绘制折线图

-(void)setData:(NSArray *)array_data
{
//获取输入元素的个数
NSInteger i_dataCount=[array_data count]; //取出元素中得最大值和最小值
CGFloat f_maxData=[[array_data objectAtIndex:0] floatValue];
CGFloat f_minData=[[array_data objectAtIndex:0]floatValue];
for (NSInteger i=0;i<i_dataCount; i++)
{
if (f_maxData<[[array_data objectAtIndex:i] floatValue])
{
f_maxData=[[array_data objectAtIndex:i] floatValue];
}
if (f_minData>[[array_data objectAtIndex:i] floatValue])
{
f_minData=[[array_data objectAtIndex:i] floatValue];
}
} //计算每个数据之间横坐标的间隔
CGFloat x_perDataPointInterval=(self.layer.frame.size.width-20)/(i_dataCount+1);
if (0==(f_maxData-f_minData))//防止分母为零
{
NSLog(@"Y轴逻辑最大值和逻辑最小值相同");
return;
} //计算每个数据1 占据几个像素点
CGFloat y_perPointInterval=(self.layer.frame.size.height-50)/(f_maxData-f_minData); //折线路径
UIBezierPath *path_data=[[UIBezierPath alloc] init]; //第一个元素起始点的坐标
[path_data moveToPoint:CGPointMake(10+x_perDataPointInterval,
self.layer.frame.size.height-(20+([[array_data objectAtIndex:0] floatValue]-f_minData)*y_perPointInterval))]; for (NSInteger i=0; i<i_dataCount-1; i++)
{
//每个元素在视图中的坐标
[path_data addLineToPoint:CGPointMake(10+x_perDataPointInterval*(i+2),
self.layer.frame.size.height-(20+([[array_data objectAtIndex:(i+1)] floatValue]-f_minData)*y_perPointInterval))];
} //折线图层
CAShapeLayer *layer_data=[CAShapeLayer layer];
layer_data.frame=CGRectMake(0, 0, self.layer.frame.size.width, self.layer.frame.size.height);
layer_data.lineCap=kCALineCapRound;
layer_data.lineWidth=1;
layer_data.fillColor=[[UIColor clearColor] CGColor];
layer_data.strokeColor=[[UIColor redColor] CGColor];
layer_data.strokeEnd=1;
layer_data.path=[path_data CGPath]; [self.layer addSublayer:layer_data];
}

CoreAnimation实现一个折线表的更多相关文章

  1. 利用CoreAnimation实现一个时间的进度条

    (个人原创,转载请注明出处 http://www.cnblogs.com/pretty-guy/p/7460334.html) 在iOS中实现进度条通常都是通过不停的设置progress来完成的,这样 ...

  2. 一、CoreAnimation之图层树详解

    CoreAnimation :在字面意思为“核心动画”,但是如果您认为它仅仅是一个动画框架,那可能就要错过一些经典功能了.动画,只是CoreAnimation功能的一小部分,毕竟人家的源头是一个叫做L ...

  3. 手把手教你打造一个心电图效果View Android自定义View

    大家好,看我像不像蘑菇-因为我在学校呆的发霉了. 思而不学则殆 丽丽说得对,我有奇怪的疑问,大都是思而不学造成的,在我书读不够的情况下想太多,大多等于白想,所以革命没成功,同志仍需努力. 好了废话不说 ...

  4. 利用sklearn对MNIST手写数据集开始一个简单的二分类判别器项目(在这个过程中学习关于模型性能的评价指标,如accuracy,precision,recall,混淆矩阵)

    .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...

  5. jq 折面板+tab切换(自己封装的插件哦!!)

    如上图所示的一个折面板效果+tab切换:最重要的js代码如下: 对于布局简单介绍下: hot_wrap_li 这个是带箭头的横条: Arrow 这个是箭头的div:hot_wrap_li_wrap 这 ...

  6. 移动端图表插件jChart.js的修改

    bug1: 折线图,设置datasetGesture : true时,Y轴的刻度值居然会变.会变也就算了,居然没地方设置不能变. bug2: 折线图,设置tap.point事件,和datasetGes ...

  7. 练习: bs4 简单爬取 + matplotlib 折线图显示 (关键词,职位数量、起薪)

    要看一种技术在本地的流行程度,最简单的就是找招聘网站按关键词搜索. 比如今天查到的职位数量是vue 1296个,react 1204个,angular 721个.国际上比较流行的是react,本地市场 ...

  8. ElementUI 不维护了?供我们选择的 Vue 组件库还有很多!

    前文回顾:Vue+Spring Boot 前后端分离的商城项目开源啦! Vue 组件千千万,只要不行咱就换. ElementUI 近况 根据我最近的观察,得知一些关于 ElementUI 维护人员都退 ...

  9. http程序接口、调用(最入门级,文末附Demo)

    HTTP协议简介 既然是基于HTTP协议开发,那么就首先要了解下HTTP协议的相关内容- 在TCP/IP体系结构中,HTTP属于应用层协议,位于TCP/IP协议的顶层.浏览Web时,浏览器通过HTTP ...

随机推荐

  1. HDU-2551 竹青遍野

    http://acm.hdu.edu.cn/showproblem.php?pid=2551 妙用for循环. 竹青遍野 Time Limit: 2000/1000 MS (Java/Others)  ...

  2. Chrome 控制台实用指南【转】

    转自伯乐在线. Chrome 控制台实用指南 前言 Chrome浏览器我想是每一个前端er必用工具之一吧,一部分原因是它速度快,体积不大,支持的新特性也比其它浏览器多,还有一部分我想就是因为它的控制台 ...

  3. How do I use a host name to look up an IP address?

    The InetAddress class can be used to perform Domain Name Server (DNS) lookups. For example, you can ...

  4. 我的ubuntu配置

    每次装系统都是非常蛋疼的过程,新装的系统还是要配置一下的 首先安装google拼音 sudo apt-get install fcitx fcitx-googlepinyin 然后按装numix主题 ...

  5. mac下教你如何开源项目托管GitHub

    自从google code关闭了下载服务了之后,GitHub作为了目前最好用的免费开源项目托管站点,众多开源项目都托管在github,其中不乏著名的播放器MPC-HC. 这里教大家如何把代码库上传到G ...

  6. 在XMPP的JAVA开源实现Openfire中,增加LBS 附近的人功能

    1. XMPP协议 与 Openfire XMPP协议是IM领域的标准协议了,具体可参考  http://xmpp.org   及RFC6120,RFC6121,RFC6122等相关文档. http: ...

  7. [React] React Fundamentals: Build a JSX Live Compiler

    we want to have the ability to write JSX and see the output live in the browser. <!doctype html&g ...

  8. xcode6下使用autolayout+sizeclass实践

    历史车轮滚滚向前,将autolayout配合sizeclass做布局的方式推上了主流,虽然有点晚,但最终还是进行了一次完整的实践,特此记录一下: 因为网上已经有很多博客介绍了autolayout配合s ...

  9. android86 监听SD卡状态,勒索软件,监听应用的安装、卸载、更新,无序广播有序广播

    * 添加权限 <uses-permission android:name="android.permission.RECEIVE_SMS"/> * 4.0以后广播接收者 ...

  10. android80 HttpClient框架提交数据 get方式

    package com.itheima.httpclient; import java.io.IOException; import java.io.InputStream; import java. ...