<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<canvas id="canvas" width="600" height="600"></canvas>
</body>
<script>
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
canvas.style.border = '1px solid red';
var seriesData = [
{
color: 'red',
title: '不及格人数',
num: .2
},
{
color: 'green',
title: '刚好及格',
num: .1
},
{
color: 'blue',
title: '良好',
num: .3
},
{
color: 'pink',
title: '优秀',
num: .4
}
];
//我们从-90度开始画圆
var temp = -90;
//园的半径
var r = 150;
//园的中心点
var x0 = 300, y0 = 300;
for(var i = 0;i<seriesData.length;i++){
//更新状态
ctx.beginPath();
ctx.moveTo(x0, y0);
//不同颜色
ctx.fillStyle = seriesData[i].color;
//每个角的角度
var jiaodu = seriesData[i].num * 360;
//起始角度
var startcome = temp * Math.PI /180;
//结束角度
var end = (temp + jiaodu) * Math.PI /180;
//开始画圆
ctx.arc(x0,y0,r,startcome,end);
//让每次 起始角度 = 下一个结束角度的和 就是每次画圆的时候的起始角度
temp +=jiaodu;
//填充
ctx.fill()
}
</script>
</html>