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 相关文章推荐
广告显示判断
Aug 31 Javascript
Javascript表格翻页效果实现思路及代码
Aug 23 Javascript
Jquery跳到页面指定位置的方法
May 12 Javascript
jQuery窗口、文档、网页各种高度的精确理解
Jul 02 Javascript
jquery判断复选框是否被选中的方法
Oct 16 Javascript
基于Bootstrap仿淘宝分页控件实现代码
Nov 07 Javascript
Jquery 整理元素选取、常用方法一览表
Nov 26 Javascript
jquery实现文本框的禁用和启用
Dec 07 Javascript
Bootstrap栅格系统的使用和理解2
Dec 14 Javascript
解决vue-cli单页面手机应用input点击手机端虚拟键盘弹出盖住input问题
Aug 25 Javascript
详解关于vue2.0工程发布上线操作步骤
Sep 27 Javascript
从0到1搭建Element的后台框架的方法步骤
Apr 10 Javascript
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&amp;mysql(六)
2006/10/09 PHP
php 之 没有mysql支持时的替代方案
2006/10/09 PHP
Discuz Uchome ajaxpost小技巧
2011/01/04 PHP
Laravel 5框架学习之日期,Mutator 和 Scope
2015/04/08 PHP
yii框架redis结合php实现秒杀效果(实例代码)
2017/10/26 PHP
看了就知道什么是JSON
2007/12/09 Javascript
Jquery Change与bind事件代码
2011/09/29 Javascript
给文字加上着重号的JS代码
2013/11/12 Javascript
关闭页面window.location事件未执行的原因及解决方法
2014/09/01 Javascript
页面加载完后自动执行一个方法的js代码
2014/09/06 Javascript
如何编写高质量JS代码(续)
2015/02/25 Javascript
Underscore.js 1.3.3 中文注释翻译说明
2015/06/25 Javascript
深入php面向对象、模式与实践
2016/02/16 Javascript
AngularJS ng-app 指令实例详解
2016/07/30 Javascript
js控制按钮,防止频繁点击响应的实例
2017/02/15 Javascript
Angular 2父子组件之间共享服务通信的实现
2017/07/04 Javascript
vue 添加vux的代码讲解
2017/11/30 Javascript
Vue+SpringBoot开发V部落博客管理平台
2017/12/27 Javascript
详解微信小程序实现仿微信聊天界面(各种细节处理)
2019/02/17 Javascript
Layui 数据表格批量删除和多条件搜索的实例
2019/09/04 Javascript
Node.js API详解之 string_decoder用法实例分析
2020/04/29 Javascript
js+canvas实现五子棋小游戏
2020/08/02 Javascript
使用JavaScript实现贪吃蛇游戏
2020/09/29 Javascript
[56:24]DOTA2上海特级锦标赛主赛事日 - 3 胜者组第二轮#1Liquid VS MVP.Phx第二局
2016/03/04 DOTA
Python中的map()函数和reduce()函数的用法
2015/04/27 Python
Python中字典映射类型的学习教程
2015/08/20 Python
matplotlib中legend位置调整解析
2017/12/19 Python
python 以16进制打印输出的方法
2018/07/09 Python
python能开发游戏吗
2020/06/11 Python
Python基于Twilio及腾讯云实现国际国内短信接口
2020/06/18 Python
银行开业庆典方案
2014/02/06 职场文书
读书伴我成长演讲稿
2014/05/07 职场文书
第28个世界无烟日活动总结
2015/02/10 职场文书
基石观后感
2015/06/12 职场文书
MySQL 自定义变量的概念及特点
2021/05/13 MySQL
windows11怎么查看自己安装的版本号? win11版本号的查看方法
2021/11/21 数码科技