@antv/g6 5.0.18 → 5.0.20

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 (261) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/dist/lib/behaviors/drag-canvas.d.ts +9 -0
  3. package/dist/lib/behaviors/drag-element.d.ts +0 -1
  4. package/dist/lib/behaviors/hover-activate.d.ts +14 -1
  5. package/dist/lib/behaviors/scroll-canvas.d.ts +9 -0
  6. package/dist/lib/elements/combos/circle.d.ts +1 -1
  7. package/dist/lib/elements/edges/cubic-radial.d.ts +24 -0
  8. package/dist/lib/elements/edges/index.d.ts +2 -0
  9. package/dist/lib/elements/effect.d.ts +7 -30
  10. package/dist/lib/elements/nodes/base-node.d.ts +2 -1
  11. package/dist/lib/elements/nodes/circle.d.ts +1 -1
  12. package/dist/lib/elements/nodes/ellipse.d.ts +1 -1
  13. package/dist/lib/elements/shapes/base-shape.d.ts +1 -1
  14. package/dist/lib/elements/shapes/polygon.d.ts +1 -1
  15. package/dist/lib/exports.d.ts +6 -5
  16. package/dist/lib/runtime/data.d.ts +1 -0
  17. package/dist/lib/runtime/element.d.ts +1 -0
  18. package/dist/lib/runtime/graph.d.ts +2 -0
  19. package/dist/lib/runtime/layout.d.ts +1 -0
  20. package/dist/lib/transforms/base-transform.d.ts +1 -0
  21. package/dist/lib/transforms/index.d.ts +3 -0
  22. package/dist/lib/transforms/position-radial-labels.d.ts +27 -0
  23. package/dist/lib/types/element.d.ts +2 -1
  24. package/dist/lib/utils/data.d.ts +14 -0
  25. package/dist/lib/utils/diff.d.ts +2 -1
  26. package/dist/lib/utils/point.d.ts +17 -5
  27. package/dist/lib/utils/position.d.ts +8 -0
  28. package/dist/lib/utils/relation.d.ts +6 -4
  29. package/dist/lib/utils/vector.d.ts +8 -0
  30. package/dist/lib/version.d.ts +1 -1
  31. package/esm/behaviors/drag-canvas.d.ts +9 -0
  32. package/esm/behaviors/drag-canvas.js +28 -3
  33. package/esm/behaviors/drag-canvas.js.map +1 -1
  34. package/esm/behaviors/drag-element.d.ts +0 -1
  35. package/esm/behaviors/drag-element.js +14 -7
  36. package/esm/behaviors/drag-element.js.map +1 -1
  37. package/esm/behaviors/hover-activate.d.ts +14 -1
  38. package/esm/behaviors/hover-activate.js +3 -2
  39. package/esm/behaviors/hover-activate.js.map +1 -1
  40. package/esm/behaviors/scroll-canvas.d.ts +9 -0
  41. package/esm/behaviors/scroll-canvas.js +31 -4
  42. package/esm/behaviors/scroll-canvas.js.map +1 -1
  43. package/esm/elements/combos/base-combo.js +6 -12
  44. package/esm/elements/combos/base-combo.js.map +1 -1
  45. package/esm/elements/combos/circle.d.ts +1 -1
  46. package/esm/elements/combos/circle.js +2 -2
  47. package/esm/elements/combos/circle.js.map +1 -1
  48. package/esm/elements/edges/base-edge.js +18 -29
  49. package/esm/elements/edges/base-edge.js.map +1 -1
  50. package/esm/elements/edges/cubic-radial.d.ts +24 -0
  51. package/esm/elements/edges/cubic-radial.js +52 -0
  52. package/esm/elements/edges/cubic-radial.js.map +1 -0
  53. package/esm/elements/edges/index.d.ts +2 -0
  54. package/esm/elements/edges/index.js +1 -0
  55. package/esm/elements/edges/index.js.map +1 -1
  56. package/esm/elements/effect.d.ts +7 -30
  57. package/esm/elements/effect.js +20 -50
  58. package/esm/elements/effect.js.map +1 -1
  59. package/esm/elements/nodes/base-node.d.ts +2 -1
  60. package/esm/elements/nodes/base-node.js +24 -31
  61. package/esm/elements/nodes/base-node.js.map +1 -1
  62. package/esm/elements/nodes/circle.d.ts +1 -1
  63. package/esm/elements/nodes/circle.js +2 -2
  64. package/esm/elements/nodes/circle.js.map +1 -1
  65. package/esm/elements/nodes/ellipse.d.ts +1 -1
  66. package/esm/elements/nodes/ellipse.js +2 -2
  67. package/esm/elements/nodes/ellipse.js.map +1 -1
  68. package/esm/elements/shapes/base-shape.d.ts +1 -1
  69. package/esm/elements/shapes/base-shape.js +1 -1
  70. package/esm/elements/shapes/polygon.d.ts +1 -1
  71. package/esm/elements/shapes/polygon.js +2 -2
  72. package/esm/elements/shapes/polygon.js.map +1 -1
  73. package/esm/exports.d.ts +6 -5
  74. package/esm/exports.js +4 -3
  75. package/esm/exports.js.map +1 -1
  76. package/esm/plugins/tooltip.js +5 -2
  77. package/esm/plugins/tooltip.js.map +1 -1
  78. package/esm/registry/build-in.js +4 -2
  79. package/esm/registry/build-in.js.map +1 -1
  80. package/esm/runtime/animation.js +12 -10
  81. package/esm/runtime/animation.js.map +1 -1
  82. package/esm/runtime/data.d.ts +1 -0
  83. package/esm/runtime/data.js +21 -13
  84. package/esm/runtime/data.js.map +1 -1
  85. package/esm/runtime/element.d.ts +1 -0
  86. package/esm/runtime/element.js +45 -18
  87. package/esm/runtime/element.js.map +1 -1
  88. package/esm/runtime/graph.d.ts +2 -0
  89. package/esm/runtime/graph.js +44 -22
  90. package/esm/runtime/graph.js.map +1 -1
  91. package/esm/runtime/layout.d.ts +1 -0
  92. package/esm/runtime/layout.js +5 -0
  93. package/esm/runtime/layout.js.map +1 -1
  94. package/esm/transforms/base-transform.d.ts +1 -0
  95. package/esm/transforms/base-transform.js +1 -0
  96. package/esm/transforms/base-transform.js.map +1 -1
  97. package/esm/transforms/collapse-expand-combo.js +3 -0
  98. package/esm/transforms/collapse-expand-combo.js.map +1 -1
  99. package/esm/transforms/collapse-expand-node.js +2 -0
  100. package/esm/transforms/collapse-expand-node.js.map +1 -1
  101. package/esm/transforms/index.d.ts +3 -0
  102. package/esm/transforms/index.js +1 -0
  103. package/esm/transforms/index.js.map +1 -1
  104. package/esm/transforms/position-radial-labels.d.ts +27 -0
  105. package/esm/transforms/position-radial-labels.js +50 -0
  106. package/esm/transforms/position-radial-labels.js.map +1 -0
  107. package/esm/types/element.d.ts +2 -1
  108. package/esm/utils/data.d.ts +14 -0
  109. package/esm/utils/data.js +39 -0
  110. package/esm/utils/data.js.map +1 -1
  111. package/esm/utils/diff.d.ts +2 -1
  112. package/esm/utils/diff.js +3 -2
  113. package/esm/utils/diff.js.map +1 -1
  114. package/esm/utils/point.d.ts +17 -5
  115. package/esm/utils/point.js +24 -8
  116. package/esm/utils/point.js.map +1 -1
  117. package/esm/utils/position.d.ts +8 -0
  118. package/esm/utils/position.js +11 -0
  119. package/esm/utils/position.js.map +1 -1
  120. package/esm/utils/relation.d.ts +6 -4
  121. package/esm/utils/relation.js +10 -8
  122. package/esm/utils/relation.js.map +1 -1
  123. package/esm/utils/vector.d.ts +8 -0
  124. package/esm/utils/vector.js +13 -0
  125. package/esm/utils/vector.js.map +1 -1
  126. package/esm/version.d.ts +1 -1
  127. package/esm/version.js +1 -1
  128. package/lib/behaviors/drag-canvas.d.ts +9 -0
  129. package/lib/behaviors/drag-canvas.js +27 -2
  130. package/lib/behaviors/drag-canvas.js.map +1 -1
  131. package/lib/behaviors/drag-element.d.ts +0 -1
  132. package/lib/behaviors/drag-element.js +14 -7
  133. package/lib/behaviors/drag-element.js.map +1 -1
  134. package/lib/behaviors/hover-activate.d.ts +14 -1
  135. package/lib/behaviors/hover-activate.js +3 -2
  136. package/lib/behaviors/hover-activate.js.map +1 -1
  137. package/lib/behaviors/scroll-canvas.d.ts +9 -0
  138. package/lib/behaviors/scroll-canvas.js +31 -4
  139. package/lib/behaviors/scroll-canvas.js.map +1 -1
  140. package/lib/elements/combos/base-combo.js +5 -11
  141. package/lib/elements/combos/base-combo.js.map +1 -1
  142. package/lib/elements/combos/circle.d.ts +1 -1
  143. package/lib/elements/combos/circle.js +2 -2
  144. package/lib/elements/combos/circle.js.map +1 -1
  145. package/lib/elements/edges/base-edge.js +18 -29
  146. package/lib/elements/edges/base-edge.js.map +1 -1
  147. package/lib/elements/edges/cubic-radial.d.ts +24 -0
  148. package/lib/elements/edges/cubic-radial.js +56 -0
  149. package/lib/elements/edges/cubic-radial.js.map +1 -0
  150. package/lib/elements/edges/index.d.ts +2 -0
  151. package/lib/elements/edges/index.js +3 -1
  152. package/lib/elements/edges/index.js.map +1 -1
  153. package/lib/elements/effect.d.ts +7 -30
  154. package/lib/elements/effect.js +20 -50
  155. package/lib/elements/effect.js.map +1 -1
  156. package/lib/elements/nodes/base-node.d.ts +2 -1
  157. package/lib/elements/nodes/base-node.js +24 -31
  158. package/lib/elements/nodes/base-node.js.map +1 -1
  159. package/lib/elements/nodes/circle.d.ts +1 -1
  160. package/lib/elements/nodes/circle.js +2 -2
  161. package/lib/elements/nodes/circle.js.map +1 -1
  162. package/lib/elements/nodes/ellipse.d.ts +1 -1
  163. package/lib/elements/nodes/ellipse.js +2 -2
  164. package/lib/elements/nodes/ellipse.js.map +1 -1
  165. package/lib/elements/shapes/base-shape.d.ts +1 -1
  166. package/lib/elements/shapes/base-shape.js +1 -1
  167. package/lib/elements/shapes/polygon.d.ts +1 -1
  168. package/lib/elements/shapes/polygon.js +2 -2
  169. package/lib/elements/shapes/polygon.js.map +1 -1
  170. package/lib/exports.d.ts +6 -5
  171. package/lib/exports.js +12 -6
  172. package/lib/exports.js.map +1 -1
  173. package/lib/plugins/tooltip.js +5 -2
  174. package/lib/plugins/tooltip.js.map +1 -1
  175. package/lib/registry/build-in.js +2 -0
  176. package/lib/registry/build-in.js.map +1 -1
  177. package/lib/runtime/animation.js +12 -10
  178. package/lib/runtime/animation.js.map +1 -1
  179. package/lib/runtime/data.d.ts +1 -0
  180. package/lib/runtime/data.js +19 -11
  181. package/lib/runtime/data.js.map +1 -1
  182. package/lib/runtime/element.d.ts +1 -0
  183. package/lib/runtime/element.js +45 -18
  184. package/lib/runtime/element.js.map +1 -1
  185. package/lib/runtime/graph.d.ts +2 -0
  186. package/lib/runtime/graph.js +44 -22
  187. package/lib/runtime/graph.js.map +1 -1
  188. package/lib/runtime/layout.d.ts +1 -0
  189. package/lib/runtime/layout.js +5 -0
  190. package/lib/runtime/layout.js.map +1 -1
  191. package/lib/transforms/base-transform.d.ts +1 -0
  192. package/lib/transforms/base-transform.js +1 -0
  193. package/lib/transforms/base-transform.js.map +1 -1
  194. package/lib/transforms/collapse-expand-combo.js +3 -0
  195. package/lib/transforms/collapse-expand-combo.js.map +1 -1
  196. package/lib/transforms/collapse-expand-node.js +2 -0
  197. package/lib/transforms/collapse-expand-node.js.map +1 -1
  198. package/lib/transforms/index.d.ts +3 -0
  199. package/lib/transforms/index.js +3 -1
  200. package/lib/transforms/index.js.map +1 -1
  201. package/lib/transforms/position-radial-labels.d.ts +27 -0
  202. package/lib/transforms/position-radial-labels.js +54 -0
  203. package/lib/transforms/position-radial-labels.js.map +1 -0
  204. package/lib/types/element.d.ts +2 -1
  205. package/lib/utils/data.d.ts +14 -0
  206. package/lib/utils/data.js +40 -0
  207. package/lib/utils/data.js.map +1 -1
  208. package/lib/utils/diff.d.ts +2 -1
  209. package/lib/utils/diff.js +3 -2
  210. package/lib/utils/diff.js.map +1 -1
  211. package/lib/utils/point.d.ts +17 -5
  212. package/lib/utils/point.js +25 -8
  213. package/lib/utils/point.js.map +1 -1
  214. package/lib/utils/position.d.ts +8 -0
  215. package/lib/utils/position.js +12 -0
  216. package/lib/utils/position.js.map +1 -1
  217. package/lib/utils/relation.d.ts +6 -4
  218. package/lib/utils/relation.js +10 -8
  219. package/lib/utils/relation.js.map +1 -1
  220. package/lib/utils/vector.d.ts +8 -0
  221. package/lib/utils/vector.js +14 -0
  222. package/lib/utils/vector.js.map +1 -1
  223. package/lib/version.d.ts +1 -1
  224. package/lib/version.js +1 -1
  225. package/package.json +3 -3
  226. package/src/behaviors/drag-canvas.ts +42 -3
  227. package/src/behaviors/drag-element.ts +17 -7
  228. package/src/behaviors/hover-activate.ts +17 -3
  229. package/src/behaviors/scroll-canvas.ts +45 -4
  230. package/src/elements/combos/base-combo.ts +5 -4
  231. package/src/elements/combos/circle.ts +2 -2
  232. package/src/elements/edges/base-edge.ts +14 -11
  233. package/src/elements/edges/cubic-radial.ts +77 -0
  234. package/src/elements/edges/index.ts +2 -0
  235. package/src/elements/effect.ts +20 -54
  236. package/src/elements/nodes/base-node.ts +24 -18
  237. package/src/elements/nodes/circle.ts +2 -2
  238. package/src/elements/nodes/ellipse.ts +2 -2
  239. package/src/elements/shapes/base-shape.ts +1 -1
  240. package/src/elements/shapes/polygon.ts +2 -2
  241. package/src/exports.ts +18 -7
  242. package/src/plugins/tooltip.ts +4 -2
  243. package/src/registry/build-in.ts +4 -0
  244. package/src/runtime/animation.ts +16 -11
  245. package/src/runtime/data.ts +20 -13
  246. package/src/runtime/element.ts +44 -18
  247. package/src/runtime/graph.ts +52 -22
  248. package/src/runtime/layout.ts +7 -0
  249. package/src/transforms/base-transform.ts +2 -0
  250. package/src/transforms/collapse-expand-combo.ts +2 -0
  251. package/src/transforms/collapse-expand-node.ts +3 -0
  252. package/src/transforms/index.ts +3 -0
  253. package/src/transforms/position-radial-labels.ts +76 -0
  254. package/src/types/element.ts +2 -1
  255. package/src/utils/data.ts +49 -0
  256. package/src/utils/diff.ts +8 -2
  257. package/src/utils/point.ts +25 -7
  258. package/src/utils/position.ts +12 -0
  259. package/src/utils/relation.ts +22 -9
  260. package/src/utils/vector.ts +13 -0
  261. package/src/version.ts +1 -1
@@ -19,6 +19,7 @@ import type {
19
19
  ElementType,
20
20
  ID,
21
21
  Node,
22
+ NodeLikeData,
22
23
  State,
23
24
  StyleIterationContext,
24
25
  } from '../types';
@@ -287,7 +288,6 @@ export class ElementController {
287
288
  const { animation, silence } = context;
288
289
 
289
290
  const { type = 'draw' } = context;
290
- const willRender = type === 'render';
291
291
 
292
292
  return this.context.animation!.animate(
293
293
  animation,
@@ -296,7 +296,7 @@ export class ElementController {
296
296
  : {
297
297
  before: () =>
298
298
  this.emit(
299
- new GraphLifeCycleEvent(GraphEvent.BEFORE_DRAW, { dataChanges, animation, render: willRender }),
299
+ new GraphLifeCycleEvent(GraphEvent.BEFORE_DRAW, { dataChanges, animation, render: type === 'render' }),
300
300
  context,
301
301
  ),
302
302
  beforeAnimate: (animation) =>
@@ -305,7 +305,7 @@ export class ElementController {
305
305
  this.emit(new AnimateEvent(GraphEvent.AFTER_ANIMATE, AnimationType.DRAW, animation, drawData), context),
306
306
  after: () =>
307
307
  this.emit(
308
- new GraphLifeCycleEvent(GraphEvent.AFTER_DRAW, { dataChanges, animation, render: willRender }),
308
+ new GraphLifeCycleEvent(GraphEvent.AFTER_DRAW, { dataChanges, animation, render: type === 'render' }),
309
309
  context,
310
310
  ),
311
311
  },
@@ -433,8 +433,24 @@ export class ElementController {
433
433
  });
434
434
  }
435
435
 
436
+ private getUpdateStageStyle(elementType: ElementType, datum: ElementDatum, context: DrawContext) {
437
+ const { stage = 'update' } = context;
438
+
439
+ // 优化 translate 阶段,直接返回 x, y, z,避免计算样式
440
+ // Optimize the translate stage, return x, y, z directly to avoid calculating style
441
+ if (stage === 'translate') {
442
+ if (elementType === 'node' || elementType === 'combo') {
443
+ const { style: { x = 0, y = 0, z = 0 } = {} } = datum as NodeLikeData;
444
+ return { x, y, z };
445
+ } else return {};
446
+ }
447
+
448
+ return this.getElementComputedStyle(elementType, datum);
449
+ }
450
+
436
451
  private updateElement(elementType: ElementType, datum: ElementDatum, context: DrawContext) {
437
452
  const id = idOf(datum);
453
+ const { stage = 'update' } = context;
438
454
 
439
455
  const element = this.getElement(id);
440
456
  if (!element) return () => null;
@@ -442,7 +458,7 @@ export class ElementController {
442
458
  this.emit(new ElementLifeCycleEvent(GraphEvent.BEFORE_ELEMENT_UPDATE, elementType, datum), context);
443
459
 
444
460
  const type = this.getElementType(elementType, datum);
445
- const style = this.getElementComputedStyle(elementType, datum);
461
+ const style = this.getUpdateStageStyle(elementType, datum, context);
446
462
 
447
463
  // 如果类型不同,需要先销毁原有元素,再创建新元素
448
464
  // If the type is different, you need to destroy the original element first, and then create a new element
@@ -454,8 +470,6 @@ export class ElementController {
454
470
  this.createElement(elementType, datum, { animation: false, silence: true });
455
471
  }
456
472
 
457
- const { stage = 'update' } = context;
458
-
459
473
  const exactStage = stage !== 'visibility' ? stage : style.visibility === 'hidden' ? 'hide' : 'show';
460
474
 
461
475
  // 避免立即将 visibility 设置为 hidden,导致元素不可见,而是在 after 阶段再设置
@@ -587,9 +601,10 @@ export class ElementController {
587
601
  public async collapseNode(id: ID, animation: boolean): Promise<void> {
588
602
  const { model, layout } = this.context;
589
603
 
590
- const preprocess = this.computeChangesAndDrawData({ stage: 'collapse', animation })!;
604
+ const preLayoutData = this.computeChangesAndDrawData({ stage: 'collapse', animation });
605
+ if (!preLayoutData) return;
591
606
 
592
- this.markDestroyElement(preprocess.drawData);
607
+ this.markDestroyElement(preLayoutData.drawData);
593
608
 
594
609
  // 进行预布局,计算出所有元素的位置
595
610
  // Perform pre-layout to calculate the position of all elements
@@ -597,7 +612,9 @@ export class ElementController {
597
612
  model.updateData(result);
598
613
 
599
614
  // 重新计算数据 / Recalculate data
600
- const { drawData } = this.computeChangesAndDrawData({ stage: 'collapse', animation })!;
615
+ const data = this.computeChangesAndDrawData({ stage: 'collapse', animation });
616
+ if (!data) return;
617
+ const { drawData } = data;
601
618
  const { add, remove, update } = drawData;
602
619
  this.markDestroyElement(drawData);
603
620
  const context = { animation, stage: 'collapse', data: drawData } as const;
@@ -637,11 +654,14 @@ export class ElementController {
637
654
 
638
655
  const position = positionOf(model.getNodeData([id])[0]);
639
656
 
657
+ const preLayoutData = this.computeChangesAndDrawData({ stage: 'expand', animation });
658
+ if (!preLayoutData) return;
659
+
640
660
  // 首先创建展开的元素,然后进行预布局
641
661
  // First create the expanded element, then perform pre-layout
642
662
  const {
643
663
  drawData: { add },
644
- } = this.computeChangesAndDrawData({ stage: 'collapse', animation })!;
664
+ } = preLayoutData;
645
665
  this.createElements(add, { animation: false, stage: 'expand', target: id });
646
666
  // 重置动画 / Reset animation
647
667
  this.context.animation!.clear();
@@ -651,16 +671,16 @@ export class ElementController {
651
671
 
652
672
  // 重新计算数据 / Recalculate data
653
673
  this.computeStyle('expand');
654
- const { drawData } = this.computeChangesAndDrawData({ stage: 'collapse', animation })!;
674
+ const data = this.computeChangesAndDrawData({ stage: 'collapse', animation });
675
+ if (!data) return;
676
+ const { drawData } = data;
655
677
  const { update } = drawData;
656
678
 
657
679
  const context = { animation, stage: 'expand', data: drawData } as const;
658
680
 
659
- // 将新增边添加到更新列表 / Add new edges to the update list
660
- add.edges.forEach((edge) => {
661
- const id = idOf(edge);
662
- if (!update.edges.has(id)) update.edges.set(id, edge);
663
- });
681
+ // 将新增节点/边添加到更新列表 / Add new nodes/edges to the update list
682
+ add.edges.forEach((edge) => update.edges.set(idOf(edge), edge));
683
+ add.nodes.forEach((node) => update.nodes.set(idOf(node), node));
664
684
 
665
685
  this.updateElements(update, context);
666
686
 
@@ -693,7 +713,10 @@ export class ElementController {
693
713
  collapsed: true,
694
714
  });
695
715
 
696
- const { dataChanges, drawData } = this.computeChangesAndDrawData({ stage: 'collapse', animation })!;
716
+ const data = this.computeChangesAndDrawData({ stage: 'collapse', animation });
717
+ if (!data) return;
718
+
719
+ const { dataChanges, drawData } = data;
697
720
  this.markDestroyElement(drawData);
698
721
  const { update, remove } = drawData;
699
722
  const context = { animation, stage: 'collapse', data: drawData } as const;
@@ -729,7 +752,10 @@ export class ElementController {
729
752
 
730
753
  // 重新计算数据 / Recalculate data
731
754
  this.computeStyle('expand');
732
- const { dataChanges, drawData } = this.computeChangesAndDrawData({ stage: 'expand', animation })!;
755
+ const data = this.computeChangesAndDrawData({ stage: 'expand', animation });
756
+ if (!data) return;
757
+
758
+ const { dataChanges, drawData } = data;
733
759
  const { add, update } = drawData;
734
760
  const context = { animation, stage: 'expand', data: drawData, target: id } as const;
735
761
 
@@ -41,6 +41,7 @@ import type {
41
41
  } from '../types';
42
42
  import { isCollapsed } from '../utils/collapsibility';
43
43
  import { sizeOf } from '../utils/dom';
44
+ import { getSubgraphRelatedEdges } from '../utils/edge';
44
45
  import { GraphLifeCycleEvent, emit } from '../utils/event';
45
46
  import { idOf } from '../utils/id';
46
47
  import { format } from '../utils/print';
@@ -87,7 +88,7 @@ export class Graph extends EventEmitter {
87
88
  constructor(options: GraphOptions) {
88
89
  super();
89
90
  this.options = Object.assign({}, Graph.defaultOptions, options);
90
- this.setOptions(this.options);
91
+ this._setOptions(this.options, true);
91
92
  this.context.graph = this;
92
93
 
93
94
  // Listening window.resize to autoResize.
@@ -117,32 +118,28 @@ export class Graph extends EventEmitter {
117
118
  * @apiCategory option
118
119
  */
119
120
  public setOptions(options: GraphOptions): void {
120
- const { behaviors, combo, data, edge, height, layout, node, plugins, theme, transforms, width, cursor, renderer } =
121
- options;
121
+ this._setOptions(options, false);
122
+ }
122
123
 
123
- if (renderer) {
124
- const canvas = this.context.canvas;
125
- if (canvas) {
126
- this.emit(GraphEvent.BEFORE_RENDERER_CHANGE, { renderer: this.options.renderer });
127
- canvas.setRenderer(renderer);
128
- this.emit(GraphEvent.AFTER_RENDERER_CHANGE, { renderer });
129
- }
130
- }
124
+ private _setOptions(options: GraphOptions, isInit: boolean) {
125
+ this.updateCanvas(options);
131
126
 
127
+ if (isInit) {
128
+ const { data } = options;
129
+ if (data) this.addData(data);
130
+ return;
131
+ }
132
132
  Object.assign(this.options, options);
133
-
134
- if (cursor) this.context.canvas?.setCursor(cursor);
133
+ const { behaviors, combo, data, edge, layout, node, plugins, theme, transforms } = options;
135
134
  if (behaviors) this.setBehaviors(behaviors);
136
- if (combo) this.setCombo(combo);
137
135
  if (data) this.setData(data);
136
+ if (node) this.setNode(node);
138
137
  if (edge) this.setEdge(edge);
138
+ if (combo) this.setCombo(combo);
139
139
  if (layout) this.setLayout(layout);
140
- if (node) this.setNode(node);
141
140
  if (theme) this.setTheme(theme);
142
141
  if (plugins) this.setPlugins(plugins);
143
142
  if (transforms) this.setTransforms(transforms);
144
- if (isNumber(width) || isNumber(height))
145
- this.setSize(width ?? this.options.width ?? 0, height ?? this.options.height ?? 0);
146
143
  }
147
144
 
148
145
  /**
@@ -1015,10 +1012,19 @@ export class Graph extends EventEmitter {
1015
1012
  private async initCanvas() {
1016
1013
  if (this.context.canvas) return await this.context.canvas.ready;
1017
1014
 
1018
- const { container = 'container', width, height, renderer, cursor, background } = this.options;
1015
+ const {
1016
+ container = 'container',
1017
+ width,
1018
+ height,
1019
+ renderer,
1020
+ cursor,
1021
+ background,
1022
+ devicePixelRatio = window.devicePixelRatio ?? 1,
1023
+ } = this.options;
1019
1024
  if (container instanceof Canvas) {
1020
1025
  this.context.canvas = container;
1021
1026
  if (cursor) container.setCursor(cursor);
1027
+ if (renderer) container.setRenderer(renderer);
1022
1028
  await container.ready;
1023
1029
  } else {
1024
1030
  const $container = isString(container) ? document.getElementById(container!) : container;
@@ -1033,6 +1039,7 @@ export class Graph extends EventEmitter {
1033
1039
  background,
1034
1040
  renderer,
1035
1041
  cursor,
1042
+ devicePixelRatio,
1036
1043
  });
1037
1044
 
1038
1045
  this.context.canvas = canvas;
@@ -1041,6 +1048,23 @@ export class Graph extends EventEmitter {
1041
1048
  }
1042
1049
  }
1043
1050
 
1051
+ private updateCanvas(options: GraphOptions) {
1052
+ const { renderer, cursor, height, width } = options;
1053
+ const canvas = this.context.canvas;
1054
+ if (!canvas) return;
1055
+
1056
+ if (renderer) {
1057
+ this.emit(GraphEvent.BEFORE_RENDERER_CHANGE, { renderer: this.options.renderer });
1058
+ canvas.setRenderer(renderer);
1059
+ this.emit(GraphEvent.AFTER_RENDERER_CHANGE, { renderer });
1060
+ }
1061
+
1062
+ if (cursor) canvas.setCursor(cursor);
1063
+
1064
+ if (isNumber(width) || isNumber(height))
1065
+ this.setSize(width ?? this.options.width ?? 0, height ?? this.options.height ?? 0);
1066
+ }
1067
+
1044
1068
  private initRuntime() {
1045
1069
  this.context.options = this.options;
1046
1070
  if (!this.context.batch) this.context.batch = new BatchController(this.context);
@@ -1620,7 +1644,7 @@ export class Graph extends EventEmitter {
1620
1644
  public async frontElement(id: ID | ID[]): Promise<void> {
1621
1645
  const ids = Array.isArray(id) ? id : [id];
1622
1646
  const { model, element } = this.context;
1623
- const config: Record<ID, number> = {};
1647
+ const zIndexes: Record<ID, number> = {};
1624
1648
 
1625
1649
  ids.map((_id) => {
1626
1650
  const zIndex = element!.getFrontZIndex(_id);
@@ -1630,12 +1654,18 @@ export class Graph extends EventEmitter {
1630
1654
  const combos = [ancestor, ...model.getDescendantsData(idOf(ancestor))];
1631
1655
  const delta = zIndex - element!.getElementZIndex(_id);
1632
1656
  combos.forEach((combo) => {
1633
- config[idOf(combo)] = this.getElementZIndex(idOf(combo)) + delta;
1657
+ zIndexes[idOf(combo)] = this.getElementZIndex(idOf(combo)) + delta;
1658
+ });
1659
+
1660
+ const { internal } = getSubgraphRelatedEdges(combos.map(idOf), (id) => model.getRelatedEdgesData(id));
1661
+ internal.forEach((edge) => {
1662
+ const edgeId = idOf(edge);
1663
+ zIndexes[edgeId] = this.getElementZIndex(edgeId) + delta;
1634
1664
  });
1635
- } else config[_id] = zIndex;
1665
+ } else zIndexes[_id] = zIndex;
1636
1666
  });
1637
1667
 
1638
- await this.setElementZIndex(config);
1668
+ await this.setElementZIndex(zIndexes);
1639
1669
  }
1640
1670
 
1641
1671
  /**
@@ -69,6 +69,13 @@ export class LayoutController {
69
69
  }
70
70
  }
71
71
  emit(graph, new GraphLifeCycleEvent(GraphEvent.AFTER_LAYOUT));
72
+ this.transformDataAfterLayout();
73
+ }
74
+
75
+ private transformDataAfterLayout() {
76
+ const transforms = this.context.transform.getTransformInstance();
77
+
78
+ Object.values(transforms).forEach((transform) => transform.afterLayout());
72
79
  }
73
80
 
74
81
  /**
@@ -9,4 +9,6 @@ export abstract class BaseTransform<T extends BaseTransformOptions = BaseTransfo
9
9
  public beforeDraw(data: DrawData, context: DrawContext): DrawData {
10
10
  return data;
11
11
  }
12
+
13
+ public afterLayout() {}
12
14
  }
@@ -1,3 +1,4 @@
1
+ import { COMBO_KEY } from '../constants';
1
2
  import type { DrawContext } from '../runtime/element';
2
3
  import type { ComboData } from '../spec';
3
4
  import { isCollapsed } from '../utils/collapsibility';
@@ -16,6 +17,7 @@ import { reassignTo } from './utils';
16
17
  export class CollapseExpandCombo extends BaseTransform {
17
18
  public beforeDraw(input: DrawData, context: DrawContext): DrawData {
18
19
  if (context.stage === 'visibility') return input;
20
+ if (!this.context.model.model.hasTreeStructure(COMBO_KEY)) return input;
19
21
 
20
22
  const { model } = this.context;
21
23
  const { add, update } = input;
@@ -47,6 +47,9 @@ export class CollapseExpandNode extends BaseTransform {
47
47
 
48
48
  public beforeDraw(input: DrawData): DrawData {
49
49
  const { graph, model } = this.context;
50
+
51
+ if (!model.model.hasTreeStructure(TREE_KEY)) return input;
52
+
50
53
  const {
51
54
  add: { nodes: nodesToAdd, edges: edgesToAdd },
52
55
  update: { nodes: nodesToUpdate },
@@ -3,7 +3,10 @@ export { BaseTransform } from './base-transform';
3
3
  export { CollapseExpandCombo } from './collapse-expand-combo';
4
4
  export { CollapseExpandNode } from './collapse-expand-node';
5
5
  export { GetEdgeActualEnds } from './get-edge-actual-ends';
6
+ export { PositionRadialLabels } from './position-radial-labels';
6
7
  export { ProcessParallelEdges } from './process-parallel-edges';
7
8
  export { UpdateRelatedEdge } from './update-related-edge';
8
9
 
9
10
  export type { BaseTransformOptions } from './base-transform';
11
+ export type { PositionRadialLabelsOptions } from './position-radial-labels';
12
+ export type { ProcessParallelEdgesOptions } from './process-parallel-edges';
@@ -0,0 +1,76 @@
1
+ import { rad2deg } from '@antv/g';
2
+ import type { RuntimeContext } from '../runtime/types';
3
+ import type { NodeData } from '../spec';
4
+ import { idOf } from '../utils/id';
5
+ import { positionOf } from '../utils/position';
6
+ import { parseSize } from '../utils/size';
7
+ import { rad, subtract } from '../utils/vector';
8
+ import type { BaseTransformOptions } from './base-transform';
9
+ import { BaseTransform } from './base-transform';
10
+
11
+ /**
12
+ * <zh/> 根据径向布局自动调整节点标签样式的配置项
13
+ *
14
+ * <en/> Options for automatically adjusting the style of node labels according to the radial layout
15
+ */
16
+ export interface PositionRadialLabelsOptions extends BaseTransformOptions {
17
+ /**
18
+ * <zh/> 偏移量
19
+ *
20
+ * <en/> Offset
21
+ */
22
+ offset?: number;
23
+ }
24
+
25
+ /**
26
+ * <zh/> 根据径向布局自动调整节点标签样式,包括位置和旋转角度
27
+ *
28
+ * <en/> Automatically adjust the style of node labels according to the radial layout, including position and rotation angle
29
+ */
30
+ export class PositionRadialLabels extends BaseTransform<PositionRadialLabelsOptions> {
31
+ static defaultOptions: Partial<PositionRadialLabelsOptions> = {
32
+ offset: 5,
33
+ };
34
+
35
+ constructor(context: RuntimeContext, options: PositionRadialLabelsOptions) {
36
+ super(context, Object.assign({}, PositionRadialLabels.defaultOptions, options));
37
+ }
38
+
39
+ private get ref(): NodeData {
40
+ return this.context.model.getRootsData()[0];
41
+ }
42
+
43
+ public afterLayout() {
44
+ const refPoint = positionOf(this.ref);
45
+
46
+ const { graph, model } = this.context;
47
+ const data = model.getData();
48
+
49
+ data.nodes?.forEach((datum) => {
50
+ if (idOf(datum) === idOf(this.ref)) return;
51
+
52
+ const radian = rad(subtract(positionOf(datum), refPoint));
53
+ const isLeft = Math.abs(radian) > Math.PI / 2;
54
+
55
+ const isLeaf = !datum.children || datum.children.length === 0;
56
+ const nodeHalfWidth = parseSize(graph.getElementRenderStyle(idOf(datum)).size)[0] / 2;
57
+ const offset = (isLeaf ? 1 : -1) * (nodeHalfWidth + this.options.offset);
58
+
59
+ const translate = `translate(${offset * Math.cos(radian)},${offset * Math.sin(radian)})`;
60
+ const rotate = `rotate(${isLeft ? rad2deg(radian) + 180 : rad2deg(radian)}deg)`;
61
+
62
+ model.updateNodeData([
63
+ {
64
+ id: idOf(datum),
65
+ style: {
66
+ labelTextAlign: isLeft === isLeaf ? 'right' : 'left',
67
+ labelTextBaseline: 'middle',
68
+ labelTransform: `${translate} ${rotate}`,
69
+ },
70
+ },
71
+ ]);
72
+ });
73
+
74
+ graph.draw();
75
+ }
76
+ }
@@ -29,13 +29,14 @@ export interface Node extends DisplayObject, ElementHooks, ElementMethods {
29
29
  *
30
30
  * <en/> Get the intersection point
31
31
  * @param point - <zh/> 外部位置 | <en/> external position
32
+ * @param useExtendedLine - <zh/> 是否使用延长线 | <en/> whether to use the extended line
32
33
  * @returns <zh/> 交点位置 | <en/> intersection point
33
34
  * @remarks
34
35
  * <zh/> 给定一个外部位置,返回当前节点与该位置的连边与节点的交点位置
35
36
  *
36
37
  * <en/> Given an external position, return the intersection point of the edge between the current node and the position and the node
37
38
  */
38
- getIntersectPoint(point: Point): Point;
39
+ getIntersectPoint(point: Point, useExtendedLine?: boolean): Point;
39
40
  }
40
41
 
41
42
  /**
package/src/utils/data.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { get } from '@antv/util';
2
2
  import type { ComboData, EdgeData, GraphData, NodeData } from '../spec';
3
+ import type { ElementDatum, ID } from '../types';
3
4
 
4
5
  /**
5
6
  * <zh/> 合并两个 节点/边/Combo 的数据
@@ -62,3 +63,51 @@ export function cloneElementData<T extends NodeData | EdgeData | ComboData>(data
62
63
  export function isEmptyData(data: GraphData) {
63
64
  return !get(data, ['nodes', 'length']) && !get(data, ['edges', 'length']) && !get(data, ['combos', 'length']);
64
65
  }
66
+
67
+ /**
68
+ * <zh/> 判断两个元素数据是否相等
69
+ *
70
+ * <en/> Determine if two element data are equal
71
+ * @param original - <zh/> 原始数据 | <en/> original data
72
+ * @param modified - <zh/> 修改后的数据 | <en/> modified data
73
+ * @returns <zh/> 是否相等 | <en/> is equal
74
+ * @remarks
75
+ * <zh/> 相比于 isEqual,这个方法不会比较更下层的数据
76
+ *
77
+ * <en/> Compared to isEqual, this method does not compare data at a lower level
78
+ */
79
+ export function isElementDataEqual(original: Partial<ElementDatum> = {}, modified: Partial<ElementDatum> = {}) {
80
+ const {
81
+ states: originalStates = [],
82
+ data: originalData = {},
83
+ style: originalStyle = {},
84
+ children: originalChildren = [],
85
+ ...originalAttrs
86
+ } = original;
87
+ const {
88
+ states: modifiedStates = [],
89
+ data: modifiedData = {},
90
+ style: modifiedStyle = {},
91
+ children: modifiedChildren = [],
92
+ ...modifiedAttrs
93
+ } = modified;
94
+
95
+ const isArrayEqual = (arr1: unknown[], arr2: unknown[]) => {
96
+ if (arr1.length !== arr2.length) return false;
97
+ return arr1.every((item, index) => item === arr2[index]);
98
+ };
99
+ const isObjectEqual = (obj1: Record<string, unknown>, obj2: Record<string, unknown>) => {
100
+ const keys1 = Object.keys(obj1);
101
+ const keys2 = Object.keys(obj2);
102
+ if (keys1.length !== keys2.length) return false;
103
+ return keys1.every((key) => obj1[key] === obj2[key]);
104
+ };
105
+
106
+ if (!isObjectEqual(originalAttrs, modifiedAttrs)) return false;
107
+ if (!isArrayEqual(originalChildren as ID[], modifiedChildren as ID[])) return false;
108
+ if (!isArrayEqual(originalStates, modifiedStates)) return false;
109
+ if (!isObjectEqual(originalData, modifiedData)) return false;
110
+ if (!isObjectEqual(originalStyle, modifiedStyle)) return false;
111
+
112
+ return true;
113
+ }
package/src/utils/diff.ts CHANGED
@@ -7,9 +7,15 @@ import { isEqual } from '@antv/util';
7
7
  * @param original - <zh/> 原始数组 | <en/> original array
8
8
  * @param modified - <zh/> 修改后的数组 | <en/> modified array
9
9
  * @param key - <zh/> 比较的 key | <en/> key to compare
10
+ * @param comparator - <zh/> 比较函数 | <en/> compare function
10
11
  * @returns <zh/> 数组差异 | <en/> array diff
11
12
  */
12
- export function arrayDiff<T>(original: T[], modified: T[], key: (d: T) => string | number) {
13
+ export function arrayDiff<T>(
14
+ original: T[],
15
+ modified: T[],
16
+ key: (d: T) => string | number,
17
+ comparator: (a?: T, b?: T) => boolean = isEqual,
18
+ ) {
13
19
  const originalMap = new Map(original.map((d) => [key(d), d]));
14
20
  const modifiedMap = new Map(modified.map((d) => [key(d), d]));
15
21
 
@@ -23,7 +29,7 @@ export function arrayDiff<T>(original: T[], modified: T[], key: (d: T) => string
23
29
 
24
30
  modifiedSet.forEach((key) => {
25
31
  if (originalSet.has(key)) {
26
- if (!isEqual(originalMap.get(key), modifiedMap.get(key))) {
32
+ if (!comparator(originalMap.get(key), modifiedMap.get(key))) {
27
33
  update.push(modifiedMap.get(key)!);
28
34
  } else {
29
35
  keep.push(modifiedMap.get(key)!);
@@ -132,6 +132,18 @@ export function isCollinear(p1: Point, p2: Point, p3: Point): boolean {
132
132
  return isLinesParallel([p1, p2], [p2, p3]);
133
133
  }
134
134
 
135
+ /**
136
+ * <zh/> 计算一个点相对于另一个点的中心对称点
137
+ *
138
+ * <en/> Calculate the center symmetric point of a point relative to another point
139
+ * @param p - <zh/> 要计算的点 | <en/> the point to calculate
140
+ * @param center - <zh/> 中心点 | <en/> the center point
141
+ * @returns <zh/> 中心对称点 | <en/> the center symmetric point
142
+ */
143
+ export function getSymmetricPoint(p: Point, center: Point): Point {
144
+ return [2 * center[0] - p[0], 2 * center[1] - p[1]];
145
+ }
146
+
135
147
  /**
136
148
  * <zh/> 获取从多边形中心到给定点的连线与多边形边缘的交点
137
149
  *
@@ -140,6 +152,7 @@ export function isCollinear(p1: Point, p2: Point, p3: Point): boolean {
140
152
  * @param center - <zh/> 多边形中心 | <en/> the center of the polygon
141
153
  * @param points - <zh/> 多边形顶点 | <en/> the vertices of the polygon
142
154
  * @param isRelativePos - <zh/> 顶点坐标是否相对中心点 | <en/> whether the vertex coordinates are relative to the center point
155
+ * @param useExtendedLine - <zh/> 是否使用延长线 | <en/> whether to use the extended line
143
156
  * @returns <zh/> 交点与相交线段 | <en/> intersection and intersecting line segment
144
157
  */
145
158
  export function getPolygonIntersectPoint(
@@ -147,6 +160,7 @@ export function getPolygonIntersectPoint(
147
160
  center: Point,
148
161
  points: Point[],
149
162
  isRelativePos = true,
163
+ useExtendedLine = false,
150
164
  ): { point: Point; line?: LineSegment } {
151
165
  for (let i = 0; i < points.length; i++) {
152
166
  let start = points[i];
@@ -157,7 +171,8 @@ export function getPolygonIntersectPoint(
157
171
  end = add(center, end);
158
172
  }
159
173
 
160
- const intersect = getLinesIntersection([center, p], [start, end]);
174
+ const refP = useExtendedLine ? getSymmetricPoint(p, center) : p;
175
+ const intersect = getLinesIntersection([center, refP], [start, end]);
161
176
  if (intersect) {
162
177
  return {
163
178
  point: intersect,
@@ -200,14 +215,15 @@ export function isPointInPolygon(point: Point, points: Point[], start?: number,
200
215
  }
201
216
 
202
217
  /**
203
- * <zh/> 获取从矩形中心到给定点的连线与矩形边缘的交点
218
+ * <zh/> 获取给定点到矩形中心的连线与矩形边缘的交点
204
219
  *
205
220
  * <en/> Gets the intersection point between the line from the center of a rectangle to a given point and the rectangle's edge
206
221
  * @param p - <zh/> 从矩形中心到矩形边缘的连线的外部点 | <en/> The point outside the rectangle from which the line to the rectangle's center is drawn
207
222
  * @param bbox - <zh/> 矩形包围盒 | <en/> the bounding box of the rectangle
223
+ * @param useExtendedLine - <zh/> 是否使用延长线 | <en/> whether to use the extended line
208
224
  * @returns <zh/> 交点 | <en/> intersection
209
225
  */
210
- export function getRectIntersectPoint(p: Point, bbox: AABB): Point {
226
+ export function getRectIntersectPoint(p: Point, bbox: AABB, useExtendedLine = false): Point {
211
227
  const center = getXYByPlacement(bbox, 'center');
212
228
  const corners = [
213
229
  getXYByPlacement(bbox, 'left-top'),
@@ -215,21 +231,23 @@ export function getRectIntersectPoint(p: Point, bbox: AABB): Point {
215
231
  getXYByPlacement(bbox, 'right-bottom'),
216
232
  getXYByPlacement(bbox, 'left-bottom'),
217
233
  ];
218
- return getPolygonIntersectPoint(p, center, corners, false).point;
234
+ return getPolygonIntersectPoint(p, center, corners, false, useExtendedLine).point;
219
235
  }
220
236
 
221
237
  /**
222
- * <zh/> 获取从椭圆中心到给定点的连线与椭圆边缘的交点
238
+ * <zh/> 获取给定点到椭圆中心的连线与椭圆边缘的交点
223
239
  *
224
240
  * <en/> Gets the intersection point between the line from the center of an ellipse to a given point and the ellipse's edge
225
241
  * @param p - <zh/> 从椭圆中心到椭圆边缘的连线的外部点 | <en/> The point outside the ellipse from which the line to the ellipse's center is drawn
226
242
  * The point outside the ellipse from which the line to the ellipse's center is drawn.
227
243
  * @param bbox - <zh/> 椭圆包围盒 | <en/> the bounding box of the ellipse
244
+ * @param useExtendedLine - <zh/> 是否使用延长线 | <en/> whether to use the extended line
228
245
  * @returns <zh/> 交点 | <en/> intersection
229
246
  */
230
- export function getEllipseIntersectPoint(p: Point, bbox: AABB): Point {
247
+ export function getEllipseIntersectPoint(p: Point, bbox: AABB, useExtendedLine = false): Point {
231
248
  const center = bbox.center;
232
- const vec = subtract(p, bbox.center);
249
+ const refP = useExtendedLine ? getSymmetricPoint(p, center) : p;
250
+ const vec = subtract(refP, bbox.center);
233
251
  const angle = Math.atan2(vec[1], vec[0]);
234
252
  if (isNaN(angle)) return center;
235
253
 
@@ -15,6 +15,18 @@ export function positionOf(datum: NodeLikeData): Point {
15
15
  return [+x, +y, +z];
16
16
  }
17
17
 
18
+ /**
19
+ * <zh/> 检查数据是否有位置坐标
20
+ *
21
+ * <en/> Check if the data has a position coordinate
22
+ * @param datum - <zh/> 节点/ combo 的数据 | <en/> data of node/combo
23
+ * @returns - <zh/> 是否有位置坐标 | <en/> Whether there is a position coordinate
24
+ */
25
+ export function hasPosition(datum: NodeLikeData): boolean {
26
+ const { x, y, z } = datum.style || {};
27
+ return x !== undefined || y !== undefined || z !== undefined;
28
+ }
29
+
18
30
  /**
19
31
  * <zh/> 获取相对位置坐标
20
32
  *