@antv/g6 5.0.0-alpha.5 → 5.0.0-alpha.6

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.
@@ -35,7 +35,7 @@ export abstract class BaseNode {
35
35
  lodStrategy?: lodStrategyObj;
36
36
  boundsCache: {
37
37
  keyShapeLocal?: AABB;
38
- labelShapeLocal?: AABB;
38
+ labelShapeGeometry?: AABB;
39
39
  };
40
40
  // cache the zoom level infomations
41
41
  protected zoomCache: {
@@ -140,10 +140,11 @@ export abstract class BaseNode {
140
140
  .forEach((id) => {
141
141
  const shape = shapeMap[id];
142
142
  if (shape?.getAttribute(LOCAL_BOUNDS_DIRTY_FLAG_KEY)) {
143
- this.boundsCache[`${id}Local`] =
144
- id === 'labelShape'
145
- ? shape.getGeometryBounds()
146
- : shape.getLocalBounds();
143
+ if (id === 'labelShape') {
144
+ this.boundsCache[`${id}Geometry`] = shape.getGeometryBounds();
145
+ } else {
146
+ this.boundsCache[`${id}Local`] = shape.getLocalBounds();
147
+ }
147
148
  shape.setAttribute(LOCAL_BOUNDS_DIRTY_FLAG_KEY, false);
148
149
  }
149
150
  });
@@ -243,28 +244,29 @@ export abstract class BaseNode {
243
244
  switch (position) {
244
245
  case 'center':
245
246
  positionPreset.y = keyShapeBox.center[1];
247
+ positionPreset.textBaseline = 'middle';
246
248
  break;
247
249
  case 'top':
248
250
  positionPreset.y = keyShapeBox.min[1];
249
251
  positionPreset.textBaseline = 'bottom';
250
- positionPreset.offsetY = -4;
252
+ positionPreset.offsetY = -2;
251
253
  break;
252
254
  case 'left':
253
255
  positionPreset.x = keyShapeBox.min[0];
254
256
  positionPreset.y = keyShapeBox.center[1];
255
257
  positionPreset.textAlign = 'right';
256
258
  positionPreset.textBaseline = 'middle';
257
- positionPreset.offsetX = -8;
259
+ positionPreset.offsetX = -4;
258
260
  break;
259
261
  case 'right':
260
262
  positionPreset.x = keyShapeBox.max[0];
261
263
  positionPreset.y = keyShapeBox.center[1];
262
264
  positionPreset.textAlign = 'left';
263
265
  positionPreset.textBaseline = 'middle';
264
- positionPreset.offsetX = 8;
266
+ positionPreset.offsetX = 4;
265
267
  break;
266
268
  default: // at bottom by default
267
- positionPreset.offsetY = 4;
269
+ positionPreset.offsetY = 2;
268
270
  break;
269
271
  }
270
272
  const offsetX = (
@@ -297,34 +299,28 @@ export abstract class BaseNode {
297
299
  const { labelShape } = shapeMap;
298
300
  if (!labelShape || !model.data.labelShape) return;
299
301
  if (
300
- !this.boundsCache.labelShapeLocal ||
302
+ !this.boundsCache.labelShapeGeometry ||
301
303
  labelShape.getAttribute(LOCAL_BOUNDS_DIRTY_FLAG_KEY)
302
304
  ) {
303
- this.boundsCache.labelShapeLocal = labelShape.getGeometryBounds();
305
+ this.boundsCache.labelShapeGeometry = labelShape.getGeometryBounds();
304
306
  labelShape.setAttribute(LOCAL_BOUNDS_DIRTY_FLAG_KEY, false);
305
307
  }
306
308
  // label's local bounds, will take scale into acount
307
- const { labelShapeLocal: textBBox } = this.boundsCache;
308
- const labelWidth = Math.min(
309
- textBBox.max[0] - textBBox.min[0],
309
+ const { labelShapeGeometry: textBBoxGeo } = this.boundsCache;
310
+ const height = textBBoxGeo.max[1] - textBBoxGeo.min[1];
311
+ const width = Math.min(
312
+ textBBoxGeo.max[0] - textBBoxGeo.min[0] + 2,
310
313
  labelShape.attributes.wordWrapWidth,
311
314
  );
312
- const height = textBBox.max[1] - textBBox.min[1];
313
- const labelAspectRatio = labelWidth / (textBBox.max[1] - textBBox.min[1]);
314
- const width = labelAspectRatio * height;
315
315
 
316
316
  const { padding, ...backgroundStyle } =
317
317
  this.mergedStyles.labelBackgroundShape;
318
- const y =
319
- labelShape.attributes.y -
320
- (labelShape.attributes.textBaseline === 'top'
321
- ? padding[0]
322
- : height / 2 + padding[0]);
318
+
323
319
  const bgStyle: any = {
324
320
  fill: '#fff',
325
321
  ...backgroundStyle,
326
- x: textBBox.center[0] - width / 2 - padding[3],
327
- y,
322
+ x: labelShape.attributes.x + textBBoxGeo.min[0] - padding[3],
323
+ y: labelShape.attributes.y + textBBoxGeo.min[1] - padding[0],
328
324
  width: width + padding[1] + padding[3],
329
325
  height: height + padding[0] + padding[2],
330
326
  };
@@ -465,20 +461,26 @@ export abstract class BaseNode {
465
461
  keyShapeStyle,
466
462
  this.boundsCache.keyShapeLocal,
467
463
  );
468
- const keyShapeWidth = keyShapeBBox.max[0] - keyShapeBBox.min[0];
464
+ const keyShapeSize = Math.min(
465
+ keyShapeBBox.max[0] - keyShapeBBox.min[0],
466
+ keyShapeBBox.max[1] - keyShapeBBox.min[1],
467
+ );
469
468
  const shapes = {};
470
- individualConfigs.forEach((config) => {
469
+ individualConfigs.forEach((config, i) => {
471
470
  const { position, ...individualStyle } = config;
472
471
  const id = `${position}BadgeShape`;
473
472
  const style = {
474
473
  ...commonStyle,
475
474
  ...individualStyle,
476
475
  };
476
+ const colorFromPalette = commonStyle.palette
477
+ ? commonStyle.palette[i % commonStyle.palette.length]
478
+ : '#7E92B5';
477
479
  const {
478
480
  text = '',
479
481
  type,
480
- color,
481
- size = keyShapeWidth / 3,
482
+ color = colorFromPalette,
483
+ size = keyShapeSize / 3,
482
484
  textColor,
483
485
  zIndex = 2,
484
486
  offsetX = 0,
@@ -491,55 +493,67 @@ export abstract class BaseNode {
491
493
  const pos = { x: 0, y: 0 };
492
494
  switch (position) {
493
495
  case 'rightTop':
494
- pos.x = keyShapeBBox.max[0] - bgHeight / 2 + offsetX;
495
- pos.y = keyShapeBBox.min[1] + size / 4 + offsetY;
496
+ case 'topRight':
497
+ pos.x = keyShapeBBox.max[0] / 2 + offsetX;
498
+ pos.y = keyShapeBBox.min[1] * 0.44 - bgHeight + offsetY;
496
499
  break;
497
500
  case 'right':
498
- pos.x = keyShapeBBox.max[0] - bgHeight / 2 + offsetX;
499
- pos.y = offsetY;
501
+ pos.x = keyShapeBBox.max[0] / 2 + offsetX;
502
+ pos.y = -bgHeight / 2 + offsetY;
500
503
  break;
501
504
  case 'rightBottom':
502
505
  case 'bottomRight':
503
- pos.x = keyShapeBBox.max[0] - bgHeight / 2 + offsetX;
504
- pos.y = keyShapeBBox.max[1] - size / 4 + offsetY;
506
+ pos.x = keyShapeBBox.max[0] / 2 + offsetX;
507
+ pos.y = keyShapeBBox.max[1] * 0.44 + offsetY;
505
508
  break;
506
509
  case 'leftTop':
507
510
  case 'topLeft':
508
- pos.x = keyShapeBBox.min[0] + bgHeight / 2 + offsetX;
509
- pos.y = keyShapeBBox.min[1] + size / 4 + offsetY;
511
+ pos.x = keyShapeBBox.min[0] / 2 + offsetX;
512
+ pos.y = keyShapeBBox.min[1] * 0.44 - bgHeight + offsetY;
510
513
  break;
511
514
  case 'left':
512
- pos.x = keyShapeBBox.min[0] + bgHeight / 2 + offsetX;
513
- pos.y = offsetY;
515
+ pos.x = keyShapeBBox.min[0] / 2 + offsetX;
516
+ pos.y = -bgHeight / 2 + offsetY;
514
517
  break;
515
518
  case 'leftBottom':
516
519
  case 'bottomLeft':
517
- pos.x = keyShapeBBox.min[0] + bgHeight / 2 + offsetX;
518
- pos.y = keyShapeBBox.max[1] - size / 4 + offsetY;
520
+ pos.x = keyShapeBBox.min[0] / 2 + offsetX;
521
+ pos.y = keyShapeBBox.max[1] * 0.44 + offsetY;
519
522
  break;
520
523
  case 'top':
521
524
  pos.x = offsetX;
522
- pos.y = keyShapeBBox.min[1] + size / 4;
525
+ pos.y = keyShapeBBox.min[1] * 0.44 - bgHeight + offsetY;
523
526
  break;
524
527
  default:
525
528
  // bottom
526
529
  pos.x = offsetX;
527
- pos.y = keyShapeBBox.max[1] - size / 4;
530
+ pos.y = keyShapeBBox.max[1] * 0.44 + offsetY;
528
531
  break;
529
532
  }
530
533
 
534
+ const positionLowerCase = position.toLowerCase();
535
+ const isLeft = positionLowerCase.includes('left');
536
+ const isCenter = !isLeft && !positionLowerCase.includes('right');
537
+
531
538
  // a radius rect (as container) + a text / icon
539
+ const fontSize = bgHeight - 3;
540
+ let textAlign = isLeft ? 'right' : 'left';
541
+ let textX = isLeft ? pos.x - 3 : pos.x + 2;
542
+ if (isCenter) {
543
+ textAlign = 'center';
544
+ textX = pos.x;
545
+ }
532
546
  shapes[id] = this.upsertShape(
533
547
  'text',
534
548
  id,
535
549
  {
536
550
  text,
537
551
  fill: textColor,
538
- fontSize: bgHeight - 3,
539
- x: pos.x,
540
- y: pos.y,
552
+ fontSize,
553
+ x: textX,
554
+ y: pos.y + bgHeight / 2,
541
555
  ...otherStyles,
542
- textAlign: position.includes('right') ? 'left' : 'right',
556
+ textAlign,
543
557
  textBaseline: 'middle',
544
558
  zIndex: (zIndex as number) + 1,
545
559
  } as GShapeStyle,
@@ -554,6 +568,8 @@ export abstract class BaseNode {
554
568
  (text as string).length <= 1
555
569
  ? bgHeight
556
570
  : Math.max(bgHeight, bbox.max[0] - bbox.min[0]) + 8;
571
+ let bgX = isLeft ? pos.x - bgWidth : pos.x;
572
+ if (isCenter) bgX = pos.x - bgWidth / 2;
557
573
  shapes[bgShapeId] = this.upsertShape(
558
574
  'rect',
559
575
  bgShapeId,
@@ -562,10 +578,10 @@ export abstract class BaseNode {
562
578
  fill: color,
563
579
  height: bgHeight,
564
580
  width: bgWidth,
565
- x: bbox.min[0] - 3, // begin at the border, minus half height
566
- y: bbox.min[1],
567
581
  radius: bgHeight / 2,
568
582
  zIndex,
583
+ x: bgX,
584
+ y: pos.y,
569
585
  ...otherStyles,
570
586
  } as GShapeStyle,
571
587
  shapeMap,
@@ -662,8 +678,39 @@ export abstract class BaseNode {
662
678
  const { position = 'bottom' } = labelStyle;
663
679
  if (!labelShape || !labelShape.isVisible()) return;
664
680
 
665
- if (position === 'bottom') labelShape.style.transformOrigin = '0';
666
- else labelShape.style.transformOrigin = '';
681
+ const keyShapeLocal = this.boundsCache.keyShapeLocal;
682
+ if (zoom < 1) {
683
+ // if it is zoom-out, do not scale the gap between keyShape and labelShape, differentiate from zoom-in by adjusting transformOrigin
684
+ if (position === 'bottom') labelShape.style.transformOrigin = '0';
685
+ else labelShape.style.transformOrigin = '';
686
+ } else {
687
+ switch (position) {
688
+ case 'bottom':
689
+ labelShape.style.transformOrigin = `0 ${
690
+ keyShapeLocal.max[1] - labelShape.attributes.y
691
+ }`;
692
+ break;
693
+ case 'right':
694
+ labelShape.style.transformOrigin = `${
695
+ keyShapeLocal.max[0] - labelShape.attributes.x
696
+ } 0`;
697
+ break;
698
+ case 'top':
699
+ labelShape.style.transformOrigin = `0 ${
700
+ keyShapeLocal.min[1] - labelShape.attributes.y
701
+ }`;
702
+ break;
703
+ case 'left':
704
+ labelShape.style.transformOrigin = `${
705
+ keyShapeLocal.min[0] - labelShape.attributes.x
706
+ } 0`;
707
+ break;
708
+ default:
709
+ // center
710
+ labelShape.style.transformOrigin = ``;
711
+ break;
712
+ }
713
+ }
667
714
  labelShape.style.transform = `scale(${balanceRatio}, ${balanceRatio})`;
668
715
  const wordWrapWidth = this.zoomCache.wordWrapWidth * zoom;
669
716
  labelShape.style.wordWrapWidth = wordWrapWidth;
@@ -671,40 +718,47 @@ export abstract class BaseNode {
671
718
  if (!labelBackgroundShape || !labelBackgroundShape.isVisible()) return;
672
719
 
673
720
  const { padding } = this.mergedStyles.labelBackgroundShape;
674
- const { width, height } = labelBackgroundShape.attributes;
721
+ const { width, height, x, y } = labelBackgroundShape.attributes;
675
722
  const [paddingTop, paddingRight, paddingBottom, paddingLeft] =
676
723
  padding as number[];
677
724
 
678
725
  switch (position) {
679
726
  case 'top':
727
+ // if it is zoom-out, do not scale the gap between keyShape and labelShape, differentiate from zoom-in by adjusting transformOrigin
680
728
  labelBackgroundShape.style.transformOrigin = `${
681
729
  paddingLeft + (width - paddingLeft - paddingRight) / 2
682
- } ${height - paddingBottom}`;
730
+ } ${zoom < 1 ? height - paddingBottom : keyShapeLocal.min[1] - y}`;
683
731
  break;
684
732
  case 'left':
685
- labelBackgroundShape.style.transformOrigin = `${width - paddingRight} ${
686
- paddingTop + (height - paddingTop - paddingBottom) / 2
687
- }`;
733
+ labelBackgroundShape.style.transformOrigin = `${
734
+ zoom < 1 ? width - paddingRight : keyShapeLocal.min[0] - x
735
+ } ${paddingTop + (height - paddingTop - paddingBottom) / 2}`;
688
736
  break;
689
737
  case 'right':
690
- labelBackgroundShape.style.transformOrigin = `${paddingLeft} ${
691
- paddingTop + (height - paddingTop - paddingBottom) / 2
692
- }`;
738
+ labelBackgroundShape.style.transformOrigin = `${
739
+ zoom < 1 ? paddingLeft : keyShapeLocal.max[0] - x
740
+ } ${paddingTop + (height - paddingTop - paddingBottom) / 2}`;
693
741
  break;
694
742
  case 'bottom':
743
+ labelBackgroundShape.style.transformOrigin = `${
744
+ paddingLeft + (width - paddingLeft - paddingRight) / 2
745
+ } ${
746
+ zoom < 1
747
+ ? paddingTop + (height - paddingTop - paddingBottom) / 2
748
+ : keyShapeLocal.max[1] - y
749
+ }`;
750
+ break;
695
751
  default:
752
+ // center
696
753
  labelBackgroundShape.style.transformOrigin = `${
697
754
  paddingLeft + (width - paddingLeft - paddingRight) / 2
698
755
  } ${paddingTop + (height - paddingTop - paddingBottom) / 2}`;
699
756
  }
700
757
 
701
- const labelBBox = labelShape.getGeometryBounds();
702
- const labelWidth = Math.min(
703
- labelBBox.max[0] - labelBBox.min[0],
704
- this.zoomCache.wordWrapWidth,
705
- );
758
+ const labelBBox = labelShape.getLocalBounds();
759
+ const labelWidth = labelBBox.max[0] - labelBBox.min[0];
706
760
  const xAxistRatio =
707
- ((labelWidth + paddingLeft + paddingRight) * balanceRatio) / width;
761
+ (labelWidth + (paddingLeft + paddingRight) * balanceRatio) / width;
708
762
 
709
763
  labelBackgroundShape.style.transform = `scale(${xAxistRatio}, ${balanceRatio})`;
710
764
  }
@@ -16,16 +16,16 @@ const nodeStroke = '#D0E4FF';
16
16
  export default {
17
17
  node: {
18
18
  palette: [
19
- '#227EFF',
20
- '#AD5CFF',
21
- '#00B8B8',
22
- '#FA822D',
23
- '#F252AF',
24
- '#1EB8F5',
25
- '#108A44',
26
- '#F4B106',
27
- '#5241A8',
28
- '#95CF21',
19
+ '#4089FF',
20
+ '#CD75FF',
21
+ '#06B8A8',
22
+ '#FF8834',
23
+ '#5888C3',
24
+ '#FA73CD',
25
+ '#07BCE0',
26
+ '#CB962A',
27
+ '#23AD61',
28
+ '#FF8075',
29
29
  ],
30
30
  lodStrategy: {
31
31
  levels: [
@@ -66,7 +66,7 @@ export default {
66
66
  lineWidth: 0,
67
67
  fill: '#000',
68
68
  opacity: 0.75,
69
- zIndex: -1,
69
+ zIndex: 1,
70
70
  lod: 0,
71
71
  },
72
72
  iconShape: {
@@ -85,7 +85,7 @@ export default {
85
85
  lod: 0,
86
86
  },
87
87
  badgeShapes: {
88
- color: 'rgb(140, 140, 140)',
88
+ palette: ['#7E92B5', '#F86254', '#EDB74B'],
89
89
  textColor: '#fff',
90
90
  zIndex: 3,
91
91
  lod: -1,
@@ -160,16 +160,17 @@ export default {
160
160
  },
161
161
  edge: {
162
162
  palette: [
163
- '#63A4FF',
164
- '#CD9CFF',
165
- '#2DEFEF',
166
- '#FFBDA1',
167
- '#F49FD0',
168
- '#80DBFF',
169
- '#41CB7C',
170
- '#FFD362',
171
- '#A192E8',
172
- '#CEFB75',
163
+ '#637088',
164
+ '#2A59A6',
165
+ '#854CA6',
166
+ '#04786D',
167
+ '#A65822',
168
+ '#39587F',
169
+ '#A34B85',
170
+ '#057A92',
171
+ '#84621B',
172
+ '#17703F',
173
+ '#A6534C',
173
174
  ],
174
175
  lodStrategy: {
175
176
  levels: [
@@ -191,6 +192,7 @@ export default {
191
192
  lineWidth: 1,
192
193
  stroke: edgeMainStroke,
193
194
  increasedLineWidthForHitTesting: 2,
195
+ zIndex: 1,
194
196
  },
195
197
  labelShape: {
196
198
  ...DEFAULT_TEXT_STYLE,
@@ -198,7 +200,7 @@ export default {
198
200
  opacity: 0.85,
199
201
  position: 'middle',
200
202
  textBaseline: 'middle',
201
- zIndex: 2,
203
+ zIndex: 3,
202
204
  textOverflow: 'ellipsis',
203
205
  wordWrap: true,
204
206
  maxLines: 1,
@@ -210,7 +212,7 @@ export default {
210
212
  lineWidth: 0,
211
213
  fill: '#000',
212
214
  opacity: 0.75,
213
- zIndex: 1,
215
+ zIndex: 2,
214
216
  lod: 0,
215
217
  },
216
218
  iconShape: {
@@ -18,16 +18,16 @@ const nodeStroke = 'rgba(0,0,0,0.85)';
18
18
  export default {
19
19
  node: {
20
20
  palette: [
21
- '#227EFF',
22
- '#AD5CFF',
23
- '#00B8B8',
24
- '#FA822D',
25
- '#F252AF',
26
- '#1EB8F5',
27
- '#108A44',
28
- '#F4B106',
29
- '#5241A8',
30
- '#95CF21',
21
+ '#4089FF',
22
+ '#CD75FF',
23
+ '#06B8A8',
24
+ '#FF8834',
25
+ '#5888C3',
26
+ '#FA73CD',
27
+ '#07BCE0',
28
+ '#CB962A',
29
+ '#23AD61',
30
+ '#FF8075',
31
31
  ],
32
32
  lodStrategy: {
33
33
  levels: [
@@ -67,7 +67,7 @@ export default {
67
67
  lineWidth: 0,
68
68
  fill: '#fff',
69
69
  opacity: 0.75,
70
- zIndex: -1,
70
+ zIndex: 1,
71
71
  lod: 0,
72
72
  },
73
73
  iconShape: {
@@ -85,7 +85,7 @@ export default {
85
85
  lod: 0,
86
86
  },
87
87
  badgeShapes: {
88
- color: 'rgb(140, 140, 140)',
88
+ palette: ['#7E92B5', '#F86254', '#EDB74B'],
89
89
  textColor: '#fff',
90
90
  zIndex: 3,
91
91
  lod: -1,
@@ -160,16 +160,17 @@ export default {
160
160
  },
161
161
  edge: {
162
162
  palette: [
163
- '#63A4FF',
164
- '#CD9CFF',
165
- '#2DEFEF',
166
- '#FFBDA1',
167
- '#F49FD0',
168
- '#80DBFF',
169
- '#41CB7C',
170
- '#FFD362',
171
- '#A192E8',
172
- '#CEFB75',
163
+ '#99ADD1',
164
+ '#4089FF',
165
+ '#CD75FF',
166
+ '#06B8A8',
167
+ '#FF8834',
168
+ '#5888C3',
169
+ '#FA73CD',
170
+ '#07BCE0',
171
+ '#CB962A',
172
+ '#23AD61',
173
+ '#FF8075',
173
174
  ],
174
175
  lodStrategy: {
175
176
  levels: [
@@ -191,13 +192,14 @@ export default {
191
192
  lineWidth: 1,
192
193
  stroke: edgeMainStroke,
193
194
  increasedLineWidthForHitTesting: 2,
195
+ zIndex: 1,
194
196
  },
195
197
  labelShape: {
196
198
  ...DEFAULT_TEXT_STYLE,
197
199
  fill: textColor,
198
200
  position: 'middle',
199
201
  textBaseline: 'middle',
200
- zIndex: 2,
202
+ zIndex: 3,
201
203
  textOverflow: 'ellipsis',
202
204
  wordWrap: true,
203
205
  maxLines: 1,
@@ -209,7 +211,7 @@ export default {
209
211
  lineWidth: 0,
210
212
  fill: '#fff',
211
213
  opacity: 0.75,
212
- zIndex: 1,
214
+ zIndex: 2,
213
215
  lod: 0,
214
216
  },
215
217
  iconShape: {
package/src/types/node.ts CHANGED
@@ -89,6 +89,7 @@ export interface NodeShapeStyles extends ItemShapeStyles {
89
89
  badgeShapes?: ShapeStyle & {
90
90
  // common badge styles
91
91
  color?: string;
92
+ palette?: string[];
92
93
  textColor?: string;
93
94
  // individual styles and their position
94
95
  [key: number]: ShapeStyle & {