JS canvas繪制環(huán)狀統(tǒng)計(jì)圖

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>環(huán)狀圖</title>

<style>

body {

? ? margin: 0;

? ? padding: 50px 0;

? ? background-color: #444;

}

ul,li {

? ? list-style: none;

? ? margin: 0;

? ? padding: 0;

}

ul li {

? ? float: left;

? ? width: 33%;

? ? text-align: center;

}

</style>

</head>

<body>

<div class="box">

? ? <ul>

? ? ? ? <li>

? ? ? ? ? ? <canvas id="one" width="200" height="200"></canvas>

? ? ? ? </li>

? ? ? ? <li>

? ? ? ? ? ? <canvas id="two" width="200" height="200"></canvas>

? ? ? ? </li>

? ? ? ? <li>

? ? ? ? ? ? <canvas id="three" width="200" height="200"></canvas>

? ? ? ? </li>

? ? </ul>

</div>

</body>

<script>

function drawCircle(_options){

? ? var options = _options || {};? ? //獲取或定義options對(duì)象;

? ? options.angle = options.angle || 1;? ? //定義默認(rèn)角度1為360度(角度范圍 0-1);

? ? options.color = options.color || '#fff';? ? //定義默認(rèn)顏色(包括字體和邊框顏色);

? ? options.lineWidth = options.lineWidth || 10;? ? //定義默認(rèn)圓描邊的寬度;

? ? options.lineCap = options.lineCap || 'square';? ? //定義描邊的樣式,默認(rèn)為直角邊,round 為圓角

? ? var oBoxOne = document.getElementById(options.id);

? ? var sCenter = oBoxOne.width / 2;? ? //獲取canvas元素的中心點(diǎn);

? ? var ctx = oBoxOne.getContext('2d');

? ? var nBegin = Math.PI / 2;? ? //定義起始角度;

? ? var nEnd = Math.PI * 2;? ? //定義結(jié)束角度;

? ? var grd = ctx.createLinearGradient(0, 0, oBoxOne.width, 0);? ? //grd定義為描邊漸變樣式;

? ? grd.addColorStop(0, 'red');

? ? grd.addColorStop(0.5, 'yellow');

? ? grd.addColorStop(1, 'green');

? ? ctx.textAlign = 'center';? ? //定義字體居中;

? ? ctx.font = 'normal normal bold 20px Arial';? ? //定義字體加粗大小字體樣式;

? ? ctx.fillStyle = options.color == 'grd' ? grd : options.color;? ? //判斷文字填充樣式為顏色,還是漸變色;

? ? ctx.fillText((options.angle * 100) + '%', sCenter, sCenter);? ? //設(shè)置填充文字;

? ? /*ctx.strokeStyle = grd;? ? //設(shè)置描邊樣式為漸變色;

? ? ctx.strokeText((options.angle * 100) + '%', sCenter, sCenter);? ? //設(shè)置描邊文字(即鏤空文字);*/

? ? ctx.lineCap = options.lineCap;

? ? ctx.strokeStyle = options.color == 'grd' ? grd : options.color;

? ? ctx.lineWidth = options.lineWidth;

? ? ctx.beginPath();? ? //設(shè)置起始路徑,這段繪制360度背景;

? ? ctx.strokeStyle = '#D8D8D8';

? ? ctx.arc(sCenter, sCenter, (sCenter - options.lineWidth), -nBegin, nEnd, false);

? ? ctx.stroke();

? ? var imd = ctx.getImageData(0, 0, 240, 240);

? ? function draw(current) {? ? //該函數(shù)實(shí)現(xiàn)角度繪制;

? ? ? ? ctx.putImageData(imd, 0, 0);

? ? ? ? ctx.beginPath();

? ? ? ? ctx.strokeStyle = options.color == 'grd' ? grd : options.color;

? ? ? ? ctx.arc(sCenter, sCenter, (sCenter - options.lineWidth), -nBegin, (nEnd * current) - nBegin, false);

? ? ? ? ctx.stroke();

? ? }

? ? var t = 0;

? ? var timer = null;

? ? function loadCanvas(angle) {? ? //該函數(shù)循環(huán)繪制指定角度,實(shí)現(xiàn)加載動(dòng)畫(huà);

? ? ? ? timer = setInterval(function(){

? ? ? ? ? ? if (t > angle) {

? ? ? ? ? ? ? ? draw(options.angle);

? ? ? ? ? ? ? ? clearInterval(timer);

? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? draw(t);

? ? ? ? ? ? ? ? t += 0.02;

? ? ? ? ? ? }

? ? ? ? }, 20);

? ? }

? ? loadCanvas(options.angle);? ? //載入百度比角度? 0-1 范圍;

? ? timer = null;

}

drawCircle({

? ? id: 'one',

? ? color: '#0000ff',

? ? angle: 0.5,

? ? lineWidth: 5

});

drawCircle({

? ? id: 'two',

? ? angle: 0.75,

? ? color: '#ff0000',

? ? lineWidth: 12

});

drawCircle({

? ? id: 'three',

? ? angle: 1,

? ? lineWidth: 15,

? ? color: 'grd'

});

</script>

</html>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容