JavaScript canvas绘制折线图


Posted in Javascript onFebruary 18, 2020

本文实例为大家分享了canvas绘制折线图的具体代码,供大家参考,具体内容如下

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <style>
 canvas {
 border: 1px solid #ccc;
 }
 </style>
</head>
<body>
<canvas width="600" height="400"></canvas>
<script>
 /*1.构造函数*/
 var LineChart = function (ctx) {
 /*获取绘图工具*/
 this.ctx = ctx || document.querySelector('canvas').getContext('2d');
 /*画布的大小*/
 this.canvasWidth = this.ctx.canvas.width;
 this.canvasHeight = this.ctx.canvas.height;
 /*网格的大小*/
 this.gridSize = 10;
 /*坐标系的间距*/
 this.space = 20;
 /*坐标原点*/
 this.x0 = this.space;
 this.y0 = this.canvasHeight - this.space;
 /*箭头的大小*/
 this.arrowSize = 10;
 /*绘制点*/
 this.dottedSize = 6;
 /*点的坐标 和数据有关系 数据可视化*/
 }
 /*2.行为方法*/
 LineChart.prototype.init = function (data) {
 this.drawGrid();
 this.drawAxis();
 this.drawDotted(data);
 };
 /*绘制网格*/
 LineChart.prototype.drawGrid = function () {
 /*x方向的线*/
 var xLineTotal = Math.floor(this.canvasHeight / this.gridSize);
 this.ctx.strokeStyle = '#eee';
 for (var i = 0; i <= xLineTotal; 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.stroke();
 }
 /*y方向的线*/
 var yLineTotal = Math.floor(this.canvasWidth / this.gridSize);
 for (var i = 0; i <= yLineTotal; 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.stroke();
 }
 };
 /*绘制坐标系*/
 LineChart.prototype.drawAxis = function () {
 /*X轴*/
 this.ctx.beginPath();
 this.ctx.strokeStyle = '#000';
 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.stroke();
 this.ctx.fill();
 /*Y轴*/
 this.ctx.beginPath();
 this.ctx.strokeStyle = '#000';
 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.drawDotted = function (data) {
 /*1.数据的坐标 需要转换 canvas坐标*/
 /*2.再进行点的绘制*/
 /*3.把线连起来*/
 var that = this;
 /*记录当前坐标*/
 var prevCanvasX = 0;
 var prevCanvasY = 0;
 data.forEach(function (item, i) {
 /* x = 原点的坐标 + 数据的坐标 */
 /* y = 原点的坐标 - 数据的坐标 */
 var canvasX = that.x0 + item.x;
 var canvasY = that.y0 - item.y;
 /*绘制点*/
 that.ctx.beginPath();
 that.ctx.moveTo(canvasX - that.dottedSize / 2, canvasY - that.dottedSize / 2);
 that.ctx.lineTo(canvasX + that.dottedSize / 2, canvasY - that.dottedSize / 2);
 that.ctx.lineTo(canvasX + that.dottedSize / 2, canvasY + that.dottedSize / 2);
 that.ctx.lineTo(canvasX - that.dottedSize / 2, canvasY + that.dottedSize / 2);
 that.ctx.closePath();
 that.ctx.fill();
 /*点的连线*/
 /*当时第一个点的时候 起点是 x0 y0*/
 /*当时不是第一个点的时候 起点是 上一个点*/
 if(i == 0){
 that.ctx.beginPath();
 that.ctx.moveTo(that.x0,that.y0);
 that.ctx.lineTo(canvasX,canvasY);
 that.ctx.stroke();
 }else{
 /*上一个点*/
 that.ctx.beginPath();
 that.ctx.moveTo(prevCanvasX,prevCanvasY);
 that.ctx.lineTo(canvasX,canvasY);
 that.ctx.stroke();
 }
 /*记录当前的坐标,下一次要用*/
 prevCanvasX = canvasX;
 prevCanvasY = canvasY;
 });
 };
 /*3.初始化*/
 var data = [
 {
 x: 100,
 y: 120
 },
 {
 x: 200,
 y: 160
 },
 {
 x: 300,
 y: 240
 },
 {
 x: 400,
 y: 120
 },
 {
 x: 500,
 y: 80
 }
 ];
 var lineChart = new LineChart();
 lineChart.init(data);
</script>
</body>
</html>

运行结果如下:

JavaScript canvas绘制折线图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
避免 showModalDialog 弹出新窗体的原因分析
May 31 Javascript
在JavaScript中获取请求的URL参数[正则]
Dec 25 Javascript
返回对象在当前级别中是第几个元素的实现代码
Jan 20 Javascript
JS判断浏览器是否支持某一个CSS3属性的方法
Oct 17 Javascript
jQuery插件实现图片轮播特效
Jun 16 Javascript
详解基于Bootstrap+angular的一个豆瓣电影app
Jun 26 Javascript
详解用node.js实现简单的反向代理
Jun 26 Javascript
使用clipboard.js实现复制功能的示例代码
Oct 16 Javascript
详解webpack + react + react-router 如何实现懒加载
Nov 20 Javascript
微信小程序自定义键盘 内部虚拟支付
Dec 20 Javascript
微信小程序 scroll-view的使用案例代码详解
Jun 11 Javascript
vue.js Router中嵌套路由的实用示例
Jun 27 Vue.js
node+multer实现图片上传的示例代码
Feb 18 #Javascript
JavaScript canvas绘制圆弧与圆形
Feb 18 #Javascript
javascript中的with语句学习笔记及用法
Feb 17 #Javascript
JS实现百度搜索框关键字推荐
Feb 17 #Javascript
js实现百度淘宝搜索功能
Feb 17 #Javascript
JavaScript使用canvas绘制随机验证码
Feb 17 #Javascript
JavaScript中this的学习笔记及用法整理
Feb 17 #Javascript
You might like
PHP 命名空间实例说明
2011/01/27 PHP
一个PHP的QRcode类与大家分享
2011/11/13 PHP
Yii2配置Nginx伪静态的方法
2017/05/05 PHP
引用其它js时如何同时处理多个window.onload事件
2014/09/02 Javascript
JavaScript中的Number数字类型学习笔记
2016/05/26 Javascript
AngularJS实现数据列表的增加、删除和上移下移等功能实例
2016/09/05 Javascript
node.js学习之base64编码解码
2016/10/21 Javascript
AngularJS实现根据变量改变动态加载模板的方法
2016/11/04 Javascript
实例教学如何写vue插件
2017/11/30 Javascript
vue this.reload 方法 配置
2018/09/12 Javascript
Vue监听一个数组id是否与另一个数组id相同的方法
2018/09/26 Javascript
对angular 监控数据模型变化的事件方法$watch详解
2018/10/09 Javascript
基于Vue-cli快速搭建项目的完整步骤
2018/11/03 Javascript
node.js 基于cheerio的爬虫工具的实现(需要登录权限的爬虫工具)
2019/04/10 Javascript
Vue基本使用之对象提供的属性功能
2019/04/30 Javascript
JS实现数组删除指定元素功能示例
2019/06/05 Javascript
如何进行微信公众号开发的本地调试的方法
2019/06/16 Javascript
javascript 对象 与 prototype 原型用法实例分析
2019/11/11 Javascript
用Python的Django框架来制作一个RSS阅读器
2015/07/22 Python
Python使用pyh生成HTML文档的方法示例
2018/03/10 Python
selenium+python自动化测试之多窗口切换
2019/01/23 Python
Python终端输出彩色字符方法详解
2020/02/11 Python
美国最好的钓鱼、狩猎和划船装备商店:Bass Pro Shops
2018/12/02 全球购物
Deux par Deux官方网站:设计师童装
2020/01/03 全球购物
四川internet信息高速公路(C#)笔试题
2012/02/29 面试题
医学专业毕业生个人求职信
2013/12/25 职场文书
会计专业应届生自荐信
2014/02/07 职场文书
企业总经理岗位职责
2014/02/13 职场文书
淘宝店铺营销方案
2014/02/13 职场文书
政法干警核心价值观心得体会
2014/09/11 职场文书
民事调解书范文
2015/05/20 职场文书
学历证明样本
2015/06/16 职场文书
SQL实现LeetCode(177.第N高薪水)
2021/08/04 MySQL
浅谈sql_@SelectProvider及使用注意说明
2021/08/04 Java/Android
教你使用一行Python代码玩遍童年的小游戏
2021/08/23 Python
关于k8s环境部署mysql主从的问题
2022/03/13 MySQL