javascript使用canvas實(shí)現(xiàn)餅狀圖效果
更新時(shí)間:2020年09月08日 15:12:07 作者:HaiRong_21
這篇文章主要為大家詳細(xì)介紹了javascript使用canvas實(shí)現(xiàn)餅狀圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
使用canvas寫一個(gè)餅狀圖,供大家參考,具體內(nèi)容如下

代碼:
<!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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- js canvas實(shí)現(xiàn)寫字動(dòng)畫效果
- JS+canvas繪制的動(dòng)態(tài)機(jī)械表動(dòng)畫效果
- JS基于HTML5的canvas標(biāo)簽實(shí)現(xiàn)炫目的色相球動(dòng)畫效果實(shí)例
- javascript+HTML5的Canvas實(shí)現(xiàn)Lab單車動(dòng)畫效果
- js+canvas實(shí)現(xiàn)畫板功能
- js+canvas實(shí)現(xiàn)刮刮獎(jiǎng)功能
- js實(shí)現(xiàn)貪吃蛇游戲 canvas繪制地圖
- 使用js和canvas實(shí)現(xiàn)時(shí)鐘效果
- javascript canvas實(shí)現(xiàn)簡易時(shí)鐘例子
- JS Canvas接口和動(dòng)畫效果大全
相關(guān)文章
讓GoogleCode的SVN下的HTML文件在FireFox下正常顯示.
GoogleCode可以作為免費(fèi)的穩(wěn)定的靜態(tài)資源空間來使用,比如JQuery的文檔就在上面2009-05-05
JS判斷、校驗(yàn)MAC地址的2個(gè)實(shí)例
這篇文章主要介紹了JS判斷、校驗(yàn)MAC地址的2個(gè)實(shí)例,需要的朋友可以參考下2014-05-05
input標(biāo)簽內(nèi)容改變的觸發(fā)事件介紹
onchange事件在內(nèi)容改變(兩次內(nèi)容有可能相等)且失去焦點(diǎn)時(shí)觸發(fā);onpropertychange事件是實(shí)時(shí)觸發(fā),每增加或刪除一個(gè)字符就會觸發(fā)2014-06-06
JS中‘hello’與new String(‘hello’)引出的問題詳解
這篇文章主要給大家介紹了關(guān)于JS中'hello'與new String('hello')引出的問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
JavaScript實(shí)現(xiàn)的浮動(dòng)層框架用法實(shí)例分析
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的浮動(dòng)層框架用法,以實(shí)例形式分析了JavaScript實(shí)現(xiàn)可關(guān)閉的半透明浮動(dòng)層相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10

