-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathpiechart.html
49 lines (49 loc) · 1.41 KB
/
piechart.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>饼状图</title>
<style media="screen">
body{
background-color: #000;
}
canvas{
background-color: #fff;
}
</style>
<script type="text/javascript">
window.onload = function(){
let my_canvas = document.getElementById('canvas');
let gd = my_canvas.getContext('2d');
function bing(gd,x,y,r,startArg,endArg,color){
gd.beginPath();
gd.moveTo(x,y);
gd.lineTo(x+Math.sin(trans(startArg))*r,y-Math.cos(trans(startArg))*r);
gd.arc(x,y,r,trans(startArg)-Math.PI/2,trans(endArg)-Math.PI/2,false);
gd.closePath();
gd.lineWidth = 1;
gd.stroke();
gd.fillStyle = color;
gd.fill();
}
function trans(x){
return Math.PI*x/180;
}
let data=[581, 273, 750, 501];
let colors=['#f00', '#f0f', '#0f0', '#00f'];
let sum = data.reduce((tem,item)=>tem+=item);
data = data.map(item=>360*(item/sum));
let beginArg = 0;
data.forEach((item,index)=>{
bing(gd,380,300,200,beginArg,beginArg+item,colors[index]);
beginArg +=item;
})
}
</script>
</head>
<body>
<canvas id="canvas" width="800" height="800">
你的浏览器不支持canvas,请升级到最新版本~~
</canvas>
</body>
</html>