layout.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.sankeyLayout = exports.getDefaultOptions = exports.getNodeAlignFunction = void 0;
  4. var util_1 = require("@antv/util");
  5. var sankey_1 = require("./sankey");
  6. var ALIGN_METHOD = {
  7. left: sankey_1.left,
  8. right: sankey_1.right,
  9. center: sankey_1.center,
  10. justify: sankey_1.justify,
  11. };
  12. /**
  13. * 默认值
  14. */
  15. var DEFAULT_OPTIONS = {
  16. nodeId: function (node) { return node.index; },
  17. nodeAlign: 'justify',
  18. nodeWidth: 0.008,
  19. nodePadding: 0.03,
  20. nodeSort: undefined,
  21. };
  22. /**
  23. * 获得 align function
  24. * @param nodeAlign
  25. * @param nodeDepth
  26. */
  27. function getNodeAlignFunction(nodeAlign) {
  28. var func = (0, util_1.isString)(nodeAlign) ? ALIGN_METHOD[nodeAlign] : (0, util_1.isFunction)(nodeAlign) ? nodeAlign : null;
  29. return func || sankey_1.justify;
  30. }
  31. exports.getNodeAlignFunction = getNodeAlignFunction;
  32. function getDefaultOptions(sankeyLayoutOptions) {
  33. return (0, util_1.assign)({}, DEFAULT_OPTIONS, sankeyLayoutOptions);
  34. }
  35. exports.getDefaultOptions = getDefaultOptions;
  36. /**
  37. * 桑基图利用数据进行布局的函数,最终返回节点、边的位置(0 - 1 的信息)
  38. * 将会修改 data 数据
  39. * @param sankeyLayoutOptions
  40. * @param data
  41. */
  42. function sankeyLayout(sankeyLayoutOptions, data) {
  43. var options = getDefaultOptions(sankeyLayoutOptions);
  44. var nodeId = options.nodeId, nodeSort = options.nodeSort, nodeAlign = options.nodeAlign, nodeWidth = options.nodeWidth, nodePadding = options.nodePadding, nodeDepth = options.nodeDepth;
  45. var sankeyProcessor = (0, sankey_1.sankey)()
  46. // .links((d: any) => d.links)
  47. // .nodes((d: any) => d.nodes)
  48. .nodeSort(nodeSort)
  49. .nodeWidth(nodeWidth)
  50. .nodePadding(nodePadding)
  51. .nodeDepth(nodeDepth)
  52. .nodeAlign(getNodeAlignFunction(nodeAlign))
  53. .extent([
  54. [0, 0],
  55. [1, 1],
  56. ])
  57. .nodeId(nodeId);
  58. // 进行桑基图布局处理
  59. var layoutData = sankeyProcessor(data);
  60. // post process (x, y), etc.
  61. var nodes = layoutData.nodes
  62. .map(function (node) {
  63. var x0 = node.x0, x1 = node.x1, y0 = node.y0, y1 = node.y1;
  64. /* points
  65. * 3---2
  66. * | |
  67. * 0---1
  68. */
  69. node.x = [x0, x1, x1, x0];
  70. node.y = [y0, y0, y1, y1];
  71. return node;
  72. })
  73. .filter(function (node) {
  74. return node.name !== null;
  75. });
  76. var links = layoutData.links
  77. .map(function (edge) {
  78. var source = edge.source, target = edge.target;
  79. var sx = source.x1;
  80. var tx = target.x0;
  81. edge.x = [sx, sx, tx, tx];
  82. var offset = edge.width / 2;
  83. edge.y = [edge.y0 + offset, edge.y0 - offset, edge.y1 + offset, edge.y1 - offset];
  84. return edge;
  85. })
  86. .filter(function (edge) {
  87. var source = edge.source, target = edge.target;
  88. return source.name !== null && target.name !== null;
  89. });
  90. return { nodes: nodes, links: links };
  91. }
  92. exports.sankeyLayout = sankeyLayout;
  93. //# sourceMappingURL=layout.js.map