CoreAnimation实现一个折线表
将折现表封装到一个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实现一个折线表的更多相关文章
- 利用CoreAnimation实现一个时间的进度条
(个人原创,转载请注明出处 http://www.cnblogs.com/pretty-guy/p/7460334.html) 在iOS中实现进度条通常都是通过不停的设置progress来完成的,这样 ...
- 一、CoreAnimation之图层树详解
CoreAnimation :在字面意思为“核心动画”,但是如果您认为它仅仅是一个动画框架,那可能就要错过一些经典功能了.动画,只是CoreAnimation功能的一小部分,毕竟人家的源头是一个叫做L ...
- 手把手教你打造一个心电图效果View Android自定义View
大家好,看我像不像蘑菇-因为我在学校呆的发霉了. 思而不学则殆 丽丽说得对,我有奇怪的疑问,大都是思而不学造成的,在我书读不够的情况下想太多,大多等于白想,所以革命没成功,同志仍需努力. 好了废话不说 ...
- 利用sklearn对MNIST手写数据集开始一个简单的二分类判别器项目(在这个过程中学习关于模型性能的评价指标,如accuracy,precision,recall,混淆矩阵)
.caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...
- jq 折面板+tab切换(自己封装的插件哦!!)
如上图所示的一个折面板效果+tab切换:最重要的js代码如下: 对于布局简单介绍下: hot_wrap_li 这个是带箭头的横条: Arrow 这个是箭头的div:hot_wrap_li_wrap 这 ...
- 移动端图表插件jChart.js的修改
bug1: 折线图,设置datasetGesture : true时,Y轴的刻度值居然会变.会变也就算了,居然没地方设置不能变. bug2: 折线图,设置tap.point事件,和datasetGes ...
- 练习: bs4 简单爬取 + matplotlib 折线图显示 (关键词,职位数量、起薪)
要看一种技术在本地的流行程度,最简单的就是找招聘网站按关键词搜索. 比如今天查到的职位数量是vue 1296个,react 1204个,angular 721个.国际上比较流行的是react,本地市场 ...
- ElementUI 不维护了?供我们选择的 Vue 组件库还有很多!
前文回顾:Vue+Spring Boot 前后端分离的商城项目开源啦! Vue 组件千千万,只要不行咱就换. ElementUI 近况 根据我最近的观察,得知一些关于 ElementUI 维护人员都退 ...
- http程序接口、调用(最入门级,文末附Demo)
HTTP协议简介 既然是基于HTTP协议开发,那么就首先要了解下HTTP协议的相关内容- 在TCP/IP体系结构中,HTTP属于应用层协议,位于TCP/IP协议的顶层.浏览Web时,浏览器通过HTTP ...
随机推荐
- [转]NHibernate之旅(6):探索NHibernate中的事务
本节内容 事务概述 1.新建对象 [测试成功提交] [测试失败回滚] 2.删除对象 3.更新对象 4.保存更新对象 结语 上一篇我们介绍了NHibernate中的Insert, Update, Del ...
- 用户故事(User Story)
摘要: 一件用户通过系统完成他一个有价值的目标(买一罐饮料)的事.这样的过程就叫“用户案例(user case)”或者“用户故事(user story)”.本文描述了敏捷开发的技巧:如何以用户故事管理 ...
- Windows游戏编程之从零开始d
Windows游戏编程之从零开始d I'm back~~恩,几个月不见,大家还好吗? 这段时间真的好多童鞋在博客里留言说或者发邮件说浅墨你回来继续更新博客吧. woxiangnifrr童鞋说每天都在来 ...
- Ubuntu全新系统一些配置
0.安装JDK,http://www.oracle.com/technetwork/java/javase/downloads/index.html 1.安装Intellij IDEA,https:/ ...
- 关于ASSERT(断言)的作用
程序一般分为Debug 版本和Release 版本,Debug 版本用于内部调试,Release 版本发行给用户使用.断言assert 是仅在Debug 版本起作用的宏,它用于检查“不应该”发生的情况 ...
- 在终端中创建一个简单的mysql表格
打开终端后输入:/usr/local/MySQL/bin/mysql -u root –p 然后输入密码:***** 创建数据库:create database work; 使用当前数据库:use w ...
- PAT 1076. Forwards on Weibo (30)
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...
- Android学习(一)
#常见布局 ###线性布局 有一个布局方向,水平或者竖直 在竖直布局下,左对齐.右对齐,水平居中生效 在水平布局下,顶部对齐.底部对齐.竖直居中生效 权重:按比例分配屏幕的剩余宽度或者高度 ###相对 ...
- 【转】maven 项目出现 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
http://blessht.iteye.com/blog/1104450 http://www.cnblogs.com/zhouyalei/archive/2011/11/30/2268606.ht ...
- ContentProvider与ContentResolver使用
例如以下内容为从网络转载: 使用ContentProvider共享数据: 当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就能够向其它应用共享其数据.虽然使用其它方 ...