Search code examples
javascriptd3.jscircle-pack

Access scale factor in d3's pack layout


I'm using d3's pack layout with the default radius function. This scales the size of the circles so that everything fits into the container.

How can I access this scale factor?


Solution

  • This code will display scaling factor in node's tooltip:

    titles
        .attr("x", function(d) { return d.x; })
        .attr("y", function(d) { return d.y; })
        .text(function(d) {
            return (    "Scale factor  : " + (+d.value)/(d.r*d.r) + "\n" +
                        "Value: "  + d.value + "\n" +
                        "R: "  + d.r); 
        });
    

    as in this test example jsfiddle.

    It looks the scaling factor is more or less the same for all leaf nodes in a circle pack, as illustrated in following two pictures:

    enter image description here

    enter image description here

    However, it may vary for all other nodes!