萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> VML實現的餅圖JavaScript類封裝

VML實現的餅圖JavaScript類封裝

 <html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">

<head>
<title></title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="ProgId" content="VisualStudio.HTML">
<meta name="Originator" content="Microsoft Visual Studio .NET 7.1">
<STYLE>v/:* { BEHAVIOR: url(#default#VML) }
o/:* { BEHAVIOR: url(#default#VML) }
.shape { BEHAVIOR: url(#default#VML) }
</STYLE>
<script language="javascript">
function Add(){
var shape=document.createElement("v:shape");
shape.type="#tooltipshape";
shape.style.width="150px";
shape.style.height="150px";
shape.coordsize="21600,21600";
shape.fillcolor="infobackground";

var textbox=document.createElement("v:textbox");
textbox.style.border="1px solid red";
textbox.style.innerHTML="測試";
shape.appendChild(textbox);

document.body.appendChild(shape);
}

function VMLPie(pContainer,pWidth,pHeight,pCaption){
this.Container=pContainer;
this.Width= pWidth || "400px";
this.Height=pHeight || "250px";
this.Caption = pCaption || "VML Chart";
this.backgroundColor="";
this.Shadow=false;
this.BorderWidth=0;
this.BorderColor=null;
this.all=new Array();
this.RandColor=function(){

return "rgb("+ parseInt( Math.random() * 255) +"," +parseInt( Math.random() * 255) +"," +parseInt( Math.random() * 255)+")";
}
this.VMLObject=null;
}

VMLPie.prototype.Draw=function(){
//畫外框
var o=document.createElement("v:group");
o.style.width=this.Width;
o.style.height=this.Height;
o.coordsize="21600,21600";
//添加一個背景層
var vRect=document.createElement("v:rect");
vRect.style.width="21600px"
vRect.style.height="21600px"
o.appendChild(vRect);

var vCaption=document.createElement("v:textbox");
vCaption.style.fontSize="24px";
vCaption.style.height="24px"
vCaption.style.fontWeight="bold";
vCaption.innerHTML=this.Caption;
vCaption.style.textAlign="center";

vRect.appendChild(vCaption);
//設置邊框大小
if(this.BorderWidth){
vRect.strokeweight=this.BorderWidth;
}
//設置邊框顏色
if(this.BorderColor){
vRect.strokecolor=this.BorderColor;
}
//設置背景顏色
if(this.backgroundColor){
vRect.fillcolor=this.backgroundColor;
}
//設置是否出現陰影
if(this.Shadow){
var vShadow=document.createElement("v:shadow");
vShadow.on="t";
vShadow.type="single";
vShadow.color="graytext";
vShadow.offset="4px,4px";
vRect.appendChild(vShadow);
}
this.VMLObject=o;
this.Container.appendChild(o);

//開始畫內部園
var oOval=document.createElement("v:oval");
oOval.style.width="15000px";
oOval.style.height="14000px";
oOval.style.top="4000px";
oOval.style.left="1000px";
oOval.fillcolor="#d5dbfb";

//本來計劃加入3D的效果,後來感覺確實不好控制,就懶得動手了
//var o3D=document.createElement("o:extrusion");
var formatStyle=' <v:fill rotate="t" angle="-135" focus="100%" type="gradient"/>';
//formatStyle+='<o:extrusion v:ext="view" color="#9cf" on="t" rotationangle="-15"';
//formatStyle+=' viewpoint="0,34.72222mm" viewpointorigin="0,.5" skewangle="105"';
//formatStyle+=' lightposition="0,50000" lightposition2="0,-50000"/>';
formatStyle+='<o:extrusion v:ext="view" backdepth="1in" on="t" viewpoint="0,34.72222mm" viewpointorigin="0,.5" skewangle="90" lightposition="-50000" lightposition2="50000" type="perspective"/>';
oOval.innerHTML=formatStyle;

o.appendChild(oOval);
this.CreatePie(o);

}
VMLPie.prototype.CreatePie=function(vGroup){
var mX=Math.pow(2,16) * 360;
//這個參數是劃圖形的關鍵
//AE x y width height startangle endangle
//x y表示圓心位置
//width height形狀的大小
//startangle endangle的計算方法如下
// 2^16 * 度數

var vTotal=0;
var startAngle=0;
var endAngle=0;
var pieAngle=0;
var prePieAngle=0;

var objRow=null;
var objCell=null;

for(i=0;i<this.all.length;i++){
vTotal+=this.all[i].Value;
}

var objLegendRect=document.createElement("v:rect");

var objLegendTable=document.createElement("table");
objLegendTable.cellPadding=0;
objLegendTable.cellSpacing=3;
objLegendTable.width="100%";
with(objLegendRect){
style.left="17000px";
style.top="4000px";
style.width="4000px";
style.height="12000px";
fillcolor="#e6e6e6";
strokeweight="1px";
}
objRow=objLegendTable.insertRow();
objCell=objRow.insertCell();
objCell.colSpan="2";
//objCell.style.border="1px outset";
objCell.style.backgroundColor="black";
objCell.style.padding="5px";
objCell.style.color="window";
objCell.style.font="caption";
objCell.innerText="總數:"+vTotal;


var vTextbox=document.createElement("v:textbox");
vTextbox.appendChild(objLegendTable);
objLegendRect.appendChild(vTextbox);

var vShadow=document.createElement("v:shadow");
vShadow.on="t";
vShadow.type="single";
vShadow.color="graytext";
vShadow.offset="2px,2px";
objLegendRect.appendChild(vShadow);


vGroup.appendChild(objLegendRect);


var strAngle="";
for(i=0;i<this.all.length;i++){ //順序的劃出各個餅圖
var vPieEl=document.createElement("v:shape");
var vPieId=document.uniqueID;
vPieEl.style.width="15000px";
vPieEl.style.height="14000px";
vPieEl.style.top="4000px";
vPieEl.style.left="1000px";
vPieEl.coordsize="1500,1400";
vPieEl.strokecolor="white";
vPieEl.id=vPieId;

pieAngle= this.all[i].Value / vTotal;
startAngle+=prePieAngle;
prePieAngle=pieAngle;
endAngle=pieAngle;

//strAngle+=this.all[i].Name +":" +this.all[i].Value+ " Start:"+startAngle +" End:"+ endAngle +"/n";

vPieEl.path="M 750 700 AE 750 700 750 700 " + parseInt(mX * startAngle) +" " + parseInt(mX * endAngle) +" xe";
vPieEl.title=this.all[i].Name +"/n所占比例:"+ endAngle * 100 +"%/n詳細描述:" +this.all[i].TooltipText;

copyright © 萬盛學電腦網 all rights reserved