Added configurable treemap layout

This commit is contained in:
Jeffrey Heer 2011-05-14 12:38:20 -07:00
Родитель 173a936d68
Коммит 32b02b6902
1 изменённых файлов: 12 добавлений и 2 удалений

Просмотреть файл

@ -1,10 +1,12 @@
// Squarified Treemaps by Mark Bruls, Kees Huizing, and Jarke J. van Wijk // 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() { d3.layout.treemap = function() {
var hierarchy = d3.layout.hierarchy(), var hierarchy = d3.layout.hierarchy(),
round = Math.round, round = Math.round,
size = [1, 1], // width, height size = [1, 1], // width, height
sticky = false, sticky = false,
stickies; stickies,
target = 0.5 * (1 + Math.sqrt(5));
// Recursively compute the node area based on value & scale. // Recursively compute the node area based on value & scale.
function scale(node, k) { function scale(node, k) {
@ -85,7 +87,9 @@ d3.layout.treemap = function() {
} }
s *= s; s *= s;
u *= u; 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`. // Positions the specified row of nodes. Modifies `rect`.
@ -162,5 +166,11 @@ d3.layout.treemap = function() {
return treemap; return treemap;
}; };
treemap.target = function(x) {
if (!arguments.length) return target;
target = x;
return treemap;
};
return treemap; return treemap;
}; };