Search code examples
javascriptanimationd3.jssunburst-diagram

D3 Sequences Sunburst Animation


I'm new to D3 and am trying to upgrade Kerryrodden's sequences sunburst with zooming and animation:

enter image description here

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:

enter image description here

How can I animate the drilldown of this sunburst?


Solution

  • 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.