sankey.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Sankey = void 0;
  4. const d3_array_1 = require("d3-array");
  5. const align_1 = require("./align");
  6. const constant_1 = require("./constant");
  7. function ascendingSourceBreadth(a, b) {
  8. return ascendingBreadth(a.source, b.source) || a.index - b.index;
  9. }
  10. function ascendingTargetBreadth(a, b) {
  11. return ascendingBreadth(a.target, b.target) || a.index - b.index;
  12. }
  13. function ascendingBreadth(a, b) {
  14. return a.y0 - b.y0;
  15. }
  16. function value(d) {
  17. return d.value;
  18. }
  19. function defaultId(d) {
  20. return d.index;
  21. }
  22. function defaultNodes(graph) {
  23. return graph.nodes;
  24. }
  25. function defaultLinks(graph) {
  26. return graph.links;
  27. }
  28. function find(nodeById, id) {
  29. const node = nodeById.get(id);
  30. if (!node)
  31. throw new Error('missing: ' + id);
  32. return node;
  33. }
  34. function computeLinkBreadths({ nodes }) {
  35. for (const node of nodes) {
  36. let y0 = node.y0;
  37. let y1 = y0;
  38. for (const link of node.sourceLinks) {
  39. link.y0 = y0 + link.width / 2;
  40. y0 += link.width;
  41. }
  42. for (const link of node.targetLinks) {
  43. link.y1 = y1 + link.width / 2;
  44. y1 += link.width;
  45. }
  46. }
  47. }
  48. function Sankey() {
  49. let x0 = 0, y0 = 0, x1 = 1, y1 = 1; // extent
  50. let dx = 24; // nodeWidth
  51. let dy = 8, py; // nodePadding
  52. let id = defaultId;
  53. let align = align_1.justify;
  54. let depth;
  55. let sort;
  56. let linkSort;
  57. let nodes = defaultNodes;
  58. let links = defaultLinks;
  59. let iterations = 6;
  60. function sankey(arg) {
  61. const graph = {
  62. nodes: nodes(arg),
  63. links: links(arg),
  64. };
  65. computeNodeLinks(graph);
  66. computeNodeValues(graph);
  67. computeNodeDepths(graph);
  68. computeNodeHeights(graph);
  69. computeNodeBreadths(graph);
  70. computeLinkBreadths(graph);
  71. return graph;
  72. }
  73. sankey.update = function (graph) {
  74. computeLinkBreadths(graph);
  75. return graph;
  76. };
  77. sankey.nodeId = function (_) {
  78. return arguments.length
  79. ? ((id = typeof _ === 'function' ? _ : (0, constant_1.constant)(_)), sankey)
  80. : id;
  81. };
  82. sankey.nodeAlign = function (_) {
  83. return arguments.length
  84. ? ((align = typeof _ === 'function' ? _ : (0, constant_1.constant)(_)), sankey)
  85. : align;
  86. };
  87. sankey.nodeDepth = function (_) {
  88. return arguments.length
  89. ? ((depth = typeof _ === 'function' ? _ : _), sankey)
  90. : depth;
  91. };
  92. sankey.nodeSort = function (_) {
  93. return arguments.length ? ((sort = _), sankey) : sort;
  94. };
  95. sankey.nodeWidth = function (_) {
  96. return arguments.length ? ((dx = +_), sankey) : dx;
  97. };
  98. sankey.nodePadding = function (_) {
  99. return arguments.length ? ((dy = py = +_), sankey) : dy;
  100. };
  101. sankey.nodes = function (_) {
  102. return arguments.length
  103. ? ((nodes = typeof _ === 'function' ? _ : (0, constant_1.constant)(_)), sankey)
  104. : nodes;
  105. };
  106. sankey.links = function (_) {
  107. return arguments.length
  108. ? ((links = typeof _ === 'function' ? _ : (0, constant_1.constant)(_)), sankey)
  109. : links;
  110. };
  111. sankey.linkSort = function (_) {
  112. return arguments.length ? ((linkSort = _), sankey) : linkSort;
  113. };
  114. sankey.size = function (_) {
  115. return arguments.length
  116. ? ((x0 = y0 = 0), (x1 = +_[0]), (y1 = +_[1]), sankey)
  117. : [x1 - x0, y1 - y0];
  118. };
  119. sankey.extent = function (_) {
  120. return arguments.length
  121. ? ((x0 = +_[0][0]),
  122. (x1 = +_[1][0]),
  123. (y0 = +_[0][1]),
  124. (y1 = +_[1][1]),
  125. sankey)
  126. : [
  127. [x0, y0],
  128. [x1, y1],
  129. ];
  130. };
  131. sankey.iterations = function (_) {
  132. return arguments.length ? ((iterations = +_), sankey) : iterations;
  133. };
  134. function computeNodeLinks({ nodes, links }) {
  135. nodes.forEach((node, idx) => {
  136. node.index = idx;
  137. node.sourceLinks = [];
  138. node.targetLinks = [];
  139. });
  140. const nodeById = new Map(nodes.map((d) => [id(d), d]));
  141. links.forEach((link, idx) => {
  142. link.index = idx;
  143. let { source, target } = link;
  144. if (typeof source !== 'object')
  145. source = link.source = find(nodeById, source);
  146. if (typeof target !== 'object')
  147. target = link.target = find(nodeById, target);
  148. source.sourceLinks.push(link);
  149. target.targetLinks.push(link);
  150. });
  151. if (linkSort != null) {
  152. for (const { sourceLinks, targetLinks } of nodes) {
  153. sourceLinks.sort(linkSort);
  154. targetLinks.sort(linkSort);
  155. }
  156. }
  157. }
  158. function computeNodeValues({ nodes }) {
  159. for (const node of nodes) {
  160. node.value =
  161. node.fixedValue === undefined
  162. ? Math.max((0, d3_array_1.sum)(node.sourceLinks, value), (0, d3_array_1.sum)(node.targetLinks, value))
  163. : node.fixedValue;
  164. }
  165. }
  166. function computeNodeDepths({ nodes }) {
  167. const n = nodes.length;
  168. let current = new Set(nodes);
  169. let next = new Set();
  170. let x = 0;
  171. while (current.size) {
  172. current.forEach((node) => {
  173. node.depth = x;
  174. for (const { target } of node.sourceLinks) {
  175. next.add(target);
  176. }
  177. });
  178. if (++x > n)
  179. throw new Error('circular link');
  180. current = next;
  181. next = new Set();
  182. }
  183. // 如果配置了 depth,则设置自定义 depth
  184. if (depth) {
  185. const maxDepth = Math.max((0, d3_array_1.max)(nodes, (d) => d.depth) + 1, 0);
  186. let node;
  187. for (let i = 0; i < nodes.length; i++) {
  188. node = nodes[i];
  189. node.depth = depth.call(null, node, maxDepth);
  190. }
  191. }
  192. }
  193. function computeNodeHeights({ nodes }) {
  194. const n = nodes.length;
  195. let current = new Set(nodes);
  196. let next = new Set();
  197. let x = 0;
  198. while (current.size) {
  199. current.forEach((node) => {
  200. node.height = x;
  201. for (const { source } of node.targetLinks) {
  202. next.add(source);
  203. }
  204. });
  205. if (++x > n)
  206. throw new Error('circular link');
  207. current = next;
  208. next = new Set();
  209. }
  210. }
  211. function computeNodeLayers({ nodes }) {
  212. const x = Math.max((0, d3_array_1.max)(nodes, (d) => d.depth) + 1, 0);
  213. const kx = (x1 - x0 - dx) / (x - 1);
  214. const columns = new Array(x).fill(0).map(() => []);
  215. for (const node of nodes) {
  216. const i = Math.max(0, Math.min(x - 1, Math.floor(align.call(null, node, x))));
  217. node.layer = i;
  218. node.x0 = x0 + i * kx;
  219. node.x1 = node.x0 + dx;
  220. if (columns[i])
  221. columns[i].push(node);
  222. else
  223. columns[i] = [node];
  224. }
  225. if (sort)
  226. for (const column of columns) {
  227. column.sort(sort);
  228. }
  229. return columns;
  230. }
  231. function initializeNodeBreadths(columns) {
  232. const ky = (0, d3_array_1.min)(columns, (c) => (y1 - y0 - (c.length - 1) * py) / (0, d3_array_1.sum)(c, value));
  233. for (const nodes of columns) {
  234. let y = y0;
  235. for (const node of nodes) {
  236. node.y0 = y;
  237. node.y1 = y + node.value * ky;
  238. y = node.y1 + py;
  239. for (const link of node.sourceLinks) {
  240. link.width = link.value * ky;
  241. }
  242. }
  243. y = (y1 - y + py) / (nodes.length + 1);
  244. for (let i = 0; i < nodes.length; ++i) {
  245. const node = nodes[i];
  246. node.y0 += y * (i + 1);
  247. node.y1 += y * (i + 1);
  248. }
  249. reorderLinks(nodes);
  250. }
  251. }
  252. function computeNodeBreadths(graph) {
  253. const columns = computeNodeLayers(graph);
  254. py = Math.min(dy, (y1 - y0) / ((0, d3_array_1.max)(columns, (c) => c.length) - 1));
  255. initializeNodeBreadths(columns);
  256. for (let i = 0; i < iterations; ++i) {
  257. const alpha = Math.pow(0.99, i);
  258. const beta = Math.max(1 - alpha, (i + 1) / iterations);
  259. relaxRightToLeft(columns, alpha, beta);
  260. relaxLeftToRight(columns, alpha, beta);
  261. }
  262. }
  263. // Reposition each node based on its incoming (target) links.
  264. function relaxLeftToRight(columns, alpha, beta) {
  265. for (let i = 1, n = columns.length; i < n; ++i) {
  266. const column = columns[i];
  267. for (const target of column) {
  268. let y = 0;
  269. let w = 0;
  270. for (const { source, value } of target.targetLinks) {
  271. const v = value * (target.layer - source.layer);
  272. y += targetTop(source, target) * v;
  273. w += v;
  274. }
  275. if (!(w > 0))
  276. continue;
  277. const dy = (y / w - target.y0) * alpha;
  278. target.y0 += dy;
  279. target.y1 += dy;
  280. reorderNodeLinks(target);
  281. }
  282. if (sort === undefined)
  283. column.sort(ascendingBreadth);
  284. if (column.length)
  285. resolveCollisions(column, beta);
  286. }
  287. }
  288. // Reposition each node based on its outgoing (source) links.
  289. function relaxRightToLeft(columns, alpha, beta) {
  290. for (let n = columns.length, i = n - 2; i >= 0; --i) {
  291. const column = columns[i];
  292. for (const source of column) {
  293. let y = 0;
  294. let w = 0;
  295. for (const { target, value } of source.sourceLinks) {
  296. const v = value * (target.layer - source.layer);
  297. y += sourceTop(source, target) * v;
  298. w += v;
  299. }
  300. if (!(w > 0))
  301. continue;
  302. const dy = (y / w - source.y0) * alpha;
  303. source.y0 += dy;
  304. source.y1 += dy;
  305. reorderNodeLinks(source);
  306. }
  307. if (sort === undefined)
  308. column.sort(ascendingBreadth);
  309. if (column.length)
  310. resolveCollisions(column, beta);
  311. }
  312. }
  313. function resolveCollisions(nodes, alpha) {
  314. const i = nodes.length >> 1;
  315. const subject = nodes[i];
  316. resolveCollisionsBottomToTop(nodes, subject.y0 - py, i - 1, alpha);
  317. resolveCollisionsTopToBottom(nodes, subject.y1 + py, i + 1, alpha);
  318. resolveCollisionsBottomToTop(nodes, y1, nodes.length - 1, alpha);
  319. resolveCollisionsTopToBottom(nodes, y0, 0, alpha);
  320. }
  321. // Push any overlapping nodes down.
  322. function resolveCollisionsTopToBottom(nodes, y, i, alpha) {
  323. for (; i < nodes.length; ++i) {
  324. const node = nodes[i];
  325. const dy = (y - node.y0) * alpha;
  326. if (dy > 1e-6)
  327. (node.y0 += dy), (node.y1 += dy);
  328. y = node.y1 + py;
  329. }
  330. }
  331. // Push any overlapping nodes up.
  332. function resolveCollisionsBottomToTop(nodes, y, i, alpha) {
  333. for (; i >= 0; --i) {
  334. const node = nodes[i];
  335. const dy = (node.y1 - y) * alpha;
  336. if (dy > 1e-6)
  337. (node.y0 -= dy), (node.y1 -= dy);
  338. y = node.y0 - py;
  339. }
  340. }
  341. function reorderNodeLinks({ sourceLinks, targetLinks }) {
  342. if (linkSort === undefined) {
  343. for (const { source: { sourceLinks }, } of targetLinks) {
  344. sourceLinks.sort(ascendingTargetBreadth);
  345. }
  346. for (const { target: { targetLinks }, } of sourceLinks) {
  347. targetLinks.sort(ascendingSourceBreadth);
  348. }
  349. }
  350. }
  351. function reorderLinks(nodes) {
  352. if (linkSort === undefined) {
  353. for (const { sourceLinks, targetLinks } of nodes) {
  354. sourceLinks.sort(ascendingTargetBreadth);
  355. targetLinks.sort(ascendingSourceBreadth);
  356. }
  357. }
  358. }
  359. // Returns the target.y0 that would produce an ideal link from source to target.
  360. function targetTop(source, target) {
  361. let y = source.y0 - ((source.sourceLinks.length - 1) * py) / 2;
  362. for (const { target: node, width } of source.sourceLinks) {
  363. if (node === target)
  364. break;
  365. y += width + py;
  366. }
  367. for (const { source: node, width } of target.targetLinks) {
  368. if (node === source)
  369. break;
  370. y -= width;
  371. }
  372. return y;
  373. }
  374. // Returns the source.y0 that would produce an ideal link from source to target.
  375. function sourceTop(source, target) {
  376. let y = target.y0 - ((target.targetLinks.length - 1) * py) / 2;
  377. for (const { source: node, width } of target.targetLinks) {
  378. if (node === source)
  379. break;
  380. y += width + py;
  381. }
  382. for (const { target: node, width } of source.sourceLinks) {
  383. if (node === target)
  384. break;
  385. y -= width;
  386. }
  387. return y;
  388. }
  389. return sankey;
  390. }
  391. exports.Sankey = Sankey;
  392. //# sourceMappingURL=sankey.js.map