d3-hierarchy.js 33 KB

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