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

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 (67) 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 +8 -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 +51 -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 +51 -25
  16. package/esm/runtime/graph.js.map +1 -1
  17. package/esm/stdlib/data/mapNodeSize.js +3 -3
  18. package/esm/stdlib/data/mapNodeSize.js.map +1 -1
  19. package/esm/stdlib/theme/dark.js +22 -22
  20. package/esm/stdlib/theme/light.js +20 -20
  21. package/esm/types/view.d.ts +7 -2
  22. package/esm/util/animate.d.ts +1 -1
  23. package/esm/util/animate.js +45 -17
  24. package/esm/util/animate.js.map +1 -1
  25. package/esm/util/shape.d.ts +2 -0
  26. package/esm/util/shape.js +7 -0
  27. package/esm/util/shape.js.map +1 -1
  28. package/lib/index.d.ts +1 -1
  29. package/lib/index.js +1 -1
  30. package/lib/item/combo.js +1 -0
  31. package/lib/item/combo.js.map +1 -1
  32. package/lib/item/edge.js +2 -1
  33. package/lib/item/edge.js.map +1 -1
  34. package/lib/item/item.js +8 -4
  35. package/lib/item/item.js.map +1 -1
  36. package/lib/runtime/controller/layout.d.ts +1 -0
  37. package/lib/runtime/controller/layout.js +50 -3
  38. package/lib/runtime/controller/layout.js.map +1 -1
  39. package/lib/runtime/controller/viewport.js +19 -3
  40. package/lib/runtime/controller/viewport.js.map +1 -1
  41. package/lib/runtime/graph.js +51 -25
  42. package/lib/runtime/graph.js.map +1 -1
  43. package/lib/stdlib/data/mapNodeSize.js +3 -3
  44. package/lib/stdlib/data/mapNodeSize.js.map +1 -1
  45. package/lib/stdlib/theme/dark.js +22 -22
  46. package/lib/stdlib/theme/light.js +20 -20
  47. package/lib/types/view.d.ts +7 -2
  48. package/lib/util/animate.d.ts +1 -1
  49. package/lib/util/animate.js +45 -17
  50. package/lib/util/animate.js.map +1 -1
  51. package/lib/util/shape.d.ts +2 -0
  52. package/lib/util/shape.js +7 -0
  53. package/lib/util/shape.js.map +1 -1
  54. package/package.json +2 -2
  55. package/src/index.ts +1 -1
  56. package/src/item/combo.ts +1 -0
  57. package/src/item/edge.ts +2 -1
  58. package/src/item/item.ts +13 -4
  59. package/src/runtime/controller/layout.ts +55 -4
  60. package/src/runtime/controller/viewport.ts +27 -3
  61. package/src/runtime/graph.ts +69 -35
  62. package/src/stdlib/data/mapNodeSize.ts +3 -3
  63. package/src/stdlib/theme/dark.ts +22 -22
  64. package/src/stdlib/theme/light.ts +20 -20
  65. package/src/types/view.ts +7 -2
  66. package/src/util/animate.ts +62 -25
  67. package/src/util/shape.ts +11 -0
@@ -482,34 +482,33 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
482
482
 
483
483
  this.once('afterlayout', async () => {
484
484
  const { autoFit } = this.specification;
485
- if (autoFit) {
486
- if (autoFit === 'view') {
487
- await this.fitView({ rules: { boundsType: 'layout' } });
488
- } else if (autoFit === 'center') {
489
- await this.fitCenter('layout');
490
- } else {
491
- const { type, effectTiming, ...others } = autoFit;
492
- if (type === 'view') {
493
- const { padding, rules } = others as {
494
- padding: Padding;
495
- rules: FitViewRules;
496
- };
497
- await this.fitView(
498
- {
499
- padding,
500
- rules: {
501
- ...rules,
502
- boundsType: 'layout',
503
- },
485
+ if (!autoFit) return;
486
+ if (autoFit === 'view') {
487
+ await this.fitView({ rules: { boundsType: 'layout' } });
488
+ } else if (autoFit === 'center') {
489
+ await this.fitCenter('layout');
490
+ } else {
491
+ const { type, effectTiming, ...others } = autoFit;
492
+ if (type === 'view') {
493
+ const { padding, rules } = others as {
494
+ padding: Padding;
495
+ rules: FitViewRules;
496
+ };
497
+ await this.fitView(
498
+ {
499
+ padding,
500
+ rules: {
501
+ ...rules,
502
+ boundsType: 'layout',
504
503
  },
505
- effectTiming,
506
- );
507
- } else if (type === 'center') {
508
- await this.fitCenter('layout', effectTiming);
509
- } else if (type === 'position') {
510
- // TODO: align
511
- await this.translateTo((others as any).position, effectTiming);
512
- }
504
+ },
505
+ effectTiming,
506
+ );
507
+ } else if (type === 'center') {
508
+ await this.fitCenter('layout', effectTiming);
509
+ } else if (type === 'position') {
510
+ // TODO: align
511
+ await this.translateTo((others as any).position, effectTiming);
513
512
  }
514
513
  }
515
514
  });
@@ -615,9 +614,17 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
615
614
  }>,
616
615
  effectTiming?: CameraAnimationOptions,
617
616
  ) {
617
+ const { x: cx, y: cy } = this.getViewportCenter();
618
+ const { dx, dy, dz } = distance;
618
619
  await this.transform(
619
620
  {
620
- translate: distance,
621
+ translate: {
622
+ dx,
623
+ dy,
624
+ dz,
625
+ targetX: cx - dx,
626
+ targetY: cy - dy,
627
+ },
621
628
  },
622
629
  effectTiming,
623
630
  );
@@ -633,7 +640,19 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
633
640
  effectTiming?: CameraAnimationOptions,
634
641
  ) {
635
642
  const { x: cx, y: cy } = this.getViewportCenter();
636
- await this.translate({ dx: cx - x, dy: cy - y }, effectTiming);
643
+ const canvasPoint = this.canvas.viewport2Canvas({ x, y });
644
+
645
+ await this.transform(
646
+ {
647
+ translate: {
648
+ dx: cx - x,
649
+ dy: cy - y,
650
+ targetX: canvasPoint.x,
651
+ targetY: canvasPoint.y,
652
+ },
653
+ },
654
+ effectTiming,
655
+ );
637
656
  }
638
657
 
639
658
  /**
@@ -745,17 +764,21 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
745
764
  direction = 'both',
746
765
  ratioRule = 'min',
747
766
  boundsType = 'render',
767
+ onlyOutOfViewport = false,
768
+ onlyZoomAtLargerThanViewport = false,
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
 
@@ -789,11 +819,15 @@ export class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
789
819
  : Math.min(wRatio, hRatio);
790
820
  }
791
821
 
822
+ if (onlyZoomAtLargerThanViewport && ratio > 1) ratio = 1;
823
+
792
824
  await this.transform(
793
825
  {
794
826
  translate: {
795
827
  dx: viewportWidth! / 2 - origin.x,
796
828
  dy: viewportHeight! / 2 - origin.y,
829
+ targetX: graphCenterX,
830
+ targetY: graphCenterY,
797
831
  },
798
832
  zoom: {
799
833
  ratio,
@@ -23,9 +23,9 @@ export const MapNodeSize = (
23
23
  ): GraphDataChanges => {
24
24
  const { dataAdded, dataUpdated, dataRemoved } = data;
25
25
  return {
26
- dataAdded: handler(dataAdded, options, graphCore),
27
- dataUpdated: handler(dataUpdated, options, graphCore),
28
- dataRemoved: handler(dataRemoved, options, graphCore),
26
+ dataAdded: handler(dataAdded || {}, options, graphCore),
27
+ dataUpdated: handler(dataUpdated || {}, options, graphCore),
28
+ dataRemoved: dataRemoved,
29
29
  };
30
30
  };
31
31
 
@@ -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
@@ -1,6 +1,8 @@
1
1
  export interface FitViewRules {
2
- /** Whehter fit it only when the graph is out of the view. */
3
- onlyOutOfViewPort?: boolean;
2
+ /** Whehter fit it only when the graph is out of the viewport. */
3
+ onlyOutOfViewport?: boolean;
4
+ /** Whether zoom the graph only when the graph is larger than the viewport. */
5
+ onlyZoomAtLargerThanViewport?: boolean;
4
6
  /** Axis to fit. */
5
7
  direction?: 'x' | 'y' | 'both';
6
8
  /** Ratio rule to fit. */
@@ -22,6 +24,9 @@ export type GraphTransformOptions = {
22
24
  dx: number;
23
25
  dy: number;
24
26
  dz: number;
27
+ targetX: number;
28
+ targetY: number;
29
+ targetZ: number;
25
30
  }>;
26
31
  rotate?: {
27
32
  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
  /**
package/src/util/shape.ts CHANGED
@@ -563,6 +563,7 @@ export const getShapeLocalBoundsByStyle = (
563
563
  min: number[];
564
564
  max: number[];
565
565
  center: number[];
566
+ halfExtents: number[];
566
567
  } => {
567
568
  const {
568
569
  r,
@@ -587,12 +588,14 @@ export const getShapeLocalBoundsByStyle = (
587
588
  min: [-radius, -radius, 0],
588
589
  max: [radius, radius, 0],
589
590
  center: [0, 0, 0],
591
+ halfExtents: [radius, radius, 0],
590
592
  };
591
593
  case 'sphere':
592
594
  return {
593
595
  min: [-radius, -radius, -radius],
594
596
  max: [radius, radius, radius],
595
597
  center: [0, 0, 0],
598
+ halfExtents: [radius, radius, radius],
596
599
  };
597
600
  case 'image':
598
601
  case 'rect':
@@ -602,18 +605,21 @@ export const getShapeLocalBoundsByStyle = (
602
605
  min: [-width / 2, -height / 2, -depth / 2],
603
606
  max: [width / 2, height / 2, depth / 2],
604
607
  center: [0, 0, 0],
608
+ halfExtents: [width / 2, height / 2, depth / 2],
605
609
  };
606
610
  case 'ellipse':
607
611
  return {
608
612
  min: [-radiusX, -radiusY, 0],
609
613
  max: [radiusX, radiusY, 0],
610
614
  center: [0, 0, 0],
615
+ halfExtents: [radiusX, radiusY, 0],
611
616
  };
612
617
  case 'line':
613
618
  return {
614
619
  min: [x1, y1, z1],
615
620
  max: [x2, y2, z2],
616
621
  center: [(x1 + x2) / 2, (y1 + y2) / 2, (z1 + z2) / 2],
622
+ halfExtents: [(x2 - x1) / 2, (y2 - y1) / 2, (z2 - z1) / 2],
617
623
  };
618
624
  case 'text':
619
625
  case 'polyline':
@@ -638,12 +644,14 @@ export const combineBounds = (
638
644
  min: number[];
639
645
  max: number[];
640
646
  center: number[];
647
+ halfExtents: number[];
641
648
  } => {
642
649
  const res = {
643
650
  center: [0, 0, 0],
644
651
  min: [Infinity, Infinity, Infinity],
645
652
  max: [-Infinity, -Infinity, -Infinity],
646
653
  size: [0, 0, 0],
654
+ halfExtents: [0, 0, 0],
647
655
  };
648
656
  bounds.forEach((bound) => {
649
657
  const { min, max } = bound;
@@ -655,6 +663,9 @@ export const combineBounds = (
655
663
  });
656
664
  });
657
665
  res.center = res.center.map((val, i) => (res.max[i] + res.min[i]) / 2);
666
+ res.halfExtents = res.halfExtents.map(
667
+ (val, i) => (res.max[i] - res.min[i]) / 2,
668
+ );
658
669
  return res;
659
670
  };
660
671