<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. Element-UI Table 实现筛选数据功能

    最近产品提出了一个筛选数据的功能,要求在表头里实现一个下拉框进行筛选. 首先, Element-ui 的官方文档,el-table-column 下有一个 filters , 用于数据的筛选和过滤, ...

  2. win10热键体验

    Alt+Tab: 横向显示正在执行的进程 Win+Tab: 3D形式展示正在执行的进程 Win+D:返回桌面(逃领导查电脑和放窥屏尴尬) Win+R: run(直接打开文件开始运行) crtl+Alt ...

  3. CharacterEncodingFilter这个spring的过滤器

    org.springframework.web.filter.CharacterEncodingFilter 对请求于响应的编码进行过滤,半路出家的和尚总是对什么都感觉到好奇,都想记录下来(

  4. 150-PHP nl2br函数(一)

    <?php $str="h t m l"; //定义一个多处换行的字串 echo "未处理前的输出形式:<br />{$str}"; #nl2 ...

  5. C# SqlBulkCopy 大量数据导入到数据库

    之前写了一篇C# 直接使用sql语句对数据库操作 (cmd.ExecuteNonQuery)的文章 这是针对数据量不大的操作,换句话说,效率太低,所以在此介绍一个效率高的.能大批量导入到数据库的方法 ...

  6. select2 智能补全模糊查询select2的下拉选择框使用

    我们在上篇文章中已经在SpringMVC基础框架的基础上应用了BootStrap的后台框架,在此基础上记录select2的使用. 应用bootstrap模板 基础项目源码下载地址为: SpringMV ...

  7. ACM-Checker Challenge

    题目描述:Checker Challenge  1000(ms)  10000(kb)  20 / 90 Examine the 6x6 checkerboard below and note tha ...

  8. 算法实战(六)Z 字形变换

    一.前言 之前因为第五题最长回文字符串需要使用到dp解法,所以我花了很长的时间来研究dp(因为每天又要上班,加上这段时间事情比较多,所以花了三个星期才搞定),好不容易算入了个门,有兴趣的同学可以看看我 ...

  9. Loadrunner安装与破解

    一.安装loadrunner 1. 点击setup.exe 2. 点击安装完整程序 3. 点击确定,安装必需程序 4. 安装vc2005的时候报了如下错,导致无法继续安装,没有报错可跳过第五步 5. ...

  10. 安装Linux系统Centos6版本

    1.下载VMware软件 2.下载Centos6文件 http://archive.kernel.org/centos-vault/6.8/isos/x86_64/CentOS-6.8-x86_64- ...