@antv/g2 5.1.22 → 5.1.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.
@@ -164,6 +164,9 @@ function hideTooltip({
164
164
  // Must be clientX, clientY.
165
165
  tooltipElement.hide(event?.clientX, event?.clientY);
166
166
  }
167
+ hideRuleY(root);
168
+ hideRuleX(root);
169
+ hideMarker(root);
167
170
  }
168
171
 
169
172
  function destroyTooltip({ root, single }) {
@@ -175,6 +178,9 @@ function destroyTooltip({ root, single }) {
175
178
  tooltipElement.destroy();
176
179
  parent.tooltipElement = undefined;
177
180
  }
181
+ hideRuleY(root);
182
+ hideRuleX(root);
183
+ hideMarker(root);
178
184
  }
179
185
 
180
186
  function showUndefined(item) {
@@ -442,6 +448,7 @@ function updateRuleY(
442
448
  ...defaults,
443
449
  },
444
450
  });
451
+
445
452
  root.appendChild(line);
446
453
  return line;
447
454
  };
@@ -472,6 +479,8 @@ function hideRuleX(root) {
472
479
 
473
480
  function updateMarker(root, { data, style, theme }) {
474
481
  if (root.markers) root.markers.forEach((d) => d.remove());
482
+ const { type = '' } = style;
483
+
475
484
  const markers = data
476
485
  .filter((d) => {
477
486
  const [{ x, y }] = d;
@@ -479,19 +488,20 @@ function updateMarker(root, { data, style, theme }) {
479
488
  })
480
489
  .map((d) => {
481
490
  const [{ color, element }, point] = d;
482
- const fill =
491
+ const originColor =
483
492
  color || // encode value
484
493
  element.style.fill ||
485
494
  element.style.stroke ||
486
495
  theme.color;
487
-
496
+ const fill = type === 'hollow' ? 'transparent' : originColor;
497
+ const stroke = type === 'hollow' ? originColor : '#fff';
488
498
  const shape = new Circle({
489
499
  style: {
490
500
  cx: point[0],
491
501
  cy: point[1],
492
502
  fill,
493
503
  r: 4,
494
- stroke: '#fff',
504
+ stroke,
495
505
  lineWidth: 2,
496
506
  ...style,
497
507
  },
@@ -593,6 +603,12 @@ export function seriesTooltip(
593
603
  if (seriesX) seriesElements.push(element);
594
604
  else if (title || items) itemElements.push(element);
595
605
  }
606
+ const inInterval = (d) => d.markType === 'interval';
607
+ const isBar =
608
+ itemElements.length &&
609
+ itemElements.every(inInterval) &&
610
+ !isPolar(coordinate);
611
+ const xof = (d) => d.__data__.x;
596
612
 
597
613
  // For band scale x, find the closest series element to focus,
598
614
  // useful for interval + line mark.
@@ -612,14 +628,18 @@ export function seriesTooltip(
612
628
  const { min, max } = d.getLocalBounds();
613
629
  return sort([min[index], max[index]]);
614
630
  };
615
- // Sort itemElements by x or y.
616
- itemElements.sort((a, b) => {
617
- const [minA, maxA] = extent(a);
618
- const [minB, maxB] = extent(b);
619
- const midA = (minA + maxA) / 2;
620
- const midB = (minB + maxB) / 2;
621
- return transposed ? midB - midA : midA - midB;
622
- });
631
+
632
+ // Sort itemElements for bisector search.
633
+ if (isBar) elements.sort((a, b) => xof(a) - xof(b));
634
+ else {
635
+ itemElements.sort((a, b) => {
636
+ const [minA, maxA] = extent(a);
637
+ const [minB, maxB] = extent(b);
638
+ const midA = (minA + maxA) / 2;
639
+ const midB = (minB + maxB) / 2;
640
+ return transposed ? midB - midA : midA - midB;
641
+ });
642
+ }
623
643
 
624
644
  // Get sortedIndex and X for each series elements
625
645
  const elementSortedX = new Map(
@@ -642,8 +662,10 @@ export function seriesTooltip(
642
662
  return normalizedX - offsetX;
643
663
  };
644
664
 
645
- const indexByFocus = (focus, I, X) => {
646
- const finalX = abstractX(focus);
665
+ const indexByFocus = (event, focus, I, X) => {
666
+ // _x is from emit event, to find the right element.
667
+ const { _x } = event;
668
+ const finalX = _x !== undefined ? scaleX.map(_x) : abstractX(focus);
647
669
  const DX = X.filter(defined);
648
670
  const [minX, maxX] = sort([DX[0], DX[DX.length - 1]]);
649
671
  // If only has one element(minX == maxX), show tooltip when hover whole chart
@@ -658,24 +680,33 @@ export function seriesTooltip(
658
680
  return I[i];
659
681
  };
660
682
 
661
- const elementsByFocus = (focus, elements) => {
662
- const index = transposed ? 1 : 0;
663
- const x = focus[index];
664
- const filtered = elements.filter((element) => {
665
- const [min, max] = extent(element);
666
- return x >= min && x <= max;
667
- });
668
- // If closet is true, always find at least one element.
669
- if (!closest || filtered.length > 0) return filtered;
670
-
671
- // Search the closet element to the focus.
672
- const search = bisector((element) => {
673
- const [min, max] = extent(element);
674
- return (min + max) / 2;
675
- }).center;
676
- const i = search(elements, x);
677
- return [elements[i]].filter(defined);
678
- };
683
+ const elementsByFocus = isBar
684
+ ? (focus, elements) => {
685
+ const search = bisector(xof).center;
686
+ const i = search(elements, abstractX(focus));
687
+ const find = elements[i];
688
+ const groups = group(elements, xof);
689
+ const selected = groups.get(xof(find));
690
+ return selected;
691
+ }
692
+ : (focus, elements) => {
693
+ const index = transposed ? 1 : 0;
694
+ const x = focus[index];
695
+ const filtered = elements.filter((element) => {
696
+ const [min, max] = extent(element);
697
+ return x >= min && x <= max;
698
+ });
699
+ // If closet is true, always find at least one element.
700
+ if (!closest || filtered.length > 0) return filtered;
701
+
702
+ // Search the closet element to the focus.
703
+ const search = bisector((element) => {
704
+ const [min, max] = extent(element);
705
+ return (min + max) / 2;
706
+ }).center;
707
+ const i = search(elements, x);
708
+ return [elements[i]].filter(defined);
709
+ };
679
710
 
680
711
  const seriesData = (element, index) => {
681
712
  const { __data__: data } = element;
@@ -706,7 +737,7 @@ export function seriesTooltip(
706
737
  const selectedSeriesData = [];
707
738
  for (const element of seriesElements) {
708
739
  const [sortedIndex, X] = elementSortedX.get(element);
709
- const index = indexByFocus(focus, sortedIndex, X);
740
+ const index = indexByFocus(event, focus, sortedIndex, X);
710
741
  if (index !== null) {
711
742
  selectedSeriesElements.push(element);
712
743
  const d = seriesData(element, index);
@@ -825,10 +856,17 @@ export function seriesTooltip(
825
856
  });
826
857
  }
827
858
 
859
+ // X in focus may related multiple points when dataset is large,
860
+ // so we need to find the first x to show tooltip.
861
+ const firstX = filteredSeriesData[0]?.[0].x;
862
+ const transformedX = firstX ?? abstractX(focus);
863
+
828
864
  emitter.emit('tooltip:show', {
829
865
  ...event,
830
866
  nativeEvent: true,
831
- data: { data: { x: invert(scale.x, abstractX(focus), true) } },
867
+ data: {
868
+ data: { x: invert(scale.x, transformedX, true) },
869
+ },
832
870
  });
833
871
  },
834
872
  wait,
@@ -837,20 +875,10 @@ export function seriesTooltip(
837
875
 
838
876
  const hide = (event: MouseEvent) => {
839
877
  hideTooltip({ root, single, emitter, event });
840
- if (crosshairs) {
841
- hideRuleY(root);
842
- hideRuleX(root);
843
- }
844
- if (marker) hideMarker(root);
845
878
  };
846
879
 
847
880
  const destroy = () => {
848
881
  destroyTooltip({ root, single });
849
- if (crosshairs) {
850
- hideRuleY(root);
851
- hideRuleX(root);
852
- }
853
- if (marker) hideMarker(root);
854
882
  };
855
883
 
856
884
  const onTooltipShow = ({ nativeEvent, data }) => {
@@ -862,7 +890,8 @@ export function seriesTooltip(
862
890
  const {
863
891
  min: [minX, minY],
864
892
  } = root.getRenderBounds();
865
- update({ offsetX: x2 + minX, offsetY: y2 + minY });
893
+ // _x is a hint, to lookup for the right element if multiple element in the same abstractX.
894
+ update({ offsetX: x2 + minX, offsetY: y2 + minY, _x: x });
866
895
  };
867
896
 
868
897
  const onTooltipHide = () => {
@@ -882,7 +911,11 @@ export function seriesTooltip(
882
911
  if (!disableNative) {
883
912
  root.addEventListener('pointerenter', update);
884
913
  root.addEventListener('pointermove', update);
885
- root.addEventListener('pointerleave', hide);
914
+ // Only emit pointerleave event when the pointer is not in the root area.
915
+ root.addEventListener('pointerleave', (e) => {
916
+ if (mousePosition(root, e)) return;
917
+ hide(e);
918
+ });
886
919
  }
887
920
  };
888
921
 
@@ -434,5 +434,12 @@ export function create({
434
434
  subtitleFontWeight: 'normal',
435
435
  subtitleTextBaseline: 'top',
436
436
  },
437
+ tooltip: {
438
+ css: {
439
+ '.g2-tooltip': {
440
+ 'font-family': 'sans-serif',
441
+ },
442
+ },
443
+ },
437
444
  };
438
445
  }