生活随笔
收集整理的這篇文章主要介紹了
小程序 mpvue 使用canvas绘制环形图表
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
本來想用css3來實現(xiàn),發(fā)現(xiàn)輪廓邊上殘影嚴重,所以直接用小程序的canvas使用來。
最終效果如下:
我的整頁代碼如下,里面已經(jīng)寫出備注來。
<template
><div
class="statistic"><canvas canvas
-id
="runCanvas" id
="runCanvas" class='canvas'></canvas
></div
>
</template
>
<script
>export default {data() {return {}},methods
: {run(x0
, y0
, nowPercent
) {const lineWidth
= 16;const radius
= x0
- lineWidth
;const sAngle
= -0.5 * Math
.PI;let eAngle
= (2 * Math
.PI / 100 * nowPercent
) - 0.5 * Math
.PI;this.ctx2
.setLineWidth(16);this.ctx2
.setStrokeStyle('#d2d2d2');this.ctx2
.setLineCap('round')this.ctx2
.arc(x0
, y0
, radius
, 0, 2 * Math
.PI, false);this.ctx2
.stroke();this.ctx2
.beginPath();this.ctx2
.setLineWidth(16);this.ctx2
.setStrokeStyle('#00D49F');this.ctx2
.setLineCap('round')this.ctx2
.arc(x0
, y0
, radius
, sAngle
, eAngle
, false);this.ctx2
.stroke();this.ctx2
.beginPath();this.ctx2
.font
= 'normal bold 40px sans-serif';this.ctx2
.setFillStyle("#00D49F");this.ctx2
.setTextAlign("center");this.ctx2
.setTextBaseline("middle");this.ctx2
.fillText(nowPercent
+ "%", x0
, y0
);this.ctx2
.draw();},draw(id
, percent
) {this.ctx2
= wx
.createCanvasContext(id
);const that
= this;wx
.createSelectorQuery().select('#' + id
).boundingClientRect(function (rect
) { var x0
= parseInt(rect
.width
/ 2); var y0
= parseInt(rect
.height
/ 2); that
.run(x0
, y0
, percent
);}).exec();},},mounted() {this.draw('runCanvas', 50);}
}
</script
><style lang
="less" scoped
>.canvas
{width
: 150px
;height
: 150px
;position
: absolute
;left
: 0;top
: 0;bottom
: 0;right
: 0;margin
: auto auto
;z
-index
: 99;}
</style
>
<style lang
="less">page
{background
: white
;}
</style
>
總結
以上是生活随笔為你收集整理的小程序 mpvue 使用canvas绘制环形图表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。