<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. php 获取时间段

    switch ($type){ case 'day'://当日 $end=date(,,,date(,date('Y'))); $where=' and '.$pre.'create_time> ...

  2. Vue父组件向子组件传值

    父组件向子组件传值 组件实例定义方式,注意:一定要使用props属性来定义父组件传递过来的数据 <script> // 创建 Vue 实例,得到 ViewModel var vm = ne ...

  3. 016、Java中使用小数

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  4. C语言备忘录——运算符优先级

    丢脸啊,今天写一道算法题,第一次没写对.改了半天愣是没看出来错哪,后面说出了一下过程,突然发现是运算符优先级惹得祸 if (!num % 2){ …… },!的运算优先级高于%,啊啊啊,丧心病狂我找了 ...

  5. Codeforces 556A:Case of the Zeros and Ones

    A. Case of the Zeros and Ones time limit per test 1 second memory limit per test 256 megabytes input ...

  6. Windows 10中使用VirtualBox

    新版Windows 10或者安装了新的更新以后,VirtualBox虚拟机就不能用了. 原因是WIndows10里面有个叫Virtualization-base security的安全机制打开了. 关 ...

  7. 145-PHP 使用<<<和HTML混编(一)

    <?php $html=<<<TEMP1 <title>PHP输出HTML代码</title> <body> <a href=#> ...

  8. 经验分享:如何搞定Personal Statement?

    最近又到申请季啦,如何自己DIY申请,如何准备文书成为众多留学生关心的问题.不管是你申请本科,硕士,还是博士,相信这篇文章都能帮助到你.下面来说一下文书中一个很重要的组成,就是个人陈述Personal ...

  9. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring使用AspectJ开发AOP基于XML和基于Annotation

    AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...

  10. js原型链理解(4)-经典继承

    经典继承就是组合继承,就是组合构造函数和原型链的优点混合继承. 1.避免引用类型的属性初始化 2.避免相同方法的多次初始化 function Super(name){ this.ages = [100 ...