@antv/g2 5.3.3-beta.0 → 5.3.3

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 (145) hide show
  1. package/dist/g2.lite.min.js +30 -22
  2. package/dist/g2.min.js +30 -22
  3. package/esm/api/runtime.d.ts +10 -0
  4. package/esm/api/runtime.js +63 -3
  5. package/esm/api/runtime.js.map +1 -1
  6. package/esm/data/utils/venn/fmin/bisect.d.ts +3 -0
  7. package/esm/data/utils/venn/fmin/bisect.js +30 -0
  8. package/esm/data/utils/venn/fmin/bisect.js.map +1 -0
  9. package/esm/data/utils/venn/fmin/blas1.d.ts +7 -0
  10. package/esm/data/utils/venn/fmin/blas1.js +38 -0
  11. package/esm/data/utils/venn/fmin/blas1.js.map +1 -0
  12. package/esm/data/utils/venn/fmin/conjugateGradient.d.ts +6 -0
  13. package/esm/data/utils/venn/fmin/conjugateGradient.js +91 -0
  14. package/esm/data/utils/venn/fmin/conjugateGradient.js.map +1 -0
  15. package/esm/data/utils/venn/fmin/gradientDescent.d.ts +10 -0
  16. package/esm/data/utils/venn/fmin/gradientDescent.js +66 -0
  17. package/esm/data/utils/venn/fmin/gradientDescent.js.map +1 -0
  18. package/esm/data/utils/venn/fmin/index.d.ts +34 -0
  19. package/esm/data/utils/venn/fmin/index.js +35 -0
  20. package/esm/data/utils/venn/fmin/index.js.map +1 -0
  21. package/esm/data/utils/venn/fmin/linesearch.d.ts +1 -0
  22. package/esm/data/utils/venn/fmin/linesearch.js +60 -0
  23. package/esm/data/utils/venn/fmin/linesearch.js.map +1 -0
  24. package/esm/data/utils/venn/fmin/nelderMead.d.ts +5 -0
  25. package/esm/data/utils/venn/fmin/nelderMead.js +133 -0
  26. package/esm/data/utils/venn/fmin/nelderMead.js.map +1 -0
  27. package/esm/data/utils/venn/layout.d.ts +2 -2
  28. package/esm/data/utils/venn/layout.js +1 -1
  29. package/esm/data/utils/venn/layout.js.map +1 -1
  30. package/esm/index.d.ts +2 -2
  31. package/esm/index.js +2 -2
  32. package/esm/index.js.map +1 -1
  33. package/esm/interaction/elementSelect.js +32 -2
  34. package/esm/interaction/elementSelect.js.map +1 -1
  35. package/esm/interaction/tooltip.d.ts +57 -0
  36. package/esm/interaction/tooltip.js +142 -82
  37. package/esm/interaction/tooltip.js.map +1 -1
  38. package/esm/interaction/utils.d.ts +2 -2
  39. package/esm/interaction/utils.js +10 -2
  40. package/esm/interaction/utils.js.map +1 -1
  41. package/esm/mark/line.js +4 -1
  42. package/esm/mark/line.js.map +1 -1
  43. package/esm/runtime/component.js +5 -2
  44. package/esm/runtime/component.js.map +1 -1
  45. package/esm/runtime/plot.js +6 -3
  46. package/esm/runtime/plot.js.map +1 -1
  47. package/esm/runtime/types/common.d.ts +1 -0
  48. package/esm/shape/connector/connector.js +19 -15
  49. package/esm/shape/connector/connector.js.map +1 -1
  50. package/esm/spec/transform.d.ts +1 -0
  51. package/esm/theme/create.d.ts +0 -1
  52. package/esm/theme/create.js +0 -1
  53. package/esm/theme/create.js.map +1 -1
  54. package/esm/transform/group.d.ts +0 -1
  55. package/esm/transform/group.js.map +1 -1
  56. package/esm/transform/groupN.d.ts +1 -1
  57. package/esm/utils/style.js +11 -2
  58. package/esm/utils/style.js.map +1 -1
  59. package/esm/utils/traverse-elements.d.ts +2 -0
  60. package/esm/utils/traverse-elements.js +13 -0
  61. package/esm/utils/traverse-elements.js.map +1 -0
  62. package/lib/api/runtime.d.ts +10 -0
  63. package/lib/api/runtime.js +70 -10
  64. package/lib/api/runtime.js.map +1 -1
  65. package/lib/data/utils/venn/fmin/bisect.d.ts +3 -0
  66. package/lib/data/utils/venn/fmin/bisect.js +34 -0
  67. package/lib/data/utils/venn/fmin/bisect.js.map +1 -0
  68. package/lib/data/utils/venn/fmin/blas1.d.ts +7 -0
  69. package/lib/data/utils/venn/fmin/blas1.js +48 -0
  70. package/lib/data/utils/venn/fmin/blas1.js.map +1 -0
  71. package/lib/data/utils/venn/fmin/conjugateGradient.d.ts +6 -0
  72. package/lib/data/utils/venn/fmin/conjugateGradient.js +96 -0
  73. package/lib/data/utils/venn/fmin/conjugateGradient.js.map +1 -0
  74. package/lib/data/utils/venn/fmin/gradientDescent.d.ts +10 -0
  75. package/lib/data/utils/venn/fmin/gradientDescent.js +71 -0
  76. package/lib/data/utils/venn/fmin/gradientDescent.js.map +1 -0
  77. package/lib/data/utils/venn/fmin/index.d.ts +34 -0
  78. package/lib/data/utils/venn/fmin/index.js +49 -0
  79. package/lib/data/utils/venn/fmin/index.js.map +1 -0
  80. package/lib/data/utils/venn/fmin/linesearch.d.ts +1 -0
  81. package/lib/data/utils/venn/fmin/linesearch.js +64 -0
  82. package/lib/data/utils/venn/fmin/linesearch.js.map +1 -0
  83. package/lib/data/utils/venn/fmin/nelderMead.d.ts +5 -0
  84. package/lib/data/utils/venn/fmin/nelderMead.js +137 -0
  85. package/lib/data/utils/venn/fmin/nelderMead.js.map +1 -0
  86. package/lib/data/utils/venn/layout.d.ts +2 -2
  87. package/lib/data/utils/venn/layout.js +1 -1
  88. package/lib/data/utils/venn/layout.js.map +1 -1
  89. package/lib/index.d.ts +2 -2
  90. package/lib/index.js +2 -1
  91. package/lib/index.js.map +1 -1
  92. package/lib/interaction/elementSelect.js +32 -2
  93. package/lib/interaction/elementSelect.js.map +1 -1
  94. package/lib/interaction/tooltip.d.ts +57 -0
  95. package/lib/interaction/tooltip.js +145 -82
  96. package/lib/interaction/tooltip.js.map +1 -1
  97. package/lib/interaction/utils.d.ts +2 -2
  98. package/lib/interaction/utils.js +10 -2
  99. package/lib/interaction/utils.js.map +1 -1
  100. package/lib/mark/line.js +3 -0
  101. package/lib/mark/line.js.map +1 -1
  102. package/lib/runtime/component.js +5 -2
  103. package/lib/runtime/component.js.map +1 -1
  104. package/lib/runtime/plot.js +6 -3
  105. package/lib/runtime/plot.js.map +1 -1
  106. package/lib/runtime/types/common.d.ts +1 -0
  107. package/lib/shape/connector/connector.js +19 -15
  108. package/lib/shape/connector/connector.js.map +1 -1
  109. package/lib/spec/transform.d.ts +1 -0
  110. package/lib/theme/create.d.ts +0 -1
  111. package/lib/theme/create.js +0 -1
  112. package/lib/theme/create.js.map +1 -1
  113. package/lib/transform/group.d.ts +0 -1
  114. package/lib/transform/group.js.map +1 -1
  115. package/lib/transform/groupN.d.ts +1 -1
  116. package/lib/utils/style.js +11 -2
  117. package/lib/utils/style.js.map +1 -1
  118. package/lib/utils/traverse-elements.d.ts +2 -0
  119. package/lib/utils/traverse-elements.js +17 -0
  120. package/lib/utils/traverse-elements.js.map +1 -0
  121. package/package.json +3 -3
  122. package/src/api/runtime.ts +107 -3
  123. package/src/data/utils/venn/fmin/bisect.ts +32 -0
  124. package/src/data/utils/venn/fmin/blas1.ts +42 -0
  125. package/src/data/utils/venn/fmin/conjugateGradient.ts +106 -0
  126. package/src/data/utils/venn/fmin/gradientDescent.ts +75 -0
  127. package/src/data/utils/venn/fmin/index.ts +35 -0
  128. package/src/data/utils/venn/fmin/linesearch.ts +70 -0
  129. package/src/data/utils/venn/fmin/nelderMead.ts +147 -0
  130. package/src/data/utils/venn/layout.ts +1 -1
  131. package/src/index.ts +2 -2
  132. package/src/interaction/elementSelect.ts +32 -3
  133. package/src/interaction/tooltip.ts +213 -132
  134. package/src/interaction/utils.ts +12 -5
  135. package/src/mark/line.ts +3 -1
  136. package/src/runtime/component.ts +5 -3
  137. package/src/runtime/plot.ts +6 -1
  138. package/src/runtime/types/common.ts +1 -0
  139. package/src/shape/connector/connector.ts +30 -19
  140. package/src/spec/transform.ts +1 -0
  141. package/src/theme/create.ts +0 -1
  142. package/src/transform/group.ts +0 -1
  143. package/src/transform/groupN.ts +1 -1
  144. package/src/utils/style.ts +11 -1
  145. package/src/utils/traverse-elements.ts +16 -0
@@ -2,6 +2,7 @@ import { Circle, DisplayObject, IElement, Line } from '@antv/g';
2
2
  import { sort, group, mean, bisector, minIndex } from '@antv/vendor/d3-array';
3
3
  import { deepMix, lowerFirst, throttle } from '@antv/util';
4
4
  import { Tooltip as TooltipComponent } from '@antv/component';
5
+ import { G2Element } from 'utils/selection';
5
6
  import { defined, groupNameOf, subObject, dataOf } from '../utils/helper';
6
7
  import { isTranspose, isPolar } from '../utils/coordinate';
7
8
  import { angle, sub, dist } from '../utils/vector';
@@ -504,7 +505,7 @@ function interactionKeyof(markState, key) {
504
505
  );
505
506
  }
506
507
 
507
- function maybeValue(specified, defaults) {
508
+ export function maybeValue(specified, defaults) {
508
509
  return specified === undefined ? defaults : specified;
509
510
  }
510
511
 
@@ -522,56 +523,111 @@ function hasSeries(markState): boolean {
522
523
  }
523
524
 
524
525
  /**
525
- * Show tooltip for series item.
526
+ * Finds a single element based on the mouse event in a non-series context (e.g., single item tooltip).
527
+ * @param root - The root display object of the chart.
528
+ * @param event - The mouse event object (e.g., pointermove, pointerdown).
529
+ * @param elements - Array of chart elements to search within.
530
+ * @param coordinate - The coordinate system of the chart (e.g., Cartesian, polar).
531
+ * @param scale - The scale configurations (e.g., x, series scales).
532
+ * @param shared - Whether the tooltip is shared among multiple elements (e.g., grouped bars).
533
+ * @returns The matched display object or `undefined` if no element is found.
534
+ * @description
535
+ * - Handles bar charts by sorting elements and using bisector search for efficient lookup.
536
+ * - For non-bar charts, directly finds the target element from the event's target.
537
+ * - Adjusts for bar spacing in grouped charts when `shared` is false.
526
538
  */
527
- export function seriesTooltip(
528
- root: DisplayObject,
529
- {
530
- elements: elementsof,
531
- sort: sortFunction,
532
- filter: filterFunction,
533
- scale,
534
- coordinate,
535
- crosshairs,
536
- crosshairsX,
537
- crosshairsY,
538
- render,
539
- groupName,
540
- emitter,
541
- wait = 50,
542
- leading = true,
543
- trailing = false,
544
- startX = 0,
545
- startY = 0,
546
- body = true,
547
- single = true,
548
- position,
549
- enterable,
550
- mount,
551
- bounding,
552
- theme,
553
- offset,
554
- disableNative = false,
555
- marker = true,
556
- preserve = false,
557
- style: _style = {},
558
- css = {},
559
- ...rest
560
- }: Record<string, any>,
561
- ) {
562
- const elements = elementsof(root);
563
- const transposed = isTranspose(coordinate);
564
- const polar = isPolar(coordinate);
565
- const style = deepMix(_style, rest);
539
+ export function findSingleElement({
540
+ root,
541
+ event,
542
+ elements,
543
+ coordinate,
544
+ scale,
545
+ shared,
546
+ }): DisplayObject | undefined {
547
+ const inInterval = (d) => d.markType === 'interval';
548
+ const isBar = elements.every(inInterval) && !isPolar(coordinate);
549
+ const scaleX = scale.x;
550
+ const scaleSeries = scale.series;
551
+ const bandWidth = scaleX?.getBandWidth?.() ?? 0;
552
+ const xof = scaleSeries
553
+ ? (d) => {
554
+ const seriesCount = Math.round(1 / scaleSeries.valueBandWidth);
555
+ return (
556
+ d.__data__.x +
557
+ d.__data__.series * bandWidth +
558
+ bandWidth / (seriesCount * 2)
559
+ );
560
+ }
561
+ : (d) => d.__data__.x + bandWidth / 2;
566
562
 
567
- const {
568
- innerWidth: plotWidth,
569
- innerHeight: plotHeight,
570
- width: mainWidth,
571
- height: mainHeight,
572
- insetLeft,
573
- insetTop,
574
- } = coordinate.getOptions();
563
+ // Sort for bisector search.
564
+ if (isBar) elements.sort((a, b) => xof(a) - xof(b));
565
+ const findElementByTarget = (event) => {
566
+ const { target } = event;
567
+ return maybeRoot(target, (node) => {
568
+ if (!node.classList) return false;
569
+ return node.classList.includes('element');
570
+ });
571
+ };
572
+
573
+ const element = isBar
574
+ ? (event) => {
575
+ const mouse = mousePosition(root, event);
576
+ if (!mouse) return;
577
+ const [abstractX] = coordinate.invert(mouse);
578
+ const search = bisector(xof).center;
579
+ const i = search(elements, abstractX);
580
+ const target = elements[i];
581
+
582
+ if (!shared) {
583
+ // For grouped bar chart without shared options.
584
+ const isGrouped = elements.find(
585
+ (d) => d !== target && xof(d) === xof(target),
586
+ );
587
+ if (isGrouped) return findElementByTarget(event);
588
+ }
589
+ return target;
590
+ }
591
+ : findElementByTarget;
592
+
593
+ return element(event);
594
+ }
595
+
596
+ /**
597
+ * Finds series-related elements and data based on the mouse event for series tooltips.
598
+ * @param root - The root display object of the chart.
599
+ * @param event - The mouse event object (e.g., pointermove, pointerdown).
600
+ * @param elements - Array of chart elements to search within.
601
+ * @param coordinate - The coordinate system of the chart (e.g., Cartesian, polar).
602
+ * @param scale - The scale configurations (e.g., x, series scales).
603
+ * @param startX - The starting X position of the plot area.
604
+ * @param startY - The starting Y position of the plot area.
605
+ * @returns An object containing:
606
+ * - `selectedElements`: Matched display objects (series and item elements).
607
+ * - `selectedData`: Corresponding data records of the selected elements.
608
+ * - `filteredSeriesData`: Filtered series data closest to the mouse focus.
609
+ * - `abstractX`: A function to convert mouse coordinates to abstract X values.
610
+ * @description
611
+ * - Splits elements into series and item elements for targeted searching.
612
+ * - Handles bar charts and band scales using bisector search and coordinate inversion.
613
+ * - Sorts elements to ensure correct visual ordering (top-to-bottom or right-to-left in transposed mode).
614
+ * - Filters and groups data to provide accurate tooltip information for series.
615
+ */
616
+ export function findSeriesElement({
617
+ root,
618
+ event,
619
+ elements,
620
+ coordinate,
621
+ scale,
622
+ startX,
623
+ startY,
624
+ }): {
625
+ selectedElements: DisplayObject[];
626
+ selectedData: Record<string, any>[];
627
+ filteredSeriesData: any[];
628
+ abstractX: (number) => number;
629
+ } {
630
+ const transposed = isTranspose(coordinate);
575
631
 
576
632
  // Split elements into series elements and item elements.
577
633
  const seriesElements = [];
@@ -698,7 +754,97 @@ export function seriesTooltip(
698
754
  }),
699
755
  );
700
756
  };
757
+ const mouse = mousePosition(root, event);
758
+ if (!mouse) return;
759
+ const focus = [mouse[0] - startX, mouse[1] - startY];
760
+ if (!focus) return;
761
+ // Get selected item element.
762
+ const selectedItems = elementsByFocus(focus, itemElements);
763
+
764
+ // Get selected data item from both series element and item element.
765
+ const selectedSeriesElements = [];
766
+ const selectedSeriesData = [];
767
+ for (const element of seriesElements) {
768
+ const [sortedIndex, X] = elementSortedX.get(element);
769
+ const index = indexByFocus(event, focus, sortedIndex, X);
770
+ if (index !== null) {
771
+ selectedSeriesElements.push(element);
772
+ const d = seriesData(element, index);
773
+ const { x, y } = d;
774
+ const p = coordinate.map([(x || 0) + offsetX, y || 0]);
775
+ selectedSeriesData.push([{ ...d, element }, p] as const);
776
+ }
777
+ }
778
+
779
+ // Filter selectedSeriesData with different x,
780
+ // make sure there is only one x closest to focusX.
781
+ const SX = Array.from(new Set(selectedSeriesData.map((d) => d[0].x)));
782
+ const closestX = SX[minIndex(SX, (x) => Math.abs(x - abstractX(focus)))];
783
+ const filteredSeriesData = selectedSeriesData.filter(
784
+ (d) => d[0].x === closestX,
785
+ );
786
+
787
+ const selectedData = [
788
+ ...filteredSeriesData.map((d) => d[0]),
789
+ ...selectedItems.map((d) => d.__data__),
790
+ ];
791
+
792
+ // Get the displayed tooltip data.
793
+ const selectedElements = [...selectedSeriesElements, ...selectedItems];
701
794
 
795
+ return { selectedElements, selectedData, filteredSeriesData, abstractX };
796
+ }
797
+
798
+ /**
799
+ * Show tooltip for series item.
800
+ */
801
+ export function seriesTooltip(
802
+ root: DisplayObject,
803
+ {
804
+ elements: elementsof,
805
+ sort: sortFunction,
806
+ filter: filterFunction,
807
+ scale,
808
+ coordinate,
809
+ crosshairs,
810
+ crosshairsX,
811
+ crosshairsY,
812
+ render,
813
+ groupName,
814
+ emitter,
815
+ wait = 50,
816
+ leading = true,
817
+ trailing = false,
818
+ startX = 0,
819
+ startY = 0,
820
+ body = true,
821
+ single = true,
822
+ position,
823
+ enterable,
824
+ mount,
825
+ bounding,
826
+ theme,
827
+ offset,
828
+ disableNative = false,
829
+ marker = true,
830
+ preserve = false,
831
+ style: _style = {},
832
+ css = {},
833
+ ...rest
834
+ }: Record<string, any>,
835
+ ) {
836
+ const elements = elementsof(root);
837
+ const style = deepMix(_style, rest);
838
+ const polar = isPolar(coordinate);
839
+ const transposed = isTranspose(coordinate);
840
+ const {
841
+ innerWidth: plotWidth,
842
+ innerHeight: plotHeight,
843
+ width: mainWidth,
844
+ height: mainHeight,
845
+ insetLeft,
846
+ insetTop,
847
+ } = coordinate.getOptions();
702
848
  const update = throttle(
703
849
  (event) => {
704
850
  const mouse = mousePosition(root, event);
@@ -706,41 +852,16 @@ export function seriesTooltip(
706
852
  const bbox = bboxOf(root);
707
853
  const x = bbox.min[0];
708
854
  const y = bbox.min[1];
709
- const focus = [mouse[0] - startX, mouse[1] - startY];
710
- if (!focus) return;
711
- // Get selected item element.
712
- const selectedItems = elementsByFocus(focus, itemElements);
713
-
714
- // Get selected data item from both series element and item element.
715
- const selectedSeriesElements = [];
716
- const selectedSeriesData = [];
717
- for (const element of seriesElements) {
718
- const [sortedIndex, X] = elementSortedX.get(element);
719
- const index = indexByFocus(event, focus, sortedIndex, X);
720
- if (index !== null) {
721
- selectedSeriesElements.push(element);
722
- const d = seriesData(element, index);
723
- const { x, y } = d;
724
- const p = coordinate.map([(x || 0) + offsetX, y || 0]);
725
- selectedSeriesData.push([{ ...d, element }, p] as const);
726
- }
727
- }
728
-
729
- // Filter selectedSeriesData with different x,
730
- // make sure there is only one x closest to focusX.
731
- const SX = Array.from(new Set(selectedSeriesData.map((d) => d[0].x)));
732
- const closestX = SX[minIndex(SX, (x) => Math.abs(x - abstractX(focus)))];
733
- const filteredSeriesData = selectedSeriesData.filter(
734
- (d) => d[0].x === closestX,
735
- );
736
-
737
- const selectedData = [
738
- ...filteredSeriesData.map((d) => d[0]),
739
- ...selectedItems.map((d) => d.__data__),
740
- ];
741
-
742
- // Get the displayed tooltip data.
743
- const selectedElements = [...selectedSeriesElements, ...selectedItems];
855
+ const { selectedElements, selectedData, filteredSeriesData, abstractX } =
856
+ findSeriesElement({
857
+ root,
858
+ event,
859
+ elements,
860
+ coordinate,
861
+ scale,
862
+ startX,
863
+ startY,
864
+ });
744
865
  const tooltipData = groupItems(
745
866
  selectedElements,
746
867
  scale,
@@ -973,56 +1094,16 @@ export function tooltip(
973
1094
  ) {
974
1095
  const elements = elementsof(root);
975
1096
  const keyGroup = group(elements, groupKey);
976
- const inInterval = (d) => d.markType === 'interval';
977
- const isBar = elements.every(inInterval) && !isPolar(coordinate);
978
- const scaleX = scale.x;
979
- const scaleSeries = scale.series;
980
- const bandWidth = scaleX?.getBandWidth?.() ?? 0;
981
- const xof = scaleSeries
982
- ? (d) => {
983
- const seriesCount = Math.round(1 / scaleSeries.valueBandWidth);
984
- return (
985
- d.__data__.x +
986
- d.__data__.series * bandWidth +
987
- bandWidth / (seriesCount * 2)
988
- );
989
- }
990
- : (d) => d.__data__.x + bandWidth / 2;
991
-
992
- // Sort for bisector search.
993
- if (isBar) elements.sort((a, b) => xof(a) - xof(b));
994
-
995
- const findElementByTarget = (event) => {
996
- const { target } = event;
997
- return maybeRoot(target, (node) => {
998
- if (!node.classList) return false;
999
- return node.classList.includes('element');
1000
- });
1001
- };
1002
-
1003
- const findElement = isBar
1004
- ? (event) => {
1005
- const mouse = mousePosition(root, event);
1006
- if (!mouse) return;
1007
- const [abstractX] = coordinate.invert(mouse);
1008
- const search = bisector(xof).center;
1009
- const i = search(elements, abstractX);
1010
- const target = elements[i];
1011
-
1012
- if (!shared) {
1013
- // For grouped bar chart without shared options.
1014
- const isGrouped = elements.find(
1015
- (d) => d !== target && xof(d) === xof(target),
1016
- );
1017
- if (isGrouped) return findElementByTarget(event);
1018
- }
1019
- return target;
1020
- }
1021
- : findElementByTarget;
1022
-
1023
1097
  const pointermove = throttle(
1024
1098
  (event) => {
1025
- const element = findElement(event);
1099
+ const element = findSingleElement({
1100
+ root,
1101
+ event,
1102
+ elements,
1103
+ coordinate,
1104
+ scale,
1105
+ shared,
1106
+ });
1026
1107
  if (!element) {
1027
1108
  hideTooltip({ root, single, emitter, event });
1028
1109
  return;
@@ -17,6 +17,7 @@ import { isPolar, isTranspose } from '../utils/coordinate';
17
17
  import { getStyle } from '../utils/style';
18
18
  import { reorder } from '../shape/utils';
19
19
  import { angle, angleBetween, sub } from '../utils/vector';
20
+ import { traverseElements } from '../utils/traverse-elements';
20
21
 
21
22
  /**
22
23
  * Given root of chart returns elements to be manipulated
@@ -139,6 +140,14 @@ const STATE_GROUPS = {
139
140
  highlight: ['active', 'inactive'],
140
141
  };
141
142
 
143
+ const setElementAttribute = (element: DisplayObject, k: string, v: string) => {
144
+ traverseElements(element, (el) => {
145
+ if ('setAttribute' in el && typeof el.setAttribute === 'function') {
146
+ (el as DisplayObject).setAttribute(k, v);
147
+ }
148
+ });
149
+ };
150
+
142
151
  export function createUseState(
143
152
  style: Record<string, any>,
144
153
  elements: Element[],
@@ -157,16 +166,14 @@ export function createUseState(
157
166
  }
158
167
  });
159
168
 
160
- return (
161
- valueof = (d, element) => d,
162
- setAttribute = (element, key, v) => element.setAttribute(key, v),
163
- ) => useState(undefined, valueof, setAttribute);
169
+ return (valueof = (d, element) => d, setAttribute = setElementAttribute) =>
170
+ useState(undefined, valueof, setAttribute);
164
171
  }
165
172
 
166
173
  export function useState(
167
174
  style: Record<string, any> | undefined,
168
175
  valueof = (d, element) => d,
169
- setAttribute = (element, key, v) => element.setAttribute(key, v),
176
+ setAttribute = setElementAttribute,
170
177
  ) {
171
178
  const STATES = '__states__';
172
179
  const ORIGINAL = '__ordinal__';
package/src/mark/line.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Vector } from '@antv/coord';
2
2
  import { group } from '@antv/vendor/d3-array';
3
- import { isParallel } from '../utils/coordinate';
3
+ import { isParallel, isPolar } from '../utils/coordinate';
4
4
  import { Mark, MarkComponent as MC, SingleMark, Vector2 } from '../runtime';
5
5
  import { LineMark } from '../spec';
6
6
  import {
@@ -68,6 +68,8 @@ const parallel: Mark = (index, scale, value, coordinate) => {
68
68
  if (PV.length === 0) {
69
69
  throw new Error('Missing encode for position channel.');
70
70
  }
71
+ // Close the loop for radar(= parallel + polar) coordinate.
72
+ if (isPolar(coordinate)) PV.push(PV[0]);
71
73
 
72
74
  // One data corresponds to one line.
73
75
  const P = Array.from(index, (i) => {
@@ -439,12 +439,14 @@ function inferAxisPositionAndOrientation(
439
439
  scale.name.startsWith('position'),
440
440
  );
441
441
  const index = matchPosition(name);
442
- if (name === positions.slice(-1)[0].name || index === null)
443
- return [null, null];
442
+ if (index === null) return [null, null];
444
443
  // infer radar axis orientation
445
444
  const [startAngle, endAngle] = angleOf(coordinates);
445
+ const positionLength = isRadar(coordinates)
446
+ ? positions.length
447
+ : positions.length - 1;
446
448
  const angle =
447
- ((endAngle - startAngle) / (positions.length - 1)) * index + startAngle;
449
+ ((endAngle - startAngle) / positionLength) * index + startAngle;
448
450
  return ['center', angle];
449
451
  }
450
452
 
@@ -789,6 +789,7 @@ function initializeState(
789
789
  // the same style with JS standard and lodash APIs.
790
790
  // @todo More proper way to index scale for different marks.
791
791
  const children = [];
792
+ const dataMap = new Map();
792
793
  for (const [mark, state] of markState.entries()) {
793
794
  const {
794
795
  // scale,
@@ -799,7 +800,9 @@ function initializeState(
799
800
  dataDomain,
800
801
  modifier,
801
802
  key: markKey,
803
+ data,
802
804
  } = mark;
805
+ dataMap.set(markKey, data);
803
806
  const { index, channels, tooltip } = state;
804
807
  const scale = Object.fromEntries(
805
808
  channels.map(({ name, scale }) => [name, scale]),
@@ -828,6 +831,7 @@ function initializeState(
828
831
  index: d,
829
832
  markKey,
830
833
  viewKey: key,
834
+ data: data[d],
831
835
  ...(tooltip && {
832
836
  title: titleOf(d),
833
837
  items: itemsOf(d),
@@ -867,6 +871,7 @@ function initializeState(
867
871
  scale: scaleInstance,
868
872
  style: framedStyle,
869
873
  components,
874
+ data: dataMap,
870
875
  labelTransform: compose(labelTransform.map(useLabelTransform)),
871
876
  };
872
877
 
@@ -1318,7 +1323,7 @@ function normalizeLabelSelector(
1318
1323
  * transitions together.
1319
1324
  */
1320
1325
  function getLocalBounds(element: DisplayObject) {
1321
- const cloneElement = element.cloneNode();
1326
+ const cloneElement = element.cloneNode(true);
1322
1327
  const animations = element.getAnimations();
1323
1328
  cloneElement.style.visibility = 'hidden';
1324
1329
  animations.forEach((animation) => {
@@ -47,6 +47,7 @@ export type G2ViewDescriptor = {
47
47
  labelTransform: LabelTransform;
48
48
  // @todo More accurate type.
49
49
  style: Record<string, any>;
50
+ data?: Map<string, any>;
50
51
  };
51
52
 
52
53
  export type G2ViewInstance = {
@@ -35,40 +35,49 @@ function inferConnectorPath(points: Vector2[]) {
35
35
  function getPoints(
36
36
  coordinate: Coordinate,
37
37
  points: Vector2[],
38
- offset1: number,
39
- offset2: number,
38
+ sourceOffsetY: number,
39
+ targetOffsetY: number,
40
+ sourceOffsetX: number,
41
+ targetOffsetX: number,
40
42
  length1 = 0,
41
43
  ): Vector2[] {
42
44
  const [[x0, y0], [x1, y1]] = points;
43
45
 
44
46
  if (isTranspose(coordinate)) {
45
- const X0 = x0 + offset1;
46
- const X1 = x1 + offset2;
47
+ const X0 = x0 + sourceOffsetY;
48
+ const X1 = x1 + targetOffsetY;
47
49
  const X = X0 + length1;
50
+ const Y0 = y0 + sourceOffsetX;
51
+ const Y1 = y1 + targetOffsetX;
48
52
  return [
49
- [X0, y0],
50
- [X, y0],
51
- [X, y1],
52
- [X1, y1],
53
+ [X0, Y0],
54
+ [X, Y0],
55
+ [X, Y1],
56
+ [X1, Y1],
53
57
  ];
54
58
  }
55
59
 
56
- const Y0 = y0 - offset1;
57
- const Y1 = y1 - offset2;
60
+ const Y0 = y0 - sourceOffsetY;
61
+ const Y1 = y1 - targetOffsetY;
58
62
  const Y = Y0 - length1;
63
+ const X0 = x0 - sourceOffsetX;
64
+ const X1 = x1 - targetOffsetX;
59
65
  return [
60
- [x0, Y0],
61
- [x0, Y],
62
- [x1, Y],
63
- [x1, Y1],
66
+ [X0, Y0],
67
+ [X0, Y],
68
+ [X1, Y],
69
+ [X1, Y1],
64
70
  ];
65
71
  }
66
72
 
67
73
  export const Connector: SC<ConnectorOptions> = (options, context) => {
68
74
  const {
69
- offset = 0,
70
- offset1 = offset,
71
- offset2 = offset,
75
+ offsetX = 0,
76
+ sourceOffsetX = offsetX,
77
+ targetOffsetX = offsetX,
78
+ offsetY = 0,
79
+ sourceOffsetY = offsetY,
80
+ targetOffsetY = offsetY,
72
81
  connectLength1: length1,
73
82
  endMarker = true,
74
83
  ...style
@@ -81,8 +90,10 @@ export const Connector: SC<ConnectorOptions> = (options, context) => {
81
90
  const P = getPoints(
82
91
  coordinate,
83
92
  points,
84
- offset1,
85
- offset2,
93
+ sourceOffsetY,
94
+ targetOffsetY,
95
+ sourceOffsetX,
96
+ targetOffsetX,
86
97
  length1 ?? connectLength1,
87
98
  );
88
99
  const makerStyle = subObject({ ...style, ...defaults }, 'endMarker');
@@ -251,6 +251,7 @@ export type GroupColorTransform = {
251
251
 
252
252
  export type GroupTransform = {
253
253
  type?: 'group';
254
+ channels?: string | string[];
254
255
  } & { [key in ChannelTypes]?: Reducer };
255
256
 
256
257
  export type BinXTransform = {
@@ -293,7 +293,6 @@ export function create({
293
293
  girdClosed: true,
294
294
  gridStrokeOpacity: 0.3,
295
295
  gridType: 'surround',
296
- label: false,
297
296
  tick: false,
298
297
  titlePosition: 'start',
299
298
  },
@@ -5,7 +5,6 @@ import { GroupN } from './groupN';
5
5
 
6
6
  export type GroupOptions = Omit<
7
7
  GroupTransform & {
8
- channels?: string | string[];
9
8
  groupBy?: (
10
9
  I: number[],
11
10
  mark: G2Mark,
@@ -19,7 +19,7 @@ export type GroupNOptions = Omit<
19
19
  options?: Record<string, any>,
20
20
  ) => number[][];
21
21
  },
22
- 'type'
22
+ 'type' | 'channels'
23
23
  >;
24
24
 
25
25
  type ReducerFunction = (I: number[], V: Primitive[]) => Primitive;
@@ -1,4 +1,5 @@
1
1
  import type { DisplayObject, BaseStyleProps } from '@antv/g';
2
+ import { traverseElements } from './traverse-elements';
2
3
 
3
4
  const defaultStyle: BaseStyleProps = {
4
5
  visibility: 'visible',
@@ -8,7 +9,16 @@ const defaultStyle: BaseStyleProps = {
8
9
  };
9
10
 
10
11
  export function getStyle(element: DisplayObject, key: string) {
11
- return element.style[key] ?? defaultStyle[key];
12
+ let value: any;
13
+ traverseElements(element, (el) => {
14
+ if (el.tagName !== 'g' && el.style?.[key] !== undefined) {
15
+ value = el.style[key];
16
+ return true;
17
+ }
18
+ return false;
19
+ });
20
+
21
+ return value ?? defaultStyle[key];
12
22
  }
13
23
 
14
24
  export function setStyle(
@@ -0,0 +1,16 @@
1
+ import { DisplayObject } from '@antv/g';
2
+
3
+ export function traverseElements(
4
+ element: DisplayObject,
5
+ visitor: (el: DisplayObject) => boolean | void,
6
+ ): boolean {
7
+ if (visitor(element)) return true;
8
+ if (element.tagName === 'g') {
9
+ const { childNodes = [] } = element;
10
+ for (const child of childNodes) {
11
+ if (traverseElements(child as DisplayObject, visitor)) return true;
12
+ }
13
+ }
14
+
15
+ return false;
16
+ }