d3
Version:
A small, free JavaScript library for manipulating documents based on data.
76 lines (65 loc) • 2.22 kB
JavaScript
var w = 960,
h = 700,
r = Math.min(w, h) / 2,
color = d3.scale.category20c();
var vis = d3.select("#chart").append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(" + w / 2 + "," + h / 2 + ")");
var partition = d3.layout.partition()
.sort(null)
.size([2 * Math.PI, r * r])
.children(function(d) { return isNaN(d.value) ? d3.entries(d.value) : null; })
.value(function(d) { return 1; });
var arc = d3.svg.arc()
.startAngle(function(d) { return d.x; })
.endAngle(function(d) { return d.x + d.dx; })
.innerRadius(function(d) { return Math.sqrt(d.y); })
.outerRadius(function(d) { return Math.sqrt(d.y + d.dy); });
d3.json("flare.json", function(json) {
vis.data(d3.entries(json)).selectAll("path")
.data(partition)
.enter().append("svg:path")
.attr("display", function(d) { return d.depth ? null : "none"; }) // hide inner ring
.attr("d", arc)
.attr("stroke", "#fff")
.attr("fill", function(d) { return color((d.children ? d : d.parent).data.key); })
.attr("fill-rule", "evenodd");
d3.select("#size").on("click", function() {
vis.selectAll("path")
.data(repartition(function(d) { return d.value; }))
.transition()
.duration(1500)
.attrTween("d", arcTween);
d3.select("#size").classed("active", true);
d3.select("#count").classed("active", false);
});
d3.select("#count").on("click", function() {
vis.selectAll("path")
.data(repartition(function(d) { return 1; }))
.transition()
.duration(1500)
.attrTween("d", arcTween);
d3.select("#size").classed("active", false);
d3.select("#count").classed("active", true);
});
});
// Compute a new partition, stashing the old value for transition.
function repartition(value) {
return function(d) {
var olds = partition(d),
news = partition.value(value)(d);
news.forEach(function(d, i) {
d.prev = olds[i];
});
return news;
};
}
// Interpolate the arcs in data space.
function arcTween(a) {
var i = d3.interpolate({x: a.prev.x, dx: a.prev.dx}, a);
return function(t) {
return arc(i(t));
};
}