Skip to content

Commit

Permalink
Fix #35 - avoid divide by zero.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed May 3, 2016
1 parent 2e8f3c3 commit e44f8a4
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/treemap/dice.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default function(parent, x0, y0, x1, y1) {
node,
i = -1,
n = nodes.length,
k = (x1 - x0) / parent.value;
k = parent.value && (x1 - x0) / parent.value;

while (++i < n) {
node = nodes[i], node.y0 = y0, node.y1 = y1;
Expand Down
2 changes: 1 addition & 1 deletion src/treemap/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default function(parent, x0, y0, x1, y1) {
node,
i = -1,
n = nodes.length,
k = (y1 - y0) / parent.value;
k = parent.value && (y1 - y0) / parent.value;

while (++i < n) {
node = nodes[i], node.x0 = x0, node.x1 = x1;
Expand Down
4 changes: 2 additions & 2 deletions src/treemap/squarify.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export default (function custom(ratio) {

// Position and record the row orientation.
squarified.push(row = {value: sumValue, dice: dx < dy, children: nodes.slice(i0, i1)});
if (row.dice) treemapDice(row, x0, y0, x1, y0 += dy * sumValue / value);
else treemapSlice(row, x0, y0, x0 += dx * sumValue / value, y1);
if (row.dice) treemapDice(row, x0, y0, x1, value ? y0 += dy * sumValue / value : y1);
else treemapSlice(row, x0, y0, value ? x0 += dx * sumValue / value : x1, y1);
value -= sumValue, i0 = i1;
}
}
Expand Down
21 changes: 19 additions & 2 deletions test/treemap/dice-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var tape = require("tape"),
round = require("./round");

tape("treemapDice(parent, x0, y0, x1, y1) generates a diced layout", function(test) {
var dice = d3_hierarchy.treemapDice,
var tile = d3_hierarchy.treemapDice,
root = {
value: 24,
children: [
Expand All @@ -16,7 +16,7 @@ tape("treemapDice(parent, x0, y0, x1, y1) generates a diced layout", function(te
{value: 1}
]
};
dice(root, 0, 0, 4, 6);
tile(root, 0, 0, 4, 6);
test.deepEqual(root.children.map(round), [
{x0: 0.00, x1: 1.00, y0: 0.00, y1: 6.00},
{x0: 1.00, x1: 2.00, y0: 0.00, y1: 6.00},
Expand All @@ -28,3 +28,20 @@ tape("treemapDice(parent, x0, y0, x1, y1) generates a diced layout", function(te
]);
test.end();
});

tape("treemapDice(parent, x0, y0, x1, y1) handles a degenerate empty parent", function(test) {
var tile = d3_hierarchy.treemapDice,
root = {
value: 0,
children: [
{value: 0},
{value: 0}
]
};
tile(root, 0, 0, 0, 4);
test.deepEqual(root.children.map(round), [
{x0: 0.00, x1: 0.00, y0: 0.00, y1: 4.00},
{x0: 0.00, x1: 0.00, y0: 0.00, y1: 4.00}
]);
test.end();
});
17 changes: 17 additions & 0 deletions test/treemap/slice-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,20 @@ tape("treemapSlice(parent, x0, y0, x1, y1) generates a sliced layout", function(
]);
test.end();
});

tape("treemapSlice(parent, x0, y0, x1, y1) handles a degenerate empty parent", function(test) {
var tile = d3_hierarchy.treemapSlice,
root = {
value: 0,
children: [
{value: 0},
{value: 0}
]
};
tile(root, 0, 0, 4, 0);
test.deepEqual(root.children.map(round), [
{x0: 0.00, x1: 4.00, y0: 0.00, y1: 0.00},
{x0: 0.00, x1: 4.00, y0: 0.00, y1: 0.00}
]);
test.end();
});
34 changes: 34 additions & 0 deletions test/treemap/squarify-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,37 @@ tape("treemapSquarify.ratio(ratio) observes the specified ratio", function(test)
]);
test.end();
});

tape("treemapSquarify(parent, x0, y0, x1, y1) handles a degenerate tall empty parent", function(test) {
var tile = d3_hierarchy.treemapSquarify,
root = {
value: 0,
children: [
{value: 0},
{value: 0}
]
};
tile(root, 0, 0, 0, 4);
test.deepEqual(root.children.map(round), [
{x0: 0.00, x1: 0.00, y0: 0.00, y1: 4.00},
{x0: 0.00, x1: 0.00, y0: 0.00, y1: 4.00}
]);
test.end();
});

tape("treemapSquarify(parent, x0, y0, x1, y1) handles a degenerate wide empty parent", function(test) {
var tile = d3_hierarchy.treemapSquarify,
root = {
value: 0,
children: [
{value: 0},
{value: 0}
]
};
tile(root, 0, 0, 4, 0);
test.deepEqual(root.children.map(round), [
{x0: 0.00, x1: 4.00, y0: 0.00, y1: 0.00},
{x0: 0.00, x1: 4.00, y0: 0.00, y1: 0.00}
]);
test.end();
});

0 comments on commit e44f8a4

Please sign in to comment.