要在網頁上畫圖表,早期沒有 Canvas 可用的時候,工程師得靠 VML(Vector Markup Language)這種 XML 格式的向量描述語言,手工組出 pie chart 的每一片扇形路徑。這份範例完整保留了那個時代的做法,也示範了如何用 JavaScript 動態產生表格型長條圖。
<HTML>
<head>
<title>JS 百分比圖和柱狀圖</title>
<xml:namespace prefix="v"/>
<style>
v\:* { behavior: url(#default#VML) }
</style>
<style>
h2 { color:#006600; margin-top:0pt; margin-bottom:0pt }
p.Chart { font-size:8pt; font-family:"Times"; color:black; text-align:right; }
</style>
</head>
<BODY text="#000000" bgcolor="#FFFFFF">
<div style='margin-top:12pt; position:relative;'>
<v:group style='height:324pt;width:432pt' coordsize="4320,3240">
<!-- 紙張:白底加上簡單的陰影 -->
<v:rect style='width:4320;height:3240' fillcolor="white">
<v:shadow on="true" offset="4pt,3pt" color="silver" />
</v:rect>
<v:rect style='position:absolute;left:40;top:40;width:4240;height:3160;'
fillcolor="#FFFFFF" strokeweight=1.5pt />
<p class=Chart style='position:absolute;left:123.1pt;top:17.8pt;
width:189.8pt;height:12.6pt;color:"#000080";font-style:italic;
font-size:18pt;text-align:center;'>Sample Piechart</p>
<p class=Chart style='position:absolute;left:225.5pt;top:310.4pt;
width:198.5pt;height:5.6pt;color:"#008000";font-family:"Arial";'>
Random numbers drawn as a simple pie</p>
<!-- 第一片:One: 30 -->
<v:shape style='position:absolute;width:4320;height:3240'
strokeweight=0.5pt fillcolor="#DBDBED"
path="M 2250 1590 AE 2250 1590 791 791 -3538944 9437184 X E" />
<v:shape style='position:absolute;width:4320;height:3240'
strokecolor="#000000" strokeweight=0.7pt>
<v:stroke joinstyle=round endcap=round />
<v:fill on="false" />
<v:path v=" M 3115 1309 L 2927 1370 E "/>
</v:shape>
<p class=Chart style='position:absolute;left:315.9pt;top:127.3pt;
width:39.3pt;height:5.6pt;text-align:left;'>One: 30</p>
<!-- 第二片:Two: 18 -->
<v:shape style='position:absolute;width:4320;height:3240'
strokeweight=0.5pt fillcolor="#AA5555"
path="M 2245 1629 AE 2245 1629 791 791 -9201254 5662310 X E" />
<!-- ... 其餘扇形與標籤省略 ... -->
</v:group>
</div>
</BODY>
</HTML>
VML 的 path 語法中,M 是 move to、L 是 line to、AE 是 elliptical arc(橢圓弧)、X 關閉路徑、E 結束。每片扇形都要手算圓心、半徑、起始角與結束角,再換成 VML 座標系(這裡是 4320×3240)的絕對數值——那些 -3538944 9437184 就是角度轉換後的結果,完全沒有可讀性可言。
相較之下,長條圖用表格就能做,簡單得多:
<script language=JavaScript>
function displayTitle(title) {
document.write("<center><i>" + title + "</i></center><br>");
}
function generateRandomNumber(num) {
return Math.round(Math.random() * (num - 1)) + 1;
}
function plottablehead(val) {
document.writeln("<table border=0 bgcolor=black width=10 height=10" +
" cellpadding=0 cellspacing=0>");
document.write("<tr><i>" + val + "</i>");
}
function plottabletail() {
document.write("</tr></table><br>");
}
// 畫 d 格寬、指定顏色的長條
function plotcolor(d, clr) {
for (var i = 1; i <= d; i++){
document.write("<td bgcolor = " + clr + "> </td>");
}
}
function setColor(foreground, background) {
document.fgColor = foreground;
document.bgColor = background;
}
setColor("orange", "black");
// 產生五組隨機長條
var colors = ["red", "blue", "green", "yellow", "gray", "midnightblue"];
for (var c = 0; c < colors.length; c++){
var a = generateRandomNumber(50);
plottablehead(a);
plotcolor(a, colors[c]);
plottabletail();
}
</script>
這裡的巧思是用一格格 10px 的 td 當像素:長度 30 就輸出 30 個 <td>,用儲存格的數量來表示數值大小。概念上很直觀,缺點是 DOM 節點數量會爆炸,50 格就是 50 個 td。
VML 不能用。它是 Microsoft 的專利格式,只有 IE 5.5 到 IE 9 支援,IE10 之後移除。那段 behavior: url(#default#VML) 的 CSS 在非 IE 瀏覽器完全無效。
document.write 不建議。它會在解析階段直接寫入文件流,若在頁面載入完成後呼叫會清空整個頁面。動態產生內容應改用 createElement 或 innerHTML。
document.bgColor / fgColor 已棄用,請改用 CSS。
表格當長條圖在小量資料時還能看,但正式使用請改用 div 加寬度百分比,或直接用 SVG。
今天的圖表方案非常成熟,依需求選一個就好:
<!-- 用純 CSS 做長條圖 -->
<style>
.bar{ height:20px; background:#4a90d9; }
</style>
<div class="bar" style="width: 60%"></div>
<!-- 用 SVG 畫圓餅圖 -->
<svg viewBox="0 0 42 42">
<circle cx="21" cy="21" r="15.9" fill="transparent"
stroke="#dbdbed" stroke-width="8" />
<circle cx="21" cy="21" r="15.9" fill="transparent"
stroke="#4a90d9" stroke-width="8"
stroke-dasharray="30 70" stroke-dashoffset="25" />
</svg>
那個 SVG 圓餅技巧很值得記住:用 stroke-dasharray 控制「填色段 / 空白段」的比例,stroke-dashoffset 控制起始角度,兩個圓疊起來就是漂亮的甜甜圈圖,完全不需要算三角函數。
需要完整的圖表功能(互動提示、動畫、圖例、響應式),請直接用 Chart.js 或 ECharts;需要高客製化的資料視覺化則用 D3.js。自己算扇形路徑的時代已經遠去,但理解背後的座標與比例換算原理,對使用這些函式庫還是有幫助的。