From c2ccf5eb7d63b6e32fca7ecd7ddd183d4b34b877 Mon Sep 17 00:00:00 2001 From: Ernest Okot Date: Sun, 4 Jun 2017 16:50:05 +0300 Subject: [PATCH] [WIP] Plottable Rewrite Partition function that fixes https://github.com/d3/d3-hierarchy/issues/91 --- src/partition/index.js | 54 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/partition/index.js diff --git a/src/partition/index.js b/src/partition/index.js new file mode 100644 index 00000000..4b6bdfc6 --- /dev/null +++ b/src/partition/index.js @@ -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; +}