| 1 | import roundNode from "./round.js";
|
|---|
| 2 | import squarify from "./squarify.js";
|
|---|
| 3 | import {required} from "../accessors.js";
|
|---|
| 4 | import constant, {constantZero} from "../constant.js";
|
|---|
| 5 |
|
|---|
| 6 | export default function() {
|
|---|
| 7 | var tile = squarify,
|
|---|
| 8 | round = false,
|
|---|
| 9 | dx = 1,
|
|---|
| 10 | dy = 1,
|
|---|
| 11 | paddingStack = [0],
|
|---|
| 12 | paddingInner = constantZero,
|
|---|
| 13 | paddingTop = constantZero,
|
|---|
| 14 | paddingRight = constantZero,
|
|---|
| 15 | paddingBottom = constantZero,
|
|---|
| 16 | paddingLeft = constantZero;
|
|---|
| 17 |
|
|---|
| 18 | function treemap(root) {
|
|---|
| 19 | root.x0 =
|
|---|
| 20 | root.y0 = 0;
|
|---|
| 21 | root.x1 = dx;
|
|---|
| 22 | root.y1 = dy;
|
|---|
| 23 | root.eachBefore(positionNode);
|
|---|
| 24 | paddingStack = [0];
|
|---|
| 25 | if (round) root.eachBefore(roundNode);
|
|---|
| 26 | return root;
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | function positionNode(node) {
|
|---|
| 30 | var p = paddingStack[node.depth],
|
|---|
| 31 | x0 = node.x0 + p,
|
|---|
| 32 | y0 = node.y0 + p,
|
|---|
| 33 | x1 = node.x1 - p,
|
|---|
| 34 | y1 = node.y1 - p;
|
|---|
| 35 | if (x1 < x0) x0 = x1 = (x0 + x1) / 2;
|
|---|
| 36 | if (y1 < y0) y0 = y1 = (y0 + y1) / 2;
|
|---|
| 37 | node.x0 = x0;
|
|---|
| 38 | node.y0 = y0;
|
|---|
| 39 | node.x1 = x1;
|
|---|
| 40 | node.y1 = y1;
|
|---|
| 41 | if (node.children) {
|
|---|
| 42 | p = paddingStack[node.depth + 1] = paddingInner(node) / 2;
|
|---|
| 43 | x0 += paddingLeft(node) - p;
|
|---|
| 44 | y0 += paddingTop(node) - p;
|
|---|
| 45 | x1 -= paddingRight(node) - p;
|
|---|
| 46 | y1 -= paddingBottom(node) - p;
|
|---|
| 47 | if (x1 < x0) x0 = x1 = (x0 + x1) / 2;
|
|---|
| 48 | if (y1 < y0) y0 = y1 = (y0 + y1) / 2;
|
|---|
| 49 | tile(node, x0, y0, x1, y1);
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | treemap.round = function(x) {
|
|---|
| 54 | return arguments.length ? (round = !!x, treemap) : round;
|
|---|
| 55 | };
|
|---|
| 56 |
|
|---|
| 57 | treemap.size = function(x) {
|
|---|
| 58 | return arguments.length ? (dx = +x[0], dy = +x[1], treemap) : [dx, dy];
|
|---|
| 59 | };
|
|---|
| 60 |
|
|---|
| 61 | treemap.tile = function(x) {
|
|---|
| 62 | return arguments.length ? (tile = required(x), treemap) : tile;
|
|---|
| 63 | };
|
|---|
| 64 |
|
|---|
| 65 | treemap.padding = function(x) {
|
|---|
| 66 | return arguments.length ? treemap.paddingInner(x).paddingOuter(x) : treemap.paddingInner();
|
|---|
| 67 | };
|
|---|
| 68 |
|
|---|
| 69 | treemap.paddingInner = function(x) {
|
|---|
| 70 | return arguments.length ? (paddingInner = typeof x === "function" ? x : constant(+x), treemap) : paddingInner;
|
|---|
| 71 | };
|
|---|
| 72 |
|
|---|
| 73 | treemap.paddingOuter = function(x) {
|
|---|
| 74 | return arguments.length ? treemap.paddingTop(x).paddingRight(x).paddingBottom(x).paddingLeft(x) : treemap.paddingTop();
|
|---|
| 75 | };
|
|---|
| 76 |
|
|---|
| 77 | treemap.paddingTop = function(x) {
|
|---|
| 78 | return arguments.length ? (paddingTop = typeof x === "function" ? x : constant(+x), treemap) : paddingTop;
|
|---|
| 79 | };
|
|---|
| 80 |
|
|---|
| 81 | treemap.paddingRight = function(x) {
|
|---|
| 82 | return arguments.length ? (paddingRight = typeof x === "function" ? x : constant(+x), treemap) : paddingRight;
|
|---|
| 83 | };
|
|---|
| 84 |
|
|---|
| 85 | treemap.paddingBottom = function(x) {
|
|---|
| 86 | return arguments.length ? (paddingBottom = typeof x === "function" ? x : constant(+x), treemap) : paddingBottom;
|
|---|
| 87 | };
|
|---|
| 88 |
|
|---|
| 89 | treemap.paddingLeft = function(x) {
|
|---|
| 90 | return arguments.length ? (paddingLeft = typeof x === "function" ? x : constant(+x), treemap) : paddingLeft;
|
|---|
| 91 | };
|
|---|
| 92 |
|
|---|
| 93 | return treemap;
|
|---|
| 94 | }
|
|---|