校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識(shí)庫(kù) > 利用html5 canvas動(dòng)態(tài)畫餅狀圖的示例代碼

利用html5 canvas動(dòng)態(tài)畫餅狀圖的示例代碼

熱門標(biāo)簽:ai電銷機(jī)器人連接網(wǎng)關(guān) 鶴壁手機(jī)自動(dòng)外呼系統(tǒng)怎么安裝 中紳電銷智能機(jī)器人 跟電銷機(jī)器人做同事 濟(jì)南辦理400電話 威海營(yíng)銷外呼系統(tǒng)招商 漳州人工外呼系統(tǒng)排名 農(nóng)村住宅地圖標(biāo)注 鄭州電銷外呼系統(tǒng)違法嗎

本文介紹了利用html5 canvas動(dòng)態(tài)畫餅狀圖的示例代碼,分享給大家,具體如下:

先來看效果圖

這里并沒引用jquery等第三方庫(kù),只是用dom操作和canvas的特性編寫的。

canvas畫圓大體分為實(shí)心圓和空心圓。

根據(jù)需求分析知道該圓為實(shí)心圓。

1.先用canvas畫實(shí)心圓

//偽代碼
var canvas = document.createElement("canvas");
var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(圓心x軸坐標(biāo),圓心y軸坐標(biāo),半徑,開始角,結(jié)束角);
ctx.fillStyle = 'green';
ctx.closePath();
ctx.fill();

2.根據(jù)不同顏色繪制餅狀圖

//偽代碼
var canvas = document.createElement("canvas");
var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(圓心x軸坐標(biāo),圓心y軸坐標(biāo),半徑,綠色開始角,綠色結(jié)束角);
ctx.fillStyle = 'green';
ctx.closePath();
ctx.fill();
ctx.beginPath();
ctx.arc(圓心x軸坐標(biāo),圓心y軸坐標(biāo),半徑,紅色開始角,紅色結(jié)束角);
ctx.fillStyle = 'red';
ctx.closePath();
ctx.fill();
ctx.beginPath();
ctx.arc(圓心x軸坐標(biāo),圓心y軸坐標(biāo),半徑,黃色開始角,黃色結(jié)束角);
ctx.fillStyle = 'yellow';
ctx.closePath();
ctx.fill();
ctx.beginPath();
ctx.arc(圓心x軸坐標(biāo),圓心y軸坐標(biāo),半徑,紫色開始角,紫色結(jié)束角);
ctx.fillStyle = 'purple';
ctx.closePath();
ctx.fill();

3.動(dòng)態(tài)繪制餅狀圖

動(dòng)態(tài)繪制圓網(wǎng)上普遍推薦三種方法:requestAnimationFrame、setInterval(定時(shí))、還有動(dòng)態(tài)算角度的。

這里我用的是第一種requestAnimationFrame方式。

在編寫的過程中發(fā)現(xiàn)一個(gè)問題,就是動(dòng)態(tài)畫圓時(shí)并不是以圓心的坐標(biāo)畫的。為了解決這一問題,需要在每次畫圓時(shí)重新將canvas的畫筆的坐標(biāo)定義為最初圓心的坐標(biāo)。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style>
        #graph {
/*            border: 1px solid black;
            height: 100%;
            width: 100%;
            box-sizing: border-box;*/
        }
    </style>
</head>
<body>
<div id="circle" style="width: 500px;float: left;"></div>
</body>
</html>
<script type="text/javascript">
(function(window,undefined){
    var data = [
        {"product":"產(chǎn)品1","sales":[192.44 ,210.54 ,220.84 ,230.11 ,220.85 ,210.59 ,205.49 ,200.55 ,195.71 ,187.46 ,180.66 ,170.90]},
        {"product":"產(chǎn)品2","sales":[122.41 ,133.16 ,145.65 ,158.00 ,164.84 ,178.62 ,185.70 ,190.13 ,195.53 ,198.88 ,204.32 ,210.91]},
        {"product":"產(chǎn)品3","sales":[170.30 ,175.00 ,170.79 ,165.10 ,165.62 ,160.92 ,155.92 ,145.77 ,145.17 ,140.27 ,135.99 ,130.33]},
        {"product":"產(chǎn)品4","sales":[165.64 ,170.15 ,175.10 ,185.32 ,190.90 ,190.01 ,187.05 ,183.74 ,177.24 ,181.90 ,179.54 ,175.98]}
    ]
        
    var dom_circle = document.getElementById('circle');
    if(dom_circle != undefined && dom_circle != null)
    {
        var canvas = document.createElement("canvas");
        dom_circle.appendChild(canvas);
        var ctx = canvas.getContext('2d');
        var defaultStyle = function(Dom,canvas){
            if(Dom.clientWidth <= 300)
            {
                canvas.width = 300;
                Dom.style.overflowX = "auto";
            }
            else{
                canvas.width = Dom.clientWidth;
            }
            if(Dom.clientHeight <= 300)
            {
                canvas.height = 300;
                Dom.style.overflowY = "auto";
            }
            else
            {
                canvas.height = Dom.clientHeight;
            }
            //坐標(biāo)軸區(qū)域
            //注意,實(shí)際畫折線圖區(qū)域還要比這個(gè)略小一點(diǎn)
            return {
                p1:'green',
                p2:'red',
                p3:'yellow',
                p4:'purple',
                x: 0 ,    //坐標(biāo)軸在canvas上的left坐標(biāo)
                y: 0 ,    //坐標(biāo)軸在canvas上的top坐標(biāo)
                maxX: canvas.width ,   //坐標(biāo)軸在canvas上的right坐標(biāo)
                maxY: canvas.height ,   //坐標(biāo)軸在canvas上的bottom坐標(biāo)
                r:(canvas.width)/2,  //起點(diǎn)
                ry:(canvas.height)/2,  //起點(diǎn)
                cr: (canvas.width)/4, //半徑
                startAngle:-(1/2*Math.PI),               //開始角度
                endAngle:(-(1/2*Math.PI)+2*Math.PI),     //結(jié)束角度
                xAngle:1*(Math.PI/180)                   //偏移量
            };
        }
        //畫圓
        var tmpAngle = -(1/2*Math.PI);
        var ds = null;
        var sum = data[0]['sales'][0]+data[0]['sales'][1]+data[0]['sales'][2]+data[0]['sales'][3]
        var percent1 = data[0]['sales'][0]/sum * Math.PI * 2 ;
        var percent2 = data[0]['sales'][1]/sum * Math.PI * 2 + percent1;
        var percent3 = data[0]['sales'][2]/sum * Math.PI * 2 + percent2;
        var percent4 = data[0]['sales'][3]/sum * Math.PI * 2 + percent3;
        console.log(percent1);
        console.log(percent2);
        console.log(percent3);
        console.log(percent4);
        var tmpSum = 0;
        var drawCircle = function(){
            
            
            if(tmpAngle >= ds.endAngle)
            {
                return false;
            }
            else if(tmpAngle+ ds.xAngle > ds.endAngle)
            {
                tmpAngle = ds.endAngle;
            }
            else{
                tmpAngle += ds.xAngle;
                tmpSum += ds.xAngle
            }
            // console.log(ds.startAngle+'***'+tmpAngle);
            // console.log(tmpSum);
            // ctx.clearRect(ds.x,ds.y,canvas.width,canvas.height);
            
            if(tmpSum > percent1 && tmpSum <percent2)
            {
                ctx.beginPath();
                ctx.moveTo(ds.r,ds.ry);
                ctx.arc(ds.r,ds.ry,ds.cr,ds.startAngle+percent1,tmpAngle);
            
                ctx.fillStyle = ds.p2;
            }
            else if(tmpSum > percent2 && tmpSum <percent3)
            {
                ctx.beginPath();
                ctx.moveTo(ds.r,ds.ry);
                ctx.arc(ds.r,ds.ry,ds.cr,ds.startAngle+percent2,tmpAngle);
                ctx.fillStyle = ds.p3;
            }
            else if(tmpSum > percent3 )
            {
                ctx.beginPath();
                ctx.moveTo(ds.r,ds.ry);
                ctx.arc(ds.r,ds.ry,ds.cr,ds.startAngle+percent3,tmpAngle);
                ctx.fillStyle = ds.p4;
            }
            else{
                ctx.beginPath();
                ctx.moveTo(ds.r,ds.ry);
                ctx.arc(ds.r,ds.ry,ds.cr,ds.startAngle,tmpAngle);
                ctx.fillStyle = ds.p1;
            }
            ctx.closePath();
            ctx.fill();
            requestAnimationFrame(drawCircle);
        }
        this.toDraw = function(){
            ds= defaultStyle(dom_circle,canvas);
            // console.log(tmpAngle);
            // console.log(ds.xAngle)
            ctx.clearRect(ds.x,ds.y,canvas.width,canvas.height);
            drawCircle();
        }
        this.toDraw();
        var self = this;
        window.onresize = function(){
            self.toDraw()
        }
    }

})(window);    
</script>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:咸陽 營(yíng)口 甘南 蘇州 萍鄉(xiāng) 紅河 文山 惠州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《利用html5 canvas動(dòng)態(tài)畫餅狀圖的示例代碼》,本文關(guān)鍵詞  利用,html5,canvas,動(dòng)態(tài),畫,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《利用html5 canvas動(dòng)態(tài)畫餅狀圖的示例代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于利用html5 canvas動(dòng)態(tài)畫餅狀圖的示例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 洛扎县| 仁布县| 新建县| 富源县| 锦州市| 苍南县| 桓仁| 天峻县| 清镇市| 鲁山县| 襄樊市| 万载县| 抚宁县| 江华| 衡山县| 夏邑县| 苍南县| 开封县| 和龙市| 高安市| 寻乌县| 陆良县| 防城港市| 兴安盟| 湄潭县| 神池县| 平舆县| 夏津县| 辉县市| 巨野县| 额尔古纳市| 仁化县| 大冶市| 东城区| 平顺县| 阜平县| 东源县| 鹰潭市| 开江县| 上杭县| 舞钢市|