Skip to content

Commit

Permalink
Fix treemap slice-dice mode.
Browse files Browse the repository at this point in the history
For even depths, it was falling back to Math.min(rect.dx, rect.dy)
instead of always "dicing" (rect.dy).
  • Loading branch information
jasondavies committed Dec 25, 2012
1 parent 74582d8 commit 963a5e8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion d3.js
Expand Up @@ -5092,7 +5092,7 @@
function squarify(node) {
var children = node.children;
if (children && children.length) {
var rect = pad(node), row = [], remaining = children.slice(), child, best = Infinity, score, u = mode === "slice" ? rect.dx : mode === "dice" || mode === "slice-dice" && node.depth & 1 ? rect.dy : Math.min(rect.dx, rect.dy), n;
var rect = pad(node), row = [], remaining = children.slice(), child, best = Infinity, score, u = mode === "slice" ? rect.dx : mode === "dice" ? rect.dy : mode === "slice-dice" ? node.depth & 1 ? rect.dy : rect.dx : Math.min(rect.dx, rect.dy), n;
scale(remaining, rect.dx * rect.dy / node.value);
row.area = 0;
while ((n = remaining.length) > 0) {
Expand Down
2 changes: 1 addition & 1 deletion d3.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/layout/treemap.js
Expand Up @@ -34,7 +34,8 @@ d3.layout.treemap = function() {
best = Infinity, // the best row score so far
score, // the current row score
u = mode === "slice" ? rect.dx
: mode === "dice" || mode === "slice-dice" && node.depth & 1 ? rect.dy
: mode === "dice" ? rect.dy
: mode === "slice-dice" ? node.depth & 1 ? rect.dy : rect.dx
: Math.min(rect.dx, rect.dy), // initial orientation
n;
scale(remaining, rect.dx * rect.dy / node.value);
Expand Down
14 changes: 14 additions & 0 deletions test/layout/treemap-test.js
Expand Up @@ -154,6 +154,20 @@ suite.addBatch({
{x: 0, y: 0, dx: 0, dy: 1},
{x: 0, y: 0, dx: 1, dy: 1}
]);
},
"slice-dice": function(treemap) {
assert.deepEqual(treemap().size([100, 10]).mode("slice-dice").nodes({children: [
{children: [{value: 1}, {value: 1}]},
{children: [{value: 1}, {value: 1}]}
]}).map(layout), [
{x: 0, y: 0, dx: 100, dy: 10},
{x: 50, y: 0, dx: 50, dy: 10},
{x: 50, y: 5, dx: 50, dy: 5},
{x: 50, y: 0, dx: 50, dy: 5},
{x: 0, y: 0, dx: 50, dy: 10},
{x: 0, y: 5, dx: 50, dy: 5},
{x: 0, y: 0, dx: 50, dy: 5}
]);
}
}
});
Expand Down

0 comments on commit 963a5e8

Please sign in to comment.