• canvas绘制饼型图


    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <canvas id="c"></canvas>
        <script>
            var cv = document.getElementById("c");
            var ctx = cv.getContext("2d");
    
            cv.width = 600;
            cv.height = 400;
            cv.style.border = "1px solid #000";
    
            // 角度转弧度方法
            function toRadian(angle) {
                return angle / 180 * Math.PI;
            }
    
            function toAngle(radain) {
                return radian / Math.PI * 180;
            }
    
            // 数据中的value值的和 为: 1
            /*var data = [{
                "value": .1,
                "color": "orange",
                "title": "社会招生"
            },{
                "value": .1,
                "color": "pink",
                "title": "公务员"
            },{
                "value": .1,
                "color": "gray",
                "title": "公开课"
            },{
                "value": .1,
                "color": "#909090",
                "title": "前端"
            },{
                "value": .2,
                "color": "red",
                "title": "应届生"
            },{
                "value": .3,
                "color": "blue",
                "title": "程序员"
            },{
                "value": .1,
                "color": "#abc",
                "title": "老司机"
            }];*/
            var data = [{
                "value": .9,
                "color": "orange",
                "title": "社会招生"
            },{
                "value": .1,
                "color": "pink",
                "title": "公务员"
            }];
    
            var startAngle = -90,                         // 起始角度
                x0 = cv.width / 2, y0 = cv.height / 2,    // 圆心点坐标
                radius = 100,                              // 半径
                curAngle = 0,                              // 结束角度
                textX = 0, textY = 0,                    // 文字的坐标
                textOffset = 20,                        // 文字到饼型图的距离
                textWidth = 0,                            // 文字的宽度
                text2Line = 0;                            // 文字的起始位置到结束位置的长度
    
            data.forEach(function(value, index) {
                ctx.beginPath();
    
                // 1 先绘制饼型图
                curAngle = value.value * 360;
                ctx.fillStyle = value.color;
                ctx.moveTo(x0, y0);
                ctx.arc(x0, y0, radius, toRadian(startAngle), toRadian( startAngle+curAngle));
                ctx.fill();
    
                // 2 绘制指向文字的线, 不要忘了将角度转化为弧度!!!
                textX = x0 + (radius + textOffset) * Math.cos( toRadian(startAngle + curAngle / 2) );
                textY = y0 +  (radius + textOffset) * Math.sin( toRadian(startAngle + curAngle / 2) );
                ctx.strokeStyle = value.color;
                ctx.moveTo(x0, y0);
                ctx.lineTo(textX, textY);
                ctx.stroke();
    
                
                // 3 绘制文字 和 文字的底线
                // 获取文字的宽度
                textWidth = ctx.measureText(value.title).width;
                if(textX <= x0) {
                    // 设置文字的对齐方式 右对齐
                    ctx.textAlign = "right";
    
                    textOffset = -textOffset;
                    textWidth = -textWidth;
                }
                // 3.1 绘制文字
                ctx.fillText(value.title, textX + textOffset, textY - 10);
    
                // 3.2 绘制文字的底线
                // 文字底线的长度 = 文字的x坐标 + 文字到线的偏移 + 文字的宽度
                text2Line = textX + textOffset + textWidth;
                ctx.moveTo(textX, textY);
                ctx.lineTo(text2Line, textY);
                ctx.stroke();
    
    
    
                // 赋值
                startAngle += curAngle;
                // 每次执行完,需要重新初始化偏移值
                // (因为上一次有可能改变了偏移值的符号)
                textOffset = 20;
            });
        </script>
    </body>
    </html>
  • 相关阅读:
    bzoj 1444: [Jsoi2009]有趣的游戏【AC自动机+dp+高斯消元】
    bzoj 3270: 博物馆【dp+高斯消元】
    bzoj 3105: [cqoi2013]新Nim游戏【线性基+贪心】
    bzoj 1923: [Sdoi2010]外星千足虫【高斯消元】
    bzoj 3629: [JLOI2014]聪明的燕姿【线性筛+dfs】
    bzoj 1296: [SCOI2009]粉刷匠【dp+背包dp】
    bzoj 3329: Xorequ【数位dp+矩阵乘法】
    bzoj 1306: [CQOI2009]match循环赛【dfs+剪枝】
    bzoj 4720: [Noip2016]换教室【期望dp】
    bzoj 2257: [Jsoi2009]瓶子和燃料【裴蜀定理+gcd】
  • 原文地址:https://www.cnblogs.com/lsy0403/p/5898048.html
Copyright © 2020-2023  润新知