HTML5 Canvas——折线图

<script type="text/javascript">
/*1.绘制网格 网格大小 10px 网格的颜色 #ddd */ /*2.绘制坐标 轴的离边距离 20px 箭头的尺寸 10px */
/*3.绘制点 点尺寸 6px */
/*4.连接点 */
/*5.准备数据*/
var data = [
{ x: 50, y: 90 },
{ x: 150, y: 150 },
{ x: 250, y: 250 },
{ x: 350, y: 130 },
{ x: 450, y: 80 }
] /*使用面向对象的方式实现*/
var LineChart = function (ctx) {
//绘制工具
this.ctx = ctx || document.querySelector('canvas').getContext('2d');
//网格大小
this.gridSize = 10;
//网格线的颜色
this.gridColor = '#fff';
//轴的离边距离
this.space = 20;
//箭头的尺寸
this.arrowSize = 10;
//点尺寸
this.pointSize = 6; //画布的高度
this.canvasHeight = this.ctx.canvas.height;
//画布的宽度
this.canvasWidth = this.ctx.canvas.width; //入口函数
//this.init();
}
//入口函数
LineChart.prototype.init = function (data) {
this.drawGrid();
this.drawXY();
this.drawPointList(data);
this.joinPoint();
};
//绘制网格
LineChart.prototype.drawGrid = function () {
/*业务*/
/*1. 绘制X轴方向的线*/
var xCount = Math.floor(this.canvasHeight / this.gridSize);
for (var i = 0; i < xCount; i++) {
this.ctx.beginPath();
this.ctx.moveTo(0, i * this.gridSize - 0.5);
this.ctx.lineTo(this.canvasWidth, i * this.gridSize - 0.5);
this.ctx.strokeStyle = this.gridColor;
this.ctx.stroke();
} /*2. 绘制Y轴方向的线*/
var yCount = Math.floor(this.canvasWidth / this.gridSize);
for (var i = 0; i < yCount; i++) {
this.ctx.beginPath();
this.ctx.moveTo(i * this.gridSize - 0.5, 0);
this.ctx.lineTo(i * this.gridSize - 0.5, this.canvasHeight);
this.ctx.strokeStyle = this.gridColor;
this.ctx.stroke();
}
}
//绘制原点和坐标轴
LineChart.prototype.drawXY = function () {
/*1.坐标原点*/
this.x0 = this.space;
this.y0 = this.canvasHeight - this.space;
/*2.绘制X轴*/
this.ctx.beginPath();
this.ctx.moveTo(this.x0, this.y0);
this.ctx.lineTo(this.canvasWidth - this.space, this.y0);
this.ctx.lineTo(this.canvasWidth - this.space - this.arrowSize, this.y0 + this.arrowSize / 2);
this.ctx.lineTo(this.canvasWidth - this.space - this.arrowSize, this.y0 - this.arrowSize / 2);
this.ctx.lineTo(this.canvasWidth - this.space, this.y0);
this.ctx.strokeStyle = '#000';
this.ctx.stroke();
this.ctx.fill();
/*3.绘制Y轴*/
this.ctx.beginPath();
this.ctx.moveTo(this.x0, this.y0);
this.ctx.lineTo(this.space, this.space);
this.ctx.lineTo(this.space + this.arrowSize / 2, this.space + this.arrowSize);
this.ctx.lineTo(this.space - this.arrowSize / 2, this.space + this.arrowSize);
this.ctx.lineTo(this.space, this.space);
this.ctx.stroke();
this.ctx.fill();
}
//绘制所有点
LineChart.prototype.drawPointList = function (data) {
//测试
//this.drawPoint(200,200);
var _this = this;
//原来的点
var oldPoint = {
x: this.x0,
y: this.y0
};
data.forEach(function (item, i) {
/*绘制小正方形 就是点*/
/*在绘制之前转成cnavas坐标*/
var x = _this.x0 + item.x;
var y = _this.y0 - item.y;
_this.drawPoint(x, y);
_this.joinPoint(oldPoint, {
x: x,
y: y
}); //连线完毕之后去记录 把这一次的点当做下一次连线的起始点
oldPoint = {
x: x,
y: y
}
});
}
LineChart.prototype.drawPoint = function (x, y) {
this.ctx.moveTo(x - this.pointSize / 2, y - this.pointSize / 2);
this.ctx.lineTo(x + this.pointSize / 2, y - this.pointSize / 2);
this.ctx.lineTo(x + this.pointSize / 2, y + this.pointSize / 2);
this.ctx.lineTo(x - this.pointSize / 2, y + this.pointSize / 2);
this.ctx.closePath();
this.ctx.fill();
};
//连接点
LineChart.prototype.joinPoint = function (oldPoint, currPoint) {
/*上一个点的坐标和现在的坐标相连*/
//oldPoint {x,y}
//currPoint {x,y}
this.ctx.beginPath();
this.ctx.moveTo(oldPoint.x, oldPoint.y);
this.ctx.lineTo(currPoint.x, currPoint.y);
this.ctx.stroke();
};
//使用
var lineChart = new LineChart();
lineChart.init(data);
</script>
HTML5 Canvas——折线图的更多相关文章
- Aristochart – 灵活的 HTML5 Canvas 折线图
Aristochart 是基于 HTML5 Canvas 的折线图功能库,具有高定制性和灵活性的特点.Aristochart 会帮助你处理图形显示,让你能够专注于业务逻辑处理. 您可能感兴趣的相关文章 ...
- html5绘制折线图
html5绘制折线图详细代码 <html> <canvas id="a_canvas" width="1000" height="7 ...
- 8个经典炫酷的HTML5 Canvas动画欣赏
HTML5非常强大,尤其是Canvas技术的应用,让HTML5几乎可以完成所有Flash能完成的效果.本文精选了8个经典炫酷的HTML5 Canvas动画欣赏,每一个都提供全部的源代码,希望对你有所帮 ...
- 超酷HTML5 Canvas图表应用Chart.js自定义提示折线图
超酷HTML5 Canvas图表应用Chart.js自定义提示折线图 效果预览 实例代码 <div class="htmleaf-container"> <div ...
- canvas图表详解系列(2):折线图
本章建议学习时间4小时 学习方式:详细阅读,并手动实现相关代码(如果没有canvas基础,需要先学习前面的canvas基础笔记) 学习目标:此教程将教会大家如何使用canvas绘制各种图表,详细分解步 ...
- HTML5 Canvas 绘制库存变化折线 画入库出库柱状图
代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...
- 用canvas绘制折线图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- canvas绘制经典折线图(一)
最终效果图如下: 实现步骤如下:注-引用了jQuery HTML代码 <!doctype html> <html lang="en"> <head&g ...
- [canvas]利用canvas绘制自适应的折线图
前段时间学习了用canvas绘制折现图,且当画布变换大小,折现图会随之变化,现附上代码 <!DOCTYPE html> <html lang="en"> & ...
随机推荐
- How To Configure NFS Client on CentOS 8 / RHEL 8
https://computingforgeeks.com/configure-nfs-client-on-centos-rhel/
- 实训23 功能FC的建立与调用
第4章:实训23 功能的生成与条用 功能简称FC 是用户编写的没有自己存储区的逻辑块 . 功能主要用来执行条用一次就可以完成的操作. 类似于C语言中的 函数 步骤一 单击确定 以后 出现了 在下面图框 ...
- P1048 数字加密
P1048 数字加密 转跳点:
- HDU - 6152 Friend-Graph(暴力)
题意:给定n个人的关系,若存在三个及以上的人两两友好或两两不友好,则"Bad Team!",否则"Great Team!". 分析:3000*3000内存100 ...
- OPENCV2.46 与2.4.10以上版本的区别
本人系统环境: CPU:Intel(R)Core(TM) i3-4160 CPU @ 3.60GHz 内存:4G 接两路摄像机,一路海康,一路艾普视达.CPU占有率95%.发现opencv2.4.10 ...
- linux下安装jdk&&Tomcat环境
linux系统 Centos6 下部署应用服务 jdk-1.7 环境安装:(切换到root用户下操作)1. 在 /usr/local 目录下创建jdk7文件 mkdir /usr/local/jdk7 ...
- 简单总结Get与Post的区别
工作当中经常遇到这两种类型的接口,也会被问到这两种类型的区别,这里简单总结一下算是一个简单的回忆吧. GET和POST是http协议的两种发送请求的方法.因为http的底层是TCP/IP,所以GET和 ...
- Docker部署Python应用程序
Docker部署Python应用程序 1. 单个py文件部署 生成Dockerfile 文件 插件用的豆瓣的镜像,,重置时间(容器的默认时间是UTC时间与宿主机的相差8小时). 文中需要三个插件(pe ...
- Android Studio模拟器无法访问网络
Android Studio3.5 模拟器无法访问网络的原因?
- Mysql安装维护笔记一
1.Centos7安装mysql rpm安装PHP7相应的yum源 $wget 'https://dev.mysql.com/get/mysql57-community-release-el7-11. ...