嘗試用SVG:http://jsfiddle.net/8fsS2/4/
HTML:
<svg id="generate" version="1.1" xmlns="http://www.w3.org/2000/svg"
width="500px" height="120px" viewBox="0 0 200 120" preserveAspectRatio="none">
<g id="chart"></g>
</svg>
的Javascript:
function deg2rad(deg) {
return deg * Math.PI/180;
}
function annularSector(centerX, centerY, startAngle, endAngle, innerRadius, outerRadius) {
startAngle = deg2rad(startAngle + 180);
endAngle = deg2rad(endAngle + 180);
var p = [
[centerX + innerRadius * Math.cos(startAngle), centerY + innerRadius * Math.sin(startAngle)]
, [centerX + outerRadius * Math.cos(startAngle), centerY + outerRadius * Math.sin(startAngle)]
, [centerX + outerRadius * Math.cos(endAngle), centerY + outerRadius * Math.sin(endAngle)]
, [centerX + innerRadius * Math.cos(endAngle), centerY + innerRadius * Math.sin(endAngle)]
];
var angleDiff = endAngle - startAngle
, largeArc = (angleDiff % (Math.PI * 2)) > Math.PI ? 1 : 0;
var commands = [];
commands.push("M" + p[0].join());
commands.push("L" + p[1].join());
commands.push("A" + [outerRadius, outerRadius].join() + " 0 " + largeArc + " 1 " + p[2].join());
commands.push("L" + p[3].join());
commands.push("A" + [innerRadius, innerRadius].join() + " 0 " + largeArc + " 0 " + p[0].join());
commands.push("z");
return commands.join(" ");
}
function create(type, attr, parent) {
var element = document.createElementNS("http://www.w3.org/2000/svg", type);
if (attr) for (var name in attr) element.setAttribute(name, attr[name]);
if (parent) parent.appendChild(element);
return element;
}
var svg = document.querySelector("svg#generate g#chart");
create("path", {
fill: "#FF0000",
d: annularSector(80, 80, 0, 180, 0, 80)
}, svg);
create("path", {
fill: "#00FF00",
d: annularSector(80, 80, 0, 65, 0, 80)
}, svg);
調整縮放視框。