@antv/component 0.8.19 → 0.8.23

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,45 @@ 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
+ return this.addShape(container, {
452
+ type: 'path',
453
+ id: this.getElementId(`item-${item.id}-radio`),
454
+ name: 'legend-item-radio',
455
+ attrs,
456
+ });
457
+ }
458
+
401
459
  // 绘制图例项
402
460
  private drawItem(item: ListItem, index: number, itemHeight: number, itemGroup: IGroup) {
403
461
  const groupId = `item-${item.id}`;
@@ -422,6 +480,7 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
422
480
  const itemName = this.get('itemName');
423
481
  const itemValue = this.get('itemValue');
424
482
  const itemBackground = this.get('itemBackground');
483
+ const radio = this.get('radio');
425
484
  const itemWidth = this.getLimitItemWidth();
426
485
 
427
486
  let curX = 0; // 记录当前 x 的位置
@@ -446,6 +505,7 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
446
505
  }
447
506
  curX = nameShape.getBBox().maxX + itemName.spacing;
448
507
  }
508
+
449
509
  if (itemValue) {
450
510
  const valueShape = this.drawItemText(subGroup, 'value', itemValue, item, itemHeight, curX, index);
451
511
  if (itemWidth) {
@@ -459,7 +519,13 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
459
519
  ellipsisLabel(true, valueShape, clamp(itemWidth - curX, 0, itemWidth));
460
520
  }
461
521
  }
522
+ curX = valueShape.getBBox().maxX + itemValue.spacing;
462
523
  }
524
+
525
+ if (radio) {
526
+ this.drawRadio(subGroup, radio, item, itemHeight, curX);
527
+ }
528
+
463
529
  // 添加透明的背景,便于拾取和包围盒计算
464
530
  if (itemBackground) {
465
531
  const bbox = subGroup.getBBox();
@@ -623,7 +689,7 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
623
689
  const leftArrow = this.drawArrow(
624
690
  subGroup,
625
691
  currentPoint,
626
- 'navigation-arrow-left',
692
+ LEFT_ARROW_NAME,
627
693
  layout === 'horizontal' ? 'up' : 'left',
628
694
  size,
629
695
  arrowStyle
@@ -650,7 +716,7 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
650
716
  const rightArrow = this.drawArrow(
651
717
  subGroup,
652
718
  currentPoint,
653
- 'navigation-arrow-right',
719
+ RIGHT_ARROW_NAME,
654
720
  layout === 'horizontal' ? 'down' : 'right',
655
721
  size,
656
722
  arrowStyle
@@ -667,15 +733,12 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
667
733
  const text = `${this.currentPageIndex}/${this.totalPagesCnt}`;
668
734
  const textShape = navigation ? navigation.getChildren()[1] : this.getElementByLocalId('navigation-text');
669
735
  const leftArrow = navigation
670
- ? navigation.findById(this.getElementId('navigation-arrow-left'))
671
- : this.getElementByLocalId('navigation-arrow-left');
736
+ ? navigation.findById(this.getElementId(LEFT_ARROW_NAME))
737
+ : this.getElementByLocalId(LEFT_ARROW_NAME);
672
738
  const rightArrow = navigation
673
- ? navigation.findById(this.getElementId('navigation-arrow-right'))
674
- : this.getElementByLocalId('navigation-arrow-right');
675
- const origBBox = textShape.getBBox();
739
+ ? navigation.findById(this.getElementId(RIGHT_ARROW_NAME))
740
+ : this.getElementByLocalId(RIGHT_ARROW_NAME);
676
741
  textShape.attr('text', text);
677
- const newBBox = textShape.getBBox();
678
- textShape.attr('x', textShape.attr('x') - (newBBox.width - origBBox.width) / 2);
679
742
  // 更新 left-arrow marker
680
743
  leftArrow.attr('opacity', this.currentPageIndex === 1 ? inactiveOpacity : opacity);
681
744
  leftArrow.attr('fill', this.currentPageIndex === 1 ? inactiveFill : fill);
@@ -684,6 +747,12 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
684
747
  rightArrow.attr('opacity', this.currentPageIndex === this.totalPagesCnt ? inactiveOpacity : opacity);
685
748
  rightArrow.attr('fill', this.currentPageIndex === this.totalPagesCnt ? inactiveFill : fill);
686
749
  rightArrow.attr('cursor', this.currentPageIndex === this.totalPagesCnt ? 'not-allowed' : 'pointer');
750
+ // 更新位置
751
+ let cursorX = leftArrow.getBBox().maxX + 2;
752
+ textShape.attr('x', cursorX);
753
+ cursorX += textShape.getBBox().width + 2;
754
+ this.updateArrowPath(rightArrow, { x: cursorX, y: 0 });
755
+
687
756
  }
688
757
 
689
758
  private drawArrow(
@@ -695,40 +764,47 @@ class Category extends LegendBase<CategoryLegendCfg> implements IList {
695
764
  style?: LegendPageNavigatorCfg['marker']['style']
696
765
  ) {
697
766
  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
767
  const shape = this.addShape(group, {
705
768
  type: 'path',
706
769
  id: this.getElementId(name),
707
770
  name,
708
771
  attrs: {
772
+ size,
773
+ direction,
709
774
  path: [['M', x + size / 2, y], ['L', x, y + size], ['L', x + size, y + size], ['Z']],
710
775
  cursor: 'pointer',
711
776
  ...style,
712
777
  },
713
778
  });
714
- shape.attr('matrix', getMatrixByAngle({ x: x + size / 2, y: y + size / 2 }, rotateMap[direction]));
779
+ shape.attr('matrix', getMatrixByAngle({ x: x + size / 2, y: y + size / 2 }, ROTATE_MAP[direction]));
715
780
 
716
781
  return shape;
717
782
  }
718
783
 
784
+ /**
785
+ * 更新分页器 arrow 组件
786
+ */
787
+ private updateArrowPath(arrow: IShape, point: { x: number; y: number }): void {
788
+ const { x, y } = point;
789
+ const { size, direction } = arrow.attr();
790
+ let matrix = getMatrixByAngle({ x: x + size / 2, y: y + size / 2 }, ROTATE_MAP[direction]);
791
+ arrow.attr('path', [['M', x + size / 2, y], ['L', x, y + size], ['L', x + size, y + size], ['Z']]);
792
+ arrow.attr('matrix', matrix);
793
+ }
794
+
719
795
  private getCurrentNavigationMatrix() {
720
796
  const { currentPageIndex, pageWidth, pageHeight } = this;
721
797
  const layout = this.get('layout');
722
798
  const translate =
723
799
  layout === 'horizontal'
724
800
  ? {
725
- x: 0,
726
- y: pageHeight * (1 - currentPageIndex),
727
- }
801
+ x: 0,
802
+ y: pageHeight * (1 - currentPageIndex),
803
+ }
728
804
  : {
729
- x: pageWidth * (1 - currentPageIndex),
730
- y: 0,
731
- };
805
+ x: pageWidth * (1 - currentPageIndex),
806
+ y: 0,
807
+ };
732
808
 
733
809
  return getMatrixByTranslate(translate);
734
810
  }
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,20 @@ 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
+ export interface LegendRadio {
824
+ /**
825
+ * radio 的配置项
826
+ * @type {ShapeAttrs}
827
+ */
828
+ style?: ShapeAttrs;
811
829
  }
812
830
 
813
831
  export interface LegendMarkerCfg {
package/src/util/theme.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export default {
2
2
  fontFamily: `
3
- "-apple-system", BlinkMacSystemFont, "Segoe UI", Roboto,"Helvetica Neue",
3
+ BlinkMacSystemFont, "Segoe UI", Roboto,"Helvetica Neue",
4
4
  Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",
5
5
  SimSun, "sans-serif"`,
6
6
  textColor: '#2C3542',