D3的新功能。製作甜甜圈圖表,但只有一些標籤正在顯示,不知道爲什麼。標籤不在D3甜甜圈圖上顯示
http://dailyspiro.com/impact.html
var data = [
{name: "Facebook", val: 26},
{name: "Gmail", val: 19},
{name: "Twitter", val: 7},
{name: "Jane's Blog", val: 5},
{name: "Other", val: 21 }
];
var w = 400,
h = 400,
r = Math.min(w, h)/2,
labelr = r + 30, // radius for label anchor
donut = d3.layout.pie(),
arc = d3.svg.arc().innerRadius(r * .6).outerRadius(r);
var vis = d3.select(".impact-chart")
.append("svg:svg")
.data([data])
.attr("width", w + 150)
.attr("height", h);
var arcs = vis.selectAll("g.arc")
.data(donut.value(function(d) { return d.val }))
.enter().append("svg:g")
.attr("class", "arc")
.attr("transform", "translate(" + (r + 30) + "," + r + ")");
var color = d3.scale.ordinal()
.range(["#C1B398", "#605951", "#FBEEC2", "#61A6AB", "#ACCEC0", "#bbb"]);
arcs.append("svg:path")
.attr("fill", function(d, i) { return color(i); })
.attr("d", arc);
arcs.append("svg:text")
.attr("transform", function(d) {
var c = arc.centroid(d),
x = c[0],
y = c[1],
// pythagorean theorem for hypotenuse
h = Math.sqrt(x*x + y*y);
return "translate(" + (x/h * labelr) + ',' +
(y/h * labelr) + ")";
})
.attr("dy", ".35em")
.attr("text-anchor", function(d) {
// are we past the center?
return (d.endAngle + d.startAngle)/2 > Math.PI ?
"end" : "start";
})
.text(function(d, i) { return d.data.name;});
</script>
小提琴鏈接請 –
http://jsfiddle.net/qA6t6/ –