VML(Vector Markup Language)曾經是在 IE 中直接畫向量的便利工具。本文以圓餅圖為例,把 CreatePIE 這段關鍵程式碼逐段拆開來講。
VML 的弧線指令用的是特殊角度單位,換算方式必須先記清楚:
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, startAngle = 0, endAngle = 0, pIEAngle = 0, prePIEAngle = 0;
// 先算出所有數值的总和
for(i=0; i<this.all.length; i++){
vTotal += this.all[i].Value;
}
這裡有個容易搞錯的觀念:子元素的 left、top、width 都是相對於容器自己的座標系統,而不是實際畫面的像素大小。
// 圖表容器的 coordsize 設為 21600,21600
// 因此 objLegendGroup 的位置一律相對於這個座標系統
var objLegendGroup = document.createElement("v:group");
with(objLegendGroup){
style.left = "17000px";
style.top = "4000px";
style.width = "4000px";
style.height = 1400 * this.all.length + "px";
coordsize = "21600,21600";
}
// 圖例底板:填色 + 描邊
var objLegendRect = document.createElement("v:rect");
objLegendRect.fillcolor = "#EBF1F9";
objLegendRect.strokeweight = 1;
with(objLegendRect){
// 設為 21600×21600,確保完全覆蓋 group 的客戶區
style.width = "21600px";
style.height = "21600px";
}
// 為圖例加上陰影
var vShadow = document.createElement("v:shadow");
vShadow.on = "t";
vShadow.type = "single";
vShadow.color = "graytext";
vShadow.offset = "4px,4px";
objLegendRect.appendChild(vShadow);
objLegendGroup.appendChild(objLegendRect);
this.LegendObject = objLegendGroup;
vGroup.appendChild(objLegendGroup);
到這裡,各區域的位置配置就完成了:一個 LegendGroup 專責圖例顯示,主 v:group 作為 pie 的繪製容器。如果從可維護性角度出發,建議把 pie 的各個 shape 也收進一個獨立的 group,日後要用程式控制整體顯示或隱藏時會方便許多。
下面的迴圈是整個圓餅圖的核心。因為程式碼關鍵,除了列出來,我也把每個語句的作用標註清楚:
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.adj = "0,0";
vPIEEl.coordsize = "1500,1400";
vPIEEl.strokecolor = "white";
vPieEl.id = vPIEId;
vPIEEl.style.zIndex = 1;
// 滑鼠移入移出的互動效果
vPIEEl.onmouseOver = "HoverPIE(this)";
vPIEEl.onmouseOut = "RestorePIE(this)";
// 角度計算:占比 × 2^16 × 360
pIEAngle = this.all[i].Value / vTotal;
startAngle += prePIEAngle;
prePieAngle = pIEAngle;
endAngle = pIEAngle;
// M 為移動到圓心,AE 畫弧(含橢圓四參數與起訖角),xe 結束路徑
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;
vPIEEl._scale = parseInt(360 * (startAngle + endAngle / 2));
// 漸層填色,角度對齊扇形中線
var objFill = document.createElement("v:fill");
objFill.rotate = "t";
objFill.focus = "100%";
objFill.type = "gradient";
objFill.angle = parseInt(360 * (startAngle + endAngle / 2));
// 扇形內的文字框
var objTextbox = document.createElement("v:textbox");
objTextbox.innerHtml = this.all[i].Name + ":" + this.all[i].Value;
objTextbox.inset = "5px 5px 5px 5px";
objTextbox.style.width = "100px";
objTextbox.style.height = "20px";
var vColor = this.RandColor();
vPIEEl.fillcolor = vColor; // 套用隨機顏色
// 開始畫圖例:色塊 + 文字
var colorTip = document.createElement("v:rect");
var iHeight = parseInt(21600 / (this.all.length * 2));
var iWidth = parseInt(iHeight
* parseInt(objLegendGroup.style.height)
/ parseInt(objLegendGroup.style.width) / 1.5);
colorTip.style.height = iHeight;
colorTip.style.width = iWidth;
colorTip.style.top = iHeight * i * 2 + parseInt(iHeight / 2);
colorTip.style.left = parseInt(iWidth / 2);
colorTip.fillcolor = vColor;
colorTip.element = vPIEId; // 記住對應的扇形 id,供互動使用
colorTip.onmouseover = "LegendMouseOverEvent()";
colorTip.onmouseout = "LegendMouseOutEvent()";
var textTip = document.createElement("v:rect");
textTip.style.top = parseInt(colorTip.style.top) - 500;
textTip.style.left = iWidth * 2;
textTip.innerHtml = "<v:textbox style=\"font-size:12px;font-weight:bold\">"
+ this.all[i].Name + "(" + this.all[i].Value + ")</v:textbox>";
objLegendGroup.appendChild(colorTip);
objLegendGroup.appendChild(textTip);
vGroup.appendChild(vPIEEl);
}
把這段程式看完,可以歸納出三個重點。
第一,座標系統要分清楚。容器的 coordsize 決定了子元素的座標尺度,它和實際顯示尺寸是兩套東西,混在一起算就一定對不上。
第二,角度累加是圓餅圖的靈魂。startAngle 與 endAngle 靠 prePIEAngle 一棒接一棒往下傳,只要這個鏈條斷了,扇形就會全部疊在同一個位置。
第三,圖例與扇形要綁定。透過 colorTip.element 記住對應扇形的 uniqueID,滑鼠移到圖例時才能反向找到圖形並高亮——這一步做到了,圖表才不只是「一張圖」,而是可互動的介面元件。