国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JavaScript canvas繪制折線圖

2024-05-06 15:44:36
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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> /*1.構造函數*/ 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.dottedSize = 6;  /*點的坐標 和數據有關系 數據可視化*/ } /*2.行為方法*/ LineChart.prototype.init = function (data) {  this.drawGrid();  this.drawAxis();  this.drawDotted(data); }; /*繪制網格*/ 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();  } }; /*繪制坐標系*/ 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.數據的坐標 需要轉換 canvas坐標*/  /*2.再進行點的繪制*/  /*3.把線連起來*/  var that = this;  /*記錄當前坐標*/  var prevCanvasX = 0;  var prevCanvasY = 0;  data.forEach(function (item, i) {   /* x = 原點的坐標 + 數據的坐標 */   /* y = 原點的坐標 - 數據的坐標 */   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();   /*點的連線*/   /*當時第一個點的時候 起點是 x0 y0*/   /*當時不是第一個點的時候 起點是 上一個點*/   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;  }); }; /*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>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 太仓市| 抚宁县| 萨迦县| 乐安县| 会泽县| 耒阳市| 日照市| 石柱| 潜江市| 平利县| 海丰县| 长顺县| 瓦房店市| 乾安县| 安仁县| 商南县| 龙州县| 余干县| 奉贤区| 嵊州市| 迁西县| 富源县| 昌平区| 乌兰县| 罗山县| 湟源县| 瑞昌市| 左贡县| 新巴尔虎右旗| 阿尔山市| 红原县| 犍为县| 九台市| 苗栗市| 南京市| 遵义市| 两当县| 淳安县| 靖西县| 若尔盖县| 维西|