Skip to content

Commit fcb658b

Browse files
committed
Add slice-dice, slice, and dice treemap modes.
See also: d3#653.
1 parent ecb3339 commit fcb658b

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

d3.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5082,7 +5082,7 @@
50825082
return vim._tree.ancestor.parent == node.parent ? vim._tree.ancestor : ancestor;
50835083
}
50845084
d3.layout.treemap = function() {
5085-
var hierarchy = d3.layout.hierarchy(), round = Math.round, size = [ 1, 1 ], padding = null, pad = d3_layout_treemapPadNull, sticky = false, stickies, ratio = .5 * (1 + Math.sqrt(5));
5085+
var hierarchy = d3.layout.hierarchy(), round = Math.round, size = [ 1, 1 ], padding = null, pad = d3_layout_treemapPadNull, sticky = false, stickies, mode = "squarify", ratio = .5 * (1 + Math.sqrt(5));
50865086
function scale(children, k) {
50875087
var i = -1, n = children.length, child, area;
50885088
while (++i < n) {
@@ -5093,13 +5093,13 @@
50935093
function squarify(node) {
50945094
var children = node.children;
50955095
if (children && children.length) {
5096-
var rect = pad(node), row = [], remaining = children.slice(), child, best = Infinity, score, u = Math.min(rect.dx, rect.dy), n;
5096+
var rect = pad(node), row = [], remaining = children.slice(), child, best = Infinity, score, u = mode === "squarify" ? Math.min(rect.dx, rect.dy) : mode === "dice" || mode === "slice-dice" && node.depth & 1 ? rect.dy : rect.dx, n;
50975097
scale(remaining, rect.dx * rect.dy / node.value);
50985098
row.area = 0;
50995099
while ((n = remaining.length) > 0) {
51005100
row.push(child = remaining[n - 1]);
51015101
row.area += child.area;
5102-
if ((score = worst(row, u)) <= best) {
5102+
if (mode !== "squarify" || (score = worst(row, u)) <= best) {
51035103
remaining.pop();
51045104
best = score;
51055105
} else {
@@ -5222,6 +5222,11 @@
52225222
ratio = x;
52235223
return treemap;
52245224
};
5225+
treemap.mode = function(x) {
5226+
if (!arguments.length) return mode;
5227+
mode = x + "";
5228+
return treemap;
5229+
};
52255230
return d3_layout_hierarchyRebind(treemap, hierarchy);
52265231
};
52275232
function d3_layout_treemapPadNull(node) {

0 commit comments

Comments
 (0)