canvas图表的绘制
绘制网格
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> canvas { border: 1px solid #ccc; } </style> </head> <body> <canvas width="600" height="400"></canvas> <script> var myCanvas = document.querySelector('canvas'); var ctx = myCanvas.getContext('2d');
var gridSize = 10; var canvasHeight = ctx.canvas.height; var canvasWidth = ctx.canvas.width; var xTotal = Math.floor(canvasHeight / gridSize); for (var i = 0; i <= xTotal; i++) { ctx.beginPath(); ctx.moveTo(0, i * gridSize - 0.5) ctx.lineTo(canvasWidth, i * gridSize - 0.5) ctx.strokeStyle = '#eee'; ctx.stroke(); } var yTotal = Math.floor(canvasWidth / gridSize); for (var i = 0; i <= yTotal; i++) { ctx.beginPath(); ctx.moveTo(i * gridSize - 0.5, 0) ctx.lineTo(i * gridSize - 0.5, canvasHeight) ctx.strokeStyle = '#eee'; ctx.stroke(); } </script> </body> </html>
|
绘制坐标系
/*1.绘制坐标系*/
/*2.确定原点*/
/*3.确定距离画布旁边的距离*/
/*4.确定坐标轴的长度*/
/*5.确定箭头的大小 是个等边三角形 10 */
/*6.绘制箭头填充*/
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> canvas { border: 1px solid #ccc; } </style> </head> <body> <canvas width="600" height="400"></canvas> <script> var myCanvas = document.querySelector('canvas'); var ctx = myCanvas.getContext('2d');
var space = 20; var arrowSize = 10;
var canvasWidth = ctx.canvas.width; var canvasHeight = ctx.canvas.height;
var x0 = space; var y0 = canvasHeight - space;
var arrowH = arrowSize/2*Math.sqrt(3)
ctx.beginPath(); ctx.moveTo(x0, y0); ctx.lineTo(canvasWidth - space,y0) ctx.stroke();
ctx.beginPath(); ctx.moveTo(canvasWidth - space - arrowH, y0 - 5); ctx.lineTo(canvasWidth - space,y0) ctx.lineTo(canvasWidth - space - arrowH, y0 + 5) ctx.closePath() ctx.fill(); ctx.stroke(); ctx.beginPath(); ctx.moveTo(x0, y0); ctx.lineTo(space,space) ctx.stroke(); ctx.beginPath(); ctx.moveTo(space-5, space+arrowH); ctx.lineTo(space,space) ctx.lineTo(space+5,space+arrowH) ctx.closePath() ctx.fill(); ctx.stroke();
</script> </body> </html>
|
绘制坐标点
/*1.绘制点*/
/*2.点的尺寸*/
/*3.以坐标中心绘制点*/
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> canvas { border: 1px solid #ccc; } </style> </head> <body> <canvas width="600" height="400"></canvas> <script> var myCanvas = document.querySelector('canvas'); var ctx = myCanvas.getContext('2d');
var coordinate = { x:100, y:100 } var dottedSize = 10; ctx.moveTo(coordinate.x - dottedSize / 2,coordinate.y - dottedSize / 2); ctx.lineTo(coordinate.x + dottedSize / 2,coordinate.y - dottedSize / 2); ctx.lineTo(coordinate.x + dottedSize / 2,coordinate.y + dottedSize / 2); ctx.lineTo(coordinate.x - dottedSize / 2,coordinate.y + dottedSize / 2); ctx.closePath(); ctx.fill();
</script> </body> </html>
|
因此,一个完整的折线图就可以拼凑一块儿组成:
完整折线图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> canvas { border: 1px solid #ccc; } </style> </head> <body> <canvas width="600" height="400"></canvas> <script> var LineChart = function(ctx) { this.ctx = ctx || document.querySelector('canvas').getContext('2d'); this.canvasWidth = this.ctx.canvas.width; this.canvasHeight = this.ctx.canvas.height; this.gridSize = 10; this.space = 20; this.x0 = this.space; this.y0 = this.canvasHeight - this.space; this.arrowSize = 10; this.arrowH = Math.floor(this.arrowSize / 2 * Math.sqrt(3)) this.dottedSize = 6; } LineChart.prototype.init = function(data) { this.drawGrid(); this.drawAxis(); this.drawDotted(data); }; LineChart.prototype.drawGrid = function() { var xLineTotal = Math.floor(this.canvasHeight / this.gridSize); this.ctx.strokeStyle = '#eee'; for (var i = 0; i <= xLineTotal; 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.stroke(); } var yLineTotal = Math.floor(this.canvasWidth / this.gridSize); for (var i = 0; i <= yLineTotal; 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.stroke(); } }; LineChart.prototype.drawAxis = function() {
this.ctx.beginPath(); this.ctx.strokeStyle = '#000'; this.ctx.moveTo(this.x0, this.y0); this.ctx.lineTo(this.canvasWidth - this.space, this.y0) this.ctx.lineTo(this.canvasWidth - this.space - this.arrowH, this.y0 - this.arrowSize / 2); this.ctx.lineTo(this.canvasWidth - this.space - this.arrowH, this.y0 + this.arrowSize / 2); this.ctx.lineTo(this.canvasWidth - this.space, this.y0) this.ctx.stroke(); this.ctx.fill();
this.ctx.beginPath(); this.ctx.strokeStyle = '#000'; 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.arrowH); this.ctx.lineTo(this.space + this.arrowSize / 2, this.space+this.arrowH); this.ctx.lineTo(this.space, this.space); this.ctx.stroke(); this.ctx.fill(); }; LineChart.prototype.drawDotted = function(data) { var that = this; var prevCanvasX = 0; var prevCanvasY = 0; data.forEach(function(item, i) { var canvasX = that.x0 + item.x; var canvasY = that.y0 - item.y; that.ctx.beginPath(); that.ctx.moveTo(canvasX - that.dottedSize / 2, canvasY - that.dottedSize / 2); that.ctx.lineTo(canvasX + that.dottedSize / 2, canvasY - that.dottedSize / 2); that.ctx.lineTo(canvasX + that.dottedSize / 2, canvasY + that.dottedSize / 2); that.ctx.lineTo(canvasX - that.dottedSize / 2, canvasY + that.dottedSize / 2); that.ctx.closePath(); that.ctx.fill(); if (i == 0) { that.ctx.beginPath(); that.ctx.moveTo(that.x0, that.y0); that.ctx.lineTo(canvasX, canvasY); that.ctx.stroke(); } else { that.ctx.beginPath(); that.ctx.moveTo(prevCanvasX, prevCanvasY); that.ctx.lineTo(canvasX, canvasY); that.ctx.stroke(); } prevCanvasX = canvasX; prevCanvasY = canvasY; }); }; var data = [{ x: 100, y: 120 }, { x: 200, y: 160 }, { x: 300, y: 240 }, { x: 400, y: 120 }, { x: 500, y: 80 } ]; var lineChart = new LineChart(); lineChart.init(data); </script> </body> </html>
|