<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——折线图的更多相关文章

  1. Aristochart – 灵活的 HTML5 Canvas 折线图

    Aristochart 是基于 HTML5 Canvas 的折线图功能库,具有高定制性和灵活性的特点.Aristochart 会帮助你处理图形显示,让你能够专注于业务逻辑处理. 您可能感兴趣的相关文章 ...

  2. html5绘制折线图

    html5绘制折线图详细代码 <html> <canvas id="a_canvas" width="1000" height="7 ...

  3. 8个经典炫酷的HTML5 Canvas动画欣赏

    HTML5非常强大,尤其是Canvas技术的应用,让HTML5几乎可以完成所有Flash能完成的效果.本文精选了8个经典炫酷的HTML5 Canvas动画欣赏,每一个都提供全部的源代码,希望对你有所帮 ...

  4. 超酷HTML5 Canvas图表应用Chart.js自定义提示折线图

    超酷HTML5 Canvas图表应用Chart.js自定义提示折线图 效果预览 实例代码 <div class="htmleaf-container"> <div ...

  5. canvas图表详解系列(2):折线图

    本章建议学习时间4小时 学习方式:详细阅读,并手动实现相关代码(如果没有canvas基础,需要先学习前面的canvas基础笔记) 学习目标:此教程将教会大家如何使用canvas绘制各种图表,详细分解步 ...

  6. HTML5 Canvas 绘制库存变化折线 画入库出库柱状图

    代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...

  7. 用canvas绘制折线图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. canvas绘制经典折线图(一)

    最终效果图如下: 实现步骤如下:注-引用了jQuery HTML代码 <!doctype html> <html lang="en"> <head&g ...

  9. [canvas]利用canvas绘制自适应的折线图

    前段时间学习了用canvas绘制折现图,且当画布变换大小,折现图会随之变化,现附上代码 <!DOCTYPE html> <html lang="en"> & ...

随机推荐

  1. 【WPF学习】第二十二章 文本控件

    WPF提供了三个用于输入文本的控件:TextBox.RichTextBox和PasswordBox.PasswordBox控件直接继承自Control类.TextBox和RichTextBox控件间接 ...

  2. 基于AQS自己实现一个同步器

    前面说了这个多,我们可以自己尝试实现一个同步器,我们可以简单的参考一下ReentrantLock这个类的实现方式,我们就简单的实现一个不可重入的独占锁吧! 一.简单分析ReentrantLock的结构 ...

  3. 数据库建模工具pd的使用

  4. MQTT 协议学习:008-在STM32上移植MQTT

    前言 通过前面了解MQTT有关概念.分析了有关的报文,我们对于这个协议也有了更深的认识.但纸上谈来终觉浅,绝知此事要躬行. 本文参考:<STM32+W5500+MQTT+Android实现远程数 ...

  5. Window Server 2019 配置篇(5)- 在域中建立WSUS以实现自动更新

    上次讲到我们的服务器群中增加了一台用于自动部署的服务器,这次我们要添加一台搭载WSUS服务的服务器,以实现对window更新的管理 那么WSUS是什么服务呢? WSUS是window server u ...

  6. P1047 编程团体赛

    转跳点:

  7. UVA - 548 Tree(二叉树的递归遍历)

    题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小. 分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小 #include<cstdio> #inc ...

  8. tf.argmax()函数作用

    tf.argmax()函数原型: def argmax(input, axis=None, name=None, dimension=None, output_type=dtypes.int64) 作 ...

  9. 二十九、SAP中输出漂亮的表格

    一.代码如下 二.输出效果如下 *&---------------------------------------------------------------------* *& ...

  10. JNI调用so动态库

    1.编写native接口 package org.demo; public class JniDemo { public static native int bmp2fea(byte[] bmp, b ...