HTML5纯div饼图

这篇博客探讨了如何利用HTML5、CSS3和JavaScript实现纯div绘制饼图的技术,适用于Firefox等高版本浏览器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

浏览器饼图生成基本上都用canvas svg  既然div可以画圆 那就能做饼图 两年前做的纯技术讨论 勿拍砖

/*JS CSS3 HTML5饼图生成类->建议使用firefox高版本浏览器;

1.完全兼容firefox浏览器,部分功能支持safari、opera、chorme;完全不支持IE9以下浏览器 o(︶︿︶)o ;
2.使用方法
var xx=new PieChart(DOM对象(在什么地方绘制),绘制饼图半径单位px,json格式对象)
例:json格式对象格式 '[{"name":"测试1","value":"100","color":"red"},{"name":"测试2","value":"200","color":"green"},{"name":"测试3","value":"200","color":"blue"},{"name":"测试4","value":"900","color":"black"},{"name":"测试5","value":"200","color":"yellow"}]';
事件绑定(部分浏览器存在兼容性问题);
xx.ePie 由饼图块的dom对象组成数组;顺序12点钟开始顺时针旋转 12点开始的为第一个dom对象。
xx.ePie[0].onclick=function(){你可以执行的方法}
*/
function PieChart(tobj,wh,data)
{
this.tobj=tobj;
this.Pwh=parseFloat(wh);
this.data=data;
this.all=0;
this.ePie=new Array();
this.init();
this.drawpic();
}

PieChart.prototype.init=function(){
for (var i=0 ;i<this.data.length ;i++)
{
this.all+=parseFloat(this.data[i].value);
}
}

PieChart.prototype.drawpic=function(){
    var nowdeg=0;
    for (var i=0;i<this.data.length;i++){
     var ls=this.creartblock((parseFloat(this.data[i].value)/this.all)*360,this.data[i].color,this.data[i].name,(parseFloat(this.data[i].value)/this.all),i);
         nowdeg+=(parseFloat(this.data[i].value)/this.all)*360;
         ls.style.transform="rotate("+nowdeg+"deg)";
         ls.style.webkitTransform="rotate("+nowdeg+"deg)";
         ls.style.msTransform="rotate("+nowdeg+"deg)";
         this.tobj.appendChild(ls);
         this.ePie.push(ls);
    }
}

PieChart.prototype.creartblock=function(p,color,name,per,ni)
{
        var mobj=document.createElement("div");
            mobj.style.height=this.Pwh+"px";
            mobj.style.width=this.Pwh+"px";
            mobj.style.position="absolute";
             mobj.style.pointerEvents="none";
            mobj.setAttribute("piename",name);
            mobj.setAttribute("piecolor",color);
            mobj.setAttribute("num",ni);
            mobj.setAttribute("pieper",(""+(per*100)).substr(0,4)+"%");
            if(p>180){
            var lspie=this.creatPieDIV(0,0,color);
            mobj.appendChild(lspie);
            lspie=this.creatPieDIV(180,360-p,color);
            mobj.appendChild(lspie);
            }else{
            lspie=this.creatPieDIV(0,180-p,color);
            mobj.appendChild(lspie);
           
    }
        
    return mobj;
}

PieChart.prototype.creatPieDIV=function(deg1,deg2,color){
        var pobj=document.createElement("div");
            pobj.style.clip="rect(0px, "+this.Pwh/2+"px, "+this.Pwh+"px, 0px)";
            pobj.style.height=this.Pwh+"px";
            pobj.style.width=this.Pwh+"px";
            pobj.style.position="absolute";
            pobj.style.transform="rotate("+deg1+"deg)";
            pobj.style.webkitTransform = "rotate("+deg1+"deg)";
            pobj.style.msTransform = "rotate("+deg1+"deg)";
            pobj.style.pointerEvents="none";

        var cobj=document.createElement("div");
            cobj.style.borderRadius=this.Pwh/2+"px";
            cobj.style.clip="rect(0px, "+this.Pwh/2+"px, "+this.Pwh+"px, 0px)";
            cobj.style.height=this.Pwh+"px";
            cobj.style.width=this.Pwh+"px";
            cobj.style.position="absolute";
            cobj.style.backgroundColor=color;
            cobj.style.transform="rotate("+deg2+"deg)";
            cobj.style.webkitTransform = "rotate("+deg2+"deg)";
            cobj.style.msTransform = "rotate("+deg2+"deg)";
            cobj.style.pointerEvents="auto";
            pobj.appendChild(cobj);

return pobj;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值