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

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,45 +299,41 @@ 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
  };
331
-
332
- return this.upsertShape(
327
+ const bgShape = this.upsertShape(
333
328
  'rect',
334
329
  'labelBackgroundShape',
335
330
  bgStyle,
336
331
  shapeMap,
337
332
  model,
338
333
  );
334
+ this.balanceShapeSize(shapeMap, this.zoomCache.zoom);
335
+
336
+ return bgShape;
339
337
  }
340
338
 
341
339
  public drawIconShape(
@@ -465,20 +463,26 @@ export abstract class BaseNode {
465
463
  keyShapeStyle,
466
464
  this.boundsCache.keyShapeLocal,
467
465
  );
468
- const keyShapeWidth = keyShapeBBox.max[0] - keyShapeBBox.min[0];
466
+ const keyShapeSize = Math.min(
467
+ keyShapeBBox.max[0] - keyShapeBBox.min[0],
468
+ keyShapeBBox.max[1] - keyShapeBBox.min[1],
469
+ );
469
470
  const shapes = {};
470
- individualConfigs.forEach((config) => {
471
+ individualConfigs.forEach((config, i) => {
471
472
  const { position, ...individualStyle } = config;
472
473
  const id = `${position}BadgeShape`;
473
474
  const style = {
474
475
  ...commonStyle,
475
476
  ...individualStyle,
476
477
  };
478
+ const colorFromPalette = commonStyle.palette
479
+ ? commonStyle.palette[i % commonStyle.palette.length]
480
+ : '#7E92B5';
477
481
  const {
478
482
  text = '',
479
483
  type,
480
- color,
481
- size = keyShapeWidth / 3,
484
+ color = colorFromPalette,
485
+ size = keyShapeSize / 3,
482
486
  textColor,
483
487
  zIndex = 2,
484
488
  offsetX = 0,
@@ -491,55 +495,67 @@ export abstract class BaseNode {
491
495
  const pos = { x: 0, y: 0 };
492
496
  switch (position) {
493
497
  case 'rightTop':
494
- pos.x = keyShapeBBox.max[0] - bgHeight / 2 + offsetX;
495
- pos.y = keyShapeBBox.min[1] + size / 4 + offsetY;
498
+ case 'topRight':
499
+ pos.x = keyShapeBBox.max[0] / 2 + offsetX;
500
+ pos.y = keyShapeBBox.min[1] * 0.44 - bgHeight + offsetY;
496
501
  break;
497
502
  case 'right':
498
- pos.x = keyShapeBBox.max[0] - bgHeight / 2 + offsetX;
499
- pos.y = offsetY;
503
+ pos.x = keyShapeBBox.max[0] / 2 + offsetX;
504
+ pos.y = -bgHeight / 2 + offsetY;
500
505
  break;
501
506
  case 'rightBottom':
502
507
  case 'bottomRight':
503
- pos.x = keyShapeBBox.max[0] - bgHeight / 2 + offsetX;
504
- pos.y = keyShapeBBox.max[1] - size / 4 + offsetY;
508
+ pos.x = keyShapeBBox.max[0] / 2 + offsetX;
509
+ pos.y = keyShapeBBox.max[1] * 0.44 + offsetY;
505
510
  break;
506
511
  case 'leftTop':
507
512
  case 'topLeft':
508
- pos.x = keyShapeBBox.min[0] + bgHeight / 2 + offsetX;
509
- pos.y = keyShapeBBox.min[1] + size / 4 + offsetY;
513
+ pos.x = keyShapeBBox.min[0] / 2 + offsetX;
514
+ pos.y = keyShapeBBox.min[1] * 0.44 - bgHeight + offsetY;
510
515
  break;
511
516
  case 'left':
512
- pos.x = keyShapeBBox.min[0] + bgHeight / 2 + offsetX;
513
- pos.y = offsetY;
517
+ pos.x = keyShapeBBox.min[0] / 2 + offsetX;
518
+ pos.y = -bgHeight / 2 + offsetY;
514
519
  break;
515
520
  case 'leftBottom':
516
521
  case 'bottomLeft':
517
- pos.x = keyShapeBBox.min[0] + bgHeight / 2 + offsetX;
518
- pos.y = keyShapeBBox.max[1] - size / 4 + offsetY;
522
+ pos.x = keyShapeBBox.min[0] / 2 + offsetX;
523
+ pos.y = keyShapeBBox.max[1] * 0.44 + offsetY;
519
524
  break;
520
525
  case 'top':
521
526
  pos.x = offsetX;
522
- pos.y = keyShapeBBox.min[1] + size / 4;
527
+ pos.y = keyShapeBBox.min[1] * 0.44 - bgHeight + offsetY;
523
528
  break;
524
529
  default:
525
530
  // bottom
526
531
  pos.x = offsetX;
527
- pos.y = keyShapeBBox.max[1] - size / 4;
532
+ pos.y = keyShapeBBox.max[1] * 0.44 + offsetY;
528
533
  break;
529
534
  }
530
535
 
536
+ const positionLowerCase = position.toLowerCase();
537
+ const isLeft = positionLowerCase.includes('left');
538
+ const isCenter = !isLeft && !positionLowerCase.includes('right');
539
+
531
540
  // a radius rect (as container) + a text / icon
541
+ const fontSize = bgHeight - 3;
542
+ let textAlign = isLeft ? 'right' : 'left';
543
+ let textX = isLeft ? pos.x - 3 : pos.x + 2;
544
+ if (isCenter) {
545
+ textAlign = 'center';
546
+ textX = pos.x;
547
+ }
532
548
  shapes[id] = this.upsertShape(
533
549
  'text',
534
550
  id,
535
551
  {
536
552
  text,
537
553
  fill: textColor,
538
- fontSize: bgHeight - 3,
539
- x: pos.x,
540
- y: pos.y,
554
+ fontSize,
555
+ x: textX,
556
+ y: pos.y + bgHeight / 2,
541
557
  ...otherStyles,
542
- textAlign: position.includes('right') ? 'left' : 'right',
558
+ textAlign,
543
559
  textBaseline: 'middle',
544
560
  zIndex: (zIndex as number) + 1,
545
561
  } as GShapeStyle,
@@ -554,6 +570,8 @@ export abstract class BaseNode {
554
570
  (text as string).length <= 1
555
571
  ? bgHeight
556
572
  : Math.max(bgHeight, bbox.max[0] - bbox.min[0]) + 8;
573
+ let bgX = isLeft ? pos.x - bgWidth : pos.x;
574
+ if (isCenter) bgX = pos.x - bgWidth / 2;
557
575
  shapes[bgShapeId] = this.upsertShape(
558
576
  'rect',
559
577
  bgShapeId,
@@ -562,10 +580,10 @@ export abstract class BaseNode {
562
580
  fill: color,
563
581
  height: bgHeight,
564
582
  width: bgWidth,
565
- x: bbox.min[0] - 3, // begin at the border, minus half height
566
- y: bbox.min[1],
567
583
  radius: bgHeight / 2,
568
584
  zIndex,
585
+ x: bgX,
586
+ y: pos.y,
569
587
  ...otherStyles,
570
588
  } as GShapeStyle,
571
589
  shapeMap,
@@ -662,8 +680,39 @@ export abstract class BaseNode {
662
680
  const { position = 'bottom' } = labelStyle;
663
681
  if (!labelShape || !labelShape.isVisible()) return;
664
682
 
665
- if (position === 'bottom') labelShape.style.transformOrigin = '0';
666
- else labelShape.style.transformOrigin = '';
683
+ const keyShapeLocal = this.boundsCache.keyShapeLocal;
684
+ if (zoom < 1) {
685
+ // if it is zoom-out, do not scale the gap between keyShape and labelShape, differentiate from zoom-in by adjusting transformOrigin
686
+ if (position === 'bottom') labelShape.style.transformOrigin = '0';
687
+ else labelShape.style.transformOrigin = '';
688
+ } else {
689
+ switch (position) {
690
+ case 'bottom':
691
+ labelShape.style.transformOrigin = `0 ${
692
+ keyShapeLocal.max[1] - labelShape.attributes.y
693
+ }`;
694
+ break;
695
+ case 'right':
696
+ labelShape.style.transformOrigin = `${
697
+ keyShapeLocal.max[0] - labelShape.attributes.x
698
+ } 0`;
699
+ break;
700
+ case 'top':
701
+ labelShape.style.transformOrigin = `0 ${
702
+ keyShapeLocal.min[1] - labelShape.attributes.y
703
+ }`;
704
+ break;
705
+ case 'left':
706
+ labelShape.style.transformOrigin = `${
707
+ keyShapeLocal.min[0] - labelShape.attributes.x
708
+ } 0`;
709
+ break;
710
+ default:
711
+ // center
712
+ labelShape.style.transformOrigin = ``;
713
+ break;
714
+ }
715
+ }
667
716
  labelShape.style.transform = `scale(${balanceRatio}, ${balanceRatio})`;
668
717
  const wordWrapWidth = this.zoomCache.wordWrapWidth * zoom;
669
718
  labelShape.style.wordWrapWidth = wordWrapWidth;
@@ -671,40 +720,47 @@ export abstract class BaseNode {
671
720
  if (!labelBackgroundShape || !labelBackgroundShape.isVisible()) return;
672
721
 
673
722
  const { padding } = this.mergedStyles.labelBackgroundShape;
674
- const { width, height } = labelBackgroundShape.attributes;
723
+ const { width, height, x, y } = labelBackgroundShape.attributes;
675
724
  const [paddingTop, paddingRight, paddingBottom, paddingLeft] =
676
725
  padding as number[];
677
726
 
678
727
  switch (position) {
679
728
  case 'top':
729
+ // if it is zoom-out, do not scale the gap between keyShape and labelShape, differentiate from zoom-in by adjusting transformOrigin
680
730
  labelBackgroundShape.style.transformOrigin = `${
681
731
  paddingLeft + (width - paddingLeft - paddingRight) / 2
682
- } ${height - paddingBottom}`;
732
+ } ${zoom < 1 ? height - paddingBottom : keyShapeLocal.min[1] - y}`;
683
733
  break;
684
734
  case 'left':
685
- labelBackgroundShape.style.transformOrigin = `${width - paddingRight} ${
686
- paddingTop + (height - paddingTop - paddingBottom) / 2
687
- }`;
735
+ labelBackgroundShape.style.transformOrigin = `${
736
+ zoom < 1 ? width - paddingRight : keyShapeLocal.min[0] - x
737
+ } ${paddingTop + (height - paddingTop - paddingBottom) / 2}`;
688
738
  break;
689
739
  case 'right':
690
- labelBackgroundShape.style.transformOrigin = `${paddingLeft} ${
691
- paddingTop + (height - paddingTop - paddingBottom) / 2
692
- }`;
740
+ labelBackgroundShape.style.transformOrigin = `${
741
+ zoom < 1 ? paddingLeft : keyShapeLocal.max[0] - x
742
+ } ${paddingTop + (height - paddingTop - paddingBottom) / 2}`;
693
743
  break;
694
744
  case 'bottom':
745
+ labelBackgroundShape.style.transformOrigin = `${
746
+ paddingLeft + (width - paddingLeft - paddingRight) / 2
747
+ } ${
748
+ zoom < 1
749
+ ? paddingTop + (height - paddingTop - paddingBottom) / 2
750
+ : keyShapeLocal.max[1] - y
751
+ }`;
752
+ break;
695
753
  default:
754
+ // center
696
755
  labelBackgroundShape.style.transformOrigin = `${
697
756
  paddingLeft + (width - paddingLeft - paddingRight) / 2
698
757
  } ${paddingTop + (height - paddingTop - paddingBottom) / 2}`;
699
758
  }
700
759
 
701
- const labelBBox = labelShape.getGeometryBounds();
702
- const labelWidth = Math.min(
703
- labelBBox.max[0] - labelBBox.min[0],
704
- this.zoomCache.wordWrapWidth,
705
- );
760
+ const { labelShapeGeometry: labelBBox } = this.boundsCache;
761
+ const labelWidth = labelBBox.max[0] - labelBBox.min[0];
706
762
  const xAxistRatio =
707
- ((labelWidth + paddingLeft + paddingRight) * balanceRatio) / width;
763
+ (labelWidth * balanceRatio + paddingLeft + paddingRight) / width;
708
764
 
709
765
  labelBackgroundShape.style.transform = `scale(${xAxistRatio}, ${balanceRatio})`;
710
766
  }
@@ -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 & {