用canvas画个饼状图

<!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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值