@antv/g6 5.0.0-beta.23 → 5.0.0-beta.24

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 (55) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/esm/index.d.ts +1 -1
  3. package/esm/index.js +1 -1
  4. package/esm/item/combo.js +1 -0
  5. package/esm/item/combo.js.map +1 -1
  6. package/esm/item/edge.js +2 -1
  7. package/esm/item/edge.js.map +1 -1
  8. package/esm/item/item.js +3 -4
  9. package/esm/item/item.js.map +1 -1
  10. package/esm/runtime/controller/layout.d.ts +1 -0
  11. package/esm/runtime/controller/layout.js +39 -4
  12. package/esm/runtime/controller/layout.js.map +1 -1
  13. package/esm/runtime/controller/viewport.js +19 -3
  14. package/esm/runtime/controller/viewport.js.map +1 -1
  15. package/esm/runtime/graph.js +28 -4
  16. package/esm/runtime/graph.js.map +1 -1
  17. package/esm/stdlib/theme/dark.js +22 -22
  18. package/esm/stdlib/theme/light.js +20 -20
  19. package/esm/types/view.d.ts +3 -0
  20. package/esm/util/animate.d.ts +1 -1
  21. package/esm/util/animate.js +45 -17
  22. package/esm/util/animate.js.map +1 -1
  23. package/lib/index.d.ts +1 -1
  24. package/lib/index.js +1 -1
  25. package/lib/item/combo.js +1 -0
  26. package/lib/item/combo.js.map +1 -1
  27. package/lib/item/edge.js +2 -1
  28. package/lib/item/edge.js.map +1 -1
  29. package/lib/item/item.js +3 -4
  30. package/lib/item/item.js.map +1 -1
  31. package/lib/runtime/controller/layout.d.ts +1 -0
  32. package/lib/runtime/controller/layout.js +38 -3
  33. package/lib/runtime/controller/layout.js.map +1 -1
  34. package/lib/runtime/controller/viewport.js +19 -3
  35. package/lib/runtime/controller/viewport.js.map +1 -1
  36. package/lib/runtime/graph.js +28 -4
  37. package/lib/runtime/graph.js.map +1 -1
  38. package/lib/stdlib/theme/dark.js +22 -22
  39. package/lib/stdlib/theme/light.js +20 -20
  40. package/lib/types/view.d.ts +3 -0
  41. package/lib/util/animate.d.ts +1 -1
  42. package/lib/util/animate.js +45 -17
  43. package/lib/util/animate.js.map +1 -1
  44. package/package.json +2 -2
  45. package/src/index.ts +1 -1
  46. package/src/item/combo.ts +1 -0
  47. package/src/item/edge.ts +2 -1
  48. package/src/item/item.ts +7 -4
  49. package/src/runtime/controller/layout.ts +37 -4
  50. package/src/runtime/controller/viewport.ts +27 -3
  51. package/src/runtime/graph.ts +40 -8
  52. package/src/stdlib/theme/dark.ts +22 -22
  53. package/src/stdlib/theme/light.ts +20 -20
  54. package/src/types/view.ts +3 -0
  55. package/src/util/animate.ts +62 -25
package/src/item/edge.ts CHANGED
@@ -128,7 +128,7 @@ export default class Edge extends Item {
128
128
  this.shapeMap, // shapeMap
129
129
  this.group,
130
130
  firstRendering ? 'buildIn' : 'update',
131
- this.changedStates,
131
+ diffState.current.map((state) => state.name),
132
132
  this.animateFrameListener,
133
133
  (canceled) => onfinish(displayModel.id, canceled),
134
134
  );
@@ -138,6 +138,7 @@ export default class Edge extends Item {
138
138
  this.visible = true;
139
139
  this.hide(false);
140
140
  }
141
+ this.changedStates = [];
141
142
  }
142
143
 
143
144
  /**
package/src/item/item.ts CHANGED
@@ -168,14 +168,14 @@ export default abstract class Item implements IItem {
168
168
  onfinish: Function = () => {},
169
169
  ) {
170
170
  // call this.renderExt.draw in extend implementations
171
- this.afterDrawShapeMap =
171
+ const afterDrawShapeMap =
172
172
  this.renderExt.afterDraw?.(displayModel, {
173
173
  ...this.shapeMap,
174
174
  ...this.afterDrawShapeMap,
175
175
  }) || {};
176
- this.shapeMap = updateShapes(
177
- this.shapeMap,
176
+ const shapeMap = updateShapes(
178
177
  this.afterDrawShapeMap,
178
+ afterDrawShapeMap,
179
179
  this.group,
180
180
  false,
181
181
  (id) => {
@@ -188,7 +188,10 @@ export default abstract class Item implements IItem {
188
188
  return true;
189
189
  },
190
190
  );
191
- this.changedStates = [];
191
+ this.shapeMap = {
192
+ ...this.shapeMap,
193
+ ...shapeMap,
194
+ };
192
195
  }
193
196
 
194
197
  /**
@@ -1,6 +1,5 @@
1
1
  import { Animation, DisplayObject, IAnimationEffectTiming } from '@antv/g';
2
- import Hierarchy from '@antv/hierarchy';
3
- import { Graph as GraphLib } from '@antv/graphlib';
2
+ import { Graph as GraphLib, ID } from '@antv/graphlib';
4
3
  import {
5
4
  isLayoutWithIterations,
6
5
  Layout,
@@ -8,7 +7,7 @@ import {
8
7
  OutNode,
9
8
  Supervisor,
10
9
  } from '@antv/layout';
11
- import registery, { stdLib } from '../../stdlib';
10
+ import registery, { Extensions, stdLib } from '../../stdlib';
12
11
  import {
13
12
  IGraph,
14
13
  isImmediatelyInvokedLayoutOptions,
@@ -38,6 +37,7 @@ export class LayoutController {
38
37
  private currentSupervisor: Supervisor | null;
39
38
  private currentAnimation: Animation | null;
40
39
  private animatedDisplayObject: DisplayObject;
40
+ private previousNodes: Map<ID, object>;
41
41
 
42
42
  constructor(graph: IGraph<any, any>) {
43
43
  this.graph = graph;
@@ -118,6 +118,15 @@ export class LayoutController {
118
118
  width,
119
119
  height,
120
120
  center,
121
+ preset: layoutNodes.map((node) => {
122
+ const { x, y, z } = node.data;
123
+ if (isNaN(x) || isNaN(y)) return;
124
+ const otherProps = this.previousNodes?.get(node.id);
125
+ return {
126
+ id: node.id,
127
+ data: { x, y, z, ...otherProps },
128
+ };
129
+ }),
121
130
  ...rest,
122
131
  });
123
132
 
@@ -165,11 +174,25 @@ export class LayoutController {
165
174
  }
166
175
 
167
176
  // Initialize layout.
177
+ const useCache = layoutCtor === Extensions.DagreLayout;
168
178
  const layout = new layoutCtor({
169
179
  nodeSize,
170
180
  width,
171
181
  height,
172
182
  center,
183
+ preset: useCache
184
+ ? layoutNodes
185
+ .map((node) => {
186
+ const { x, y, z } = node.data;
187
+ if (isNaN(x) || isNaN(y)) return;
188
+ const otherProps = this.previousNodes?.get(node.id);
189
+ return {
190
+ id: node.id,
191
+ data: { x, y, z, ...otherProps },
192
+ };
193
+ })
194
+ .filter(Boolean)
195
+ : undefined,
173
196
  ...rest,
174
197
  });
175
198
  this.currentLayout = layout;
@@ -314,7 +337,17 @@ export class LayoutController {
314
337
 
315
338
  private updateNodesPosition(positions: LayoutMapping, animate = true) {
316
339
  const { nodes, edges } = positions;
317
- this.graph.updateNodePosition(nodes, undefined, !animate);
340
+ this.previousNodes = new Map();
341
+ const nodePositions = nodes.map((node) => {
342
+ const { x, y, z, ...others } = node.data;
343
+ this.previousNodes.set(node.id, others);
344
+ const data = isNaN(z) ? { x, y } : { x, y, z };
345
+ return {
346
+ id: node.id,
347
+ data,
348
+ };
349
+ });
350
+ this.graph.updateNodePosition(nodePositions, undefined, !animate);
318
351
  const edgeToUpdate = edges
319
352
  .filter((edge) => edge.data.controlPoints)
320
353
  .map((edge) => ({
@@ -26,6 +26,8 @@ export class ViewportController {
26
26
  }: ViewportChangeHookParams) {
27
27
  const camera = this.graph.canvas.getCamera();
28
28
  const transientCamera = this.graph.transientCanvas.getCamera();
29
+ camera.cancelLandmarkAnimation();
30
+ transientCamera.cancelLandmarkAnimation();
29
31
  const {
30
32
  translate,
31
33
  rotate,
@@ -48,11 +50,33 @@ export class ViewportController {
48
50
  }> = {};
49
51
 
50
52
  if (translate) {
51
- const { dx = 0, dy = 0, dz = 0 } = translate;
52
53
  const [px, py, pz] = camera.getPosition();
53
54
  const [fx, fy, fz] = camera.getFocalPoint();
54
- landmarkOptions.position = [px - dx, py - dy, pz - dz];
55
- landmarkOptions.focalPoint = [fx - dx, fy - dy, fz - dz];
55
+ const { dx = 0, dy = 0, dz = 0, targetX, targetY, targetZ } = translate;
56
+ const animateTargetPosition = [targetX, targetY, targetZ];
57
+ const animateTargetFocal = [targetX, targetY, targetZ];
58
+ if (isNaN(targetX)) {
59
+ animateTargetPosition[0] = px - dx;
60
+ animateTargetFocal[0] = fx - dx;
61
+ }
62
+ if (isNaN(targetY)) {
63
+ animateTargetPosition[1] = py - dy;
64
+ animateTargetFocal[1] = fy - dy;
65
+ }
66
+ if (isNaN(targetZ)) {
67
+ animateTargetPosition[2] = pz - dz;
68
+ animateTargetFocal[2] = fz - dz;
69
+ }
70
+ landmarkOptions.position = animateTargetPosition as [
71
+ number,
72
+ number,
73
+ number,
74
+ ];
75
+ landmarkOptions.focalPoint = animateTargetFocal as [
76
+ number,
77
+ number,
78
+ number,
79
+ ];
56
80
  }
57
81
 
58
82
  if (zoom) {
@@ -615,9 +615,17 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
615
615
  }>,
616
616
  effectTiming?: CameraAnimationOptions,
617
617
  ) {
618
+ const { x: cx, y: cy } = this.getViewportCenter();
619
+ const { dx, dy, dz } = distance;
618
620
  await this.transform(
619
621
  {
620
- translate: distance,
622
+ translate: {
623
+ dx,
624
+ dy,
625
+ dz,
626
+ targetX: cx - dx,
627
+ targetY: cy - dy,
628
+ },
621
629
  },
622
630
  effectTiming,
623
631
  );
@@ -633,7 +641,19 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
633
641
  effectTiming?: CameraAnimationOptions,
634
642
  ) {
635
643
  const { x: cx, y: cy } = this.getViewportCenter();
636
- await this.translate({ dx: cx - x, dy: cy - y }, effectTiming);
644
+ const canvasPoint = this.canvas.viewport2Canvas({ x, y });
645
+
646
+ await this.transform(
647
+ {
648
+ translate: {
649
+ dx: cx - x,
650
+ dy: cy - y,
651
+ targetX: canvasPoint.x,
652
+ targetY: canvasPoint.y,
653
+ },
654
+ },
655
+ effectTiming,
656
+ );
637
657
  }
638
658
 
639
659
  /**
@@ -745,17 +765,20 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
745
765
  direction = 'both',
746
766
  ratioRule = 'min',
747
767
  boundsType = 'render',
768
+ onlyOutOfViewPort = true,
748
769
  } = rules || {};
749
770
 
750
771
  const {
772
+ min,
773
+ max,
751
774
  center: [graphCenterX, graphCenterY],
752
775
  halfExtents,
753
- } =
754
- boundsType === 'render'
755
- ? // Get the bounds of the whole graph content.
756
- this.canvas.document.documentElement.getBounds()
757
- : // Get the bounds of the nodes positions while the graph content is not ready.
758
- getLayoutBounds(this);
776
+ } = boundsType === 'render'
777
+ ? // Get the bounds of the whole graph content.
778
+ this.canvas.document.documentElement.getBounds()
779
+ : // Get the bounds of the nodes positions while the graph content is not ready.
780
+ getLayoutBounds(this);
781
+
759
782
  const origin = this.canvas.canvas2Viewport({
760
783
  x: graphCenterX,
761
784
  y: graphCenterY,
@@ -771,6 +794,13 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
771
794
  y: viewportHeight! - bottom,
772
795
  });
773
796
 
797
+ const isOutOfView =
798
+ min[0] < tlInCanvas.x ||
799
+ min[1] < tlInCanvas.y ||
800
+ max[0] > brInCanvas.x ||
801
+ max[1] > brInCanvas.y;
802
+ if (onlyOutOfViewPort && !isOutOfView) return;
803
+
774
804
  const targetViewWidth = brInCanvas.x - tlInCanvas.x;
775
805
  const targetViewHeight = brInCanvas.y - tlInCanvas.y;
776
806
 
@@ -794,6 +824,8 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
794
824
  translate: {
795
825
  dx: viewportWidth! / 2 - origin.x,
796
826
  dy: viewportHeight! / 2 - origin.y,
827
+ targetX: graphCenterX,
828
+ targetY: graphCenterY,
797
829
  },
798
830
  zoom: {
799
831
  ratio,
@@ -1,10 +1,10 @@
1
1
  import { DEFAULT_SHAPE_STYLE, DEFAULT_TEXT_STYLE } from '../../constant';
2
2
  import { ThemeSpecification } from '../../types/theme';
3
3
 
4
- const subjectColor = 'rgb(34,126,255)';
4
+ const subjectColor = 'rgb(23,131,255)';
5
5
  const textColor = 'rgba(255,255,255,0.85)';
6
6
 
7
- const nodeColor = 'rgb(34,126,255)';
7
+ const nodeColor = 'rgb(23,131,255)';
8
8
  const edgeColor = 'rgb(153, 173, 209)';
9
9
  const comboFill = 'rgb(253, 253, 253)';
10
10
  const comboStroke = 'rgba(153,173,209,1)';
@@ -17,16 +17,16 @@ const nodeStroke = '#D0E4FF';
17
17
  export const DarkTheme = {
18
18
  node: {
19
19
  palette: [
20
- '#4089FF',
21
- '#CD75FF',
22
- '#06B8A8',
23
- '#FF8834',
24
- '#5888C3',
25
- '#FA73CD',
26
- '#07BCE0',
27
- '#CB962A',
28
- '#23AD61',
29
- '#FF8075',
20
+ '#1783FF',
21
+ '#00C9C9',
22
+ '#F08F56',
23
+ '#D580FF',
24
+ '#7863FF',
25
+ '#DB9D0D',
26
+ '#60C42D',
27
+ '#FF80CA',
28
+ '#2491B3',
29
+ '#17C76F',
30
30
  ],
31
31
  lodStrategy: {
32
32
  levels: [
@@ -170,16 +170,16 @@ export const DarkTheme = {
170
170
  edge: {
171
171
  palette: [
172
172
  '#637088',
173
- '#2A59A6',
174
- '#854CA6',
175
- '#04786D',
176
- '#A65822',
177
- '#39587F',
178
- '#A34B85',
179
- '#057A92',
180
- '#84621B',
181
- '#17703F',
182
- '#A6534C',
173
+ '#1783FF',
174
+ '#00C9C9',
175
+ '#F08F56',
176
+ '#D580FF',
177
+ '#7863FF',
178
+ '#DB9D0D',
179
+ '#60C42D',
180
+ '#FF80CA',
181
+ '#2491B3',
182
+ '#17C76F',
183
183
  ],
184
184
  lodStrategy: {
185
185
  levels: [
@@ -21,16 +21,16 @@ const disabledStroke = 'rgba(153,173,209,1)';
21
21
  export const LightTheme = {
22
22
  node: {
23
23
  palette: [
24
- '#4089FF',
25
- '#CD75FF',
26
- '#06B8A8',
27
- '#FF8834',
28
- '#5888C3',
29
- '#FA73CD',
30
- '#07BCE0',
31
- '#CB962A',
32
- '#23AD61',
33
- '#FF8075',
24
+ '#1783FF',
25
+ '#00C9C9',
26
+ '#F08F56',
27
+ '#D580FF',
28
+ '#7863FF',
29
+ '#DB9D0D',
30
+ '#60C42D',
31
+ '#FF80CA',
32
+ '#2491B3',
33
+ '#17C76F',
34
34
  ],
35
35
  lodStrategy: {
36
36
  levels: [
@@ -172,16 +172,16 @@ export const LightTheme = {
172
172
  edge: {
173
173
  palette: [
174
174
  '#99ADD1',
175
- '#4089FF',
176
- '#CD75FF',
177
- '#06B8A8',
178
- '#FF8834',
179
- '#5888C3',
180
- '#FA73CD',
181
- '#07BCE0',
182
- '#CB962A',
183
- '#23AD61',
184
- '#FF8075',
175
+ '#1783FF',
176
+ '#00C9C9',
177
+ '#F08F56',
178
+ '#D580FF',
179
+ '#7863FF',
180
+ '#DB9D0D',
181
+ '#60C42D',
182
+ '#FF80CA',
183
+ '#2491B3',
184
+ '#17C76F',
185
185
  ],
186
186
  lodStrategy: {
187
187
  levels: [
package/src/types/view.ts CHANGED
@@ -22,6 +22,9 @@ export type GraphTransformOptions = {
22
22
  dx: number;
23
23
  dy: number;
24
24
  dz: number;
25
+ targetX: number;
26
+ targetY: number;
27
+ targetZ: number;
25
28
  }>;
26
29
  rotate?: {
27
30
  angle: number;
@@ -6,14 +6,10 @@ import {
6
6
  Path,
7
7
  Polyline,
8
8
  } from '@antv/g';
9
- import {
10
- AnimateTiming,
11
- IAnimate,
12
- IAnimates,
13
- IStateAnimate,
14
- } from '../types/animate';
15
- import { ItemDisplayModel, ItemShapeStyles, ShapeStyle } from '../types/item';
9
+ import { AnimateTiming, IAnimate, IAnimates } from '../types/animate';
10
+ import { ItemShapeStyles, ShapeStyle } from '../types/item';
16
11
  import { isArrayOverlap, replaceElements } from './array';
12
+ import { clone, isString, uniq } from '@antv/util';
17
13
 
18
14
  /**
19
15
  * Initial(timing = show) shape animation start from init shape styles, and end to the shape's style config.
@@ -28,9 +24,8 @@ export const getShapeAnimateBeginStyles = (shape) => {
28
24
  offsetDistance: 0,
29
25
  };
30
26
  if (['line', 'polyline', 'path'].includes(shapeType)) {
31
- const totalLength = shape.getTotalLength();
32
27
  return {
33
- lineDash: [0, totalLength],
28
+ lineDash: [0, '100%'],
34
29
  ...commonStyles,
35
30
  };
36
31
  } else if (shapeType === 'circle') {
@@ -136,13 +131,11 @@ const groupTimingAnimates = (
136
131
  const isStateUpdate = segmentedTiming === 'stateUpdate';
137
132
  animates[isStateUpdate ? 'update' : segmentedTiming].forEach((item: any) => {
138
133
  const { order = 0, states } = item;
139
- if (
140
- !isStateUpdate ||
141
- (isStateUpdate && isArrayOverlap(states, changedStates))
142
- ) {
143
- timingAnimateGroups[order] = timingAnimateGroups[order] || [];
144
- timingAnimateGroups[order].push(item);
134
+ if (states?.length) {
135
+ if (!isArrayOverlap(states, changedStates)) return;
145
136
  }
137
+ timingAnimateGroups[order] = timingAnimateGroups[order] || [];
138
+ timingAnimateGroups[order].push(item);
146
139
  });
147
140
  return timingAnimateGroups;
148
141
  };
@@ -255,7 +248,13 @@ const runAnimateGroupOnShapes = (
255
248
  }
256
249
  return animation;
257
250
  });
258
- if (maxDurationIdx > -1) animations[maxDurationIdx].onfinish = onfinish;
251
+ if (maxDurationIdx > -1) {
252
+ const selfOnfinish = animations[maxDurationIdx].onfinish;
253
+ animations[maxDurationIdx].onfinish = () => {
254
+ selfOnfinish?.();
255
+ onfinish?.();
256
+ };
257
+ }
259
258
  return animations;
260
259
  };
261
260
 
@@ -276,19 +275,51 @@ const runAnimateOnShape = (
276
275
  animateConfig,
277
276
  ) => {
278
277
  let animateArr;
278
+ let percentLineDash = false;
279
279
  if (!fields?.length) {
280
280
  animateArr = getStyleDiff(shape.attributes, targetStyle);
281
281
  } else {
282
282
  animateArr = [{}, {}];
283
283
  fields.forEach((key) => {
284
- animateArr[0][key] = shape.attributes.hasOwnProperty(key)
285
- ? shape.style[key]
286
- : beginStyle[key];
287
- animateArr[1][key] =
288
- targetStyle[key] === undefined ? animateArr[0][key] : targetStyle[key];
289
- if (key === 'lineDash' && animateArr[1][key].includes('100%')) {
290
- const totalLength = (shape as Line | Polyline | Path).getTotalLength();
291
- replaceElements(animateArr[1][key], '100%', totalLength);
284
+ animateArr[0][key] =
285
+ shape.style[key] === undefined
286
+ ? clone(beginStyle[key])
287
+ : clone(shape.style[key]);
288
+ if (targetStyle[key] === undefined) return;
289
+ animateArr[1][key] = clone(targetStyle[key]);
290
+ if (key === 'lineDash') {
291
+ const beginPercents = uniq(
292
+ animateArr[0][key].filter(
293
+ (val) => isString(val) && val.includes('%'),
294
+ ),
295
+ );
296
+ const targetPercents = uniq(
297
+ animateArr[1][key].filter(
298
+ (val) => isString(val) && val.includes('%'),
299
+ ),
300
+ );
301
+ if (beginPercents.length || targetPercents.length) {
302
+ percentLineDash = clone(animateArr[0].lineDash);
303
+ const totalLength = (
304
+ shape as Line | Polyline | Path
305
+ ).getTotalLength();
306
+ beginPercents.forEach((percent, i) => {
307
+ replaceElements(
308
+ animateArr[0][key],
309
+ percent,
310
+ (Number(percent.replace('%', '')) / 100) * totalLength,
311
+ );
312
+ });
313
+ targetPercents.forEach((percent, i) => {
314
+ replaceElements(
315
+ animateArr[1][key],
316
+ percent,
317
+ (Number(percent.replace('%', '')) / 100) * totalLength,
318
+ );
319
+ });
320
+ }
321
+ } else if (key === 'offsetDistance') {
322
+ animateArr[1][key] = animateArr[1][key] || 1;
292
323
  }
293
324
  });
294
325
  }
@@ -300,7 +331,13 @@ const runAnimateOnShape = (
300
331
  );
301
332
  return;
302
333
  }
303
- return shape.animate(animateArr, animateConfig);
334
+ const animation = shape.animate(animateArr, animateConfig);
335
+ if (percentLineDash) {
336
+ animation.onfinish = () => {
337
+ shape.style.lineDash = percentLineDash;
338
+ };
339
+ }
340
+ return animation;
304
341
  };
305
342
 
306
343
  /**