@antv/g6 5.0.0-beta.20 → 5.0.0-beta.22

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 (209) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/esm/index.d.ts +16 -9
  3. package/esm/index.js +1 -1
  4. package/esm/runtime/controller/data.d.ts +27 -10
  5. package/esm/runtime/controller/data.js +240 -216
  6. package/esm/runtime/controller/data.js.map +1 -1
  7. package/esm/runtime/controller/item.js +2 -0
  8. package/esm/runtime/controller/item.js.map +1 -1
  9. package/esm/runtime/graph.d.ts +11 -4
  10. package/esm/runtime/graph.js +23 -5
  11. package/esm/runtime/graph.js.map +1 -1
  12. package/esm/stdlib/behavior/activate-relations.d.ts +1 -1
  13. package/esm/stdlib/behavior/brush-select.d.ts +2 -2
  14. package/esm/stdlib/behavior/click-select.d.ts +1 -1
  15. package/esm/stdlib/behavior/click-select.js +3 -1
  16. package/esm/stdlib/behavior/click-select.js.map +1 -1
  17. package/esm/stdlib/behavior/collapse-expand-combo.d.ts +1 -1
  18. package/esm/stdlib/behavior/collapse-expand-tree.d.ts +1 -1
  19. package/esm/stdlib/behavior/drag-canvas.js +1 -1
  20. package/esm/stdlib/behavior/drag-combo.d.ts +1 -1
  21. package/esm/stdlib/behavior/drag-node.d.ts +1 -1
  22. package/esm/stdlib/behavior/shortcuts-call.d.ts +1 -1
  23. package/esm/stdlib/behavior/zoom-canvas.js +1 -1
  24. package/esm/stdlib/data/index.d.ts +1 -0
  25. package/esm/stdlib/data/index.js +1 -0
  26. package/esm/stdlib/data/index.js.map +1 -1
  27. package/esm/stdlib/data/mapNodeSize.d.ts +3 -4
  28. package/esm/stdlib/data/mapNodeSize.js +10 -0
  29. package/esm/stdlib/data/mapNodeSize.js.map +1 -1
  30. package/esm/stdlib/data/processParallelEdges.d.ts +21 -0
  31. package/esm/stdlib/data/processParallelEdges.js +127 -0
  32. package/esm/stdlib/data/processParallelEdges.js.map +1 -0
  33. package/esm/stdlib/data/transformV4Data.d.ts +2 -3
  34. package/esm/stdlib/data/transformV4Data.js +8 -0
  35. package/esm/stdlib/data/transformV4Data.js.map +1 -1
  36. package/esm/stdlib/data/validateData.d.ts +3 -4
  37. package/esm/stdlib/data/validateData.js +9 -1
  38. package/esm/stdlib/data/validateData.js.map +1 -1
  39. package/esm/stdlib/index.d.ts +15 -8
  40. package/esm/stdlib/index.js +2 -1
  41. package/esm/stdlib/index.js.map +1 -1
  42. package/esm/stdlib/item/node/donut.d.ts +3 -3
  43. package/esm/stdlib/plugin/hull/hullComponent.d.ts +1 -1
  44. package/esm/stdlib/plugin/legend/index.d.ts +1 -1
  45. package/esm/stdlib/plugin/snapline/index.d.ts +2 -2
  46. package/esm/stdlib/plugin/snapline/index.js +239 -239
  47. package/esm/stdlib/plugin/snapline/index.js.map +1 -1
  48. package/esm/stdlib/plugin/timebar/index.d.ts +4 -4
  49. package/esm/stdlib/plugin/timebar/index.js +4 -4
  50. package/esm/stdlib/plugin/timebar/index.js.map +1 -1
  51. package/esm/stdlib/plugin/tooltip/index.d.ts +1 -1
  52. package/esm/stdlib/plugin/watermaker/index.d.ts +1 -1
  53. package/esm/stdlib/themeSolver/spec.js +21 -6
  54. package/esm/stdlib/themeSolver/spec.js.map +1 -1
  55. package/esm/types/animate.d.ts +2 -2
  56. package/esm/types/behavior.d.ts +1 -1
  57. package/esm/types/combo.d.ts +7 -7
  58. package/esm/types/common.d.ts +5 -5
  59. package/esm/types/data.d.ts +11 -6
  60. package/esm/types/edge.d.ts +9 -9
  61. package/esm/types/event.d.ts +1 -1
  62. package/esm/types/graph.d.ts +6 -0
  63. package/esm/types/history.d.ts +2 -2
  64. package/esm/types/hook.d.ts +1 -1
  65. package/esm/types/item.d.ts +13 -13
  66. package/esm/types/item.js.map +1 -1
  67. package/esm/types/layout.d.ts +6 -6
  68. package/esm/types/loop.d.ts +2 -2
  69. package/esm/types/node.d.ts +7 -7
  70. package/esm/types/render.d.ts +1 -1
  71. package/esm/types/spec.d.ts +4 -3
  72. package/esm/types/stdlib.d.ts +1 -1
  73. package/esm/types/theme.d.ts +8 -8
  74. package/esm/types/view.d.ts +2 -2
  75. package/esm/util/array.d.ts +1 -0
  76. package/esm/util/array.js +3 -0
  77. package/esm/util/array.js.map +1 -1
  78. package/esm/util/data.d.ts +10 -0
  79. package/esm/util/data.js +36 -0
  80. package/esm/util/data.js.map +1 -1
  81. package/esm/util/event.d.ts +2 -2
  82. package/esm/util/layout.d.ts +1 -1
  83. package/esm/util/loop.d.ts +1 -0
  84. package/esm/util/loop.js +10 -0
  85. package/esm/util/loop.js.map +1 -1
  86. package/esm/util/shape.d.ts +2 -1
  87. package/esm/util/shape.js +2 -1
  88. package/esm/util/shape.js.map +1 -1
  89. package/esm/util/shape3d.d.ts +1 -1
  90. package/esm/util/warn.d.ts +1 -1
  91. package/lib/index.d.ts +16 -9
  92. package/lib/index.js +1 -1
  93. package/lib/runtime/controller/data.d.ts +27 -10
  94. package/lib/runtime/controller/data.js +238 -214
  95. package/lib/runtime/controller/data.js.map +1 -1
  96. package/lib/runtime/controller/item.js +2 -0
  97. package/lib/runtime/controller/item.js.map +1 -1
  98. package/lib/runtime/graph.d.ts +11 -4
  99. package/lib/runtime/graph.js +23 -5
  100. package/lib/runtime/graph.js.map +1 -1
  101. package/lib/stdlib/behavior/activate-relations.d.ts +1 -1
  102. package/lib/stdlib/behavior/brush-select.d.ts +2 -2
  103. package/lib/stdlib/behavior/click-select.d.ts +1 -1
  104. package/lib/stdlib/behavior/click-select.js +3 -1
  105. package/lib/stdlib/behavior/click-select.js.map +1 -1
  106. package/lib/stdlib/behavior/collapse-expand-combo.d.ts +1 -1
  107. package/lib/stdlib/behavior/collapse-expand-tree.d.ts +1 -1
  108. package/lib/stdlib/behavior/drag-canvas.js +1 -1
  109. package/lib/stdlib/behavior/drag-combo.d.ts +1 -1
  110. package/lib/stdlib/behavior/drag-node.d.ts +1 -1
  111. package/lib/stdlib/behavior/shortcuts-call.d.ts +1 -1
  112. package/lib/stdlib/behavior/zoom-canvas.js +1 -1
  113. package/lib/stdlib/data/index.d.ts +1 -0
  114. package/lib/stdlib/data/index.js +1 -0
  115. package/lib/stdlib/data/index.js.map +1 -1
  116. package/lib/stdlib/data/mapNodeSize.d.ts +3 -4
  117. package/lib/stdlib/data/mapNodeSize.js +11 -1
  118. package/lib/stdlib/data/mapNodeSize.js.map +1 -1
  119. package/lib/stdlib/data/processParallelEdges.d.ts +21 -0
  120. package/lib/stdlib/data/processParallelEdges.js +131 -0
  121. package/lib/stdlib/data/processParallelEdges.js.map +1 -0
  122. package/lib/stdlib/data/transformV4Data.d.ts +2 -3
  123. package/lib/stdlib/data/transformV4Data.js +9 -1
  124. package/lib/stdlib/data/transformV4Data.js.map +1 -1
  125. package/lib/stdlib/data/validateData.d.ts +3 -4
  126. package/lib/stdlib/data/validateData.js +10 -2
  127. package/lib/stdlib/data/validateData.js.map +1 -1
  128. package/lib/stdlib/index.d.ts +15 -8
  129. package/lib/stdlib/index.js +2 -1
  130. package/lib/stdlib/index.js.map +1 -1
  131. package/lib/stdlib/item/node/donut.d.ts +3 -3
  132. package/lib/stdlib/plugin/hull/hullComponent.d.ts +1 -1
  133. package/lib/stdlib/plugin/legend/index.d.ts +1 -1
  134. package/lib/stdlib/plugin/snapline/index.d.ts +2 -2
  135. package/lib/stdlib/plugin/snapline/index.js +239 -239
  136. package/lib/stdlib/plugin/snapline/index.js.map +1 -1
  137. package/lib/stdlib/plugin/timebar/index.d.ts +4 -4
  138. package/lib/stdlib/plugin/timebar/index.js +4 -4
  139. package/lib/stdlib/plugin/timebar/index.js.map +1 -1
  140. package/lib/stdlib/plugin/tooltip/index.d.ts +1 -1
  141. package/lib/stdlib/plugin/watermaker/index.d.ts +1 -1
  142. package/lib/stdlib/themeSolver/spec.js +20 -5
  143. package/lib/stdlib/themeSolver/spec.js.map +1 -1
  144. package/lib/types/animate.d.ts +2 -2
  145. package/lib/types/behavior.d.ts +1 -1
  146. package/lib/types/combo.d.ts +7 -7
  147. package/lib/types/common.d.ts +5 -5
  148. package/lib/types/data.d.ts +11 -6
  149. package/lib/types/edge.d.ts +9 -9
  150. package/lib/types/event.d.ts +1 -1
  151. package/lib/types/event.js +2 -2
  152. package/lib/types/event.js.map +1 -1
  153. package/lib/types/graph.d.ts +6 -0
  154. package/lib/types/history.d.ts +2 -2
  155. package/lib/types/history.js +1 -1
  156. package/lib/types/history.js.map +1 -1
  157. package/lib/types/hook.d.ts +1 -1
  158. package/lib/types/item.d.ts +13 -13
  159. package/lib/types/item.js +1 -1
  160. package/lib/types/item.js.map +1 -1
  161. package/lib/types/layout.d.ts +6 -6
  162. package/lib/types/loop.d.ts +2 -2
  163. package/lib/types/loop.js +1 -1
  164. package/lib/types/loop.js.map +1 -1
  165. package/lib/types/node.d.ts +7 -7
  166. package/lib/types/render.d.ts +1 -1
  167. package/lib/types/spec.d.ts +4 -3
  168. package/lib/types/stdlib.d.ts +1 -1
  169. package/lib/types/theme.d.ts +8 -8
  170. package/lib/types/view.d.ts +2 -2
  171. package/lib/util/array.d.ts +1 -0
  172. package/lib/util/array.js +5 -1
  173. package/lib/util/array.js.map +1 -1
  174. package/lib/util/data.d.ts +10 -0
  175. package/lib/util/data.js +38 -1
  176. package/lib/util/data.js.map +1 -1
  177. package/lib/util/event.d.ts +2 -2
  178. package/lib/util/layout.d.ts +1 -1
  179. package/lib/util/loop.d.ts +1 -0
  180. package/lib/util/loop.js +11 -1
  181. package/lib/util/loop.js.map +1 -1
  182. package/lib/util/shape.d.ts +2 -1
  183. package/lib/util/shape.js +1 -0
  184. package/lib/util/shape.js.map +1 -1
  185. package/lib/util/shape3d.d.ts +1 -1
  186. package/lib/util/warn.d.ts +1 -1
  187. package/package.json +3 -3
  188. package/src/index.ts +1 -1
  189. package/src/runtime/controller/data.ts +309 -229
  190. package/src/runtime/controller/item.ts +2 -0
  191. package/src/runtime/graph.ts +26 -8
  192. package/src/stdlib/behavior/click-select.ts +3 -1
  193. package/src/stdlib/behavior/drag-canvas.ts +1 -1
  194. package/src/stdlib/behavior/zoom-canvas.ts +1 -1
  195. package/src/stdlib/data/index.ts +1 -0
  196. package/src/stdlib/data/mapNodeSize.ts +20 -1
  197. package/src/stdlib/data/processParallelEdges.ts +203 -0
  198. package/src/stdlib/data/transformV4Data.ts +14 -1
  199. package/src/stdlib/data/validateData.ts +16 -2
  200. package/src/stdlib/index.ts +3 -1
  201. package/src/stdlib/themeSolver/spec.ts +23 -7
  202. package/src/types/data.ts +6 -0
  203. package/src/types/graph.ts +6 -0
  204. package/src/types/item.ts +2 -1
  205. package/src/types/spec.ts +1 -0
  206. package/src/util/array.ts +12 -0
  207. package/src/util/data.ts +46 -1
  208. package/src/util/loop.ts +11 -0
  209. package/src/util/shape.ts +1 -0
@@ -2,11 +2,11 @@ import EventEmitter from '@antv/event-emitter';
2
2
  import {
3
3
  AABB,
4
4
  Canvas,
5
+ Cursor,
5
6
  DataURLType,
6
7
  DisplayObject,
7
8
  PointLike,
8
9
  Rect,
9
- Cursor,
10
10
  } from '@antv/g';
11
11
  import { GraphChange, ID } from '@antv/graphlib';
12
12
  import {
@@ -46,7 +46,9 @@ import type {
46
46
  StandardLayoutOptions,
47
47
  } from '../types/layout';
48
48
  import type { NodeDisplayModel, NodeModel, NodeModelData } from '../types/node';
49
+ import { Plugin as PluginBase } from '../types/plugin';
49
50
  import type { RendererName } from '../types/render';
51
+ import { ComboMapper, EdgeMapper, NodeMapper } from '../types/spec';
50
52
  import type {
51
53
  ThemeOptionsOf,
52
54
  ThemeRegistry,
@@ -54,11 +56,9 @@ import type {
54
56
  } from '../types/theme';
55
57
  import { FitViewRules, GraphTransformOptions } from '../types/view';
56
58
  import { changeRenderer, createCanvas } from '../util/canvas';
57
- import { formatPadding } from '../util/shape';
58
- import { getLayoutBounds } from '../util/layout';
59
59
  import { createDOM } from '../util/dom';
60
- import { Plugin as PluginBase } from '../types/plugin';
61
- import { ComboMapper, EdgeMapper, NodeMapper } from '../types/spec';
60
+ import { getLayoutBounds } from '../util/layout';
61
+ import { formatPadding } from '../util/shape';
62
62
  import {
63
63
  DataController,
64
64
  ExtensionController,
@@ -70,6 +70,7 @@ import {
70
70
  } from './controller';
71
71
  import { PluginController } from './controller/plugin';
72
72
  import Hook from './hooks';
73
+ import { isEmptyGraph } from '../util/data';
73
74
 
74
75
  export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
75
76
  extends EventEmitter
@@ -583,6 +584,7 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
583
584
  options: GraphTransformOptions,
584
585
  effectTiming?: CameraAnimationOptions,
585
586
  ): Promise<void> {
587
+ if (isEmptyGraph(this, true)) return;
586
588
  const { tileLodSize } = this.specification.optimize || {};
587
589
  await this.hooks.viewportchange.emitLinearAsync({
588
590
  transform: options,
@@ -852,7 +854,7 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
852
854
  }
853
855
 
854
856
  /**
855
- * Get item by id. We don't want to
857
+ * Get item by id. We don't want users call this private API.
856
858
  * @param id
857
859
  * @returns Node | Edge | Combo
858
860
  */
@@ -1048,6 +1050,16 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1048
1050
  return this.dataController.findChildren(comboId, 'combo');
1049
1051
  }
1050
1052
 
1053
+ /**
1054
+ * Get item type by id.
1055
+ * @param id
1056
+ * @returns 'node' | 'edge' | 'combo'
1057
+ * @group Data
1058
+ */
1059
+ public getTypeById(id: ID) {
1060
+ return this.itemController.getItemById(id)?.getType();
1061
+ }
1062
+
1051
1063
  /*
1052
1064
  * Get the display model of a node / edge / combo.
1053
1065
  * @param id item id
@@ -1594,6 +1606,7 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1594
1606
  value: boolean,
1595
1607
  ) {
1596
1608
  const idArr = isArray(ids) ? ids : [ids];
1609
+ if (ids === undefined || !idArr.length) return;
1597
1610
  const stateArr = isArray(states) ? states : [states];
1598
1611
  const stateOption = { ids: idArr, states: stateArr, value };
1599
1612
  const changes = {
@@ -1607,6 +1620,7 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1607
1620
  });
1608
1621
  this.emit('afteritemstatechange', {
1609
1622
  ids: idArr,
1623
+ itemTypes: idArr.map((id) => this.getTypeById(id)),
1610
1624
  states,
1611
1625
  value,
1612
1626
  action: 'updateState',
@@ -1954,8 +1968,9 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1954
1968
  });
1955
1969
  // update the graph specification
1956
1970
  modesArr.forEach((mode) => {
1957
- this.specification.modes[mode] =
1958
- this.specification.modes[mode].concat(behaviorsArr);
1971
+ this.specification.modes[mode] = (
1972
+ this.specification.modes[mode] || []
1973
+ ).concat(behaviorsArr);
1959
1974
  });
1960
1975
  }
1961
1976
  /**
@@ -1975,6 +1990,9 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1975
1990
  // update the graph specification
1976
1991
  modesArr.forEach((mode) => {
1977
1992
  behaviorKeys.forEach((key) => {
1993
+ if (!this.specification.modes[mode]) {
1994
+ return;
1995
+ }
1978
1996
  const oldBehavior = this.specification.modes[mode].find(
1979
1997
  (behavior) => isObject(behavior) && behavior.key === key,
1980
1998
  );
@@ -141,7 +141,9 @@ export class ClickSelect extends Behavior {
141
141
  // Not multiple, clear all currently selected items
142
142
  this.graph.setItemState(this.selectedIds, state, false);
143
143
  }
144
- this.graph.setItemState(itemId, state, isSelectAction);
144
+ if (!this.selectedIds.includes(itemId)) {
145
+ this.graph.setItemState(itemId, state, isSelectAction);
146
+ }
145
147
  });
146
148
 
147
149
  if (isSelectAction) {
@@ -252,7 +252,7 @@ export class DragCanvas extends Behavior {
252
252
  const shouldDebounce =
253
253
  typeof graphBehaviorOptimize === 'boolean'
254
254
  ? graphBehaviorOptimize
255
- : this.graph.getAllNodesData().length < graphBehaviorOptimize;
255
+ : this.graph.getAllNodesData().length > graphBehaviorOptimize;
256
256
 
257
257
  const now = Date.now();
258
258
  if (
@@ -242,7 +242,7 @@ export class ZoomCanvas extends Behavior {
242
242
  const shouldDebounce =
243
243
  typeof graphBehaviorOptimize === 'boolean'
244
244
  ? graphBehaviorOptimize
245
- : graph.getAllNodesData().length < graphBehaviorOptimize;
245
+ : graph.getAllNodesData().length > graphBehaviorOptimize;
246
246
 
247
247
  const now = Date.now();
248
248
  if (
@@ -1,3 +1,4 @@
1
1
  export * from './mapNodeSize';
2
2
  export * from './transformV4Data';
3
3
  export * from './validateData';
4
+ export * from './processParallelEdges';
@@ -1,4 +1,4 @@
1
- import { GraphCore } from '../../types/data';
1
+ import { GraphCore, GraphDataChanges } from '../../types/data';
2
2
  import {
3
3
  GraphData,
4
4
  ComboUserModel,
@@ -12,7 +12,24 @@ import {
12
12
  * @param graphCore the graph core stores the previous data.
13
13
  * @returns formatted data.
14
14
  */
15
+
15
16
  export const MapNodeSize = (
17
+ data: GraphDataChanges,
18
+ options: {
19
+ field?: string;
20
+ range?: [number, number];
21
+ } = {},
22
+ graphCore?: GraphCore,
23
+ ): GraphDataChanges => {
24
+ const { dataAdded, dataUpdated, dataRemoved } = data;
25
+ return {
26
+ dataAdded: handler(dataAdded, options, graphCore),
27
+ dataUpdated: handler(dataUpdated, options, graphCore),
28
+ dataRemoved: handler(dataRemoved, options, graphCore),
29
+ };
30
+ };
31
+
32
+ const handler = (
16
33
  data: GraphData,
17
34
  options: {
18
35
  field?: string;
@@ -24,6 +41,8 @@ export const MapNodeSize = (
24
41
  if (!field) return data;
25
42
  const { nodes } = data;
26
43
 
44
+ if (!nodes) return data;
45
+
27
46
  const nodeMap = new Map();
28
47
  // map the value to node size
29
48
  let maxValue = -Infinity,
@@ -0,0 +1,203 @@
1
+ import { Edge } from '@antv/graphlib';
2
+ import { loopPosition } from '../../util/loop';
3
+ import { EdgeUserModel, ID, GraphCore } from '../../types';
4
+ import { uniqBy } from '../../util/array';
5
+ import { EdgeUserModelData } from '../../types/edge';
6
+ import { GraphDataChanges } from '../../types/data';
7
+
8
+ /**
9
+ * Process edges which might overlap. For edges that share the same target and source nodes.
10
+ * @param data input user data.
11
+ * @param options.edgeIds The specific edge IDs to process, all edges by default.
12
+ * @param options.offsetDiff The offset between two parallel edges, 15 by default.
13
+ * @param options.multiEdgeType The edge type for the parallel edges, 'quadratic' by default. You can assign any custom edge type based on 'quadratic' to it.
14
+ * @param options.singleEdgeType The edge type for the single edge between two nodes, undefined by default, which means the type of the edge is kept unchanged as it is in the input data.
15
+ * @param options.loopEdgeType The edge type for a self-loop edge, undefined by default, which means the type of the edge is kept unchanged as it is in the input data.
16
+ * @returns formatted data.
17
+ */
18
+
19
+ type ParallelEdgesOptions = {
20
+ edgeIds?: ID[];
21
+ offsetDiff?: number;
22
+ multiEdgeType?: string;
23
+ singleEdgeType?: string;
24
+ loopEdgeType?: string;
25
+ };
26
+
27
+ export const ProcessParallelEdges = (
28
+ data: GraphDataChanges,
29
+ options: ParallelEdgesOptions = {},
30
+ graphCore?: GraphCore,
31
+ ): GraphDataChanges => {
32
+ // Identify the edges to be processed and their associated edges
33
+ const { edgeIds = [] } = options;
34
+ const {
35
+ dataAdded: { edges: edgesAdded = [] } = {},
36
+ dataUpdated: { edges: edgesUpdated = [] } = {},
37
+ dataRemoved: { edges: edgesRemoved = [] } = {},
38
+ } = data;
39
+
40
+ const prevEdges = graphCore ? graphCore.getAllEdges() : [];
41
+ const edgeIdsToAdd = new Set(edgesAdded?.map((edge) => edge.id));
42
+ const edgeIdsToRemove = new Set(edgesRemoved?.map((edge) => edge.id));
43
+ const cacheEdges = uniqBy([...prevEdges, ...edgesUpdated], 'id');
44
+
45
+ let edges = [...edgesRemoved, ...edgesUpdated, ...edgesAdded];
46
+
47
+ // Recognize parallel edges prior to the update
48
+ edgesUpdated.forEach((newModel) => {
49
+ const oldModel = graphCore?.getEdge(newModel.id);
50
+ if (!oldModel) return;
51
+ const { id, source, target } = newModel;
52
+ if (
53
+ (source && oldModel.source !== source) ||
54
+ (target && oldModel.target !== target)
55
+ ) {
56
+ const prevParallelEdges = getParallelEdges(
57
+ graphCore.getEdge(id),
58
+ cacheEdges,
59
+ );
60
+ edges = [...prevParallelEdges, ...edges];
61
+ }
62
+ });
63
+
64
+ // Recognize parallel edges following the modification
65
+ edges.forEach((newModel) => {
66
+ const newParallelEdges = getParallelEdges(newModel, cacheEdges);
67
+ edges = [...newParallelEdges, ...edges];
68
+ });
69
+
70
+ // Remove deleted edges and filter edges based on edgeIds.
71
+ edges = uniqBy(
72
+ edges.filter((edge) => !edgeIdsToRemove.has(edge.id)),
73
+ 'id',
74
+ );
75
+ if (edgeIds.length > 0) {
76
+ edges = edges.filter((edge) => edgeIds.includes(edge.id));
77
+ }
78
+
79
+ // Calculate the offset for parallel edges
80
+ edges = calculateOffset(edges, options);
81
+
82
+ // Render based on the type of modification
83
+ return {
84
+ ...data,
85
+ dataAdded: {
86
+ ...data.dataAdded,
87
+ edges: edges.filter((edge) => edgeIdsToAdd.has(edge.id)),
88
+ },
89
+ dataUpdated: {
90
+ ...data.dataUpdated,
91
+ edges: edges.filter((edge) => !edgeIdsToAdd.has(edge.id)),
92
+ },
93
+ };
94
+ };
95
+
96
+ /**
97
+ * Get all edges parallel to a given edge
98
+ * @param edgeModel the data model of the edge currently under consideration
99
+ * @param allEdges the set of all edges in graph
100
+ */
101
+ const getParallelEdges = (
102
+ edgeModel: EdgeUserModel,
103
+ allEdges: Edge<EdgeUserModelData>[],
104
+ ) => {
105
+ const { id, source, target } = edgeModel;
106
+ return allEdges.filter((edge) => {
107
+ return (
108
+ (edge.id !== id && edge.source === source && edge.target === target) ||
109
+ (edge.source === target && edge.target === source)
110
+ );
111
+ });
112
+ };
113
+
114
+ /**
115
+ * Calculate the offset for parallel edges.
116
+ * For each set of parallel edges, alternately assign positive and negative offsets to visually separate them.
117
+ * @param edges parallel edges affected by the modification
118
+ * @param options processing configuration
119
+ */
120
+ const calculateOffset = (
121
+ edges: EdgeUserModel[],
122
+ options: ParallelEdgesOptions,
123
+ ) => {
124
+ const {
125
+ offsetDiff = 15,
126
+ multiEdgeType = 'quadratic-edge',
127
+ singleEdgeType = undefined,
128
+ loopEdgeType = undefined,
129
+ } = options;
130
+
131
+ const len = edges.length;
132
+ const cod = offsetDiff * 2;
133
+
134
+ const edgeMap = new Map<string, EdgeUserModel[]>();
135
+ const processedEdgesSet = new Set<number>();
136
+ const reverses = {};
137
+
138
+ for (let i = 0; i < len; i++) {
139
+ if (processedEdgesSet.has(i)) continue;
140
+
141
+ const edge = edges[i];
142
+ const { source, target } = edge;
143
+ const sourceTarget = `${source}-${target}`;
144
+
145
+ if (!edgeMap.has(sourceTarget)) edgeMap.set(sourceTarget, []);
146
+
147
+ edgeMap.get(sourceTarget)!.push(edge);
148
+ processedEdgesSet.add(i);
149
+
150
+ for (let j = i + 1; j < len; j++) {
151
+ if (processedEdgesSet.has(j)) continue;
152
+
153
+ const sedge = edges[j];
154
+ const src = sedge.source;
155
+ const dst = sedge.target;
156
+
157
+ if (
158
+ (source === dst && target === src) ||
159
+ (source === src && target === dst)
160
+ ) {
161
+ edgeMap.get(sourceTarget)!.push(sedge);
162
+ processedEdgesSet.add(j);
163
+ if (source === dst && target === src) {
164
+ reverses[`${src}|${dst}|${edgeMap.get(sourceTarget)?.length - 1}`] =
165
+ true;
166
+ }
167
+ }
168
+ }
169
+ }
170
+
171
+ edgeMap.forEach((arcEdges, key) => {
172
+ const { length } = arcEdges;
173
+ for (let k = 0; k < length; k++) {
174
+ const current = arcEdges[k];
175
+ current.data ||= {};
176
+
177
+ if (current.source === current.target) {
178
+ if (loopEdgeType) current.data.type = loopEdgeType;
179
+ current.data.keyShape ||= {};
180
+ // @ts-ignore
181
+ current.data.keyShape.loopCfg = {
182
+ position: loopPosition[k % 8],
183
+ dist: Math.floor(k / 8) * 20 + 50,
184
+ };
185
+ } else if (length === 1) {
186
+ if (singleEdgeType) current.data.type = singleEdgeType;
187
+ } else {
188
+ current.data.type = multiEdgeType;
189
+ const sign =
190
+ (k % 2 === 0 ? 1 : -1) *
191
+ (reverses[`${current.source}|${current.target}|${k}`] ? -1 : 1);
192
+ current.data.keyShape ||= {};
193
+ // @ts-ignore
194
+ current.data.keyShape.curveOffset =
195
+ length % 2 === 1
196
+ ? sign * Math.ceil(k / 2) * cod
197
+ : sign * (Math.floor(k / 2) * cod + offsetDiff);
198
+ }
199
+ }
200
+ });
201
+
202
+ return edges;
203
+ };
@@ -1,5 +1,5 @@
1
1
  import { isArray, isNumber, uniqueId } from '@antv/util';
2
- import { GraphCore } from '../../types/data';
2
+ import { GraphCore, GraphDataChanges } from '../../types/data';
3
3
  import {
4
4
  GraphData,
5
5
  ComboUserModel,
@@ -14,6 +14,19 @@ import {
14
14
  * @returns formatted data.
15
15
  */
16
16
  export const TransformV4Data = (
17
+ data: GraphDataChanges,
18
+ options = {},
19
+ graphCore?: GraphCore,
20
+ ): GraphDataChanges => {
21
+ const { dataAdded, dataUpdated, dataRemoved } = data;
22
+ return {
23
+ dataAdded: handler(dataAdded, options, graphCore),
24
+ dataUpdated: handler(dataUpdated, options, graphCore),
25
+ dataRemoved: handler(dataRemoved, options, graphCore),
26
+ };
27
+ };
28
+
29
+ const handler = (
17
30
  data: GraphData,
18
31
  options = {},
19
32
  graphCore?: GraphCore,
@@ -1,19 +1,33 @@
1
1
  import { uniqueId } from '@antv/util';
2
- import { GraphCore } from '../../types/data';
2
+ import { GraphCore, GraphDataChanges } from '../../types/data';
3
3
  import {
4
4
  GraphData,
5
5
  ComboUserModel,
6
6
  EdgeUserModel,
7
7
  NodeUserModel,
8
8
  } from '../../types';
9
+ import { deconstructData } from '../../util/data';
9
10
 
10
11
  /**
11
- * Validate and format the graph data.
12
+ * Validate and format the graph data which will be added.
12
13
  * @param data input user data.
13
14
  * @param graphCore the graph core stores the previous data.
14
15
  * @returns formatted data.
15
16
  */
16
17
  export const ValidateData = (
18
+ data: GraphDataChanges,
19
+ options = {},
20
+ graphCore?: GraphCore,
21
+ ): GraphDataChanges => {
22
+ const { dataAdded, dataRemoved, dataUpdated } = data;
23
+ return {
24
+ dataAdded: handler(dataAdded, options, graphCore),
25
+ dataRemoved: handler(dataRemoved, options, graphCore),
26
+ dataUpdated: handler(dataUpdated, options, graphCore),
27
+ };
28
+ };
29
+
30
+ const handler = (
17
31
  data: GraphData,
18
32
  options = {},
19
33
  graphCore?: GraphCore,
@@ -13,7 +13,8 @@ import * as Themes from './theme';
13
13
  import * as ThemeSolvers from './themeSolver';
14
14
  import * as Plugins from './plugin';
15
15
 
16
- const { ValidateData, TransformV4Data, MapNodeSize } = Transforms;
16
+ const { ValidateData, TransformV4Data, MapNodeSize, ProcessParallelEdges } =
17
+ Transforms;
17
18
 
18
19
  const { compactBox, dendrogram, indented, mindmap } = Hierarchy;
19
20
 
@@ -215,6 +216,7 @@ const Extensions = {
215
216
  ValidateData,
216
217
  TransformV4Data,
217
218
  MapNodeSize,
219
+ ProcessParallelEdges,
218
220
  // themes
219
221
  LightTheme,
220
222
  DarkTheme,
@@ -1,4 +1,4 @@
1
- import { isArray } from '@antv/util';
1
+ import { isArray, isObject } from '@antv/util';
2
2
  import {
3
3
  NodeStyleSets,
4
4
  EdgeStyleSets,
@@ -50,12 +50,28 @@ export class SpecThemeSolver extends BaseThemeSolver {
50
50
 
51
51
  if (dataTypeField && !getStyleSets) {
52
52
  getStyleSets = (paletteProps) => {
53
- return paletteProps.map((color) => ({
54
- default: {
55
- keyShape:
56
- itemType === 'edge' ? { stroke: color } : { fill: color },
57
- },
58
- }));
53
+ if (isArray(paletteProps)) {
54
+ return paletteProps.map((color) => ({
55
+ default: {
56
+ keyShape:
57
+ itemType === 'edge' ? { stroke: color } : { fill: color },
58
+ },
59
+ }));
60
+ }
61
+ if (isObject(paletteProps)) {
62
+ const res = {};
63
+ Object.keys(paletteProps).map((dataType) => {
64
+ res[dataType] = {
65
+ default: {
66
+ keyShape:
67
+ itemType === 'edge'
68
+ ? { stroke: paletteProps[dataType] }
69
+ : { fill: paletteProps[dataType] },
70
+ },
71
+ };
72
+ });
73
+ return res;
74
+ }
59
75
  };
60
76
  }
61
77
 
package/src/types/data.ts CHANGED
@@ -14,6 +14,12 @@ export interface GraphData {
14
14
  combos?: ComboUserModel[];
15
15
  }
16
16
 
17
+ export interface GraphDataChanges {
18
+ dataAdded: GraphData;
19
+ dataUpdated: GraphData;
20
+ dataRemoved: GraphData;
21
+ }
22
+
17
23
  export type TreeData =
18
24
  | ITreeData<NodeUserModelData>
19
25
  | ITreeData<NodeUserModelData>[];
@@ -139,6 +139,12 @@ export interface IGraph<
139
139
  * @group Data
140
140
  */
141
141
  getComboChildrenData: (comboId: ID) => (ComboModel | NodeModel)[];
142
+ /**
143
+ * Get item type by id.
144
+ * @param id
145
+ * @returns 'node' | 'edge' | 'combo'
146
+ */
147
+ getTypeById: (id: ID) => ITEM_TYPE;
142
148
  /**
143
149
  * Input data and render the graph.
144
150
  * If there is old data, diffs and changes it.
package/src/types/item.ts CHANGED
@@ -115,7 +115,8 @@ export type SHAPE_TYPE =
115
115
  | 'polyline'
116
116
  | 'line'
117
117
  | 'path'
118
- | 'text';
118
+ | 'text'
119
+ | 'group';
119
120
 
120
121
  export type SHAPE_TYPE_3D = 'sphere' | 'cube' | 'plane';
121
122
 
package/src/types/spec.ts CHANGED
@@ -93,6 +93,7 @@ export interface Specification<
93
93
  | string[]
94
94
  | {
95
95
  type: string;
96
+ activeLifecycle: string | string[];
96
97
  [param: string]: unknown; // TODO: generate by plugins
97
98
  }[]
98
99
  | TransformerFn[];
package/src/util/array.ts CHANGED
@@ -66,3 +66,15 @@ export function replaceElements(arr, target, replaceWith) {
66
66
  }
67
67
  return arr;
68
68
  }
69
+
70
+ export const uniqBy = <T>(arr: T[], key: keyof T): T[] => {
71
+ return Object.values(
72
+ arr.reduce(
73
+ (map, item) => ({
74
+ ...map,
75
+ [`${item[key]}`]: item,
76
+ }),
77
+ {},
78
+ ),
79
+ );
80
+ };
package/src/util/data.ts CHANGED
@@ -1,7 +1,12 @@
1
1
  import { ID, TreeData } from '@antv/graphlib';
2
2
  import { isArray } from '@antv/util';
3
3
  import { depthFirstSearch, connectedComponent } from '@antv/algorithm';
4
- import { GraphCore, GraphData } from '../types/data';
4
+ import {
5
+ DataChangeType,
6
+ GraphCore,
7
+ GraphData,
8
+ GraphDataChanges,
9
+ } from '../types/data';
5
10
  import { IGraph } from '../types/graph';
6
11
  import { NodeModel, NodeUserModel } from '../types';
7
12
  import { NodeUserModelData } from '../types/node';
@@ -304,3 +309,43 @@ export const traverse = (treeData, callback) => {
304
309
  });
305
310
  }
306
311
  };
312
+
313
+ export const DEFAULT_ACTIVE_DATA_LIFECYCLE = ['read'];
314
+
315
+ export const AVAILABLE_DATA_LIFECYCLE = [
316
+ 'read',
317
+ 'changeData',
318
+ 'updateData',
319
+ 'addData',
320
+ 'removeData',
321
+ ];
322
+
323
+ export const dataLifecycleMap: Record<string, string> = {
324
+ replace: 'read',
325
+ mergeReplace: 'changeData',
326
+ union: 'addData',
327
+ remove: 'removeData',
328
+ update: 'updateData',
329
+ };
330
+
331
+ /**
332
+ * Whether the graph is empty.
333
+ * @param graph graph instance.
334
+ * @param excludeInvisibles whether exclude invisibles. true means the function should test without visible items.
335
+ * @returns graph is empty or not.
336
+ */
337
+ export const isEmptyGraph = (graph, excludeInvisibles = false): Boolean => {
338
+ const nodes = graph.getAllNodesData();
339
+ const combos = graph.getAllCombosData();
340
+ if (nodes.length === 0 && combos.length === 0) return true;
341
+
342
+ if (excludeInvisibles) {
343
+ const edges = graph.getAllEdgesData();
344
+ const hasVisibleItems =
345
+ nodes.filter((node) => graph.getItemVisible(node.id)).length +
346
+ edges.filter((edge) => graph.getItemVisible(edge.id)).length +
347
+ combos.filter((combo) => graph.getItemVisible(combo.id)).length;
348
+ if (!hasVisibleItems) return true;
349
+ }
350
+ return false;
351
+ };
package/src/util/loop.ts CHANGED
@@ -2,6 +2,17 @@ import { Tuple3Number } from '@antv/g';
2
2
  import { vec2 } from 'gl-matrix';
3
3
  import { LOOP_POSITION, LoopPosition } from '../types/loop';
4
4
 
5
+ export const loopPosition = [
6
+ 'top',
7
+ 'top-right',
8
+ 'right',
9
+ 'bottom-right',
10
+ 'bottom',
11
+ 'bottom-left',
12
+ 'left',
13
+ 'top-left',
14
+ ];
15
+
5
16
  const PI_OVER_8 = Math.PI / 8;
6
17
 
7
18
  const radiansMap = {
package/src/util/shape.ts CHANGED
@@ -44,6 +44,7 @@ export const ShapeTagMap = {
44
44
  text: Text,
45
45
  image: Image,
46
46
  path: Path,
47
+ group: Group,
47
48
  };
48
49
 
49
50
  const LINE_TYPES = ['line', 'polyline', 'path'];