1. 程式人生 > >初探canvas製作一個折線圖——(面向物件)

初探canvas製作一個折線圖——(面向物件)

canvas

  • 準備一塊畫布
 <style>
        canvas {
            border: 1px solid #ccc;
        }
    </style>
<canvas width="600" height="400"></canvas>

起步——start

<script>
    var myCanvas = document.querySelector('canvas');
    var ctx = myCanvas.getContext('2d');
//建構函式 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.arrowSize=10; //原點 this.x0=this.space; this.y0=this.canvasHeight-this.space; //繪製點 this.dottedSize=6; //點的座標——和資料有關係,資料視覺化 }; //行為方法 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); 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.strokeStyle='#eee'; 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.strokeStyle='#eee'; 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; }); }; //初始化 這裡的資料本應該是從後臺傳過來的json資料,這裡模擬一下 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>

效果

在這裡插入圖片描述