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"> & ...
随机推荐
- mysql 索引入门
创建索引的语法结构:
- lpwizard 生成的 allegro 封装中 .psx 文件使用方法。
lpwizard 有时候生成 allegro 封装的时候会生成 .psx 文件,这个文件其实是脚本文件,用于某些特殊形状焊盘的处理. 具体的使用方法如下: 在Allegro中,选择 File > ...
- ListView的DrawSubItem时间添加边框,字体变粗问题
procedure TFrmrdp.ListView1AdvancedCustomDrawSubItem(Sender: TCustomListView; Item: TListItem; SubIt ...
- J. Cola
J. Cola time limit per test 4.0 s memory limit per test 64 MB input standard input output standard o ...
- MQTT 协议学习:Retained(保留消息) 与 LWT(最后遗嘱)
背景导入 让我们来看一下这个场景: 你有一个温度传感器,它每三个小时向一个 Topic 发布当前的温度.那么问题来了,有一个新的订阅者在它刚刚发布了当前温度之后订阅了这个主题,那么这个订阅端什么时候能 ...
- 【转帖】影响超 10 亿设备,博通和 Cypress 芯片曝惊天漏洞,苹果、华为、三星等中招
影响超 10 亿设备,博通和 Cypress 芯片曝惊天漏洞,苹果.华为.三星等中招 https://www.infoq.cn/article/lpNEQGrxZL22gHDPBE2z 26 ...
- win10下pip3安装tesserocr时报错
使用pip3在线安装tesserocr时报错,刚开始报错内容是提示未安装vs2014,安装完以后报错内容如下 ERROR: Command errored out with exit status 1 ...
- jQuery原理系列-常用Dom操作
1. 事件绑定$(el).bind ie使用attachEvent,其它浏览器使用addEventListener,不同的是ie多了个on前缀,this绑定在window上,需要用call和apply ...
- 利用ThoughtWorks.QRCode生成二维码
一.项目添加ThoughtWorks.QRCode.dll和System.Drawing.dll的引用 二.创建二维码公共处理类(QRCodeHandler.cs) /// <summary&g ...
- express连接数据库 读取表
connection 连接数据库 connection.query 查询表 1.依赖 const mysql = require('mysql'); 连接数据库代码 var connecti ...