欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript canvas繪制折線圖

 更新時間:2020年02月18日 09:17:18   作者:碼農(nóng)陽神  
這篇文章主要為大家詳細(xì)介紹了JavaScript canvas繪制折線圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了canvas繪制折線圖的具體代碼,供大家參考,具體內(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)原點*/
 this.x0 = this.space;
 this.y0 = this.canvasHeight - this.space;
 /*箭頭的大小*/
 this.arrowSize = 10;
 /*繪制點*/
 this.dottedSize = 6;
 /*點的坐標(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();
 };
 /*繪制所有點*/
 LineChart.prototype.drawDotted = function (data) {
 /*1.數(shù)據(jù)的坐標(biāo) 需要轉(zhuǎn)換 canvas坐標(biāo)*/
 /*2.再進(jìn)行點的繪制*/
 /*3.把線連起來*/
 var that = this;
 /*記錄當(dāng)前坐標(biāo)*/
 var prevCanvasX = 0;
 var prevCanvasY = 0;
 data.forEach(function (item, i) {
 /* x = 原點的坐標(biāo) + 數(shù)據(jù)的坐標(biāo) */
 /* y = 原點的坐標(biāo) - 數(shù)據(jù)的坐標(biāo) */
 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();
 /*點的連線*/
 /*當(dāng)時第一個點的時候 起點是 x0 y0*/
 /*當(dāng)時不是第一個點的時候 起點是 上一個點*/
 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();
 }
 /*記錄當(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é)果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • js圖片輪播手動切換特效

    js圖片輪播手動切換特效

    這篇文章主要為大家詳細(xì)介紹了js圖片輪播手動切換特效,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • JS修改iframe頁面背景顏色的方法

    JS修改iframe頁面背景顏色的方法

    這篇文章主要介紹了JS修改iframe頁面背景顏色的方法,涉及javascript操作iframe頁面樣式的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • JS小練習(xí)代碼之二

    JS小練習(xí)代碼之二

    JS練習(xí)代碼
    2008-10-10
  • js判斷空對象的實例(超簡單)

    js判斷空對象的實例(超簡單)

    下面小編就為大家?guī)硪黄猨s判斷空對象的實例(超簡單)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-07-07
  • 前端面向?qū)ο缶幊讨瓻S5語法ES6語法詳解

    前端面向?qū)ο缶幊讨瓻S5語法ES6語法詳解

    這篇文章主要為大家介紹了前端面向?qū)ο缶幊讨瓻S5語法ES6語法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Javascript類型轉(zhuǎn)換詳解

    Javascript類型轉(zhuǎn)換詳解

    這篇文章主要為大家介紹了Javascript類型轉(zhuǎn)換,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • 可以讀取EXCEL文件的js代碼

    可以讀取EXCEL文件的js代碼

    js讀取 EXCEL 文件 的實現(xiàn)代碼,比較全了大家可以自行測試了。
    2009-08-08
  • JavaScript實現(xiàn)生成GUID(全局統(tǒng)一標(biāo)識符)

    JavaScript實現(xiàn)生成GUID(全局統(tǒng)一標(biāo)識符)

    這篇文章主要介紹了JavaScript實現(xiàn)生成GUID(全局統(tǒng)一標(biāo)識符),本文寫成了一個GUID生成類,使用也非常方便,需要的朋友可以參考下
    2014-09-09
  • js中遍歷Map對象的簡單實例

    js中遍歷Map對象的簡單實例

    下面小編就為大家?guī)硪黄猨s中遍歷Map對象的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08
  • tangram.js庫實現(xiàn)js類的方式實例分析

    tangram.js庫實現(xiàn)js類的方式實例分析

    這篇文章主要介紹了tangram.js庫實現(xiàn)js類的方式,結(jié)合實例形式分析了tangram.js庫實現(xiàn)類的創(chuàng)建、繼承等相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01

最新評論