国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

javascript使用canvas實(shí)現(xiàn)餅狀圖效果

瀏覽:5日期:2023-06-17 08:04:07

使用canvas寫一個(gè)餅狀圖,供大家參考,具體內(nèi)容如下

javascript使用canvas實(shí)現(xiàn)餅狀圖效果

代碼:

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>Document</title></head><body> <canvas id=’canvas’ width=’800’ height=’400’ style='border: 1px solid red;'></canvas> <script> let data = [ { title: '服飾1', money: 400 }, { title: '服飾2', money: 300 }, { title: '服飾3', money: 400 }, { title: '服飾4', money: 200 }, { title: '服飾5', money: 500 }, { title: '服飾6', money: 180 }, { title: '服飾7', money: 500 }] /** @type {HTMLCanvasElement} */ let canvas = document.querySelector('#canvas'); let ctx = canvas.getContext('2d'); let r = 100; let money = function (obj, sum) { for (let i = 0; i < obj.length; i++) { sum += data[i].money } return sum; } let totalmoney = money(data, 0); let nowsum = 0; let start = 0; let end = 0; let R = 100; let i=0; data.forEach(function (item) { ctx.beginPath() nowsum += item.money; end = (nowsum / totalmoney) ctx.moveTo(150, 150); ctx.arc(150, 150, R, start*Math.PI*2, end*Math.PI*2) start = end; //產(chǎn)生隨機(jī)顏色 ctx.fillStyle = ’#’ + Math.floor(Math.random() * 0xffffff).toString(16); ctx.rect(350,5+(35*i),30,30); ctx.font='14px 黑體' ctx.fillText(item.title,400,25+(35*i)) ctx.strokeStyle = 'gray' ctx.fill(); ctx.stroke(); i++; }) </script></body></html>

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 合水县| 本溪市| 买车| 洞口县| 延安市| 巴塘县| 常熟市| 通海县| 孝义市| 广昌县| 安庆市| 灵宝市| 襄樊市| 桦川县| 永定县| 德惠市| 福州市| 凯里市| 惠水县| 新乡市| 大姚县| 河曲县| 保德县| 太保市| 河东区| 吉林市| 电白县| 白银市| 营口市| 福安市| 班戈县| 阜康市| 望城县| 清远市| 钟祥市| 两当县| 延庆县| 新龙县| 新巴尔虎左旗| 金溪县| 光泽县|