@antv/g6 5.0.25 → 5.0.27

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 (229) hide show
  1. package/README.en-US.md +5 -5
  2. package/README.md +5 -5
  3. package/dist/g6.min.js +78 -5
  4. package/dist/lib/behaviors/auto-adapt-label.d.ts +99 -0
  5. package/dist/lib/behaviors/fix-element-size.d.ts +63 -19
  6. package/dist/lib/behaviors/index.d.ts +2 -0
  7. package/dist/lib/behaviors/optimize-viewport-transform.d.ts +8 -4
  8. package/dist/lib/elements/combos/base-combo.d.ts +1 -1
  9. package/dist/lib/exports.d.ts +3 -3
  10. package/dist/lib/plugins/edge-bundling/index.d.ts +97 -0
  11. package/dist/lib/plugins/index.d.ts +2 -0
  12. package/dist/lib/runtime/data.d.ts +1 -1
  13. package/dist/lib/runtime/element.d.ts +1 -1
  14. package/dist/lib/runtime/viewport.d.ts +3 -1
  15. package/dist/lib/transforms/map-node-size.d.ts +12 -21
  16. package/dist/lib/transforms/process-parallel-edges.d.ts +4 -0
  17. package/dist/lib/transforms/utils.d.ts +9 -0
  18. package/dist/lib/types/centrality.d.ts +20 -0
  19. package/dist/lib/types/index.d.ts +1 -0
  20. package/dist/lib/utils/animation.d.ts +1 -1
  21. package/dist/lib/utils/bbox.d.ts +9 -0
  22. package/dist/lib/utils/centrality.d.ts +53 -0
  23. package/dist/lib/utils/padding.d.ts +16 -0
  24. package/dist/lib/utils/transform.d.ts +2 -14
  25. package/dist/lib/version.d.ts +1 -1
  26. package/esm/animations/executor.js +1 -0
  27. package/esm/animations/executor.js.map +1 -1
  28. package/esm/behaviors/auto-adapt-label.d.ts +99 -0
  29. package/esm/behaviors/auto-adapt-label.js +167 -0
  30. package/esm/behaviors/auto-adapt-label.js.map +1 -0
  31. package/esm/behaviors/fix-element-size.d.ts +63 -19
  32. package/esm/behaviors/fix-element-size.js +142 -59
  33. package/esm/behaviors/fix-element-size.js.map +1 -1
  34. package/esm/behaviors/hover-activate.js +1 -2
  35. package/esm/behaviors/hover-activate.js.map +1 -1
  36. package/esm/behaviors/index.d.ts +2 -0
  37. package/esm/behaviors/index.js +1 -0
  38. package/esm/behaviors/index.js.map +1 -1
  39. package/esm/behaviors/optimize-viewport-transform.d.ts +8 -4
  40. package/esm/behaviors/optimize-viewport-transform.js +31 -11
  41. package/esm/behaviors/optimize-viewport-transform.js.map +1 -1
  42. package/esm/elements/combos/base-combo.d.ts +1 -1
  43. package/esm/elements/combos/base-combo.js +1 -1
  44. package/esm/elements/combos/base-combo.js.map +1 -1
  45. package/esm/elements/nodes/base-node.js +1 -1
  46. package/esm/elements/nodes/base-node.js.map +1 -1
  47. package/esm/elements/shapes/base-shape.js +1 -1
  48. package/esm/elements/shapes/base-shape.js.map +1 -1
  49. package/esm/exports.d.ts +3 -3
  50. package/esm/exports.js +1 -1
  51. package/esm/exports.js.map +1 -1
  52. package/esm/plugins/edge-bundling/index.d.ts +97 -0
  53. package/esm/plugins/edge-bundling/index.js +252 -0
  54. package/esm/plugins/edge-bundling/index.js.map +1 -0
  55. package/esm/plugins/fisheye/index.js +1 -1
  56. package/esm/plugins/fisheye/index.js.map +1 -1
  57. package/esm/plugins/index.d.ts +2 -0
  58. package/esm/plugins/index.js +1 -0
  59. package/esm/plugins/index.js.map +1 -1
  60. package/esm/preset.js +0 -2
  61. package/esm/preset.js.map +1 -1
  62. package/esm/registry/build-in.js +4 -2
  63. package/esm/registry/build-in.js.map +1 -1
  64. package/esm/runtime/behavior.js +1 -1
  65. package/esm/runtime/behavior.js.map +1 -1
  66. package/esm/runtime/data.d.ts +1 -1
  67. package/esm/runtime/element.d.ts +1 -1
  68. package/esm/runtime/element.js +3 -0
  69. package/esm/runtime/element.js.map +1 -1
  70. package/esm/runtime/viewport.d.ts +3 -1
  71. package/esm/runtime/viewport.js +13 -4
  72. package/esm/runtime/viewport.js.map +1 -1
  73. package/esm/transforms/collapse-expand-combo.js +0 -1
  74. package/esm/transforms/collapse-expand-combo.js.map +1 -1
  75. package/esm/transforms/map-node-size.d.ts +12 -21
  76. package/esm/transforms/map-node-size.js +34 -178
  77. package/esm/transforms/map-node-size.js.map +1 -1
  78. package/esm/transforms/place-radial-labels.js +5 -3
  79. package/esm/transforms/place-radial-labels.js.map +1 -1
  80. package/esm/transforms/process-parallel-edges.d.ts +4 -0
  81. package/esm/transforms/process-parallel-edges.js +41 -37
  82. package/esm/transforms/process-parallel-edges.js.map +1 -1
  83. package/esm/transforms/utils.d.ts +9 -0
  84. package/esm/transforms/utils.js +11 -0
  85. package/esm/transforms/utils.js.map +1 -1
  86. package/esm/types/centrality.d.ts +20 -0
  87. package/esm/types/centrality.js +2 -0
  88. package/esm/types/centrality.js.map +1 -0
  89. package/esm/types/index.d.ts +1 -0
  90. package/esm/utils/animation.d.ts +1 -1
  91. package/esm/utils/bbox.d.ts +9 -0
  92. package/esm/utils/bbox.js +15 -0
  93. package/esm/utils/bbox.js.map +1 -1
  94. package/esm/utils/centrality.d.ts +53 -0
  95. package/esm/utils/centrality.js +177 -0
  96. package/esm/utils/centrality.js.map +1 -0
  97. package/esm/utils/edge.js +5 -2
  98. package/esm/utils/edge.js.map +1 -1
  99. package/esm/utils/element.js +1 -1
  100. package/esm/utils/element.js.map +1 -1
  101. package/esm/utils/padding.d.ts +16 -0
  102. package/esm/utils/padding.js +22 -0
  103. package/esm/utils/padding.js.map +1 -1
  104. package/esm/utils/polygon.js +3 -3
  105. package/esm/utils/polygon.js.map +1 -1
  106. package/esm/utils/transform.d.ts +2 -14
  107. package/esm/utils/transform.js +18 -23
  108. package/esm/utils/transform.js.map +1 -1
  109. package/esm/version.d.ts +1 -1
  110. package/esm/version.js +1 -1
  111. package/lib/animations/executor.js +1 -0
  112. package/lib/animations/executor.js.map +1 -1
  113. package/lib/behaviors/auto-adapt-label.d.ts +99 -0
  114. package/lib/behaviors/auto-adapt-label.js +171 -0
  115. package/lib/behaviors/auto-adapt-label.js.map +1 -0
  116. package/lib/behaviors/fix-element-size.d.ts +63 -19
  117. package/lib/behaviors/fix-element-size.js +141 -58
  118. package/lib/behaviors/fix-element-size.js.map +1 -1
  119. package/lib/behaviors/hover-activate.js +1 -2
  120. package/lib/behaviors/hover-activate.js.map +1 -1
  121. package/lib/behaviors/index.d.ts +2 -0
  122. package/lib/behaviors/index.js +3 -1
  123. package/lib/behaviors/index.js.map +1 -1
  124. package/lib/behaviors/optimize-viewport-transform.d.ts +8 -4
  125. package/lib/behaviors/optimize-viewport-transform.js +31 -11
  126. package/lib/behaviors/optimize-viewport-transform.js.map +1 -1
  127. package/lib/elements/combos/base-combo.d.ts +1 -1
  128. package/lib/elements/combos/base-combo.js +1 -1
  129. package/lib/elements/combos/base-combo.js.map +1 -1
  130. package/lib/elements/nodes/base-node.js +1 -1
  131. package/lib/elements/nodes/base-node.js.map +1 -1
  132. package/lib/elements/shapes/base-shape.js +1 -1
  133. package/lib/elements/shapes/base-shape.js.map +1 -1
  134. package/lib/exports.d.ts +3 -3
  135. package/lib/exports.js +3 -2
  136. package/lib/exports.js.map +1 -1
  137. package/lib/plugins/edge-bundling/index.d.ts +97 -0
  138. package/lib/plugins/edge-bundling/index.js +256 -0
  139. package/lib/plugins/edge-bundling/index.js.map +1 -0
  140. package/lib/plugins/fisheye/index.js +1 -1
  141. package/lib/plugins/fisheye/index.js.map +1 -1
  142. package/lib/plugins/index.d.ts +2 -0
  143. package/lib/plugins/index.js +3 -1
  144. package/lib/plugins/index.js.map +1 -1
  145. package/lib/preset.js +0 -2
  146. package/lib/preset.js.map +1 -1
  147. package/lib/registry/build-in.js +2 -0
  148. package/lib/registry/build-in.js.map +1 -1
  149. package/lib/runtime/behavior.js +1 -1
  150. package/lib/runtime/behavior.js.map +1 -1
  151. package/lib/runtime/data.d.ts +1 -1
  152. package/lib/runtime/element.d.ts +1 -1
  153. package/lib/runtime/element.js +3 -0
  154. package/lib/runtime/element.js.map +1 -1
  155. package/lib/runtime/viewport.d.ts +3 -1
  156. package/lib/runtime/viewport.js +12 -3
  157. package/lib/runtime/viewport.js.map +1 -1
  158. package/lib/transforms/collapse-expand-combo.js +0 -1
  159. package/lib/transforms/collapse-expand-combo.js.map +1 -1
  160. package/lib/transforms/map-node-size.d.ts +12 -21
  161. package/lib/transforms/map-node-size.js +33 -177
  162. package/lib/transforms/map-node-size.js.map +1 -1
  163. package/lib/transforms/place-radial-labels.js +5 -3
  164. package/lib/transforms/place-radial-labels.js.map +1 -1
  165. package/lib/transforms/process-parallel-edges.d.ts +4 -0
  166. package/lib/transforms/process-parallel-edges.js +41 -37
  167. package/lib/transforms/process-parallel-edges.js.map +1 -1
  168. package/lib/transforms/utils.d.ts +9 -0
  169. package/lib/transforms/utils.js +12 -0
  170. package/lib/transforms/utils.js.map +1 -1
  171. package/lib/types/centrality.d.ts +20 -0
  172. package/lib/types/centrality.js +3 -0
  173. package/lib/types/centrality.js.map +1 -0
  174. package/lib/types/index.d.ts +1 -0
  175. package/lib/utils/animation.d.ts +1 -1
  176. package/lib/utils/bbox.d.ts +9 -0
  177. package/lib/utils/bbox.js +16 -0
  178. package/lib/utils/bbox.js.map +1 -1
  179. package/lib/utils/centrality.d.ts +53 -0
  180. package/lib/utils/centrality.js +187 -0
  181. package/lib/utils/centrality.js.map +1 -0
  182. package/lib/utils/edge.js +5 -2
  183. package/lib/utils/edge.js.map +1 -1
  184. package/lib/utils/element.js +1 -1
  185. package/lib/utils/element.js.map +1 -1
  186. package/lib/utils/padding.d.ts +16 -0
  187. package/lib/utils/padding.js +24 -0
  188. package/lib/utils/padding.js.map +1 -1
  189. package/lib/utils/polygon.js +3 -3
  190. package/lib/utils/polygon.js.map +1 -1
  191. package/lib/utils/transform.d.ts +2 -14
  192. package/lib/utils/transform.js +18 -25
  193. package/lib/utils/transform.js.map +1 -1
  194. package/lib/version.d.ts +1 -1
  195. package/lib/version.js +1 -1
  196. package/package.json +8 -8
  197. package/src/animations/executor.ts +1 -0
  198. package/src/behaviors/auto-adapt-label.ts +255 -0
  199. package/src/behaviors/fix-element-size.ts +213 -83
  200. package/src/behaviors/hover-activate.ts +2 -3
  201. package/src/behaviors/index.ts +2 -0
  202. package/src/behaviors/optimize-viewport-transform.ts +36 -18
  203. package/src/elements/combos/base-combo.ts +1 -1
  204. package/src/elements/nodes/base-node.ts +1 -1
  205. package/src/elements/shapes/base-shape.ts +1 -1
  206. package/src/exports.ts +3 -0
  207. package/src/plugins/edge-bundling/index.ts +373 -0
  208. package/src/plugins/fisheye/index.ts +1 -1
  209. package/src/plugins/index.ts +2 -0
  210. package/src/preset.ts +0 -2
  211. package/src/registry/build-in.ts +4 -0
  212. package/src/runtime/behavior.ts +1 -0
  213. package/src/runtime/element.ts +3 -1
  214. package/src/runtime/viewport.ts +14 -4
  215. package/src/transforms/collapse-expand-combo.ts +0 -1
  216. package/src/transforms/map-node-size.ts +52 -209
  217. package/src/transforms/place-radial-labels.ts +6 -3
  218. package/src/transforms/process-parallel-edges.ts +50 -41
  219. package/src/transforms/utils.ts +12 -0
  220. package/src/types/centrality.ts +8 -0
  221. package/src/types/index.ts +1 -0
  222. package/src/utils/bbox.ts +17 -0
  223. package/src/utils/centrality.ts +209 -0
  224. package/src/utils/edge.ts +6 -3
  225. package/src/utils/element.ts +1 -1
  226. package/src/utils/padding.ts +24 -0
  227. package/src/utils/polygon.ts +3 -3
  228. package/src/utils/transform.ts +25 -32
  229. package/src/version.ts +1 -1
@@ -0,0 +1,373 @@
1
+ import { isEmpty } from '@antv/util';
2
+ import { GraphEvent } from '../../constants';
3
+ import type { RuntimeContext } from '../../runtime/types';
4
+ import type { EdgeData } from '../../spec';
5
+ import type { ID, Point } from '../../types';
6
+ import { getPolylinePath } from '../../utils/edge';
7
+ import { idOf } from '../../utils/id';
8
+ import { positionOf } from '../../utils/position';
9
+ import { add, distance, divide, dot, multiply, subtract, toVector2 } from '../../utils/vector';
10
+ import type { BasePluginOptions } from '../base-plugin';
11
+ import { BasePlugin } from '../base-plugin';
12
+
13
+ /**
14
+ * <zh/> 边绑定插件的配置项
15
+ *
16
+ * <en/> Edge bundling options
17
+ */
18
+ export interface EdgeBundlingOptions extends BasePluginOptions {
19
+ /**
20
+ * <zh/> 边的强度
21
+ *
22
+ * <en/> The strength of the edge
23
+ * @defaultValue 0.1
24
+ */
25
+ K?: number;
26
+ /**
27
+ * <zh/> 初始步长。在后续的周期中,步长将双倍递增
28
+ *
29
+ * <en/> An initial step size. In subsequent cycles, the step size will double incrementally
30
+ * @defaultValue 0.1
31
+ */
32
+ lambda?: number;
33
+ /**
34
+ * <zh/> 模拟周期数
35
+ *
36
+ * <en/> The number of simulation cycles
37
+ * @defaultValue 6
38
+ */
39
+ cycles?: number;
40
+ /**
41
+ * <zh/> 初始切割点数。在后续的周期中,切割点数将根据 `divRate` 逐步递增
42
+ *
43
+ * <en/> An initial number of subdivision points for each edge. In subsequent cycles, the number of subdivision points will increase gradually according to `divRate`
44
+ * @defaultValue 1
45
+ */
46
+ divisions?: number;
47
+ /**
48
+ * <zh/> 切割点数增长率
49
+ *
50
+ * <en/> The rate at which the number of subdivision points increases
51
+ * @defaultValue 2
52
+ */
53
+ divRate?: number;
54
+ /**
55
+ * <zh/> 指定在第一个周期中执行的迭代次数。在后续的周期中,迭代次数将根据 `iterRate` 逐步递减
56
+ *
57
+ * <en/> The number of iteration steps during the first cycle. In subsequent cycles, the number of iterations will decrease gradually according to `iterRate`
58
+ * @defaultValue 90
59
+ */
60
+ iterations?: number;
61
+ /**
62
+ * <zh/> 迭代次数递减率
63
+ *
64
+ * <en/> The rate at which the number of iterations decreases
65
+ * @defaultValue 2 / 3
66
+ */
67
+ iterRate?: number;
68
+ /**
69
+ * <zh/> 边兼容性阈值,决定了哪些边应该被绑定在一起
70
+ *
71
+ * <en/> Edge compatibility threshold, which determines which edges should be bundled together
72
+ * @defaultValue 0.6
73
+ */
74
+ bundleThreshold?: number;
75
+ }
76
+
77
+ /**
78
+ * <zh/> 边绑定
79
+ *
80
+ * <en/> Edge bundling
81
+ * @remarks
82
+ * <zh/> 边绑定(Edge Bundling)是一种图可视化技术,用于减少复杂网络图中的视觉混乱,并揭示图中的高级别模式和结构。其思想是将相邻的边捆绑在一起。
83
+ *
84
+ * <zh/> G6 中提供的边绑定插件是基于 FEDB(Force-Directed Edge Bundling for Graph Visualization)一文的实现:将边建模为可以相互吸引的柔性弹簧,通过自组织的方式进行捆绑。
85
+ *
86
+ * <en/> Edge bundling is a graph visualization technique used to reduce visual clutter in complex network graphs and reveal high-level patterns and structures in the graph. The idea is to bundle adjacent edges together.
87
+ *
88
+ * <en/> The edge bundling plugin provided in G6 is based on the implementation of the paper FEDB (Force-Directed Edge Bundling for Graph Visualization): modeling edges as flexible springs that can attract each other and bundling them in a self-organizing way.
89
+ */
90
+ export class EdgeBundling extends BasePlugin<EdgeBundlingOptions> {
91
+ static defaultOptions: Partial<EdgeBundlingOptions> = {
92
+ K: 0.1,
93
+ lambda: 0.1,
94
+ divisions: 1,
95
+ divRate: 2,
96
+ cycles: 6,
97
+ iterations: 90,
98
+ iterRate: 2 / 3,
99
+ bundleThreshold: 0.6,
100
+ };
101
+
102
+ constructor(context: RuntimeContext, options?: EdgeBundlingOptions) {
103
+ super(context, Object.assign({}, EdgeBundling.defaultOptions, options));
104
+ this.bindEvents();
105
+ }
106
+
107
+ private edgeBundles: Record<ID, EdgeData[]> = {};
108
+
109
+ private edgePoints: Record<ID, Point[]> = {};
110
+
111
+ private get nodeMap(): Record<ID, Point> {
112
+ const nodes = this.context.model.getNodeData();
113
+ return Object.fromEntries(nodes.map((node) => [idOf(node), toVector2(positionOf(node))]));
114
+ }
115
+
116
+ private divideEdges(divisions: number) {
117
+ const edges = this.context.model.getEdgeData();
118
+
119
+ edges.forEach((edge) => {
120
+ const edgeId = idOf(edge);
121
+ this.edgePoints[edgeId] ||= [];
122
+
123
+ const source = this.nodeMap[edge.source];
124
+ const target = this.nodeMap[edge.target];
125
+
126
+ if (divisions === 1) {
127
+ this.edgePoints[edgeId].push(source);
128
+ this.edgePoints[edgeId].push(divide(add(source, target), 2));
129
+ this.edgePoints[edgeId].push(target);
130
+ } else {
131
+ const edgeLength =
132
+ this.edgePoints[edgeId].length === 0
133
+ ? // edge is a straight line
134
+ distance(source, target)
135
+ : // edge is a polyline
136
+ getEdgeLength(this.edgePoints[edgeId]);
137
+
138
+ const divisionLength = edgeLength / (divisions + 1);
139
+ let currentDivisionLength = divisionLength;
140
+
141
+ const newEdgePoints: Point[] = [source];
142
+
143
+ for (let i = 1; i < this.edgePoints[edgeId].length; i++) {
144
+ const prevEp = this.edgePoints[edgeId][i - 1];
145
+ const ep = this.edgePoints[edgeId][i];
146
+ let oriDivisionLength = distance(ep, prevEp);
147
+
148
+ while (oriDivisionLength > currentDivisionLength) {
149
+ const ratio = currentDivisionLength / oriDivisionLength;
150
+ const edgePoint = add(prevEp, multiply(subtract(ep, prevEp), ratio));
151
+ newEdgePoints.push(edgePoint);
152
+
153
+ oriDivisionLength -= currentDivisionLength;
154
+ currentDivisionLength = divisionLength;
155
+ }
156
+
157
+ currentDivisionLength -= oriDivisionLength;
158
+ }
159
+
160
+ newEdgePoints.push(target);
161
+ this.edgePoints[edgeId] = newEdgePoints;
162
+ }
163
+ });
164
+ }
165
+
166
+ private getVectorPosition(edge: EdgeData): VectorPosition {
167
+ const source = this.nodeMap[edge.source];
168
+ const target = this.nodeMap[edge.target];
169
+ const [vx, vy] = subtract(target, source);
170
+ const length = distance(source, target);
171
+ return { source, target, vx, vy, length };
172
+ }
173
+
174
+ private measureEdgeCompatibility(edge1: EdgeData, edge2: EdgeData) {
175
+ const vector1 = this.getVectorPosition(edge1);
176
+ const vector2 = this.getVectorPosition(edge2);
177
+
178
+ const ac = getAngleCompatibility(vector1, vector2);
179
+ const sc = getScaleCompatibility(vector1, vector2);
180
+ const pc = getPositionCompatibility(vector1, vector2);
181
+ const vc = getVisibilityCompatibility(vector1, vector2);
182
+
183
+ return ac * sc * pc * vc;
184
+ }
185
+
186
+ private getEdgeBundles() {
187
+ const edgeBundles: Record<ID, EdgeData[]> = {};
188
+ const bundleThreshold = this.options.bundleThreshold;
189
+ const edges = this.context.model.getEdgeData();
190
+
191
+ edges.forEach((edge1, i) => {
192
+ edges.forEach((edge2, j) => {
193
+ if (j <= i) return;
194
+
195
+ const compatibility = this.measureEdgeCompatibility(edge1, edge2);
196
+ if (compatibility >= bundleThreshold) {
197
+ edgeBundles[idOf(edge1)] ||= [];
198
+ edgeBundles[idOf(edge1)].push(edge2);
199
+ edgeBundles[idOf(edge2)] ||= [];
200
+ edgeBundles[idOf(edge2)].push(edge1);
201
+ }
202
+ });
203
+ });
204
+ return edgeBundles;
205
+ }
206
+
207
+ private getSpringForce(divisions: { pre: Point; cur: Point; next: Point }, kp: number): Point {
208
+ const { pre, cur, next } = divisions;
209
+ return multiply(subtract(add(pre, next), multiply(cur, 2)), kp);
210
+ }
211
+
212
+ private getElectrostaticForce(pidx: number, edge: EdgeData): Point {
213
+ if (isEmpty(this.edgeBundles)) {
214
+ this.edgeBundles = this.getEdgeBundles();
215
+ }
216
+
217
+ const edgeBundle = this.edgeBundles[idOf(edge)];
218
+ let resForce: Point = [0, 0];
219
+
220
+ edgeBundle?.forEach((eb) => {
221
+ const p1 = this.edgePoints[idOf(eb)][pidx];
222
+ const p2 = this.edgePoints[idOf(edge)][pidx];
223
+ const force = subtract(p1, p2);
224
+ const length = distance(p1, p2);
225
+ resForce = add(resForce, multiply(force, 1 / length));
226
+ });
227
+
228
+ return resForce;
229
+ }
230
+
231
+ private getEdgeForces(edge: EdgeData, divisions: number, lambda: number): Point[] {
232
+ const source = this.nodeMap[edge.source];
233
+ const target = this.nodeMap[edge.target];
234
+ const kp = this.options.K / (distance(source, target) * (divisions + 1));
235
+ const edgePointForces: Point[] = [[0, 0]];
236
+ const edgeId = idOf(edge);
237
+
238
+ for (let i = 1; i < divisions; i++) {
239
+ const spring = this.getSpringForce(
240
+ {
241
+ pre: this.edgePoints[edgeId][i - 1],
242
+ cur: this.edgePoints[edgeId][i],
243
+ next: this.edgePoints[edgeId][i + 1] || [0, 0],
244
+ },
245
+ kp,
246
+ );
247
+ const electrostatic = this.getElectrostaticForce(i, edge);
248
+ edgePointForces.push(multiply(add(spring, electrostatic), lambda));
249
+ }
250
+
251
+ edgePointForces.push([0, 0]);
252
+
253
+ return edgePointForces;
254
+ }
255
+
256
+ protected onBundle = () => {
257
+ const { model, element } = this.context;
258
+ const edges = model.getEdgeData();
259
+ this.divideEdges(this.options.divisions);
260
+
261
+ const { cycles, iterRate, divRate } = this.options;
262
+ let { lambda, divisions, iterations } = this.options;
263
+ for (let i = 0; i < cycles; i++) {
264
+ for (let j = 0; j < iterations; j++) {
265
+ const forces: Record<ID, Point[]> = {};
266
+ edges.forEach((edge) => {
267
+ if (edge.source === edge.target) return;
268
+ const edgeId = idOf(edge);
269
+ forces[edgeId] = this.getEdgeForces(edge, divisions, lambda);
270
+
271
+ for (let p = 0; p < divisions + 1; p++) {
272
+ this.edgePoints[edgeId] ||= [];
273
+ this.edgePoints[edgeId][p] = add(this.edgePoints[edgeId][p], forces[edgeId][p]);
274
+ }
275
+ });
276
+ }
277
+
278
+ // parameters for next cycle
279
+ lambda /= 2;
280
+ divisions *= divRate;
281
+ iterations *= iterRate;
282
+ this.divideEdges(divisions);
283
+ }
284
+
285
+ edges.forEach((edge) => {
286
+ const edgeId = idOf(edge);
287
+ const edgeEl = element!.getElement(edgeId);
288
+ edgeEl?.update({ d: getPolylinePath(this.edgePoints[edgeId]) });
289
+ });
290
+ };
291
+
292
+ private bindEvents() {
293
+ const { graph } = this.context;
294
+
295
+ graph.on(GraphEvent.AFTER_RENDER, this.onBundle);
296
+ }
297
+
298
+ private unbindEvents() {
299
+ const { graph } = this.context;
300
+
301
+ graph.off(GraphEvent.AFTER_RENDER, this.onBundle);
302
+ }
303
+
304
+ public destroy(): void {
305
+ this.unbindEvents();
306
+ super.destroy();
307
+ }
308
+ }
309
+
310
+ interface VectorPosition {
311
+ source: Point;
312
+ target: Point;
313
+ vx: number;
314
+ vy: number;
315
+ length: number;
316
+ }
317
+
318
+ // The larger the angle between edges P and Q, the smaller Ca(P,Q).
319
+ // Ca(P,Q) is 0 if P and Q are orthogonal and 1 if P and Q are parallel.
320
+ const getAngleCompatibility = (p: VectorPosition, q: VectorPosition): number => {
321
+ return Math.abs(dot([p.vx, p.vy], [q.vx, q.vy]) / (p.length * q.length));
322
+ };
323
+
324
+ // Cs(P,Q) is 1 if P and Q have equal length and approaches 0 if the ratio between the longest and the shortest edge approaches ∞.
325
+ const getScaleCompatibility = (p: VectorPosition, q: VectorPosition): number => {
326
+ const aLength = (p.length + q.length) / 2;
327
+ return 2 / (aLength / Math.min(p.length, q.length) + Math.max(p.length, q.length) / aLength);
328
+ };
329
+
330
+ // Cp(P,Q) is 1 if Pm and Qm coincide and approaches 0 if ||Pm −Qm|| approaches ∞.
331
+ const getPositionCompatibility = (p: VectorPosition, q: VectorPosition): number => {
332
+ const aLength = (p.length + q.length) / 2;
333
+
334
+ const pMid = divide(add(p.source, p.target), 2);
335
+ const qMid = divide(add(q.source, q.target), 2);
336
+
337
+ return aLength / (aLength + distance(pMid, qMid));
338
+ };
339
+
340
+ const projectPointToEdge = (p: Point, e: VectorPosition): Point => {
341
+ if (e.source[0] === e.target[0]) return [e.source[0], p[1]];
342
+ if (e.source[1] === e.target[1]) return [p[0], e.source[1]];
343
+ const k = (e.source[1] - e.target[1]) / (e.source[0] - e.target[0]);
344
+ const x = (k * k * e.source[0] + k * (p[1] - e.source[1]) + p[0]) / (k * k + 1);
345
+ const y = k * (x - e.source[0]) + e.source[1];
346
+ return [x, y];
347
+ };
348
+
349
+ const getEdgeVisibility = (p: VectorPosition, q: VectorPosition): number => {
350
+ const is = projectPointToEdge(q.source, p);
351
+ const it = projectPointToEdge(q.target, p);
352
+ const iMid = divide(add(is, it), 2);
353
+ const pMid = divide(add(p.source, p.target), 2);
354
+ if (distance(is, it) === 0) return 0;
355
+ return Math.max(0, 1 - (2 * distance(pMid, iMid)) / distance(is, it));
356
+ };
357
+
358
+ const getVisibilityCompatibility = (p: VectorPosition, q: VectorPosition): number => {
359
+ return Math.min(getEdgeVisibility(p, q), getEdgeVisibility(q, p));
360
+ };
361
+
362
+ /**
363
+ * Calculate the length of a polyline
364
+ * @param points - The points of the polyline
365
+ * @returns The length of the polyline
366
+ */
367
+ const getEdgeLength = (points: Point[]): number => {
368
+ let length = 0;
369
+ for (let i = 1; i < points.length; i++) {
370
+ length += distance(points[i], points[i - 1]);
371
+ }
372
+ return length;
373
+ };
@@ -277,7 +277,7 @@ export class Fisheye extends BasePlugin<FisheyeOptions> {
277
277
 
278
278
  const update = (nodeId: ID, style: NodeStyle) => {
279
279
  const node = element!.getElement(nodeId) as Node;
280
- node.update(style);
280
+ node?.update(style);
281
281
 
282
282
  graph.getRelatedEdgesData(nodeId).forEach((datum) => {
283
283
  relatedEdges.add(idOf(datum));
@@ -3,6 +3,7 @@ export { BasePlugin } from './base-plugin';
3
3
  export { BubbleSets } from './bubble-sets';
4
4
  export { CameraSetting } from './camera-setting';
5
5
  export { Contextmenu } from './contextmenu';
6
+ export { EdgeBundling } from './edge-bundling';
6
7
  export { EdgeFilterLens } from './edge-filter-lens';
7
8
  export { Fisheye } from './fisheye';
8
9
  export { Fullscreen } from './fullscreen';
@@ -22,6 +23,7 @@ export type { BasePluginOptions } from './base-plugin';
22
23
  export type { BubbleSetsOptions } from './bubble-sets';
23
24
  export type { CameraSettingOptions } from './camera-setting';
24
25
  export type { ContextmenuOptions } from './contextmenu';
26
+ export type { EdgeBundlingOptions } from './edge-bundling';
25
27
  export type { EdgeFilterLensOptions } from './edge-filter-lens';
26
28
  export type { FisheyeOptions } from './fisheye';
27
29
  export type { FullscreenOptions } from './fullscreen';
package/src/preset.ts CHANGED
@@ -1,5 +1,3 @@
1
- import { runtime } from '@antv/g';
2
1
  import { registerBuiltInExtensions } from './registry/build-in';
3
2
 
4
- runtime.enableCSSParsing = false;
5
3
  registerBuiltInExtensions();
@@ -12,6 +12,7 @@ import {
12
12
  } from '@antv/g';
13
13
  import { ComboCollapse, ComboExpand, Fade, NodeCollapse, NodeExpand, PathIn, PathOut, Translate } from '../animations';
14
14
  import {
15
+ AutoAdaptLabel,
15
16
  BrushSelect,
16
17
  ClickSelect,
17
18
  CollapseExpand,
@@ -73,6 +74,7 @@ import {
73
74
  Background,
74
75
  BubbleSets,
75
76
  Contextmenu,
77
+ EdgeBundling,
76
78
  EdgeFilterLens,
77
79
  Fisheye,
78
80
  Fullscreen,
@@ -127,6 +129,7 @@ const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
127
129
  'focus-element': FocusElement,
128
130
  'hover-activate': HoverActivate,
129
131
  'lasso-select': LassoSelect,
132
+ 'auto-adapt-label': AutoAdaptLabel,
130
133
  'optimize-viewport-transform': OptimizeViewportTransform,
131
134
  'scroll-canvas': ScrollCanvas,
132
135
  'zoom-canvas': ZoomCanvas,
@@ -188,6 +191,7 @@ const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
188
191
  },
189
192
  plugin: {
190
193
  'bubble-sets': BubbleSets,
194
+ 'edge-bundling': EdgeBundling,
191
195
  'edge-filter-lens': EdgeFilterLens,
192
196
  'grid-line': GridLine,
193
197
  background: Background,
@@ -85,6 +85,7 @@ export class BehaviorController extends ExtensionController<BaseBehavior<CustomB
85
85
  ...stdEvent,
86
86
  type: CommonEvent.POINTER_LEAVE,
87
87
  target: this.currentTarget,
88
+ targetType: this.currentTargetType,
88
89
  });
89
90
  }
90
91
  if (targetElement) {
@@ -134,6 +134,9 @@ export class ElementController {
134
134
  private computeElementDefaultStyle(elementType: ElementType, context: StyleIterationContext) {
135
135
  const { options } = this.context;
136
136
  const defaultStyle = options[elementType]?.style || {};
137
+ if ('transform' in defaultStyle && Array.isArray(defaultStyle.transform)) {
138
+ defaultStyle.transform = [...defaultStyle.transform];
139
+ }
137
140
  this.defaultStyle[idOf(context.datum)] = computeElementCallbackStyle(defaultStyle as any, context);
138
141
  }
139
142
 
@@ -387,7 +390,6 @@ export class ElementController {
387
390
  const targetZIndex = this.getElementZIndex(context.target!);
388
391
  if (!style.zIndex || style.zIndex < targetZIndex) style.zIndex = targetZIndex + (style.zIndex ?? 0);
389
392
  }
390
-
391
393
  const element = this.container.appendChild(
392
394
  new Ctor({
393
395
  id,
@@ -3,7 +3,7 @@ import { clamp, isNumber, pick } from '@antv/util';
3
3
  import { AnimationType, GraphEvent } from '../constants';
4
4
  import type { FitViewOptions, ID, Point, TransformOptions, Vector2, ViewportAnimationEffectTiming } from '../types';
5
5
  import { getAnimationOptions } from '../utils/animation';
6
- import { getBBoxSize, getCombinedBBox, isPointInBBox } from '../utils/bbox';
6
+ import { getBBoxSize, getCombinedBBox, getExpandedBBox, isBBoxInside, isPointInBBox } from '../utils/bbox';
7
7
  import { AnimateEvent, ViewportEvent, emit } from '../utils/event';
8
8
  import { isPoint } from '../utils/is';
9
9
  import { parsePadding } from '../utils/padding';
@@ -279,19 +279,29 @@ export class ViewportController {
279
279
  *
280
280
  * <en/> Determine whether the point or bounding box is in the viewport
281
281
  * @param target - <zh/> 点或包围盒 | <en/> Point or bounding box
282
+ * @param complete - <zh/> 是否完全在视口中 | <en/> Whether it is completely in the viewport
283
+ * @param tolerance - <zh/> 视口外的容差 | <en/> Tolerance outside the viewport
282
284
  * @returns - <zh/> 是否在视口中 | <en/> Whether it is in the viewport
283
285
  */
284
- public isInViewport(target: Point | AABB) {
286
+ public isInViewport(target: Point | AABB, complete = false, tolerance = 0) {
285
287
  const { graph } = this.context;
286
288
  const size = this.getCanvasSize();
287
289
 
288
290
  const [x1, y1] = graph.getCanvasByViewport([0, 0]);
289
291
  const [x2, y2] = graph.getCanvasByViewport(size);
290
292
 
291
- const viewportBBox = new AABB();
293
+ let viewportBBox = new AABB();
292
294
  viewportBBox.setMinMax([x1, y1, 0], [x2, y2, 0]);
293
295
 
294
- return isPoint(target) ? isPointInBBox(target, viewportBBox) : viewportBBox.intersects(target);
296
+ if (tolerance) {
297
+ viewportBBox = getExpandedBBox(viewportBBox, tolerance);
298
+ }
299
+
300
+ return isPoint(target)
301
+ ? isPointInBBox(target, viewportBBox)
302
+ : !complete
303
+ ? viewportBBox.intersects(target)
304
+ : isBBoxInside(target, viewportBBox);
295
305
  }
296
306
 
297
307
  public cancelAnimation() {
@@ -4,7 +4,6 @@ import type { ComboData } from '../spec';
4
4
  import { isCollapsed } from '../utils/collapsibility';
5
5
  import { getSubgraphRelatedEdges } from '../utils/edge';
6
6
  import { idOf } from '../utils/id';
7
- // import { reassignTo } from '../utils/transform';
8
7
  import { BaseTransform } from './base-transform';
9
8
  import type { DrawData } from './types';
10
9
  import { reassignTo } from './utils';