问题
I'm new to D3 and am trying to upgrade Kerryrodden's sequences sunburst with zooming and animation:
I've added the zooming opportunity with the onclick
event and fully redraw the paths:
function click(d)
{
d3.select("#container").selectAll("path").remove();
var nodes = partition.nodes(d)
.filter(function(d) {
return (d.dx > 0.005); // 0.005 radians = 0.29 degrees
}) ;
var path = vis.data([d]).selectAll("path")
.data(nodes)
.enter().append("svg:path")
.attr("display", function(d) { return d.depth ? null : "none"; })
.attr("d", arc)
.attr("fill-rule", "evenodd")
.style("fill", function(d) { return colors[d.name]; })
.style("opacity", 1)
.on("mouseover", mouseover)
.on("click", click);
// Get total size of the tree = value of root node from partition.
totalSize = path.node().__data__.value;
}
But now I have some troubles with animation. I found many versions of attrTween:
bl.ocks.org/mbostock/1306365, bl.ocks.org/mbostock/4348373),
but none of them work in my case.
Here's the my CodePen:
How can I animate the drilldown of this sunburst?
回答1:
Solution founded:
Added functions arcTween and stash for axis interpolation
function arcTween(a){
var i = d3.interpolate({x: a.x0, dx: a.dx0}, a);
return function(t) {
var b = i(t);
a.x0 = b.x;
a.dx0 = b.dx;
return arc(b);
};
};
function stash(d) {
d.x0 = 0; // d.x;
d.dx0 = 0; //d.dx;
};
and transition() property to the paths initialization:
path.each(stash)
.transition()
.duration(750)
.attrTween("d", arcTween);
Thanks All.
来源:https://stackoverflow.com/questions/22733117/d3-sequences-sunburst-animation