@antv/layout 0.2.0 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (297) hide show
  1. package/dist/layout.min.js +1 -1
  2. package/dist/layout.min.js.LICENSE.txt +0 -9
  3. package/dist/layout.min.js.map +1 -1
  4. package/es/layout/circular.js.map +1 -1
  5. package/es/layout/comboCombined.js +3 -19
  6. package/es/layout/comboCombined.js.map +1 -1
  7. package/es/layout/concentric.js.map +1 -1
  8. package/es/layout/dagre/graph.d.ts +91 -0
  9. package/es/layout/dagre/graph.js +4 -0
  10. package/es/layout/dagre/graph.js.map +1 -0
  11. package/es/layout/dagre/index.d.ts +3 -4
  12. package/es/layout/dagre/index.js +0 -2
  13. package/es/layout/dagre/index.js.map +1 -1
  14. package/es/layout/dagre/src/acyclic.d.ts +1 -2
  15. package/es/layout/dagre/src/acyclic.js +7 -7
  16. package/es/layout/dagre/src/acyclic.js.map +1 -1
  17. package/es/layout/dagre/src/add-border-segments.d.ts +1 -2
  18. package/es/layout/dagre/src/add-border-segments.js +5 -5
  19. package/es/layout/dagre/src/add-border-segments.js.map +1 -1
  20. package/es/layout/dagre/src/coordinate-system.d.ts +1 -2
  21. package/es/layout/dagre/src/coordinate-system.js +15 -5
  22. package/es/layout/dagre/src/coordinate-system.js.map +1 -1
  23. package/es/layout/dagre/src/data/list.d.ts +9 -5
  24. package/es/layout/dagre/src/data/list.js +25 -27
  25. package/es/layout/dagre/src/data/list.js.map +1 -1
  26. package/es/layout/dagre/src/debug.d.ts +2 -3
  27. package/es/layout/dagre/src/debug.js +3 -5
  28. package/es/layout/dagre/src/debug.js.map +1 -1
  29. package/es/layout/dagre/src/greedy-fas.d.ts +2 -3
  30. package/es/layout/dagre/src/greedy-fas.js +16 -15
  31. package/es/layout/dagre/src/greedy-fas.js.map +1 -1
  32. package/es/layout/dagre/src/layout.d.ts +2 -3
  33. package/es/layout/dagre/src/layout.js +170 -91
  34. package/es/layout/dagre/src/layout.js.map +1 -1
  35. package/es/layout/dagre/src/nesting-graph.d.ts +1 -2
  36. package/es/layout/dagre/src/nesting-graph.js +15 -10
  37. package/es/layout/dagre/src/nesting-graph.js.map +1 -1
  38. package/es/layout/dagre/src/normalize.d.ts +1 -2
  39. package/es/layout/dagre/src/normalize.js +12 -11
  40. package/es/layout/dagre/src/normalize.js.map +1 -1
  41. package/es/layout/dagre/src/order/add-subgraph-constraints.d.ts +1 -2
  42. package/es/layout/dagre/src/order/add-subgraph-constraints.js.map +1 -1
  43. package/es/layout/dagre/src/order/barycenter.d.ts +1 -2
  44. package/es/layout/dagre/src/order/barycenter.js.map +1 -1
  45. package/es/layout/dagre/src/order/build-layer-graph.d.ts +2 -3
  46. package/es/layout/dagre/src/order/build-layer-graph.js +12 -8
  47. package/es/layout/dagre/src/order/build-layer-graph.js.map +1 -1
  48. package/es/layout/dagre/src/order/cross-count.d.ts +2 -3
  49. package/es/layout/dagre/src/order/cross-count.js +13 -12
  50. package/es/layout/dagre/src/order/cross-count.js.map +1 -1
  51. package/es/layout/dagre/src/order/index.d.ts +2 -3
  52. package/es/layout/dagre/src/order/index.js +17 -15
  53. package/es/layout/dagre/src/order/index.js.map +1 -1
  54. package/es/layout/dagre/src/order/init-data-order.d.ts +1 -2
  55. package/es/layout/dagre/src/order/init-data-order.js +3 -5
  56. package/es/layout/dagre/src/order/init-data-order.js.map +1 -1
  57. package/es/layout/dagre/src/order/init-order.d.ts +2 -3
  58. package/es/layout/dagre/src/order/init-order.js +1 -2
  59. package/es/layout/dagre/src/order/init-order.js.map +1 -1
  60. package/es/layout/dagre/src/order/resolve-conflicts.d.ts +18 -3
  61. package/es/layout/dagre/src/order/resolve-conflicts.js +9 -29
  62. package/es/layout/dagre/src/order/resolve-conflicts.js.map +1 -1
  63. package/es/layout/dagre/src/order/sort-subgraph.d.ts +6 -3
  64. package/es/layout/dagre/src/order/sort-subgraph.js +19 -14
  65. package/es/layout/dagre/src/order/sort-subgraph.js.map +1 -1
  66. package/es/layout/dagre/src/order/sort.d.ts +6 -1
  67. package/es/layout/dagre/src/order/sort.js +2 -2
  68. package/es/layout/dagre/src/order/sort.js.map +1 -1
  69. package/es/layout/dagre/src/parent-dummy-chains.d.ts +1 -2
  70. package/es/layout/dagre/src/parent-dummy-chains.js +47 -44
  71. package/es/layout/dagre/src/parent-dummy-chains.js.map +1 -1
  72. package/es/layout/dagre/src/position/bk.d.ts +22 -31
  73. package/es/layout/dagre/src/position/bk.js +49 -83
  74. package/es/layout/dagre/src/position/bk.js.map +1 -1
  75. package/es/layout/dagre/src/position/index.d.ts +1 -2
  76. package/es/layout/dagre/src/position/index.js +12 -14
  77. package/es/layout/dagre/src/position/index.js.map +1 -1
  78. package/es/layout/dagre/src/rank/feasible-tree.d.ts +5 -6
  79. package/es/layout/dagre/src/rank/feasible-tree.js +1 -2
  80. package/es/layout/dagre/src/rank/feasible-tree.js.map +1 -1
  81. package/es/layout/dagre/src/rank/index.d.ts +2 -3
  82. package/es/layout/dagre/src/rank/index.js.map +1 -1
  83. package/es/layout/dagre/src/rank/network-simplex.d.ts +8 -11
  84. package/es/layout/dagre/src/rank/network-simplex.js +18 -31
  85. package/es/layout/dagre/src/rank/network-simplex.js.map +1 -1
  86. package/es/layout/dagre/src/rank/util.d.ts +4 -5
  87. package/es/layout/dagre/src/rank/util.js +37 -20
  88. package/es/layout/dagre/src/rank/util.js.map +1 -1
  89. package/es/layout/dagre/src/util.d.ts +29 -48
  90. package/es/layout/dagre/src/util.js +91 -101
  91. package/es/layout/dagre/src/util.js.map +1 -1
  92. package/es/layout/dagre.d.ts +1 -1
  93. package/es/layout/dagre.js +28 -24
  94. package/es/layout/dagre.js.map +1 -1
  95. package/es/layout/fruchterman.js.map +1 -1
  96. package/es/layout/gForce.js +14 -6
  97. package/es/layout/gForce.js.map +1 -1
  98. package/lib/index.js +5 -1
  99. package/lib/index.js.map +1 -1
  100. package/lib/layout/circular.js.map +1 -1
  101. package/lib/layout/comboCombined.js +2 -18
  102. package/lib/layout/comboCombined.js.map +1 -1
  103. package/lib/layout/comboForce.js +5 -5
  104. package/lib/layout/comboForce.js.map +1 -1
  105. package/lib/layout/concentric.js.map +1 -1
  106. package/lib/layout/dagre/graph.d.ts +91 -0
  107. package/lib/layout/dagre/graph.js +28 -0
  108. package/lib/layout/dagre/graph.js.map +1 -0
  109. package/lib/layout/dagre/index.d.ts +3 -4
  110. package/lib/layout/dagre/index.js +0 -2
  111. package/lib/layout/dagre/index.js.map +1 -1
  112. package/lib/layout/dagre/src/acyclic.d.ts +1 -2
  113. package/lib/layout/dagre/src/acyclic.js +7 -7
  114. package/lib/layout/dagre/src/acyclic.js.map +1 -1
  115. package/lib/layout/dagre/src/add-border-segments.d.ts +1 -2
  116. package/lib/layout/dagre/src/add-border-segments.js +5 -8
  117. package/lib/layout/dagre/src/add-border-segments.js.map +1 -1
  118. package/lib/layout/dagre/src/coordinate-system.d.ts +1 -2
  119. package/lib/layout/dagre/src/coordinate-system.js +15 -5
  120. package/lib/layout/dagre/src/coordinate-system.js.map +1 -1
  121. package/lib/layout/dagre/src/data/list.d.ts +9 -5
  122. package/lib/layout/dagre/src/data/list.js +25 -26
  123. package/lib/layout/dagre/src/data/list.js.map +1 -1
  124. package/lib/layout/dagre/src/debug.d.ts +2 -3
  125. package/lib/layout/dagre/src/debug.js +6 -11
  126. package/lib/layout/dagre/src/debug.js.map +1 -1
  127. package/lib/layout/dagre/src/greedy-fas.d.ts +2 -3
  128. package/lib/layout/dagre/src/greedy-fas.js +41 -15
  129. package/lib/layout/dagre/src/greedy-fas.js.map +1 -1
  130. package/lib/layout/dagre/src/layout.d.ts +2 -3
  131. package/lib/layout/dagre/src/layout.js +171 -100
  132. package/lib/layout/dagre/src/layout.js.map +1 -1
  133. package/lib/layout/dagre/src/nesting-graph.d.ts +1 -2
  134. package/lib/layout/dagre/src/nesting-graph.js +15 -13
  135. package/lib/layout/dagre/src/nesting-graph.js.map +1 -1
  136. package/lib/layout/dagre/src/normalize.d.ts +1 -2
  137. package/lib/layout/dagre/src/normalize.js +12 -14
  138. package/lib/layout/dagre/src/normalize.js.map +1 -1
  139. package/lib/layout/dagre/src/order/add-subgraph-constraints.d.ts +1 -2
  140. package/lib/layout/dagre/src/order/add-subgraph-constraints.js.map +1 -1
  141. package/lib/layout/dagre/src/order/barycenter.d.ts +1 -2
  142. package/lib/layout/dagre/src/order/barycenter.js.map +1 -1
  143. package/lib/layout/dagre/src/order/build-layer-graph.d.ts +2 -3
  144. package/lib/layout/dagre/src/order/build-layer-graph.js +13 -12
  145. package/lib/layout/dagre/src/order/build-layer-graph.js.map +1 -1
  146. package/lib/layout/dagre/src/order/cross-count.d.ts +2 -3
  147. package/lib/layout/dagre/src/order/cross-count.js +14 -13
  148. package/lib/layout/dagre/src/order/cross-count.js.map +1 -1
  149. package/lib/layout/dagre/src/order/index.d.ts +2 -3
  150. package/lib/layout/dagre/src/order/index.js +15 -13
  151. package/lib/layout/dagre/src/order/index.js.map +1 -1
  152. package/lib/layout/dagre/src/order/init-data-order.d.ts +1 -2
  153. package/lib/layout/dagre/src/order/init-data-order.js +3 -5
  154. package/lib/layout/dagre/src/order/init-data-order.js.map +1 -1
  155. package/lib/layout/dagre/src/order/init-order.d.ts +2 -3
  156. package/lib/layout/dagre/src/order/init-order.js +1 -2
  157. package/lib/layout/dagre/src/order/init-order.js.map +1 -1
  158. package/lib/layout/dagre/src/order/resolve-conflicts.d.ts +18 -3
  159. package/lib/layout/dagre/src/order/resolve-conflicts.js +9 -29
  160. package/lib/layout/dagre/src/order/resolve-conflicts.js.map +1 -1
  161. package/lib/layout/dagre/src/order/sort-subgraph.d.ts +6 -3
  162. package/lib/layout/dagre/src/order/sort-subgraph.js +16 -11
  163. package/lib/layout/dagre/src/order/sort-subgraph.js.map +1 -1
  164. package/lib/layout/dagre/src/order/sort.d.ts +6 -1
  165. package/lib/layout/dagre/src/order/sort.js +2 -5
  166. package/lib/layout/dagre/src/order/sort.js.map +1 -1
  167. package/lib/layout/dagre/src/parent-dummy-chains.d.ts +1 -2
  168. package/lib/layout/dagre/src/parent-dummy-chains.js +47 -44
  169. package/lib/layout/dagre/src/parent-dummy-chains.js.map +1 -1
  170. package/lib/layout/dagre/src/position/bk.d.ts +22 -31
  171. package/lib/layout/dagre/src/position/bk.js +75 -85
  172. package/lib/layout/dagre/src/position/bk.js.map +1 -1
  173. package/lib/layout/dagre/src/position/index.d.ts +1 -2
  174. package/lib/layout/dagre/src/position/index.js +14 -17
  175. package/lib/layout/dagre/src/position/index.js.map +1 -1
  176. package/lib/layout/dagre/src/rank/feasible-tree.d.ts +5 -6
  177. package/lib/layout/dagre/src/rank/feasible-tree.js +3 -7
  178. package/lib/layout/dagre/src/rank/feasible-tree.js.map +1 -1
  179. package/lib/layout/dagre/src/rank/index.d.ts +2 -3
  180. package/lib/layout/dagre/src/rank/index.js.map +1 -1
  181. package/lib/layout/dagre/src/rank/network-simplex.d.ts +8 -11
  182. package/lib/layout/dagre/src/rank/network-simplex.js +27 -35
  183. package/lib/layout/dagre/src/rank/network-simplex.js.map +1 -1
  184. package/lib/layout/dagre/src/rank/util.d.ts +4 -5
  185. package/lib/layout/dagre/src/rank/util.js +36 -19
  186. package/lib/layout/dagre/src/rank/util.js.map +1 -1
  187. package/lib/layout/dagre/src/util.d.ts +29 -48
  188. package/lib/layout/dagre/src/util.js +80 -92
  189. package/lib/layout/dagre/src/util.js.map +1 -1
  190. package/lib/layout/dagre.d.ts +1 -1
  191. package/lib/layout/dagre.js +27 -23
  192. package/lib/layout/dagre.js.map +1 -1
  193. package/lib/layout/er/core.js +5 -1
  194. package/lib/layout/er/core.js.map +1 -1
  195. package/lib/layout/force/force-in-a-box.js +7 -3
  196. package/lib/layout/force/force-in-a-box.js.map +1 -1
  197. package/lib/layout/force/force.js +5 -1
  198. package/lib/layout/force/force.js.map +1 -1
  199. package/lib/layout/force/index.js +5 -1
  200. package/lib/layout/force/index.js.map +1 -1
  201. package/lib/layout/fruchterman.js.map +1 -1
  202. package/lib/layout/gForce.js +10 -2
  203. package/lib/layout/gForce.js.map +1 -1
  204. package/lib/layout/grid.js +2 -2
  205. package/lib/layout/grid.js.map +1 -1
  206. package/lib/layout/index.js +5 -1
  207. package/lib/layout/index.js.map +1 -1
  208. package/lib/layout/radial/index.js +5 -1
  209. package/lib/layout/radial/index.js.map +1 -1
  210. package/lib/registy/index.js +1 -1
  211. package/lib/registy/index.js.map +1 -1
  212. package/lib/util/index.js +5 -1
  213. package/lib/util/index.js.map +1 -1
  214. package/package.json +3 -2
  215. package/src/index.ts +7 -0
  216. package/src/layout/base.ts +54 -0
  217. package/src/layout/circular.ts +369 -0
  218. package/src/layout/comboCombined.ts +390 -0
  219. package/src/layout/comboForce.ts +873 -0
  220. package/src/layout/concentric.ts +289 -0
  221. package/src/layout/constants.ts +21 -0
  222. package/src/layout/dagre/graph.ts +104 -0
  223. package/src/layout/dagre/index.ts +31 -0
  224. package/src/layout/dagre/src/acyclic.ts +58 -0
  225. package/src/layout/dagre/src/add-border-segments.ts +47 -0
  226. package/src/layout/dagre/src/coordinate-system.ts +77 -0
  227. package/src/layout/dagre/src/data/list.ts +60 -0
  228. package/src/layout/dagre/src/debug.ts +30 -0
  229. package/src/layout/dagre/src/greedy-fas.ts +144 -0
  230. package/src/layout/dagre/src/layout.ts +580 -0
  231. package/src/layout/dagre/src/nesting-graph.ts +143 -0
  232. package/src/layout/dagre/src/normalize.ts +96 -0
  233. package/src/layout/dagre/src/order/add-subgraph-constraints.ts +29 -0
  234. package/src/layout/dagre/src/order/barycenter.ts +26 -0
  235. package/src/layout/dagre/src/order/build-layer-graph.ts +82 -0
  236. package/src/layout/dagre/src/order/cross-count.ts +77 -0
  237. package/src/layout/dagre/src/order/index.ts +105 -0
  238. package/src/layout/dagre/src/order/init-data-order.ts +27 -0
  239. package/src/layout/dagre/src/order/init-order.ts +56 -0
  240. package/src/layout/dagre/src/order/resolve-conflicts.ts +152 -0
  241. package/src/layout/dagre/src/order/sort-subgraph.ts +105 -0
  242. package/src/layout/dagre/src/order/sort.ts +76 -0
  243. package/src/layout/dagre/src/parent-dummy-chains.ts +102 -0
  244. package/src/layout/dagre/src/position/bk.ts +494 -0
  245. package/src/layout/dagre/src/position/index.ts +82 -0
  246. package/src/layout/dagre/src/rank/feasible-tree.ts +165 -0
  247. package/src/layout/dagre/src/rank/index.ts +54 -0
  248. package/src/layout/dagre/src/rank/network-simplex.ts +225 -0
  249. package/src/layout/dagre/src/rank/util.ts +157 -0
  250. package/src/layout/dagre/src/util.ts +308 -0
  251. package/src/layout/dagre.ts +423 -0
  252. package/src/layout/dagreCompound.ts +518 -0
  253. package/src/layout/er/core.ts +117 -0
  254. package/src/layout/er/forceGrid.ts +95 -0
  255. package/src/layout/er/grid.ts +185 -0
  256. package/src/layout/er/index.ts +68 -0
  257. package/src/layout/er/mysqlWorkbench.ts +345 -0
  258. package/src/layout/er/type.ts +39 -0
  259. package/src/layout/force/force-in-a-box.ts +400 -0
  260. package/src/layout/force/force.ts +391 -0
  261. package/src/layout/force/index.ts +1 -0
  262. package/src/layout/forceAtlas2/body.ts +115 -0
  263. package/src/layout/forceAtlas2/index.ts +556 -0
  264. package/src/layout/forceAtlas2/quad.ts +115 -0
  265. package/src/layout/forceAtlas2/quadTree.ts +107 -0
  266. package/src/layout/fruchterman.ts +361 -0
  267. package/src/layout/gForce.ts +487 -0
  268. package/src/layout/gpu/fruchterman.ts +314 -0
  269. package/src/layout/gpu/fruchtermanShader.ts +204 -0
  270. package/src/layout/gpu/gForce.ts +406 -0
  271. package/src/layout/gpu/gForceShader.ts +221 -0
  272. package/src/layout/grid.ts +391 -0
  273. package/src/layout/index.ts +45 -0
  274. package/src/layout/layout.ts +75 -0
  275. package/src/layout/mds.ts +140 -0
  276. package/src/layout/radial/index.ts +1 -0
  277. package/src/layout/radial/mds.ts +51 -0
  278. package/src/layout/radial/radial.ts +500 -0
  279. package/src/layout/radial/radialNonoverlapForce.ts +189 -0
  280. package/src/layout/random.ts +75 -0
  281. package/src/layout/types.ts +421 -0
  282. package/src/registy/index.ts +43 -0
  283. package/src/util/array.ts +1 -0
  284. package/src/util/function.ts +64 -0
  285. package/src/util/gpu.ts +254 -0
  286. package/src/util/index.ts +6 -0
  287. package/src/util/math.ts +158 -0
  288. package/src/util/number.ts +8 -0
  289. package/src/util/object.ts +28 -0
  290. package/src/util/string.ts +18 -0
  291. package/CHANGELOG.md +0 -78
  292. package/es/layout/dagre/src/graphlib.d.ts +0 -2
  293. package/es/layout/dagre/src/graphlib.js +0 -51
  294. package/es/layout/dagre/src/graphlib.js.map +0 -1
  295. package/lib/layout/dagre/src/graphlib.d.ts +0 -2
  296. package/lib/layout/dagre/src/graphlib.js +0 -56
  297. package/lib/layout/dagre/src/graphlib.js.map +0 -1
@@ -0,0 +1,873 @@
1
+ /**
2
+ * @fileOverview Combo force layout
3
+ * @author shiwu.wyy@antfin.com
4
+ */
5
+
6
+ import {
7
+ Edge,
8
+ OutNode,
9
+ PointTuple,
10
+ IndexMap,
11
+ Combo,
12
+ ComboTree,
13
+ Point,
14
+ ComboForceLayoutOptions
15
+ } from "./types";
16
+ import { Base } from "./base";
17
+ import { isArray, isNumber, isFunction, traverseTreeUp, isObject, getEdgeTerminal } from "../util";
18
+
19
+ type Node = OutNode & {
20
+ depth: number;
21
+ itemType?: string;
22
+ comboId?: string;
23
+ };
24
+
25
+ type NodeMap = {
26
+ [key: string]: Node;
27
+ };
28
+
29
+ type ComboMap = {
30
+ [key: string]: Combo;
31
+ };
32
+
33
+ /**
34
+ * force layout for graph with combos
35
+ */
36
+ export class ComboForceLayout extends Base {
37
+ /** 布局中心 */
38
+ public center: PointTuple = [0, 0];
39
+
40
+ /** 停止迭代的最大迭代数 */
41
+ public maxIteration: number = 100;
42
+
43
+ /** 重力大小,影响图的紧凑程度 */
44
+ public gravity: number = 10;
45
+
46
+ /** 群组中心力大小 */
47
+ public comboGravity: number = 10;
48
+
49
+ /** 默认边长度 */
50
+ public linkDistance: number | ((d?: unknown) => number) = 10;
51
+
52
+ /** 每次迭代位移的衰减相关参数 */
53
+ public alpha: number = 1;
54
+
55
+ public alphaMin: number = 0.001;
56
+
57
+ public alphaDecay: number = 1 - this.alphaMin ** (1 / 300);
58
+
59
+ public alphaTarget: number = 0;
60
+
61
+ /** 节点运动速度衰减参数 */
62
+ public velocityDecay: number = 0.6;
63
+
64
+ /** 边引力大小 */
65
+ public edgeStrength: number | ((d?: unknown) => number) = 0.6;
66
+
67
+ /** 节点引力大小 */
68
+ public nodeStrength: number | ((d?: unknown) => number) = 30;
69
+
70
+ /** 是否开启防止重叠 */
71
+ public preventOverlap: boolean = false;
72
+
73
+ /** 是否开启节点之间的防止重叠 */
74
+ public preventNodeOverlap: boolean = false;
75
+
76
+ /** 是否开启 Combo 之间的防止重叠 */
77
+ public preventComboOverlap: boolean = false;
78
+
79
+ /** 防止重叠的碰撞力大小 */
80
+ public collideStrength: number | undefined = undefined;
81
+
82
+ /** 防止重叠的碰撞力大小 */
83
+ public nodeCollideStrength: number | undefined = 0.5;
84
+
85
+ /** 防止重叠的碰撞力大小 */
86
+ public comboCollideStrength: number | undefined = 0.5;
87
+
88
+ /** 节点大小,用于防止重叠 */
89
+ public nodeSize: number | number[] | ((d?: unknown) => number) | undefined;
90
+
91
+ /** 节点最小间距,防止重叠时的间隙 */
92
+ public nodeSpacing: ((d?: unknown) => number) | number | undefined;
93
+
94
+ /** Combo 最小间距,防止重叠时的间隙 */
95
+ public comboSpacing: ((d?: unknown) => number) | number | undefined = 20;
96
+
97
+ /** Combo 内部的 padding */
98
+ public comboPadding:
99
+ | ((d?: unknown) => number)
100
+ | number
101
+ | number[]
102
+ | undefined = 10;
103
+
104
+ /** 优化计算斥力的速度,两节点间距超过 optimizeRangeFactor * width 则不再计算斥力和重叠斥力 */
105
+ public optimizeRangeFactor: number = 1;
106
+
107
+ /** 每次迭代的回调函数 */
108
+ public onTick: () => void = () => {};
109
+
110
+ /** 迭代结束的回调函数 */
111
+ public onLayoutEnd: () => void = () => {};
112
+
113
+ /** 根据边两端节点层级差距的调整引力系数的因子,取值范围 [0, 1]。层级差距越大,引力越小 */
114
+ public depthAttractiveForceScale: number = 1;
115
+
116
+ /** 根据边两端节点层级差距的调整斥力系数的因子,取值范围 [1, Infinity]。层级差距越大,斥力越大 */
117
+ public depthRepulsiveForceScale: number = 2;
118
+
119
+ /** 内部计算参数 */
120
+ public nodes: Node[] = [];
121
+
122
+ public edges: Edge[] = [];
123
+
124
+ public combos: Combo[] = [];
125
+
126
+ private comboTrees: ComboTree[] = [];
127
+
128
+ // add a virtual root to comboTrees
129
+ private comboTree: ComboTree;
130
+
131
+ private width: number = 300;
132
+
133
+ private height: number = 300;
134
+
135
+ private bias: number[] = [];
136
+
137
+ private nodeMap: NodeMap = {};
138
+
139
+ private oriComboMap: ComboMap = {};
140
+
141
+ private indexMap: IndexMap = {};
142
+
143
+ private comboMap: ComboMap = {};
144
+
145
+ private previousLayouted: boolean = false;
146
+
147
+ constructor(options?: ComboForceLayoutOptions) {
148
+ super();
149
+ this.updateCfg(options);
150
+ }
151
+
152
+ public getDefaultCfg() {
153
+ return {
154
+ maxIteration: 100,
155
+ center: [0, 0],
156
+ gravity: 10,
157
+ speed: 1,
158
+ comboGravity: 30,
159
+ preventOverlap: false,
160
+ preventComboOverlap: true,
161
+ preventNodeOverlap: true,
162
+ nodeSpacing: undefined,
163
+ collideStrength: undefined,
164
+ nodeCollideStrength: 0.5,
165
+ comboCollideStrength: 0.5,
166
+ comboSpacing: 20,
167
+ comboPadding: 10,
168
+ edgeStrength: 0.6,
169
+ nodeStrength: 30,
170
+ linkDistance: 10
171
+ };
172
+ }
173
+
174
+ /**
175
+ * 执行布局
176
+ */
177
+ public execute() {
178
+ const self = this;
179
+ const nodes = self.nodes;
180
+ const center = self.center;
181
+ self.comboTree = {
182
+ id: "comboTreeRoot",
183
+ depth: -1,
184
+ children: self.comboTrees
185
+ };
186
+
187
+ if (!nodes || nodes.length === 0) {
188
+ if (self.onLayoutEnd) self.onLayoutEnd();
189
+ return;
190
+ }
191
+ if (nodes.length === 1) {
192
+ nodes[0].x = center[0];
193
+ nodes[0].y = center[1];
194
+ if (self.onLayoutEnd) self.onLayoutEnd();
195
+ return;
196
+ }
197
+
198
+ self.initVals();
199
+
200
+ // layout
201
+ self.run();
202
+ if (self.onLayoutEnd) self.onLayoutEnd();
203
+ }
204
+
205
+ public run() {
206
+ const self = this;
207
+ const nodes = self.nodes;
208
+ const maxIteration = self.previousLayouted
209
+ ? self.maxIteration / 5
210
+ : self.maxIteration;
211
+ if (!self.width && typeof window !== "undefined") {
212
+ self.width = window.innerWidth;
213
+ }
214
+ if (!self.height && typeof window !== "undefined") {
215
+ self.height = window.innerHeight;
216
+ }
217
+ const center = self.center;
218
+ const velocityDecay = self.velocityDecay;
219
+
220
+ // init the positions to make the nodes with same combo gather around the combo
221
+ const comboMap = self.comboMap;
222
+ if (!self.previousLayouted) self.initPos(comboMap);
223
+
224
+ // iterate
225
+ for (let i = 0; i < maxIteration; i++) {
226
+ const displacements: Point[] = [];
227
+ nodes.forEach((_, j) => {
228
+ displacements[j] = { x: 0, y: 0 };
229
+ });
230
+ self.applyCalculate(displacements);
231
+
232
+ // gravity for combos
233
+ self.applyComboCenterForce(displacements);
234
+
235
+ // move
236
+ nodes.forEach((n, j) => {
237
+ if (!isNumber(n.x) || !isNumber(n.y)) return;
238
+ n.x += displacements[j].x * velocityDecay;
239
+ n.y += displacements[j].y * velocityDecay;
240
+ });
241
+ self.alpha += (self.alphaTarget - self.alpha) * self.alphaDecay;
242
+ self.onTick();
243
+ }
244
+
245
+ // move to center
246
+ const meanCenter = [0, 0];
247
+ nodes.forEach((n) => {
248
+ if (!isNumber(n.x) || !isNumber(n.y)) return;
249
+ meanCenter[0] += n.x;
250
+ meanCenter[1] += n.y;
251
+ });
252
+ meanCenter[0] /= nodes.length;
253
+ meanCenter[1] /= nodes.length;
254
+ const centerOffset = [center[0] - meanCenter[0], center[1] - meanCenter[1]];
255
+ nodes.forEach((n, j) => {
256
+ if (!isNumber(n.x) || !isNumber(n.y)) return;
257
+ n.x += centerOffset[0];
258
+ n.y += centerOffset[1];
259
+ });
260
+
261
+ // arrange the empty combo
262
+ self.combos.forEach((combo) => {
263
+ const mapped = comboMap[combo.id];
264
+ if (mapped && mapped.empty) {
265
+ combo.x = mapped.cx || combo.x;
266
+ combo.y = mapped.cy || combo.y;
267
+ }
268
+ });
269
+
270
+ self.previousLayouted = true;
271
+ }
272
+
273
+ private initVals() {
274
+ const self = this;
275
+ const edges = self.edges;
276
+ const nodes = self.nodes;
277
+ const combos = self.combos;
278
+ const count: any = {};
279
+
280
+ const nodeMap: NodeMap = {};
281
+ const indexMap: IndexMap = {};
282
+ nodes.forEach((node, i) => {
283
+ nodeMap[node.id] = node;
284
+ indexMap[node.id] = i;
285
+ });
286
+ self.nodeMap = nodeMap;
287
+ self.indexMap = indexMap;
288
+
289
+ const oriComboMap: ComboMap = {};
290
+ combos.forEach((combo) => {
291
+ oriComboMap[combo.id] = combo;
292
+ });
293
+ self.oriComboMap = oriComboMap;
294
+ self.comboMap = self.getComboMap();
295
+
296
+ const preventOverlap = self.preventOverlap;
297
+ self.preventComboOverlap = self.preventComboOverlap || preventOverlap;
298
+ self.preventNodeOverlap = self.preventNodeOverlap || preventOverlap;
299
+
300
+ const collideStrength = self.collideStrength;
301
+ if (collideStrength) {
302
+ self.comboCollideStrength = collideStrength;
303
+ self.nodeCollideStrength = collideStrength;
304
+ }
305
+ self.comboCollideStrength = self.comboCollideStrength
306
+ ? self.comboCollideStrength
307
+ : 0;
308
+ self.nodeCollideStrength = self.nodeCollideStrength
309
+ ? self.nodeCollideStrength
310
+ : 0;
311
+
312
+ // get edge bias
313
+ for (let i = 0; i < edges.length; ++i) {
314
+ const source = getEdgeTerminal(edges[i], 'source');
315
+ const target = getEdgeTerminal(edges[i], 'target');
316
+ if (count[source]) count[source]++;
317
+ else count[source] = 1;
318
+ if (count[target]) count[target]++;
319
+ else count[target] = 1;
320
+ }
321
+ const bias = [];
322
+ for (let i = 0; i < edges.length; ++i) {
323
+ const source = getEdgeTerminal(edges[i], 'source');
324
+ const target = getEdgeTerminal(edges[i], 'target');
325
+ bias[i] = count[source] / (count[source] + count[target]);
326
+ }
327
+ this.bias = bias;
328
+
329
+ const nodeSize = self.nodeSize;
330
+ const nodeSpacing = self.nodeSpacing;
331
+ let nodeSizeFunc: (d: any) => number;
332
+ let nodeSpacingFunc: (d: any) => number;
333
+
334
+ // nodeSpacing to function
335
+ if (isNumber(nodeSpacing)) {
336
+ nodeSpacingFunc = () => nodeSpacing as any;
337
+ } else if (isFunction(nodeSpacing)) {
338
+ nodeSpacingFunc = nodeSpacing;
339
+ } else {
340
+ nodeSpacingFunc = () => 0;
341
+ }
342
+ this.nodeSpacing = nodeSpacingFunc;
343
+
344
+ // nodeSize to function
345
+ if (!nodeSize) {
346
+ nodeSizeFunc = (d) => {
347
+ if (d.size) {
348
+ if (isArray(d.size)) {
349
+ const res = d.size[0] > d.size[1] ? d.size[0] : d.size[1];
350
+ return res / 2;
351
+ } if (isObject(d.size)) {
352
+ const res = d.size.width > d.size.height ? d.size.width : d.size.height;
353
+ return res / 2;
354
+ }
355
+ return d.size / 2;
356
+ }
357
+ return 10;
358
+ };
359
+ } else if (isFunction(nodeSize)) {
360
+ nodeSizeFunc = (d) => {
361
+ return nodeSize(d);
362
+ };
363
+ } else if (isArray(nodeSize)) {
364
+ const larger = nodeSize[0] > nodeSize[1] ? nodeSize[0] : nodeSize[1];
365
+ const radius = larger / 2;
366
+ nodeSizeFunc = (d) => radius;
367
+ } else {
368
+ // number type
369
+ const radius = nodeSize / 2;
370
+ nodeSizeFunc = (d) => radius;
371
+ }
372
+ this.nodeSize = nodeSizeFunc;
373
+
374
+ // comboSpacing to function
375
+ const comboSpacing = self.comboSpacing;
376
+ let comboSpacingFunc: (d: any) => number;
377
+ if (isNumber(comboSpacing)) {
378
+ comboSpacingFunc = () => comboSpacing as any;
379
+ } else if (isFunction(comboSpacing)) {
380
+ comboSpacingFunc = comboSpacing;
381
+ } else {
382
+ // null type
383
+ comboSpacingFunc = () => 0;
384
+ }
385
+ this.comboSpacing = comboSpacingFunc;
386
+
387
+ // comboPadding to function
388
+ const comboPadding = self.comboPadding;
389
+ let comboPaddingFunc: (d: any) => number;
390
+ if (isNumber(comboPadding)) {
391
+ comboPaddingFunc = () => comboPadding as any;
392
+ } else if (isArray(comboPadding)) {
393
+ comboPaddingFunc = () => Math.max.apply(null, comboPadding);
394
+ } else if (isFunction(comboPadding)) {
395
+ comboPaddingFunc = comboPadding;
396
+ } else {
397
+ // null type
398
+ comboPaddingFunc = () => 0;
399
+ }
400
+ this.comboPadding = comboPaddingFunc;
401
+
402
+ // linkDistance to function
403
+ let linkDistance = this.linkDistance;
404
+ let linkDistanceFunc;
405
+ if (!linkDistance) {
406
+ linkDistance = 10;
407
+ }
408
+ if (isNumber(linkDistance)) {
409
+ linkDistanceFunc = (d: any) => {
410
+ return linkDistance;
411
+ };
412
+ } else {
413
+ linkDistanceFunc = linkDistance;
414
+ }
415
+ this.linkDistance = linkDistanceFunc as ((d?: unknown) => number);
416
+
417
+ // linkStrength to function
418
+ let edgeStrength = this.edgeStrength;
419
+ let edgeStrengthFunc;
420
+ if (!edgeStrength) {
421
+ edgeStrength = 1;
422
+ }
423
+ if (isNumber(edgeStrength)) {
424
+ edgeStrengthFunc = (d: any) => {
425
+ return edgeStrength;
426
+ };
427
+ } else {
428
+ edgeStrengthFunc = edgeStrength;
429
+ }
430
+ this.edgeStrength = edgeStrengthFunc as ((d?: unknown) => number);
431
+
432
+ // nodeStrength to function
433
+ let nodeStrength = this.nodeStrength;
434
+ let nodeStrengthFunc;
435
+ if (!nodeStrength) {
436
+ nodeStrength = 30;
437
+ }
438
+ if (isNumber(nodeStrength)) {
439
+ nodeStrengthFunc = (d: any) => {
440
+ return nodeStrength;
441
+ };
442
+ } else {
443
+ nodeStrengthFunc = nodeStrength;
444
+ }
445
+ this.nodeStrength = nodeStrengthFunc as ((d?: unknown) => number);
446
+ }
447
+
448
+ private initPos(comboMap: ComboMap) {
449
+ const self = this;
450
+ const nodes = self.nodes;
451
+ nodes.forEach((node, i) => {
452
+ const comboId = (node as any).comboId;
453
+ const combo: any = comboMap[comboId];
454
+ if (comboId && combo) {
455
+ node.x = combo.cx + 100 / (i + 1);
456
+ node.y = combo.cy + 100 / (i + 1);
457
+ } else {
458
+ node.x = 100 / (i + 1);
459
+ node.y = 100 / (i + 1);
460
+ }
461
+ });
462
+ }
463
+
464
+ private getComboMap() {
465
+ const self = this;
466
+ const nodeMap = self.nodeMap;
467
+ const comboTrees = self.comboTrees;
468
+ const oriComboMap = self.oriComboMap;
469
+ const comboMap: ComboMap = {};
470
+
471
+ (comboTrees || []).forEach((ctree: any) => {
472
+ const treeChildren: Combo[] | Node[] = [];
473
+ traverseTreeUp<ComboTree>(ctree, (treeNode) => {
474
+ if (treeNode.itemType === "node") return true; // skip it
475
+ if (!oriComboMap[treeNode.id]) return true; // means it is hidden, skip it
476
+ if (comboMap[treeNode.id] === undefined) {
477
+ const combo = {
478
+ id: treeNode.id,
479
+ name: treeNode.id,
480
+ cx: 0,
481
+ cy: 0,
482
+ count: 0,
483
+ depth: self.oriComboMap[treeNode.id].depth as number || 0,
484
+ children: [] as any
485
+ };
486
+ comboMap[treeNode.id] = combo;
487
+ }
488
+ const children = treeNode.children;
489
+ if (children) {
490
+ children.forEach((child: any) => {
491
+ if (!comboMap[child.id] && !nodeMap[child.id]) return true; // means it is hidden
492
+ treeChildren.push(child);
493
+ });
494
+ }
495
+ const c: any = comboMap[treeNode.id];
496
+ c.cx = 0;
497
+ c.cy = 0;
498
+
499
+ if (treeChildren.length === 0) {
500
+ c.empty = true;
501
+ const oriCombo = oriComboMap[treeNode.id];
502
+ c.cx = oriCombo.x as number;
503
+ c.cy = oriCombo.y as number;
504
+ }
505
+
506
+ treeChildren.forEach((child: Combo | Node) => {
507
+ (c.count as number)++;
508
+ if (child.itemType !== "node") {
509
+ const childCombo = comboMap[child.id];
510
+ if (isNumber(childCombo.cx)) c.cx += childCombo.cx;
511
+ if (isNumber(childCombo.cy)) c.cy += childCombo.cy;
512
+ return;
513
+ }
514
+ const node = nodeMap[child.id];
515
+ // means the node is hidden, skip it
516
+ if (!node) return;
517
+
518
+ if (isNumber(node.x)) {
519
+ c.cx += node.x;
520
+ }
521
+ if (isNumber(node.y)) {
522
+ c.cy += node.y;
523
+ }
524
+ });
525
+ c.cx /= (c.count || 1) as number;
526
+ c.cy /= (c.count || 1) as number;
527
+
528
+ c.children = treeChildren as any;
529
+
530
+ return true;
531
+ });
532
+ });
533
+
534
+ return comboMap;
535
+ }
536
+
537
+ private applyComboCenterForce(displacements: Point[]) {
538
+ const self = this;
539
+ const gravity = self.gravity;
540
+ const comboGravity = self.comboGravity || gravity;
541
+ const alpha = this.alpha;
542
+ const comboTrees = self.comboTrees;
543
+ const indexMap = self.indexMap;
544
+ const nodeMap = self.nodeMap;
545
+ const comboMap = self.comboMap;
546
+
547
+ (comboTrees || []).forEach((ctree) => {
548
+ traverseTreeUp<ComboTree>(ctree, (treeNode) => {
549
+ if (treeNode.itemType === "node") return true; // skip it
550
+ const combo = comboMap[treeNode.id];
551
+ // means the combo is hidden, skip it
552
+ if (!combo) return true;
553
+ const c: any = comboMap[treeNode.id];
554
+
555
+ // higher depth the combo, larger the gravity
556
+ const gravityScale = (((c.depth as number) + 1) / 10) * 0.5;
557
+ // apply combo center force for all the descend nodes in this combo
558
+ // and update the center position and count for this combo
559
+ const comboX = c.cx;
560
+ const comboY = c.cy;
561
+ c.cx = 0;
562
+ c.cy = 0;
563
+ c.children!.forEach((child: any) => {
564
+ if (child.itemType !== "node") {
565
+ const childCombo = comboMap[child.id];
566
+ if (childCombo && isNumber(childCombo.cx)) c.cx += childCombo.cx;
567
+ if (childCombo && isNumber(childCombo.cy)) c.cy += childCombo.cy;
568
+ return;
569
+ }
570
+ const node = nodeMap[child.id];
571
+ const vecX = (node.x - comboX) || 0.005;
572
+ const vecY = (node.y - comboY) || 0.005;
573
+ const l = Math.sqrt(vecX * vecX + vecY * vecY);
574
+ const childIdx = indexMap[node.id];
575
+ const params = ((comboGravity * alpha) / l) * gravityScale;
576
+ displacements[childIdx].x -= vecX * params;
577
+ displacements[childIdx].y -= vecY * params;
578
+
579
+ if (isNumber(node.x)) c.cx += node.x;
580
+ if (isNumber(node.y)) c.cy += node.y;
581
+ });
582
+ c.cx /= (c.count || 1) as number;
583
+ c.cy /= (c.count || 1) as number;
584
+ return true;
585
+ });
586
+ });
587
+ }
588
+
589
+ private applyCalculate(displacements: Point[]) {
590
+ const self = this;
591
+ const comboMap = self.comboMap;
592
+ const nodes = self.nodes;
593
+ // store the vx, vy, and distance to reduce dulplicate calculation
594
+ const vecMap: any = {};
595
+ nodes.forEach((v, i) => {
596
+ nodes.forEach((u, j) => {
597
+ if (i < j) return;
598
+ const vx = (v.x - u.x) || 0.005;
599
+ const vy = (v.y - u.y) || 0.005;
600
+ let vl2 = vx * vx + vy * vy;
601
+ const vl = Math.sqrt(vl2);
602
+ if (vl2 < 1) vl2 = vl;
603
+ vecMap[`${v.id}-${u.id}`] = { vx, vy, vl2, vl };
604
+ vecMap[`${u.id}-${v.id}`] = { vl2, vl, vx: -vx, vy: -vy };
605
+ });
606
+ });
607
+ // get the sizes of the combos
608
+ self.updateComboSizes(comboMap);
609
+ self.calRepulsive(displacements, vecMap);
610
+ self.calAttractive(displacements, vecMap);
611
+
612
+ const preventComboOverlap = self.preventComboOverlap;
613
+ if (preventComboOverlap) self.comboNonOverlapping(displacements, comboMap);
614
+ }
615
+
616
+ /**
617
+ * Update the sizes of the combos according to their children
618
+ * Used for combos nonoverlap, but not re-render the combo shapes
619
+ */
620
+ private updateComboSizes(comboMap: ComboMap) {
621
+ const self = this;
622
+ const comboTrees = self.comboTrees;
623
+ const nodeMap = self.nodeMap;
624
+ const nodeSize = self.nodeSize as ((d?: unknown) => number);
625
+ const comboSpacing = self.comboSpacing as ((d?: unknown) => number);
626
+ const comboPadding = self.comboPadding as ((d?: unknown) => number);
627
+ (comboTrees || []).forEach((ctree) => {
628
+ const treeChildren: Combo[] | Node[] = [];
629
+ traverseTreeUp<ComboTree>(ctree, (treeNode: ComboTree) => {
630
+ if (treeNode.itemType === "node") return true; // skip it
631
+ const c = comboMap[treeNode.id];
632
+ // means the combo is hidden, skip it
633
+ if (!c) return false;
634
+ const children = treeNode.children;
635
+ if (children) {
636
+ children.forEach((child: any) => {
637
+ // means the combo is hidden.
638
+ if (!comboMap[child.id] && !nodeMap[child.id]) return;
639
+ treeChildren.push(child);
640
+ });
641
+ }
642
+
643
+ c.minX = Infinity;
644
+ c.minY = Infinity;
645
+ c.maxX = -Infinity;
646
+ c.maxY = -Infinity;
647
+ treeChildren.forEach((child: any) => {
648
+ if (child.itemType !== "node") return true; // skip it
649
+ const node = nodeMap[child.id];
650
+ if (!node) return true; // means it is hidden
651
+ const r = nodeSize(node);
652
+ const nodeMinX = node.x - r;
653
+ const nodeMinY = node.y - r;
654
+ const nodeMaxX = node.x + r;
655
+ const nodeMaxY = node.y + r;
656
+ if (c.minX! > nodeMinX) c.minX = nodeMinX;
657
+ if (c.minY! > nodeMinY) c.minY = nodeMinY;
658
+ if (c.maxX! < nodeMaxX) c.maxX = nodeMaxX;
659
+ if (c.maxY! < nodeMaxY) c.maxY = nodeMaxY;
660
+ });
661
+ let minSize = self.oriComboMap[treeNode.id].size || 10;
662
+ if (isArray(minSize)) minSize = minSize[0];
663
+ const maxLength = Math.max(
664
+ c.maxX - c.minX,
665
+ c.maxY - c.minY,
666
+ minSize as number
667
+ );
668
+ c.r = maxLength / 2 + comboSpacing(c) / 2 + comboPadding(c);
669
+
670
+ return true;
671
+ });
672
+ });
673
+ }
674
+
675
+ /**
676
+ * prevent the overlappings among combos
677
+ */
678
+ private comboNonOverlapping(displacements: Point[], comboMap: ComboMap) {
679
+ const self = this;
680
+ const comboTree = self.comboTree;
681
+ const comboCollideStrength = self.comboCollideStrength as number;
682
+ const indexMap = self.indexMap;
683
+ const nodeMap = self.nodeMap;
684
+
685
+ traverseTreeUp<ComboTree>(comboTree, (treeNode) => {
686
+ if (
687
+ !comboMap[treeNode.id] &&
688
+ !nodeMap[treeNode.id] &&
689
+ treeNode.id !== "comboTreeRoot"
690
+ ) {
691
+ return false;
692
+ } // means it is hidden
693
+ const children = treeNode.children;
694
+ // 同个子树下的子 combo 间两两对比
695
+ if (children && children.length > 1) {
696
+ children.forEach((v, i) => {
697
+ if (v.itemType === "node") return false; // skip it
698
+ const cv: any = comboMap[v.id];
699
+ if (!cv) return; // means it is hidden, skip it
700
+ children.forEach((u, j) => {
701
+ if (i <= j) return false;
702
+ if (u.itemType === "node") return false; // skip it
703
+ const cu: any = comboMap[u.id];
704
+ if (!cu) return false; // means it is hidden, skip it
705
+ const vx = (cv.cx - cu.cx) || 0.005;
706
+ const vy = (cv.cy - cu.cy) || 0.005;
707
+ const l = vx * vx + vy * vy;
708
+ const rv = cv.r as number || 1;
709
+ const ru = cu.r as number || 1;
710
+ const r = rv + ru;
711
+ const ru2 = ru * ru;
712
+ const rv2 = rv * rv;
713
+ // overlapping
714
+ if (l < r * r) {
715
+ const vnodes = v.children;
716
+ if (!vnodes || vnodes.length === 0) return false; // skip it
717
+ const unodes = u.children;
718
+ if (!unodes || unodes.length === 0) return false; // skip it
719
+ const sqrtl = Math.sqrt(l);
720
+ const ll = ((r - sqrtl) / sqrtl) * comboCollideStrength;
721
+ const xl = vx * ll;
722
+ const yl = vy * ll;
723
+ const rratio = ru2 / (rv2 + ru2);
724
+ const irratio = 1 - rratio;
725
+ // 两兄弟 combo 的子节点上施加斥力
726
+ vnodes.forEach((vn) => {
727
+ if (vn.itemType !== "node") return false; // skip it
728
+ if (!nodeMap[vn.id]) return; // means it is hidden, skip it
729
+ const vindex = indexMap[vn.id];
730
+ unodes.forEach((un) => {
731
+ if (un.itemType !== "node") return false;
732
+ if (!nodeMap[un.id]) return false; // means it is hidden, skip it
733
+ const uindex = indexMap[un.id];
734
+ displacements[vindex].x += xl * rratio;
735
+ displacements[vindex].y += yl * rratio;
736
+ displacements[uindex].x -= xl * irratio;
737
+ displacements[uindex].y -= yl * irratio;
738
+ });
739
+ });
740
+ }
741
+ });
742
+ });
743
+ }
744
+ return true;
745
+ });
746
+ }
747
+
748
+ /**
749
+ * Calculate the repulsive force between each node pair
750
+ * @param displacements The array stores the displacements for nodes
751
+ * @param vecMap The map stores vector between each node pair
752
+ */
753
+ private calRepulsive(displacements: Point[], vecMap: any) {
754
+ const self = this;
755
+ const nodes = self.nodes;
756
+ const max = self.width * self.optimizeRangeFactor;
757
+ const nodeStrength = self.nodeStrength as (d?: unknown) => number;
758
+ const alpha = self.alpha;
759
+ const nodeCollideStrength = self.nodeCollideStrength as number;
760
+ const preventNodeOverlap = self.preventNodeOverlap;
761
+ const nodeSizeFunc = self.nodeSize as ((d?: unknown) => number);
762
+ const nodeSpacingFunc = self.nodeSpacing as ((d?: unknown) => number);
763
+ const scale = self.depthRepulsiveForceScale;
764
+ const center = self.center;
765
+ nodes.forEach((v, i) => {
766
+ if (!v.x || !v.y) return;
767
+
768
+ // center gravity
769
+ if (center) {
770
+ const gravity = self.gravity;
771
+ const vecX = (v.x - center[0]) || 0.005;
772
+ const vecY = (v.y - center[1]) || 0.005;
773
+ const l = Math.sqrt(vecX * vecX + vecY * vecY);
774
+ displacements[i].x -= (vecX * gravity * alpha) / l;
775
+ displacements[i].y -= (vecY * gravity * alpha) / l;
776
+ }
777
+
778
+ nodes.forEach((u, j) => {
779
+ if (i === j) {
780
+ return;
781
+ }
782
+ if (!u.x || !u.y) return;
783
+ const { vl2, vl } = vecMap[`${v.id}-${u.id}`];
784
+ if (vl > max) return;
785
+
786
+ const { vx, vy } = vecMap[`${v.id}-${u.id}`];
787
+
788
+ let depthDiff = Math.log(Math.abs(u.depth - v.depth) / 10) + 1 || 1;
789
+ depthDiff = depthDiff < 1 ? 1 : depthDiff;
790
+ if (u.comboId !== v.comboId) depthDiff += 1;
791
+ const depthParam = depthDiff ? scale ** depthDiff : 1;
792
+
793
+ const params = ((nodeStrength(u) * alpha) / vl2) * depthParam;
794
+ displacements[i].x += vx * params;
795
+ displacements[i].y += vy * params;
796
+
797
+ // prevent node overlappings
798
+ if (i < j && preventNodeOverlap) {
799
+ const ri = (nodeSizeFunc(v) + nodeSpacingFunc(v)) || 1;
800
+ const rj = (nodeSizeFunc(u) + nodeSpacingFunc(u)) || 1;
801
+ const r = ri + rj;
802
+ if (vl2 < r * r) {
803
+ const ll = ((r - vl) / vl) * nodeCollideStrength;
804
+ const rj2 = rj * rj;
805
+ let rratio = rj2 / (ri * ri + rj2);
806
+ const xl = vx * ll;
807
+ const yl = vy * ll;
808
+ displacements[i].x += xl * rratio;
809
+ displacements[i].y += yl * rratio;
810
+ rratio = 1 - rratio;
811
+ displacements[j].x -= xl * rratio;
812
+ displacements[j].y -= yl * rratio;
813
+ }
814
+ }
815
+ });
816
+ });
817
+ }
818
+
819
+ /**
820
+ * Calculate the attractive force between the node pair with edge
821
+ * @param displacements The array stores the displacements for nodes
822
+ * @param vecMap The map stores vector between each node pair
823
+ */
824
+ private calAttractive(displacements: Point[], vecMap: any) {
825
+ const self = this;
826
+ const edges = self.edges;
827
+ const linkDistance = self.linkDistance as (d?: unknown) => number;
828
+ const alpha = self.alpha;
829
+ const edgeStrength = self.edgeStrength as (d?: unknown) => number;
830
+ const bias = self.bias;
831
+ const scale = self.depthAttractiveForceScale;
832
+ edges.forEach((e, i) => {
833
+ const source = getEdgeTerminal(e, 'source');
834
+ const target = getEdgeTerminal(e, 'target');
835
+ if (!source || !target || source === target) return;
836
+ const uIndex = self.indexMap[source];
837
+ const vIndex = self.indexMap[target];
838
+ const u: Node = self.nodeMap[source];
839
+ const v: Node = self.nodeMap[target];
840
+ if (!u || !v) return;
841
+
842
+ let depthDiff = u.depth === v.depth ? 0 : Math.log(Math.abs(u.depth - v.depth) / 10);
843
+ if (u.comboId === v.comboId) {
844
+ depthDiff = depthDiff / 2;
845
+ }
846
+ let depthParam = depthDiff ? scale ** depthDiff : 1;
847
+ if (u.comboId !== v.comboId && depthParam === 1) {
848
+ depthParam = scale / 2;
849
+ } else if (u.comboId === v.comboId) {
850
+ depthParam = 2;
851
+ }
852
+
853
+ if (!isNumber(v.x) || !isNumber(u.x) || !isNumber(v.y) || !isNumber(u.y)) {
854
+ return;
855
+ }
856
+ const { vl, vx, vy } = vecMap[`${target}-${source}`];
857
+ const l =
858
+ ((vl - linkDistance(e)) / vl) * alpha * edgeStrength(e) * depthParam;
859
+ const vecX = vx * l;
860
+ const vecY = vy * l;
861
+
862
+ const b = bias[i];
863
+ displacements[vIndex].x -= vecX * b;
864
+ displacements[vIndex].y -= vecY * b;
865
+ displacements[uIndex].x += vecX * (1 - b);
866
+ displacements[uIndex].y += vecY * (1 - b);
867
+ });
868
+ }
869
+
870
+ public getType() {
871
+ return "comboForce";
872
+ }
873
+ }