4

I am extending the d3 zoomable Sunburst, in particular Coffee Flavor Wheel:

enter image description here

... with a relatively big dataset (about 1200 nodes). I have added labels as such:

var textEnter = text.enter().append("text")
            .style("fill-opacity", 1)
            .attr("text-anchor", function(d) {
                return x(d.x + d.dx / 2) > Math.PI ? "end" : "start";
            })
            .attr("dy", ".2em")
            .attr("transform", function(d) {
                var angle = x(d.x + d.dx / 2) * 180 / Math.PI - 90;
                return "rotate(" + angle + ")translate(" + (y(d.y) + padding) + ")rotate(" + (angle > 90 ? -180 : 0) + ")";
            })
                .on("click", click);

        textEnter.append("tspan")
            .attr("x", 0)
            .text(function(d) { 
                return d.type != "root"  ? d.name : ""; 
            });
        // Add label or not according to its size.  
        textEnter.style("opacity", function(e) { var hgt = e.dx*Math.PI*2*y(e.y + e.dy/2); return hgt < 5 ? 0 : 1;}); 

And in the click(d) function:

   text
                .style("visibility", function(e) {
                   return isParentOf(d, e) ? null : d3.select(this).style("visibility");
                })
                .transition()
                .duration(duration)
                .attrTween("text-anchor", function(d) {
                   return function() {
                       return x(d.x + d.dx / 2) > Math.PI ? "end" : "start";
                   };
                })
                .attrTween("transform", function(d) {
                   return function() {
                       var angle = x(d.x + d.dx / 2) * 180 / Math.PI - 90;
                       return "rotate(" + angle + ")translate(" + (y(d.y) + padding) + ")rotate(" + (angle > 90 ? -180 : 0) + ")";
                   };
                })
                .style("fill-opacity", function(e) { return isParentOf(d, e) ? 1 : 1e-6; })
                .style("opacity", function(e) { var hgt = (e.dx/d.dx)*Math.PI*2*(y(e.y + e.dy/2) - y(d.y)); return hgt < 5 ? 0 : 1;})
                .each("end", function(e) {
                    d3.select(this).style("visibility", isParentOf(d, e) ? null : "hidden");
                });

The problem is that when I add this feature, the whole Sunburst loads very slowly and the click transition doesn't interchange very smoothly at all. How can I optimize this code so that it loads faster?

UPDATE:

I managed to solve the problem by changing from the Coffee Flavour Wheel to the Zoomable Sunburst with Labels example. I am not completely sure why the other one makes it faster. I suspect it has to do with attrTween and that the transition in the Zoomable Sunburst with Labels makes it more properly.

VividD
  • 10,456
  • 6
  • 64
  • 111
Perry
  • 1,709
  • 1
  • 17
  • 25

0 Answers0