html5 canvas 画图表

时间:2022-09-25 15:22:43
(function () {
var canvas = document.createElement("canvas");
canvas.width = 800;
canvas.height = 400;
canvas.innerText = "抱歉,你的浏览器不支持canvas T_T";
document.getElementById('leoncanvas').appendChild(canvas); var data = { "aaa": 1000, "bbb": 600, "ccc": 200, "ddd": 400, "eee": 550, "fff": 50, "ggg": 10, "hhh": 280 };
var yAxisValueStep = 100; var width = canvas.width;
var height = canvas.height; // data length calc & value analysis
var length = 0, min = 0, max = 0; for (var i in data) {
length++;
if (data[i] < min)
min = data[i];
if (data[i] > max)
max = data[i];
} var xLength = width * 0.9;
var yLength = height * 0.7;
var left = width * 0.12;
var bottom = height * 0.08; // origin point
var x0 = left;
var y0 = height - bottom; var p0 = { x: x0, y: y0 };
var px = { x: left + xLength, y: p0.y };
var py = { x: p0.x, y: p0.y - yLength }; var xScaleMarkWidth = xLength / (length + 3);
var yScaleMarkWidth = yLength / (length + 3); // begin to draw axis
var context = canvas.getContext('2d');
context.beginPath();
// offset 0.5 to draw 1 pixel line
//http://kilianvalkhof.com/2010/design/the-problem-with-svg-and-canvas/
// xAxis
context.moveTo(p0.x + 0.5, p0.y + 0.5);
context.lineTo(px.x + 0.5, px.y + 0.5);
// yAxis
context.moveTo(p0.x + 0.5, p0.y + 0.5);
context.lineTo(py.x + 0.5, py.y + 0.5); context.font = "normal lighter 12px sans-serif"; // scale marker
for (var i = 0; i < length + 2; i++) {
// xaxis
context.moveTo(p0.x + (i + 1) * xScaleMarkWidth + 0.5, p0.y + 0.5);
context.lineTo(p0.x + (i + 1) * xScaleMarkWidth + 0.5, p0.y + 2 + 0.5); // yaxis
context.moveTo(p0.x + 0.5, p0.y - (i + 1) * yScaleMarkWidth + 0.5);
context.lineTo(p0.x + 0.5 - 2, p0.y - (i + 1) * yScaleMarkWidth + 0.5);
} // y axis marker value
for (var i = 0; i <= length + 2; i++) {
// yaxis value
context.fillText(yAxisValueStep * i, p0.x + 0.5 - 30, p0.y - i * yScaleMarkWidth + 3);
} // draw column chart
var lengthPerValue = (yScaleMarkWidth * (length + 2)) / max;
var rectX, rectY;
var i = 0; for (var p in data) {
rectX = p0.x + (i + 1) * xScaleMarkWidth + 0.5 - xScaleMarkWidth * 0.25;
rectY = p0.y - lengthPerValue * data[p]; // draw column
context.fillStyle = "rgba(255,0,0,0.6)";
context.fillRect(rectX, rectY, xScaleMarkWidth / 2, lengthPerValue * data[p]); // add text
context.fillStyle = 'rgb(0,0,0)';
// column value
context.fillText(data[p], rectX, rectY - 15);
// x value
context.fillText(p, rectX + xScaleMarkWidth * 0.1, rectY + lengthPerValue * data[p] + 20);
i++;
} context.lineWidth = 1;
context.stroke();
context.closePath();
})();

柱状图效果如下:

(function () {
var canvas = document.createElement("canvas");
canvas.width = 800;
canvas.height = 400;
canvas.innerText = "抱歉,你的浏览器不支持canvas T_T";
document.getElementById('leoncanvas1').appendChild(canvas); var data = { "aaa": 1000, "bbb": 600, "ccc": 200, "ddd": 400, "eee": 550, "fff": 50, "ggg": 10, "hhh": 280 };
var yAxisValueStep = 100; var width = canvas.width;
var height = canvas.height; // data length calc & value analysis
var length = 0, min = 0, max = 0; for (var i in data) {
length++;
if (data[i] < min)
min = data[i];
if (data[i] > max)
max = data[i];
} var xLength = width * 0.9;
var yLength = height * 0.7;
var left = width * 0.12;
var bottom = height * 0.08; // origin point
var x0 = left;
var y0 = height - bottom; var p0 = { x: x0, y: y0 };
var px = { x: left + xLength, y: p0.y };
var py = { x: p0.x, y: p0.y - yLength }; var xScaleMarkWidth = xLength / (length + 3);
var yScaleMarkWidth = yLength / (length + 3); // begin to draw axis
var context = canvas.getContext('2d');
context.beginPath();
// offset 0.5 to draw 1 pixel line
//http://kilianvalkhof.com/2010/design/the-problem-with-svg-and-canvas/
// xAxis
context.moveTo(p0.x + 0.5, p0.y + 0.5);
context.lineTo(px.x + 0.5, px.y + 0.5);
// yAxis
context.moveTo(p0.x + 0.5, p0.y + 0.5);
context.lineTo(py.x + 0.5, py.y + 0.5); // scale marker
for (var i = 0; i < length + 2; i++) {
// xaxis
context.moveTo(p0.x + (i + 1) * xScaleMarkWidth + 0.5, p0.y + 0.5);
context.lineTo(p0.x + (i + 1) * xScaleMarkWidth + 0.5, p0.y + 2 + 0.5); // yaxis
context.moveTo(p0.x + 0.5, p0.y - (i + 1) * yScaleMarkWidth + 0.5);
context.lineTo(p0.x + 0.5 - 2, p0.y - (i + 1) * yScaleMarkWidth + 0.5);
} // y axis marker value
for (var i = 0; i <= length + 2; i++) {
// yaxis value
context.fillText(yAxisValueStep * i, p0.x + 0.5 - 30, p0.y - i * yScaleMarkWidth + 3);
} // draw column chart
var lengthPerValue = (yScaleMarkWidth * (length + 2)) / max;
//var rectX, rectY;
var x, y;
var i = 0; context.font = "normal lighter 12px sans-serif"; // set axis text & marker
for (var p in data) {
x = p0.x + (i + 1) * xScaleMarkWidth + 0.5;
y = p0.y - lengthPerValue * data[p]; // add text
context.fillStyle = 'rgb(0,0,0)'; // point value
context.fillText(data[p], x, y - 15);
// x value
context.fillText(p, x + xScaleMarkWidth * 0.1, y + lengthPerValue * data[p] + 20);
i++;
} context.stroke();
context.closePath(); context.beginPath();
context.strokeStyle = "rgba(255,0,0,0.8)";
context.lineCap = "square";
context.lineJoin = "miter";
context.lineWidth = 2; i = 0;
for (var p in data) {
x = p0.x + (i + 1) * xScaleMarkWidth + 0.5;
y = p0.y - lengthPerValue * data[p]; if (i == 0) {
context.moveTo(x, y);
}
else {
context.lineTo(x, y);
}
i++;
} context.stroke();
context.closePath();
})();

折线图效果如下:

html5 canvas 画图表的更多相关文章

  1. HTML5 Canvas 画虚线组件

    前段时间由于项目需要,用到了HTML5 Canvas画图,但是没有画虚线的方法,自己写了一个HTML5 画虚线的组件. dashedLine.js if (window.CanvasRendering ...

  2. HTML5 Canvas 画钟表

    画钟表是2D画图的老生常谈,我也不能免俗弄了一个.代码如下: <!DOCTYPE html> <html lang="utf-8"> <meta ht ...

  3. CSS3进度条 和 HTML5 Canvas画圆环

    看到一些高大上的进度条插件,然后想自己用CSS写.经过搜索资料之后,终于成功了.为了以后方便拿来用,或者复习.将代码贴出. HTML代码: 只需要两个div,外面的为一个有border的div id为 ...

  4. html5 canvas画饼

    1. [图片] lxdpie.jpg ​2. [文件] lqdpie.html ~ 801B     下载(7) <!DOCTYPE HTML PUBLIC "-//W3C//DTD ...

  5. html5 canvas画不出图像的原因

    很久没写博客了,今年过年的时候,家里出了意外,现在心里依然很难受.6月份之前一直忙着写毕业论文,答辩完6月初回公司继续上班,今天刚好周末有空,就写下之前碰到一个问题. 做一个图像查看器(基于Chrom ...

  6. html5 canvas画流程图

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  7. HTML5 Canvas画数字时钟

    先不说废话,没代码算个蛋. 一些地方注释都写得比较清楚,不过这只是部分,因为只有秒针,但是时针,分针的逻辑都是一致的. 代码中有些坐标不知道为什么较不准,看看就好

  8. html5 canvas画进度条

    这个ie8的兼容是个问题,ie8 的innerHTML有问题啊,添加两个附件吧 <!DOCTYPE html> <html> <head> <meta cha ...

  9. html5 canvas 画hello ketty

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

随机推荐

  1. python 学习笔记9(面向对象)

    面向过程.函数式.面向对象 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象(Object Oriented Programmin ...

  2. QuickFIX&sol;N 动态存储配置信息

    Acceptor或者Initiator能够为您维护尽可能多的FIX会话,因而FIX会话标识的唯一性非常重要.在QuickFIX/N中,一个FIX会话的唯一标识是由:BeginString(FIX版本号 ...

  3. &period;Net额外小工具

    http://blog.csdn.net/zhumi/article/details/49076 用于编写单元测试的 NUnit用于创建代码文档资料的 NDoc用于生成解决方案的 NAnt用于生成代码 ...

  4. 巧用C&num;做中间语言 实现Java调用&period;net DLL

    本文将详细为大家介绍一个java调用.net DLL的方法,以实现特殊的客户的特殊要求:“在Java项目中必须使用其提供的用.net写的DLL加密机制!” 环境与工具: ◆.net framework ...

  5. ORACLE SQL单行函数(一)【weber出品必属精品】

    1.SUBSTR:求父串中的子串 SUBSTR('HelloWorld',1,5) 1:代表子串的起始位置,如果为正,正数,如果为负,倒数 5:代表字串的终止位置,只能向右数,可以省略,如果省略就是数 ...

  6. web&period;xml在listener作用与用途

    一.WebContextLoaderListener 监听类 它能捕捉到server的启动和停止,在启动和停止触发里面的方法做对应的操作! 它必须在web.xml 中配置才干使用,是配置监听类的 二. ...

  7. Codefoces 723B Text Document Analysis

    B. Text Document Analysis time limit per test:1 second memory limit per test:256 megabytes input:sta ...

  8. Linux:DNS服务器搭建及配置

    1.yum install -y bind bind-chroot bind-utils 2.编辑DNS主配置文件 vim /etc/named.conf   修改如下标红色处即可: options ...

  9. check选择样式

    样式一(H5): <form action="#">  <div class="wrapper">    <div class=& ...

  10. 2019第十届蓝桥杯 E题 迷宫

    /*输入 30 50 01010101001011001001010110010110100100001000101010 00001000100000101010010000100000001001 ...