d3-hierarchy.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. // https://d3js.org/d3-hierarchy/ v1.1.9 Copyright 2019 Mike Bostock
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (global = global || self, factory(global.d3 = global.d3 || {}));
  6. }(this, function (exports) { 'use strict';
  7. function defaultSeparation(a, b) {
  8. return a.parent === b.parent ? 1 : 2;
  9. }
  10. function meanX(children) {
  11. return children.reduce(meanXReduce, 0) / children.length;
  12. }
  13. function meanXReduce(x, c) {
  14. return x + c.x;
  15. }
  16. function maxY(children) {
  17. return 1 + children.reduce(maxYReduce, 0);
  18. }
  19. function maxYReduce(y, c) {
  20. return Math.max(y, c.y);
  21. }
  22. function leafLeft(node) {
  23. var children;
  24. while (children = node.children) node = children[0];
  25. return node;
  26. }
  27. function leafRight(node) {
  28. var children;
  29. while (children = node.children) node = children[children.length - 1];
  30. return node;
  31. }
  32. function cluster() {
  33. var separation = defaultSeparation,
  34. dx = 1,
  35. dy = 1,
  36. nodeSize = false;
  37. function cluster(root) {
  38. var previousNode,
  39. x = 0;
  40. // First walk, computing the initial x & y values.
  41. root.eachAfter(function(node) {
  42. var children = node.children;
  43. if (children) {
  44. node.x = meanX(children);
  45. node.y = maxY(children);
  46. } else {
  47. node.x = previousNode ? x += separation(node, previousNode) : 0;
  48. node.y = 0;
  49. previousNode = node;
  50. }
  51. });
  52. var left = leafLeft(root),
  53. right = leafRight(root),
  54. x0 = left.x - separation(left, right) / 2,
  55. x1 = right.x + separation(right, left) / 2;
  56. // Second walk, normalizing x & y to the desired size.
  57. return root.eachAfter(nodeSize ? function(node) {
  58. node.x = (node.x - root.x) * dx;
  59. node.y = (root.y - node.y) * dy;
  60. } : function(node) {
  61. node.x = (node.x - x0) / (x1 - x0) * dx;
  62. node.y = (1 - (root.y ? node.y / root.y : 1)) * dy;
  63. });
  64. }
  65. cluster.separation = function(x) {
  66. return arguments.length ? (separation = x, cluster) : separation;
  67. };
  68. cluster.size = function(x) {
  69. return arguments.length ? (nodeSize = false, dx = +x[0], dy = +x[1], cluster) : (nodeSize ? null : [dx, dy]);
  70. };
  71. cluster.nodeSize = function(x) {
  72. return arguments.length ? (nodeSize = true, dx = +x[0], dy = +x[1], cluster) : (nodeSize ? [dx, dy] : null);
  73. };
  74. return cluster;
  75. }
  76. function count(node) {
  77. var sum = 0,
  78. children = node.children,
  79. i = children && children.length;
  80. if (!i) sum = 1;
  81. else while (--i >= 0) sum += children[i].value;
  82. node.value = sum;
  83. }
  84. function node_count() {
  85. return this.eachAfter(count);
  86. }
  87. function node_each(callback) {
  88. var node = this, current, next = [node], children, i, n;
  89. do {
  90. current = next.reverse(), next = [];
  91. while (node = current.pop()) {
  92. callback(node), children = node.children;
  93. if (children) for (i = 0, n = children.length; i < n; ++i) {
  94. next.push(children[i]);
  95. }
  96. }
  97. } while (next.length);
  98. return this;
  99. }
  100. function node_eachBefore(callback) {
  101. var node = this, nodes = [node], children, i;
  102. while (node = nodes.pop()) {
  103. callback(node), children = node.children;
  104. if (children) for (i = children.length - 1; i >= 0; --i) {
  105. nodes.push(children[i]);
  106. }
  107. }
  108. return this;
  109. }
  110. function node_eachAfter(callback) {
  111. var node = this, nodes = [node], next = [], children, i, n;
  112. while (node = nodes.pop()) {
  113. next.push(node), children = node.children;
  114. if (children) for (i = 0, n = children.length; i < n; ++i) {
  115. nodes.push(children[i]);
  116. }
  117. }
  118. while (node = next.pop()) {
  119. callback(node);
  120. }
  121. return this;
  122. }
  123. function node_sum(value) {
  124. return this.eachAfter(function(node) {
  125. var sum = +value(node.data) || 0,
  126. children = node.children,
  127. i = children && children.length;
  128. while (--i >= 0) sum += children[i].value;
  129. node.value = sum;
  130. });
  131. }
  132. function node_sort(compare) {
  133. return this.eachBefore(function(node) {
  134. if (node.children) {
  135. node.children.sort(compare);
  136. }
  137. });
  138. }
  139. function node_path(end) {
  140. var start = this,
  141. ancestor = leastCommonAncestor(start, end),
  142. nodes = [start];
  143. while (start !== ancestor) {
  144. start = start.parent;
  145. nodes.push(start);
  146. }
  147. var k = nodes.length;
  148. while (end !== ancestor) {
  149. nodes.splice(k, 0, end);
  150. end = end.parent;
  151. }
  152. return nodes;
  153. }
  154. function leastCommonAncestor(a, b) {
  155. if (a === b) return a;
  156. var aNodes = a.ancestors(),
  157. bNodes = b.ancestors(),
  158. c = null;
  159. a = aNodes.pop();
  160. b = bNodes.pop();
  161. while (a === b) {
  162. c = a;
  163. a = aNodes.pop();
  164. b = bNodes.pop();
  165. }
  166. return c;
  167. }
  168. function node_ancestors() {
  169. var node = this, nodes = [node];
  170. while (node = node.parent) {
  171. nodes.push(node);
  172. }
  173. return nodes;
  174. }
  175. function node_descendants() {
  176. var nodes = [];
  177. this.each(function(node) {
  178. nodes.push(node);
  179. });
  180. return nodes;
  181. }
  182. function node_leaves() {
  183. var leaves = [];
  184. this.eachBefore(function(node) {
  185. if (!node.children) {
  186. leaves.push(node);
  187. }
  188. });
  189. return leaves;
  190. }
  191. function node_links() {
  192. var root = this, links = [];
  193. root.each(function(node) {
  194. if (node !== root) { // Don’t include the root’s parent, if any.
  195. links.push({source: node.parent, target: node});
  196. }
  197. });
  198. return links;
  199. }
  200. function hierarchy(data, children) {
  201. var root = new Node(data),
  202. valued = +data.value && (root.value = data.value),
  203. node,
  204. nodes = [root],
  205. child,
  206. childs,
  207. i,
  208. n;
  209. if (children == null) children = defaultChildren;
  210. while (node = nodes.pop()) {
  211. if (valued) node.value = +node.data.value;
  212. if ((childs = children(node.data)) && (n = childs.length)) {
  213. node.children = new Array(n);
  214. for (i = n - 1; i >= 0; --i) {
  215. nodes.push(child = node.children[i] = new Node(childs[i]));
  216. child.parent = node;
  217. child.depth = node.depth + 1;
  218. }
  219. }
  220. }
  221. return root.eachBefore(computeHeight);
  222. }
  223. function node_copy() {
  224. return hierarchy(this).eachBefore(copyData);
  225. }
  226. function defaultChildren(d) {
  227. return d.children;
  228. }
  229. function copyData(node) {
  230. node.data = node.data.data;
  231. }
  232. function computeHeight(node) {
  233. var height = 0;
  234. do node.height = height;
  235. while ((node = node.parent) && (node.height < ++height));
  236. }
  237. function Node(data) {
  238. this.data = data;
  239. this.depth =
  240. this.height = 0;
  241. this.parent = null;
  242. }
  243. Node.prototype = hierarchy.prototype = {
  244. constructor: Node,
  245. count: node_count,
  246. each: node_each,
  247. eachAfter: node_eachAfter,
  248. eachBefore: node_eachBefore,
  249. sum: node_sum,
  250. sort: node_sort,
  251. path: node_path,
  252. ancestors: node_ancestors,
  253. descendants: node_descendants,
  254. leaves: node_leaves,
  255. links: node_links,
  256. copy: node_copy
  257. };
  258. var slice = Array.prototype.slice;
  259. function shuffle(array) {
  260. var m = array.length,
  261. t,
  262. i;
  263. while (m) {
  264. i = Math.random() * m-- | 0;
  265. t = array[m];
  266. array[m] = array[i];
  267. array[i] = t;
  268. }
  269. return array;
  270. }
  271. function enclose(circles) {
  272. var i = 0, n = (circles = shuffle(slice.call(circles))).length, B = [], p, e;
  273. while (i < n) {
  274. p = circles[i];
  275. if (e && enclosesWeak(e, p)) ++i;
  276. else e = encloseBasis(B = extendBasis(B, p)), i = 0;
  277. }
  278. return e;
  279. }
  280. function extendBasis(B, p) {
  281. var i, j;
  282. if (enclosesWeakAll(p, B)) return [p];
  283. // If we get here then B must have at least one element.
  284. for (i = 0; i < B.length; ++i) {
  285. if (enclosesNot(p, B[i])
  286. && enclosesWeakAll(encloseBasis2(B[i], p), B)) {
  287. return [B[i], p];
  288. }
  289. }
  290. // If we get here then B must have at least two elements.
  291. for (i = 0; i < B.length - 1; ++i) {
  292. for (j = i + 1; j < B.length; ++j) {
  293. if (enclosesNot(encloseBasis2(B[i], B[j]), p)
  294. && enclosesNot(encloseBasis2(B[i], p), B[j])
  295. && enclosesNot(encloseBasis2(B[j], p), B[i])
  296. && enclosesWeakAll(encloseBasis3(B[i], B[j], p), B)) {
  297. return [B[i], B[j], p];
  298. }
  299. }
  300. }
  301. // If we get here then something is very wrong.
  302. throw new Error;
  303. }
  304. function enclosesNot(a, b) {
  305. var dr = a.r - b.r, dx = b.x - a.x, dy = b.y - a.y;
  306. return dr < 0 || dr * dr < dx * dx + dy * dy;
  307. }
  308. function enclosesWeak(a, b) {
  309. var dr = a.r - b.r + 1e-6, dx = b.x - a.x, dy = b.y - a.y;
  310. return dr > 0 && dr * dr > dx * dx + dy * dy;
  311. }
  312. function enclosesWeakAll(a, B) {
  313. for (var i = 0; i < B.length; ++i) {
  314. if (!enclosesWeak(a, B[i])) {
  315. return false;
  316. }
  317. }
  318. return true;
  319. }
  320. function encloseBasis(B) {
  321. switch (B.length) {
  322. case 1: return encloseBasis1(B[0]);
  323. case 2: return encloseBasis2(B[0], B[1]);
  324. case 3: return encloseBasis3(B[0], B[1], B[2]);
  325. }
  326. }
  327. function encloseBasis1(a) {
  328. return {
  329. x: a.x,
  330. y: a.y,
  331. r: a.r
  332. };
  333. }
  334. function encloseBasis2(a, b) {
  335. var x1 = a.x, y1 = a.y, r1 = a.r,
  336. x2 = b.x, y2 = b.y, r2 = b.r,
  337. x21 = x2 - x1, y21 = y2 - y1, r21 = r2 - r1,
  338. l = Math.sqrt(x21 * x21 + y21 * y21);
  339. return {
  340. x: (x1 + x2 + x21 / l * r21) / 2,
  341. y: (y1 + y2 + y21 / l * r21) / 2,
  342. r: (l + r1 + r2) / 2
  343. };
  344. }
  345. function encloseBasis3(a, b, c) {
  346. var x1 = a.x, y1 = a.y, r1 = a.r,
  347. x2 = b.x, y2 = b.y, r2 = b.r,
  348. x3 = c.x, y3 = c.y, r3 = c.r,
  349. a2 = x1 - x2,
  350. a3 = x1 - x3,
  351. b2 = y1 - y2,
  352. b3 = y1 - y3,
  353. c2 = r2 - r1,
  354. c3 = r3 - r1,
  355. d1 = x1 * x1 + y1 * y1 - r1 * r1,
  356. d2 = d1 - x2 * x2 - y2 * y2 + r2 * r2,
  357. d3 = d1 - x3 * x3 - y3 * y3 + r3 * r3,
  358. ab = a3 * b2 - a2 * b3,
  359. xa = (b2 * d3 - b3 * d2) / (ab * 2) - x1,
  360. xb = (b3 * c2 - b2 * c3) / ab,
  361. ya = (a3 * d2 - a2 * d3) / (ab * 2) - y1,
  362. yb = (a2 * c3 - a3 * c2) / ab,
  363. A = xb * xb + yb * yb - 1,
  364. B = 2 * (r1 + xa * xb + ya * yb),
  365. C = xa * xa + ya * ya - r1 * r1,
  366. r = -(A ? (B + Math.sqrt(B * B - 4 * A * C)) / (2 * A) : C / B);
  367. return {
  368. x: x1 + xa + xb * r,
  369. y: y1 + ya + yb * r,
  370. r: r
  371. };
  372. }
  373. function place(b, a, c) {
  374. var dx = b.x - a.x, x, a2,
  375. dy = b.y - a.y, y, b2,
  376. d2 = dx * dx + dy * dy;
  377. if (d2) {
  378. a2 = a.r + c.r, a2 *= a2;
  379. b2 = b.r + c.r, b2 *= b2;
  380. if (a2 > b2) {
  381. x = (d2 + b2 - a2) / (2 * d2);
  382. y = Math.sqrt(Math.max(0, b2 / d2 - x * x));
  383. c.x = b.x - x * dx - y * dy;
  384. c.y = b.y - x * dy + y * dx;
  385. } else {
  386. x = (d2 + a2 - b2) / (2 * d2);
  387. y = Math.sqrt(Math.max(0, a2 / d2 - x * x));
  388. c.x = a.x + x * dx - y * dy;
  389. c.y = a.y + x * dy + y * dx;
  390. }
  391. } else {
  392. c.x = a.x + c.r;
  393. c.y = a.y;
  394. }
  395. }
  396. function intersects(a, b) {
  397. var dr = a.r + b.r - 1e-6, dx = b.x - a.x, dy = b.y - a.y;
  398. return dr > 0 && dr * dr > dx * dx + dy * dy;
  399. }
  400. function score(node) {
  401. var a = node._,
  402. b = node.next._,
  403. ab = a.r + b.r,
  404. dx = (a.x * b.r + b.x * a.r) / ab,
  405. dy = (a.y * b.r + b.y * a.r) / ab;
  406. return dx * dx + dy * dy;
  407. }
  408. function Node$1(circle) {
  409. this._ = circle;
  410. this.next = null;
  411. this.previous = null;
  412. }
  413. function packEnclose(circles) {
  414. if (!(n = circles.length)) return 0;
  415. var a, b, c, n, aa, ca, i, j, k, sj, sk;
  416. // Place the first circle.
  417. a = circles[0], a.x = 0, a.y = 0;
  418. if (!(n > 1)) return a.r;
  419. // Place the second circle.
  420. b = circles[1], a.x = -b.r, b.x = a.r, b.y = 0;
  421. if (!(n > 2)) return a.r + b.r;
  422. // Place the third circle.
  423. place(b, a, c = circles[2]);
  424. // Initialize the front-chain using the first three circles a, b and c.
  425. a = new Node$1(a), b = new Node$1(b), c = new Node$1(c);
  426. a.next = c.previous = b;
  427. b.next = a.previous = c;
  428. c.next = b.previous = a;
  429. // Attempt to place each remaining circle…
  430. pack: for (i = 3; i < n; ++i) {
  431. place(a._, b._, c = circles[i]), c = new Node$1(c);
  432. // Find the closest intersecting circle on the front-chain, if any.
  433. // “Closeness” is determined by linear distance along the front-chain.
  434. // “Ahead” or “behind” is likewise determined by linear distance.
  435. j = b.next, k = a.previous, sj = b._.r, sk = a._.r;
  436. do {
  437. if (sj <= sk) {
  438. if (intersects(j._, c._)) {
  439. b = j, a.next = b, b.previous = a, --i;
  440. continue pack;
  441. }
  442. sj += j._.r, j = j.next;
  443. } else {
  444. if (intersects(k._, c._)) {
  445. a = k, a.next = b, b.previous = a, --i;
  446. continue pack;
  447. }
  448. sk += k._.r, k = k.previous;
  449. }
  450. } while (j !== k.next);
  451. // Success! Insert the new circle c between a and b.
  452. c.previous = a, c.next = b, a.next = b.previous = b = c;
  453. // Compute the new closest circle pair to the centroid.
  454. aa = score(a);
  455. while ((c = c.next) !== b) {
  456. if ((ca = score(c)) < aa) {
  457. a = c, aa = ca;
  458. }
  459. }
  460. b = a.next;
  461. }
  462. // Compute the enclosing circle of the front chain.
  463. a = [b._], c = b; while ((c = c.next) !== b) a.push(c._); c = enclose(a);
  464. // Translate the circles to put the enclosing circle around the origin.
  465. for (i = 0; i < n; ++i) a = circles[i], a.x -= c.x, a.y -= c.y;
  466. return c.r;
  467. }
  468. function siblings(circles) {
  469. packEnclose(circles);
  470. return circles;
  471. }
  472. function optional(f) {
  473. return f == null ? null : required(f);
  474. }
  475. function required(f) {
  476. if (typeof f !== "function") throw new Error;
  477. return f;
  478. }
  479. function constantZero() {
  480. return 0;
  481. }
  482. function constant(x) {
  483. return function() {
  484. return x;
  485. };
  486. }
  487. function defaultRadius(d) {
  488. return Math.sqrt(d.value);
  489. }
  490. function index() {
  491. var radius = null,
  492. dx = 1,
  493. dy = 1,
  494. padding = constantZero;
  495. function pack(root) {
  496. root.x = dx / 2, root.y = dy / 2;
  497. if (radius) {
  498. root.eachBefore(radiusLeaf(radius))
  499. .eachAfter(packChildren(padding, 0.5))
  500. .eachBefore(translateChild(1));
  501. } else {
  502. root.eachBefore(radiusLeaf(defaultRadius))
  503. .eachAfter(packChildren(constantZero, 1))
  504. .eachAfter(packChildren(padding, root.r / Math.min(dx, dy)))
  505. .eachBefore(translateChild(Math.min(dx, dy) / (2 * root.r)));
  506. }
  507. return root;
  508. }
  509. pack.radius = function(x) {
  510. return arguments.length ? (radius = optional(x), pack) : radius;
  511. };
  512. pack.size = function(x) {
  513. return arguments.length ? (dx = +x[0], dy = +x[1], pack) : [dx, dy];
  514. };
  515. pack.padding = function(x) {
  516. return arguments.length ? (padding = typeof x === "function" ? x : constant(+x), pack) : padding;
  517. };
  518. return pack;
  519. }
  520. function radiusLeaf(radius) {
  521. return function(node) {
  522. if (!node.children) {
  523. node.r = Math.max(0, +radius(node) || 0);
  524. }
  525. };
  526. }
  527. function packChildren(padding, k) {
  528. return function(node) {
  529. if (children = node.children) {
  530. var children,
  531. i,
  532. n = children.length,
  533. r = padding(node) * k || 0,
  534. e;
  535. if (r) for (i = 0; i < n; ++i) children[i].r += r;
  536. e = packEnclose(children);
  537. if (r) for (i = 0; i < n; ++i) children[i].r -= r;
  538. node.r = e + r;
  539. }
  540. };
  541. }
  542. function translateChild(k) {
  543. return function(node) {
  544. var parent = node.parent;
  545. node.r *= k;
  546. if (parent) {
  547. node.x = parent.x + k * node.x;
  548. node.y = parent.y + k * node.y;
  549. }
  550. };
  551. }
  552. function roundNode(node) {
  553. node.x0 = Math.round(node.x0);
  554. node.y0 = Math.round(node.y0);
  555. node.x1 = Math.round(node.x1);
  556. node.y1 = Math.round(node.y1);
  557. }
  558. function treemapDice(parent, x0, y0, x1, y1) {
  559. var nodes = parent.children,
  560. node,
  561. i = -1,
  562. n = nodes.length,
  563. k = parent.value && (x1 - x0) / parent.value;
  564. while (++i < n) {
  565. node = nodes[i], node.y0 = y0, node.y1 = y1;
  566. node.x0 = x0, node.x1 = x0 += node.value * k;
  567. }
  568. }
  569. function partition() {
  570. var dx = 1,
  571. dy = 1,
  572. padding = 0,
  573. round = false;
  574. function partition(root) {
  575. var n = root.height + 1;
  576. root.x0 =
  577. root.y0 = padding;
  578. root.x1 = dx;
  579. root.y1 = dy / n;
  580. root.eachBefore(positionNode(dy, n));
  581. if (round) root.eachBefore(roundNode);
  582. return root;
  583. }
  584. function positionNode(dy, n) {
  585. return function(node) {
  586. if (node.children) {
  587. treemapDice(node, node.x0, dy * (node.depth + 1) / n, node.x1, dy * (node.depth + 2) / n);
  588. }
  589. var x0 = node.x0,
  590. y0 = node.y0,
  591. x1 = node.x1 - padding,
  592. y1 = node.y1 - padding;
  593. if (x1 < x0) x0 = x1 = (x0 + x1) / 2;
  594. if (y1 < y0) y0 = y1 = (y0 + y1) / 2;
  595. node.x0 = x0;
  596. node.y0 = y0;
  597. node.x1 = x1;
  598. node.y1 = y1;
  599. };
  600. }
  601. partition.round = function(x) {
  602. return arguments.length ? (round = !!x, partition) : round;
  603. };
  604. partition.size = function(x) {
  605. return arguments.length ? (dx = +x[0], dy = +x[1], partition) : [dx, dy];
  606. };
  607. partition.padding = function(x) {
  608. return arguments.length ? (padding = +x, partition) : padding;
  609. };
  610. return partition;
  611. }
  612. var keyPrefix = "$", // Protect against keys like “__proto__”.
  613. preroot = {depth: -1},
  614. ambiguous = {};
  615. function defaultId(d) {
  616. return d.id;
  617. }
  618. function defaultParentId(d) {
  619. return d.parentId;
  620. }
  621. function stratify() {
  622. var id = defaultId,
  623. parentId = defaultParentId;
  624. function stratify(data) {
  625. var d,
  626. i,
  627. n = data.length,
  628. root,
  629. parent,
  630. node,
  631. nodes = new Array(n),
  632. nodeId,
  633. nodeKey,
  634. nodeByKey = {};
  635. for (i = 0; i < n; ++i) {
  636. d = data[i], node = nodes[i] = new Node(d);
  637. if ((nodeId = id(d, i, data)) != null && (nodeId += "")) {
  638. nodeKey = keyPrefix + (node.id = nodeId);
  639. nodeByKey[nodeKey] = nodeKey in nodeByKey ? ambiguous : node;
  640. }
  641. }
  642. for (i = 0; i < n; ++i) {
  643. node = nodes[i], nodeId = parentId(data[i], i, data);
  644. if (nodeId == null || !(nodeId += "")) {
  645. if (root) throw new Error("multiple roots");
  646. root = node;
  647. } else {
  648. parent = nodeByKey[keyPrefix + nodeId];
  649. if (!parent) throw new Error("missing: " + nodeId);
  650. if (parent === ambiguous) throw new Error("ambiguous: " + nodeId);
  651. if (parent.children) parent.children.push(node);
  652. else parent.children = [node];
  653. node.parent = parent;
  654. }
  655. }
  656. if (!root) throw new Error("no root");
  657. root.parent = preroot;
  658. root.eachBefore(function(node) { node.depth = node.parent.depth + 1; --n; }).eachBefore(computeHeight);
  659. root.parent = null;
  660. if (n > 0) throw new Error("cycle");
  661. return root;
  662. }
  663. stratify.id = function(x) {
  664. return arguments.length ? (id = required(x), stratify) : id;
  665. };
  666. stratify.parentId = function(x) {
  667. return arguments.length ? (parentId = required(x), stratify) : parentId;
  668. };
  669. return stratify;
  670. }
  671. function defaultSeparation$1(a, b) {
  672. return a.parent === b.parent ? 1 : 2;
  673. }
  674. // function radialSeparation(a, b) {
  675. // return (a.parent === b.parent ? 1 : 2) / a.depth;
  676. // }
  677. // This function is used to traverse the left contour of a subtree (or
  678. // subforest). It returns the successor of v on this contour. This successor is
  679. // either given by the leftmost child of v or by the thread of v. The function
  680. // returns null if and only if v is on the highest level of its subtree.
  681. function nextLeft(v) {
  682. var children = v.children;
  683. return children ? children[0] : v.t;
  684. }
  685. // This function works analogously to nextLeft.
  686. function nextRight(v) {
  687. var children = v.children;
  688. return children ? children[children.length - 1] : v.t;
  689. }
  690. // Shifts the current subtree rooted at w+. This is done by increasing
  691. // prelim(w+) and mod(w+) by shift.
  692. function moveSubtree(wm, wp, shift) {
  693. var change = shift / (wp.i - wm.i);
  694. wp.c -= change;
  695. wp.s += shift;
  696. wm.c += change;
  697. wp.z += shift;
  698. wp.m += shift;
  699. }
  700. // All other shifts, applied to the smaller subtrees between w- and w+, are
  701. // performed by this function. To prepare the shifts, we have to adjust
  702. // change(w+), shift(w+), and change(w-).
  703. function executeShifts(v) {
  704. var shift = 0,
  705. change = 0,
  706. children = v.children,
  707. i = children.length,
  708. w;
  709. while (--i >= 0) {
  710. w = children[i];
  711. w.z += shift;
  712. w.m += shift;
  713. shift += w.s + (change += w.c);
  714. }
  715. }
  716. // If vi-’s ancestor is a sibling of v, returns vi-’s ancestor. Otherwise,
  717. // returns the specified (default) ancestor.
  718. function nextAncestor(vim, v, ancestor) {
  719. return vim.a.parent === v.parent ? vim.a : ancestor;
  720. }
  721. function TreeNode(node, i) {
  722. this._ = node;
  723. this.parent = null;
  724. this.children = null;
  725. this.A = null; // default ancestor
  726. this.a = this; // ancestor
  727. this.z = 0; // prelim
  728. this.m = 0; // mod
  729. this.c = 0; // change
  730. this.s = 0; // shift
  731. this.t = null; // thread
  732. this.i = i; // number
  733. }
  734. TreeNode.prototype = Object.create(Node.prototype);
  735. function treeRoot(root) {
  736. var tree = new TreeNode(root, 0),
  737. node,
  738. nodes = [tree],
  739. child,
  740. children,
  741. i,
  742. n;
  743. while (node = nodes.pop()) {
  744. if (children = node._.children) {
  745. node.children = new Array(n = children.length);
  746. for (i = n - 1; i >= 0; --i) {
  747. nodes.push(child = node.children[i] = new TreeNode(children[i], i));
  748. child.parent = node;
  749. }
  750. }
  751. }
  752. (tree.parent = new TreeNode(null, 0)).children = [tree];
  753. return tree;
  754. }
  755. // Node-link tree diagram using the Reingold-Tilford "tidy" algorithm
  756. function tree() {
  757. var separation = defaultSeparation$1,
  758. dx = 1,
  759. dy = 1,
  760. nodeSize = null;
  761. function tree(root) {
  762. var t = treeRoot(root);
  763. // Compute the layout using Buchheim et al.’s algorithm.
  764. t.eachAfter(firstWalk), t.parent.m = -t.z;
  765. t.eachBefore(secondWalk);
  766. // If a fixed node size is specified, scale x and y.
  767. if (nodeSize) root.eachBefore(sizeNode);
  768. // If a fixed tree size is specified, scale x and y based on the extent.
  769. // Compute the left-most, right-most, and depth-most nodes for extents.
  770. else {
  771. var left = root,
  772. right = root,
  773. bottom = root;
  774. root.eachBefore(function(node) {
  775. if (node.x < left.x) left = node;
  776. if (node.x > right.x) right = node;
  777. if (node.depth > bottom.depth) bottom = node;
  778. });
  779. var s = left === right ? 1 : separation(left, right) / 2,
  780. tx = s - left.x,
  781. kx = dx / (right.x + s + tx),
  782. ky = dy / (bottom.depth || 1);
  783. root.eachBefore(function(node) {
  784. node.x = (node.x + tx) * kx;
  785. node.y = node.depth * ky;
  786. });
  787. }
  788. return root;
  789. }
  790. // Computes a preliminary x-coordinate for v. Before that, FIRST WALK is
  791. // applied recursively to the children of v, as well as the function
  792. // APPORTION. After spacing out the children by calling EXECUTE SHIFTS, the
  793. // node v is placed to the midpoint of its outermost children.
  794. function firstWalk(v) {
  795. var children = v.children,
  796. siblings = v.parent.children,
  797. w = v.i ? siblings[v.i - 1] : null;
  798. if (children) {
  799. executeShifts(v);
  800. var midpoint = (children[0].z + children[children.length - 1].z) / 2;
  801. if (w) {
  802. v.z = w.z + separation(v._, w._);
  803. v.m = v.z - midpoint;
  804. } else {
  805. v.z = midpoint;
  806. }
  807. } else if (w) {
  808. v.z = w.z + separation(v._, w._);
  809. }
  810. v.parent.A = apportion(v, w, v.parent.A || siblings[0]);
  811. }
  812. // Computes all real x-coordinates by summing up the modifiers recursively.
  813. function secondWalk(v) {
  814. v._.x = v.z + v.parent.m;
  815. v.m += v.parent.m;
  816. }
  817. // The core of the algorithm. Here, a new subtree is combined with the
  818. // previous subtrees. Threads are used to traverse the inside and outside
  819. // contours of the left and right subtree up to the highest common level. The
  820. // vertices used for the traversals are vi+, vi-, vo-, and vo+, where the
  821. // superscript o means outside and i means inside, the subscript - means left
  822. // subtree and + means right subtree. For summing up the modifiers along the
  823. // contour, we use respective variables si+, si-, so-, and so+. Whenever two
  824. // nodes of the inside contours conflict, we compute the left one of the
  825. // greatest uncommon ancestors using the function ANCESTOR and call MOVE
  826. // SUBTREE to shift the subtree and prepare the shifts of smaller subtrees.
  827. // Finally, we add a new thread (if necessary).
  828. function apportion(v, w, ancestor) {
  829. if (w) {
  830. var vip = v,
  831. vop = v,
  832. vim = w,
  833. vom = vip.parent.children[0],
  834. sip = vip.m,
  835. sop = vop.m,
  836. sim = vim.m,
  837. som = vom.m,
  838. shift;
  839. while (vim = nextRight(vim), vip = nextLeft(vip), vim && vip) {
  840. vom = nextLeft(vom);
  841. vop = nextRight(vop);
  842. vop.a = v;
  843. shift = vim.z + sim - vip.z - sip + separation(vim._, vip._);
  844. if (shift > 0) {
  845. moveSubtree(nextAncestor(vim, v, ancestor), v, shift);
  846. sip += shift;
  847. sop += shift;
  848. }
  849. sim += vim.m;
  850. sip += vip.m;
  851. som += vom.m;
  852. sop += vop.m;
  853. }
  854. if (vim && !nextRight(vop)) {
  855. vop.t = vim;
  856. vop.m += sim - sop;
  857. }
  858. if (vip && !nextLeft(vom)) {
  859. vom.t = vip;
  860. vom.m += sip - som;
  861. ancestor = v;
  862. }
  863. }
  864. return ancestor;
  865. }
  866. function sizeNode(node) {
  867. node.x *= dx;
  868. node.y = node.depth * dy;
  869. }
  870. tree.separation = function(x) {
  871. return arguments.length ? (separation = x, tree) : separation;
  872. };
  873. tree.size = function(x) {
  874. return arguments.length ? (nodeSize = false, dx = +x[0], dy = +x[1], tree) : (nodeSize ? null : [dx, dy]);
  875. };
  876. tree.nodeSize = function(x) {
  877. return arguments.length ? (nodeSize = true, dx = +x[0], dy = +x[1], tree) : (nodeSize ? [dx, dy] : null);
  878. };
  879. return tree;
  880. }
  881. function treemapSlice(parent, x0, y0, x1, y1) {
  882. var nodes = parent.children,
  883. node,
  884. i = -1,
  885. n = nodes.length,
  886. k = parent.value && (y1 - y0) / parent.value;
  887. while (++i < n) {
  888. node = nodes[i], node.x0 = x0, node.x1 = x1;
  889. node.y0 = y0, node.y1 = y0 += node.value * k;
  890. }
  891. }
  892. var phi = (1 + Math.sqrt(5)) / 2;
  893. function squarifyRatio(ratio, parent, x0, y0, x1, y1) {
  894. var rows = [],
  895. nodes = parent.children,
  896. row,
  897. nodeValue,
  898. i0 = 0,
  899. i1 = 0,
  900. n = nodes.length,
  901. dx, dy,
  902. value = parent.value,
  903. sumValue,
  904. minValue,
  905. maxValue,
  906. newRatio,
  907. minRatio,
  908. alpha,
  909. beta;
  910. while (i0 < n) {
  911. dx = x1 - x0, dy = y1 - y0;
  912. // Find the next non-empty node.
  913. do sumValue = nodes[i1++].value; while (!sumValue && i1 < n);
  914. minValue = maxValue = sumValue;
  915. alpha = Math.max(dy / dx, dx / dy) / (value * ratio);
  916. beta = sumValue * sumValue * alpha;
  917. minRatio = Math.max(maxValue / beta, beta / minValue);
  918. // Keep adding nodes while the aspect ratio maintains or improves.
  919. for (; i1 < n; ++i1) {
  920. sumValue += nodeValue = nodes[i1].value;
  921. if (nodeValue < minValue) minValue = nodeValue;
  922. if (nodeValue > maxValue) maxValue = nodeValue;
  923. beta = sumValue * sumValue * alpha;
  924. newRatio = Math.max(maxValue / beta, beta / minValue);
  925. if (newRatio > minRatio) { sumValue -= nodeValue; break; }
  926. minRatio = newRatio;
  927. }
  928. // Position and record the row orientation.
  929. rows.push(row = {value: sumValue, dice: dx < dy, children: nodes.slice(i0, i1)});
  930. if (row.dice) treemapDice(row, x0, y0, x1, value ? y0 += dy * sumValue / value : y1);
  931. else treemapSlice(row, x0, y0, value ? x0 += dx * sumValue / value : x1, y1);
  932. value -= sumValue, i0 = i1;
  933. }
  934. return rows;
  935. }
  936. var squarify = (function custom(ratio) {
  937. function squarify(parent, x0, y0, x1, y1) {
  938. squarifyRatio(ratio, parent, x0, y0, x1, y1);
  939. }
  940. squarify.ratio = function(x) {
  941. return custom((x = +x) > 1 ? x : 1);
  942. };
  943. return squarify;
  944. })(phi);
  945. function index$1() {
  946. var tile = squarify,
  947. round = false,
  948. dx = 1,
  949. dy = 1,
  950. paddingStack = [0],
  951. paddingInner = constantZero,
  952. paddingTop = constantZero,
  953. paddingRight = constantZero,
  954. paddingBottom = constantZero,
  955. paddingLeft = constantZero;
  956. function treemap(root) {
  957. root.x0 =
  958. root.y0 = 0;
  959. root.x1 = dx;
  960. root.y1 = dy;
  961. root.eachBefore(positionNode);
  962. paddingStack = [0];
  963. if (round) root.eachBefore(roundNode);
  964. return root;
  965. }
  966. function positionNode(node) {
  967. var p = paddingStack[node.depth],
  968. x0 = node.x0 + p,
  969. y0 = node.y0 + p,
  970. x1 = node.x1 - p,
  971. y1 = node.y1 - p;
  972. if (x1 < x0) x0 = x1 = (x0 + x1) / 2;
  973. if (y1 < y0) y0 = y1 = (y0 + y1) / 2;
  974. node.x0 = x0;
  975. node.y0 = y0;
  976. node.x1 = x1;
  977. node.y1 = y1;
  978. if (node.children) {
  979. p = paddingStack[node.depth + 1] = paddingInner(node) / 2;
  980. x0 += paddingLeft(node) - p;
  981. y0 += paddingTop(node) - p;
  982. x1 -= paddingRight(node) - p;
  983. y1 -= paddingBottom(node) - p;
  984. if (x1 < x0) x0 = x1 = (x0 + x1) / 2;
  985. if (y1 < y0) y0 = y1 = (y0 + y1) / 2;
  986. tile(node, x0, y0, x1, y1);
  987. }
  988. }
  989. treemap.round = function(x) {
  990. return arguments.length ? (round = !!x, treemap) : round;
  991. };
  992. treemap.size = function(x) {
  993. return arguments.length ? (dx = +x[0], dy = +x[1], treemap) : [dx, dy];
  994. };
  995. treemap.tile = function(x) {
  996. return arguments.length ? (tile = required(x), treemap) : tile;
  997. };
  998. treemap.padding = function(x) {
  999. return arguments.length ? treemap.paddingInner(x).paddingOuter(x) : treemap.paddingInner();
  1000. };
  1001. treemap.paddingInner = function(x) {
  1002. return arguments.length ? (paddingInner = typeof x === "function" ? x : constant(+x), treemap) : paddingInner;
  1003. };
  1004. treemap.paddingOuter = function(x) {
  1005. return arguments.length ? treemap.paddingTop(x).paddingRight(x).paddingBottom(x).paddingLeft(x) : treemap.paddingTop();
  1006. };
  1007. treemap.paddingTop = function(x) {
  1008. return arguments.length ? (paddingTop = typeof x === "function" ? x : constant(+x), treemap) : paddingTop;
  1009. };
  1010. treemap.paddingRight = function(x) {
  1011. return arguments.length ? (paddingRight = typeof x === "function" ? x : constant(+x), treemap) : paddingRight;
  1012. };
  1013. treemap.paddingBottom = function(x) {
  1014. return arguments.length ? (paddingBottom = typeof x === "function" ? x : constant(+x), treemap) : paddingBottom;
  1015. };
  1016. treemap.paddingLeft = function(x) {
  1017. return arguments.length ? (paddingLeft = typeof x === "function" ? x : constant(+x), treemap) : paddingLeft;
  1018. };
  1019. return treemap;
  1020. }
  1021. function binary(parent, x0, y0, x1, y1) {
  1022. var nodes = parent.children,
  1023. i, n = nodes.length,
  1024. sum, sums = new Array(n + 1);
  1025. for (sums[0] = sum = i = 0; i < n; ++i) {
  1026. sums[i + 1] = sum += nodes[i].value;
  1027. }
  1028. partition(0, n, parent.value, x0, y0, x1, y1);
  1029. function partition(i, j, value, x0, y0, x1, y1) {
  1030. if (i >= j - 1) {
  1031. var node = nodes[i];
  1032. node.x0 = x0, node.y0 = y0;
  1033. node.x1 = x1, node.y1 = y1;
  1034. return;
  1035. }
  1036. var valueOffset = sums[i],
  1037. valueTarget = (value / 2) + valueOffset,
  1038. k = i + 1,
  1039. hi = j - 1;
  1040. while (k < hi) {
  1041. var mid = k + hi >>> 1;
  1042. if (sums[mid] < valueTarget) k = mid + 1;
  1043. else hi = mid;
  1044. }
  1045. if ((valueTarget - sums[k - 1]) < (sums[k] - valueTarget) && i + 1 < k) --k;
  1046. var valueLeft = sums[k] - valueOffset,
  1047. valueRight = value - valueLeft;
  1048. if ((x1 - x0) > (y1 - y0)) {
  1049. var xk = (x0 * valueRight + x1 * valueLeft) / value;
  1050. partition(i, k, valueLeft, x0, y0, xk, y1);
  1051. partition(k, j, valueRight, xk, y0, x1, y1);
  1052. } else {
  1053. var yk = (y0 * valueRight + y1 * valueLeft) / value;
  1054. partition(i, k, valueLeft, x0, y0, x1, yk);
  1055. partition(k, j, valueRight, x0, yk, x1, y1);
  1056. }
  1057. }
  1058. }
  1059. function sliceDice(parent, x0, y0, x1, y1) {
  1060. (parent.depth & 1 ? treemapSlice : treemapDice)(parent, x0, y0, x1, y1);
  1061. }
  1062. var resquarify = (function custom(ratio) {
  1063. function resquarify(parent, x0, y0, x1, y1) {
  1064. if ((rows = parent._squarify) && (rows.ratio === ratio)) {
  1065. var rows,
  1066. row,
  1067. nodes,
  1068. i,
  1069. j = -1,
  1070. n,
  1071. m = rows.length,
  1072. value = parent.value;
  1073. while (++j < m) {
  1074. row = rows[j], nodes = row.children;
  1075. for (i = row.value = 0, n = nodes.length; i < n; ++i) row.value += nodes[i].value;
  1076. if (row.dice) treemapDice(row, x0, y0, x1, y0 += (y1 - y0) * row.value / value);
  1077. else treemapSlice(row, x0, y0, x0 += (x1 - x0) * row.value / value, y1);
  1078. value -= row.value;
  1079. }
  1080. } else {
  1081. parent._squarify = rows = squarifyRatio(ratio, parent, x0, y0, x1, y1);
  1082. rows.ratio = ratio;
  1083. }
  1084. }
  1085. resquarify.ratio = function(x) {
  1086. return custom((x = +x) > 1 ? x : 1);
  1087. };
  1088. return resquarify;
  1089. })(phi);
  1090. exports.cluster = cluster;
  1091. exports.hierarchy = hierarchy;
  1092. exports.pack = index;
  1093. exports.packEnclose = enclose;
  1094. exports.packSiblings = siblings;
  1095. exports.partition = partition;
  1096. exports.stratify = stratify;
  1097. exports.tree = tree;
  1098. exports.treemap = index$1;
  1099. exports.treemapBinary = binary;
  1100. exports.treemapDice = treemapDice;
  1101. exports.treemapResquarify = resquarify;
  1102. exports.treemapSlice = treemapSlice;
  1103. exports.treemapSliceDice = sliceDice;
  1104. exports.treemapSquarify = squarify;
  1105. Object.defineProperty(exports, '__esModule', { value: true });
  1106. }));