Skip to content

Commit

Permalink
[WIP] Plottable Rewrite
Browse files Browse the repository at this point in the history
Partition function that fixes d3/d3-hierarchy#91
  • Loading branch information
ernest-okot committed Jun 4, 2017
1 parent ff254df commit c2ccf5e
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/partition/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import roundNode from "d3-hierarchy/src/treemap/round";
import treemapDice from "d3-hierarchy/src/treemap/dice";

// Fixes https://github.com/d3/d3-hierarchy/issues/91
export default function() {
var dx = 1,
dy = 1,
padding = 0,
round = false,
rootDepth = null;

function partition(root) {
var n = root.height + 1;
rootDepth = root.depth;
root.x0 = root.y0 = padding;
root.x1 = dx;
root.y1 = dy / n;
root.eachBefore(positionNode(dy, n));
if (round) root.eachBefore(roundNode);
return root;
}

function positionNode(dy, n) {
return function(node) {
if (node.children) {
treemapDice(node, node.x0, dy * (node.depth - rootDepth + 1) / n, node.x1, dy * (node.depth - rootDepth + 2) / n);
}
var x0 = node.x0,
y0 = node.y0,
x1 = node.x1 - padding,
y1 = node.y1 - padding;
if (x1 < x0) x0 = x1 = (x0 + x1) / 2;
if (y1 < y0) y0 = y1 = (y0 + y1) / 2;
node.x0 = x0;
node.y0 = y0;
node.x1 = x1;
node.y1 = y1;
};
}

partition.round = function(x) {
return arguments.length ? (round = !!x, partition) : round;
};

partition.size = function(x) {
return arguments.length ? (dx = +x[0], dy = +x[1], partition) : [dx, dy];
};

partition.padding = function(x) {
return arguments.length ? (padding = +x, partition) : padding;
};

return partition;
}

0 comments on commit c2ccf5e

Please sign in to comment.