forceGraph.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. var __rest = (this && this.__rest) || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  5. t[p] = s[p];
  6. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  7. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  8. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  9. t[p[i]] = s[p[i]];
  10. }
  11. return t;
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.ForceGraph = void 0;
  15. const d3_force_1 = require("d3-force");
  16. const util_1 = require("@antv/util");
  17. const helper_1 = require("../utils/helper");
  18. const mark_1 = require("../utils/mark");
  19. const utils_1 = require("./utils");
  20. function dataTransform(data, layout, encode) {
  21. const { nodes, links } = data;
  22. const { joint, nodeStrength, linkStrength } = layout;
  23. const { nodeKey = (d) => d.id, linkKey = (d) => d.id } = encode;
  24. const nodeForce = (0, d3_force_1.forceManyBody)();
  25. const linkForce = (0, d3_force_1.forceLink)(links).id((0, utils_1.field)(linkKey));
  26. typeof nodeStrength === 'function' && nodeForce.strength(nodeStrength);
  27. typeof linkStrength === 'function' && linkForce.strength(linkStrength);
  28. const simulation = (0, d3_force_1.forceSimulation)(nodes)
  29. .force('link', linkForce)
  30. .force('charge', nodeForce);
  31. joint
  32. ? simulation.force('center', (0, d3_force_1.forceCenter)())
  33. : simulation.force('x', (0, d3_force_1.forceX)()).force('y', (0, d3_force_1.forceY)());
  34. simulation.stop();
  35. const n = Math.ceil(Math.log(simulation.alphaMin()) / Math.log(1 - simulation.alphaDecay()));
  36. for (let i = 0; i < n; i++)
  37. simulation.tick();
  38. return {
  39. nodesData: nodes,
  40. linksData: links,
  41. };
  42. }
  43. const ForceGraph = (options) => {
  44. return () => {
  45. const DEFAULT_LAYOUT_OPTIONS = {
  46. joint: true,
  47. };
  48. const DEFAULT_LINK_OPTIONS = {
  49. type: 'link',
  50. axis: false,
  51. legend: false,
  52. encode: {
  53. x: [(d) => d.source.x, (d) => d.target.x],
  54. y: [(d) => d.source.y, (d) => d.target.y],
  55. },
  56. style: {
  57. stroke: '#999',
  58. strokeOpacity: 0.6,
  59. },
  60. };
  61. const DEFAULT_NODE_OPTIONS = {
  62. type: 'point',
  63. axis: false,
  64. legend: false,
  65. encode: {
  66. x: 'x',
  67. y: 'y',
  68. size: 5,
  69. color: 'group',
  70. shape: 'point',
  71. },
  72. scale: {
  73. color: { type: 'ordinal' },
  74. },
  75. style: {
  76. stroke: '#fff',
  77. },
  78. };
  79. const DEFAULT_LABEL_OPTIONS = {
  80. text: '',
  81. };
  82. const { data, encode: e = {}, scale, style = {}, layout = {}, nodeLabels = [], linkLabels = [], animate = {}, tooltip = {}, } = options;
  83. const { nodeKey = (d) => d.id, linkKey = (d) => d.id } = e, restEncode = __rest(e, ["nodeKey", "linkKey"]);
  84. const encode = Object.assign({ nodeKey, linkKey }, restEncode);
  85. const nodeEncode = (0, helper_1.subObject)(encode, 'node');
  86. const linkEncode = (0, helper_1.subObject)(encode, 'link');
  87. const { links, nodes } = (0, utils_1.initializeData)(data, encode);
  88. const { nodesData, linksData } = dataTransform({ links, nodes }, (0, util_1.deepMix)({}, DEFAULT_LAYOUT_OPTIONS, layout), encode);
  89. const linkTooltip = (0, mark_1.subTooltip)(tooltip, 'link', {
  90. items: [
  91. (d) => ({ name: 'source', value: (0, utils_1.field)(linkKey)(d.source) }),
  92. (d) => ({ name: 'target', value: (0, utils_1.field)(linkKey)(d.target) }),
  93. ],
  94. });
  95. const nodeTooltip = (0, mark_1.subTooltip)(tooltip, 'node', {
  96. items: [(d) => ({ name: 'key', value: (0, utils_1.field)(nodeKey)(d) })],
  97. }, true);
  98. return [
  99. (0, util_1.deepMix)({}, DEFAULT_LINK_OPTIONS, {
  100. data: linksData,
  101. encode: linkEncode,
  102. labels: linkLabels,
  103. style: (0, helper_1.subObject)(style, 'link'),
  104. tooltip: linkTooltip,
  105. animate: (0, mark_1.maybeAnimation)(animate, 'link'),
  106. }),
  107. (0, util_1.deepMix)({}, DEFAULT_NODE_OPTIONS, {
  108. data: nodesData,
  109. encode: Object.assign({}, nodeEncode),
  110. scale,
  111. style: (0, helper_1.subObject)(style, 'node'),
  112. tooltip: nodeTooltip,
  113. labels: [
  114. Object.assign(Object.assign({}, DEFAULT_LABEL_OPTIONS), (0, helper_1.subObject)(style, 'label')),
  115. ...nodeLabels,
  116. ],
  117. animate: (0, mark_1.maybeAnimation)(animate, 'link'),
  118. }),
  119. ];
  120. };
  121. };
  122. exports.ForceGraph = ForceGraph;
  123. exports.ForceGraph.props = {};
  124. //# sourceMappingURL=forceGraph.js.map