diff --git a/src/layout/treemap.js b/src/layout/treemap.js index 50587a34..ec8a3e8e 100644 --- a/src/layout/treemap.js +++ b/src/layout/treemap.js @@ -1,10 +1,12 @@ // Squarified Treemaps by Mark Bruls, Kees Huizing, and Jarke J. van Wijk +// Modified to support a target aspect ratio by Jeff Heer d3.layout.treemap = function() { var hierarchy = d3.layout.hierarchy(), round = Math.round, size = [1, 1], // width, height sticky = false, - stickies; + stickies, + target = 0.5 * (1 + Math.sqrt(5)); // Recursively compute the node area based on value & scale. function scale(node, k) { @@ -85,7 +87,9 @@ d3.layout.treemap = function() { } s *= s; u *= u; - return Math.max((u * rmax) / s, s / (u * rmin)); + r = Math.abs((u * rmax) / s - target); + s = Math.abs(s / (u * rmin) - target); + return Math.max(r, s); } // Positions the specified row of nodes. Modifies `rect`. @@ -162,5 +166,11 @@ d3.layout.treemap = function() { return treemap; }; + treemap.target = function(x) { + if (!arguments.length) return target; + target = x; + return treemap; + }; + return treemap; };