@antv/component 0.8.20 → 0.8.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.
@@ -1,7 +1,14 @@
1
- import { IGroup } from '@antv/g-base';
1
+ import { IGroup, IShape } from '@antv/g-base';
2
2
  import { clamp, deepMix, each, filter, get, mix, isNumber, isFunction } from '@antv/util';
3
3
  import { IList } from '../interfaces';
4
- import { CategoryLegendCfg, LegendPageNavigatorCfg, LegendItemNameCfg, LegendMarkerCfg, ListItem } from '../types';
4
+ import {
5
+ CategoryLegendCfg,
6
+ LegendPageNavigatorCfg,
7
+ LegendItemNameCfg,
8
+ LegendMarkerCfg,
9
+ ListItem,
10
+ LegendRadio,
11
+ } from '../types';
5
12
  import { ellipsisLabel } from '../util/label';
6
13
  import { getMatrixByAngle, getMatrixByTranslate } from '../util/matrix';
7
14
  import { getStatesStyle } from '../util/state';
@@ -36,10 +43,19 @@ const textStyle = {
36
43
  textAlign: 'start',
37
44
  textBaseline: 'middle',
38
45
  fontFamily: Theme.fontFamily,
39
- fontWeight: "normal",
46
+ fontWeight: 'normal',
40
47
  lineHeight: 12,
41
48
  };
42
49
 
50
+ const RIGHT_ARROW_NAME = 'navigation-arrow-right';
51
+ const LEFT_ARROW_NAME = 'navigation-arrow-left';
52
+
53
+ const ROTATE_MAP = {
54
+ right: (90 * Math.PI) / 180,
55
+ left: ((360 - 90) * Math.PI) / 180,
56
+ up: 0,
57
+ down: (180 * Math.PI) / 180,
58
+ };
43
59
  class Category extends LegendBase<CategoryLegendCfg> implements IList {
44
60
  private currentPageIndex = 1;
45
61
  private totalPagesCnt = 1;
@@ -64,6 +80,7 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
64
80
  maxWidth: null,
65
81
  maxHeight: null,
66
82
  marker: {},
83
+ radio: null,
67
84
  items: [],
68
85
  itemStates: {},
69
86
  itemBackground: {},
@@ -92,7 +109,7 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
92
109
  },
93
110
  pageNavigator: DEFAULT_PAGE_NAVIGATOR,
94
111
  itemName: {
95
- spacing: 16, // 如果右边有 value 使用这个间距
112
+ spacing: 16, // 如果右边有 value 或者 RadioIcon 使用这个间距
96
113
  style: textStyle,
97
114
  },
98
115
  marker: {
@@ -106,6 +123,7 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
106
123
  alignRight: false, // 只有itemWidth 不为 null 时此属性有效
107
124
  formatter: null,
108
125
  style: textStyle,
126
+ spacing: 6, // 如果右边有 RadioIcon 使用在这个间距
109
127
  },
110
128
  itemStates: {
111
129
  active: {
@@ -334,7 +352,7 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
334
352
  if (itemHeight < fontSize) {
335
353
  itemHeight = fontSize;
336
354
  }
337
- })
355
+ });
338
356
  } else if (style) {
339
357
  itemHeight = style.fontSize;
340
358
  }
@@ -390,6 +408,7 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
390
408
  ...textStyle,
391
409
  ...(isFunction(style) ? style(item, index, this.getItems()) : style),
392
410
  };
411
+
393
412
  return this.addShape(container, {
394
413
  type: 'text',
395
414
  id: this.getElementId(`item-${item.id}-${textName}`),
@@ -398,6 +417,47 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
398
417
  });
399
418
  }
400
419
 
420
+ private drawRadio(container: IGroup, radioCfg: LegendRadio, item: ListItem, itemHeight: number, x: number) {
421
+ const style = radioCfg.style || {};
422
+ // 以用户设置的 r 为主
423
+ const r = style.r ?? itemHeight / 2;
424
+ const lineWidth = (r * 3.6) / 8;
425
+ const [x0, y0] = [x + r, itemHeight / 2 - r];
426
+ const [x1, y1] = [x0 + r, y0 + r];
427
+ const [x2, y2] = [x0, y1 + r];
428
+ const [x3, y3] = [x, y0 + r];
429
+ const { showRadio } = item;
430
+ const attrs = {
431
+ path: [
432
+ ['M', x0, y0],
433
+ ['A', r, r, 0, 0, 1, x1, y1],
434
+ ['L', x1 - lineWidth, y1],
435
+ ['L', x1, y1],
436
+ ['A', r, r, 0, 0, 1, x2, y2],
437
+ ['L', x2, y2 - lineWidth],
438
+ ['L', x2, y2],
439
+ ['A', r, r, 0, 0, 1, x3, y3],
440
+ ['L', x3 + lineWidth, y3],
441
+ ['L', x3, y3],
442
+ ['A', r, r, 0, 0, 1, x0, y0],
443
+ ['L', x0, y0 + lineWidth],
444
+ ],
445
+ stroke: '#000000',
446
+ fill: '#ffffff',
447
+ ...style,
448
+ opacity: showRadio ? 0.45 : 0,
449
+ };
450
+
451
+ const radioShape = this.addShape(container, {
452
+ type: 'path',
453
+ id: this.getElementId(`item-${item.id}-radio`),
454
+ name: 'legend-item-radio',
455
+ attrs,
456
+ });
457
+ radioShape.set('tip', radioCfg.tip);
458
+ return radioShape;
459
+ }
460
+
401
461
  // 绘制图例项
402
462
  private drawItem(item: ListItem, index: number, itemHeight: number, itemGroup: IGroup) {
403
463
  const groupId = `item-${item.id}`;
@@ -422,6 +482,7 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
422
482
  const itemName = this.get('itemName');
423
483
  const itemValue = this.get('itemValue');
424
484
  const itemBackground = this.get('itemBackground');
485
+ const radio = this.get('radio');
425
486
  const itemWidth = this.getLimitItemWidth();
426
487
 
427
488
  let curX = 0; // 记录当前 x 的位置
@@ -446,6 +507,7 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
446
507
  }
447
508
  curX = nameShape.getBBox().maxX + itemName.spacing;
448
509
  }
510
+
449
511
  if (itemValue) {
450
512
  const valueShape = this.drawItemText(subGroup, 'value', itemValue, item, itemHeight, curX, index);
451
513
  if (itemWidth) {
@@ -459,7 +521,13 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
459
521
  ellipsisLabel(true, valueShape, clamp(itemWidth - curX, 0, itemWidth));
460
522
  }
461
523
  }
524
+ curX = valueShape.getBBox().maxX + itemValue.spacing;
462
525
  }
526
+
527
+ if (radio) {
528
+ this.drawRadio(subGroup, radio, item, itemHeight, curX);
529
+ }
530
+
463
531
  // 添加透明的背景,便于拾取和包围盒计算
464
532
  if (itemBackground) {
465
533
  const bbox = subGroup.getBBox();
@@ -623,7 +691,7 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
623
691
  const leftArrow = this.drawArrow(
624
692
  subGroup,
625
693
  currentPoint,
626
- 'navigation-arrow-left',
694
+ LEFT_ARROW_NAME,
627
695
  layout === 'horizontal' ? 'up' : 'left',
628
696
  size,
629
697
  arrowStyle
@@ -650,7 +718,7 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
650
718
  const rightArrow = this.drawArrow(
651
719
  subGroup,
652
720
  currentPoint,
653
- 'navigation-arrow-right',
721
+ RIGHT_ARROW_NAME,
654
722
  layout === 'horizontal' ? 'down' : 'right',
655
723
  size,
656
724
  arrowStyle
@@ -667,15 +735,12 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
667
735
  const text = `${this.currentPageIndex}/${this.totalPagesCnt}`;
668
736
  const textShape = navigation ? navigation.getChildren()[1] : this.getElementByLocalId('navigation-text');
669
737
  const leftArrow = navigation
670
- ? navigation.findById(this.getElementId('navigation-arrow-left'))
671
- : this.getElementByLocalId('navigation-arrow-left');
738
+ ? navigation.findById(this.getElementId(LEFT_ARROW_NAME))
739
+ : this.getElementByLocalId(LEFT_ARROW_NAME);
672
740
  const rightArrow = navigation
673
- ? navigation.findById(this.getElementId('navigation-arrow-right'))
674
- : this.getElementByLocalId('navigation-arrow-right');
675
- const origBBox = textShape.getBBox();
741
+ ? navigation.findById(this.getElementId(RIGHT_ARROW_NAME))
742
+ : this.getElementByLocalId(RIGHT_ARROW_NAME);
676
743
  textShape.attr('text', text);
677
- const newBBox = textShape.getBBox();
678
- textShape.attr('x', textShape.attr('x') - (newBBox.width - origBBox.width) / 2);
679
744
  // 更新 left-arrow marker
680
745
  leftArrow.attr('opacity', this.currentPageIndex === 1 ? inactiveOpacity : opacity);
681
746
  leftArrow.attr('fill', this.currentPageIndex === 1 ? inactiveFill : fill);
@@ -684,6 +749,12 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
684
749
  rightArrow.attr('opacity', this.currentPageIndex === this.totalPagesCnt ? inactiveOpacity : opacity);
685
750
  rightArrow.attr('fill', this.currentPageIndex === this.totalPagesCnt ? inactiveFill : fill);
686
751
  rightArrow.attr('cursor', this.currentPageIndex === this.totalPagesCnt ? 'not-allowed' : 'pointer');
752
+ // 更新位置
753
+ let cursorX = leftArrow.getBBox().maxX + 2;
754
+ textShape.attr('x', cursorX);
755
+ cursorX += textShape.getBBox().width + 2;
756
+ this.updateArrowPath(rightArrow, { x: cursorX, y: 0 });
757
+
687
758
  }
688
759
 
689
760
  private drawArrow(
@@ -695,40 +766,47 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
695
766
  style?: LegendPageNavigatorCfg['marker']['style']
696
767
  ) {
697
768
  const { x, y } = currentPoint;
698
- const rotateMap = {
699
- right: (90 * Math.PI) / 180,
700
- left: ((360 - 90) * Math.PI) / 180,
701
- up: 0,
702
- down: (180 * Math.PI) / 180,
703
- };
704
769
  const shape = this.addShape(group, {
705
770
  type: 'path',
706
771
  id: this.getElementId(name),
707
772
  name,
708
773
  attrs: {
774
+ size,
775
+ direction,
709
776
  path: [['M', x + size / 2, y], ['L', x, y + size], ['L', x + size, y + size], ['Z']],
710
777
  cursor: 'pointer',
711
778
  ...style,
712
779
  },
713
780
  });
714
- shape.attr('matrix', getMatrixByAngle({ x: x + size / 2, y: y + size / 2 }, rotateMap[direction]));
781
+ shape.attr('matrix', getMatrixByAngle({ x: x + size / 2, y: y + size / 2 }, ROTATE_MAP[direction]));
715
782
 
716
783
  return shape;
717
784
  }
718
785
 
786
+ /**
787
+ * 更新分页器 arrow 组件
788
+ */
789
+ private updateArrowPath(arrow: IShape, point: { x: number; y: number }): void {
790
+ const { x, y } = point;
791
+ const { size, direction } = arrow.attr();
792
+ let matrix = getMatrixByAngle({ x: x + size / 2, y: y + size / 2 }, ROTATE_MAP[direction]);
793
+ arrow.attr('path', [['M', x + size / 2, y], ['L', x, y + size], ['L', x + size, y + size], ['Z']]);
794
+ arrow.attr('matrix', matrix);
795
+ }
796
+
719
797
  private getCurrentNavigationMatrix() {
720
798
  const { currentPageIndex, pageWidth, pageHeight } = this;
721
799
  const layout = this.get('layout');
722
800
  const translate =
723
801
  layout === 'horizontal'
724
802
  ? {
725
- x: 0,
726
- y: pageHeight * (1 - currentPageIndex),
727
- }
803
+ x: 0,
804
+ y: pageHeight * (1 - currentPageIndex),
805
+ }
728
806
  : {
729
- x: pageWidth * (1 - currentPageIndex),
730
- y: 0,
731
- };
807
+ x: pageWidth * (1 - currentPageIndex),
808
+ y: 0,
809
+ };
732
810
 
733
811
  return getMatrixByTranslate(translate);
734
812
  }
package/src/types.ts CHANGED
@@ -269,7 +269,7 @@ export interface AxisTitleCfg {
269
269
  * 文本对齐方式
270
270
  * @type {string} start, center, end
271
271
  */
272
- position?: string;
272
+ position?: string;
273
273
  }
274
274
 
275
275
  export interface BaseCfg {
@@ -593,6 +593,11 @@ export interface CategoryLegendCfg extends LegendBaseCfg {
593
593
  * @type {LegendItemValueCfg}
594
594
  */
595
595
  itemValue?: LegendItemValueCfg;
596
+ /**
597
+ *
598
+ * @type {LegendRadio}
599
+ */
600
+ radio?: LegendRadio;
596
601
  /**
597
602
  * 最大宽度
598
603
  * @type {number}
@@ -616,11 +621,11 @@ export interface CategoryLegendCfg extends LegendBaseCfg {
616
621
  /**
617
622
  * 是否翻页
618
623
  */
619
- flipPage?: boolean;
620
- /**
621
- * 翻页行数(只适用于横向)
622
- */
623
- maxRow?: number;
624
+ flipPage?: boolean;
625
+ /**
626
+ * 翻页行数(只适用于横向)
627
+ */
628
+ maxRow?: number;
624
629
  /**
625
630
  * 分页器配置
626
631
  * @type {LegendPageNavigatorCfg}
@@ -807,7 +812,27 @@ export interface LegendItemValueCfg {
807
812
  * 图例项附加值的配置
808
813
  * @type {ShapeAttrs}
809
814
  */
810
- style?: ShapeAttrs | ShapeAttrsCallback;
815
+ style?: ShapeAttrs | ShapeAttrsCallback;
816
+ /**
817
+ * 图例值和后面的间隔,可以控制和 RadioIcon 的间距
818
+ * @type {number}
819
+ */
820
+ spacing?: number;
821
+ }
822
+
823
+ /**
824
+ * radio 的配置项
825
+ */
826
+ export interface LegendRadio {
827
+ /**
828
+ * radio 样式
829
+ * @type {ShapeAttrs}
830
+ */
831
+ style?: ShapeAttrs;
832
+ /**
833
+ * radio 的提示信息
834
+ */
835
+ tip?: string;
811
836
  }
812
837
 
813
838
  export interface LegendMarkerCfg {
@@ -857,6 +882,11 @@ export interface LegendPageNavigatorCfg {
857
882
 
858
883
  export type TooltipPosition = 'top' | 'left' | 'right' | 'bottom' | 'auto';
859
884
  export interface TooltipCfg extends HtmlComponentCfg {
885
+ /**
886
+ * DOM 节点的 id
887
+ * @type {string}
888
+ */
889
+ containerId?: string;
860
890
  /**
861
891
  * 位置 x
862
892
  * @type {number}