您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)JavaScript canvas怎么繪制折線圖的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
具體內(nèi)容如下
<!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> /*1.構(gòu)造函數(shù)*/ var LineChart = function (ctx) { /*獲取繪圖工具*/ this.ctx = ctx || document.querySelector('canvas').getContext('2d'); /*畫布的大小*/ this.canvasWidth = this.ctx.canvas.width; this.canvasHeight = this.ctx.canvas.height; /*網(wǎng)格的大小*/ this.gridSize = 10; /*坐標(biāo)系的間距*/ this.space = 20; /*坐標(biāo)原點(diǎn)*/ this.x0 = this.space; this.y0 = this.canvasHeight - this.space; /*箭頭的大小*/ this.arrowSize = 10; /*繪制點(diǎn)*/ this.dottedSize = 6; /*點(diǎn)的坐標(biāo) 和數(shù)據(jù)有關(guān)系 數(shù)據(jù)可視化*/ } /*2.行為方法*/ LineChart.prototype.init = function (data) { this.drawGrid(); this.drawAxis(); this.drawDotted(data); }; /*繪制網(wǎng)格*/ LineChart.prototype.drawGrid = function () { /*x方向的線*/ 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(); } /*y方向的線*/ 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(); } }; /*繪制坐標(biāo)系*/ LineChart.prototype.drawAxis = function () { /*X軸*/ 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.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.stroke(); this.ctx.fill(); /*Y軸*/ 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.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(); }; /*繪制所有點(diǎn)*/ LineChart.prototype.drawDotted = function (data) { /*1.數(shù)據(jù)的坐標(biāo) 需要轉(zhuǎn)換 canvas坐標(biāo)*/ /*2.再進(jìn)行點(diǎn)的繪制*/ /*3.把線連起來*/ var that = this; /*記錄當(dāng)前坐標(biāo)*/ var prevCanvasX = 0; var prevCanvasY = 0; data.forEach(function (item, i) { /* x = 原點(diǎn)的坐標(biāo) + 數(shù)據(jù)的坐標(biāo) */ /* y = 原點(diǎn)的坐標(biāo) - 數(shù)據(jù)的坐標(biāo) */ var canvasX = that.x0 + item.x; var canvasY = that.y0 - item.y; /*繪制點(diǎn)*/ 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(); /*點(diǎn)的連線*/ /*當(dāng)時(shí)第一個(gè)點(diǎn)的時(shí)候 起點(diǎn)是 x0 y0*/ /*當(dāng)時(shí)不是第一個(gè)點(diǎn)的時(shí)候 起點(diǎn)是 上一個(gè)點(diǎn)*/ if(i == 0){ that.ctx.beginPath(); that.ctx.moveTo(that.x0,that.y0); that.ctx.lineTo(canvasX,canvasY); that.ctx.stroke(); }else{ /*上一個(gè)點(diǎn)*/ that.ctx.beginPath(); that.ctx.moveTo(prevCanvasX,prevCanvasY); that.ctx.lineTo(canvasX,canvasY); that.ctx.stroke(); } /*記錄當(dāng)前的坐標(biāo),下一次要用*/ prevCanvasX = canvasX; prevCanvasY = canvasY; }); }; /*3.初始化*/ 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>
運(yùn)行結(jié)果如下:
1.JavaScript主要用來向HTML頁面添加交互行為。 2.JavaScript可以直接嵌入到HTML頁面,但寫成單獨(dú)的js文件有利于結(jié)構(gòu)和行為的分離。 3.JavaScript具有跨平臺(tái)特性,在絕大多數(shù)瀏覽器的支持下,可以在多種平臺(tái)下運(yùn)行。
感謝各位的閱讀!關(guān)于“JavaScript canvas怎么繪制折線圖”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。