@antv/g2 5.1.21 → 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
  },
@@ -533,13 +543,6 @@ function hasSeries(markState): boolean {
533
543
  );
534
544
  }
535
545
 
536
- function findElement(node: DisplayObject) {
537
- return maybeRoot(node, (node) => {
538
- if (!node.classList) return false;
539
- return node.classList.includes('element');
540
- });
541
- }
542
-
543
546
  /**
544
547
  * Show tooltip for series item.
545
548
  */
@@ -600,8 +603,16 @@ export function seriesTooltip(
600
603
  if (seriesX) seriesElements.push(element);
601
604
  else if (title || items) itemElements.push(element);
602
605
  }
603
-
604
- const isBandScale = !!(transposed ? scale.y : scale.x).getBandWidth;
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;
612
+
613
+ // For band scale x, find the closest series element to focus,
614
+ // useful for interval + line mark.
615
+ const isBandScale = !!scale.x.getBandWidth;
605
616
  const closest = isBandScale && itemElements.length > 0;
606
617
 
607
618
  // Sorted elements from top to bottom visually,
@@ -617,14 +628,18 @@ export function seriesTooltip(
617
628
  const { min, max } = d.getLocalBounds();
618
629
  return sort([min[index], max[index]]);
619
630
  };
620
- // Sort itemElements by x or y.
621
- itemElements.sort((a, b) => {
622
- const [minA, maxA] = extent(a);
623
- const [minB, maxB] = extent(b);
624
- const midA = (minA + maxA) / 2;
625
- const midB = (minB + maxB) / 2;
626
- return transposed ? midB - midA : midA - midB;
627
- });
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
+ }
628
643
 
629
644
  // Get sortedIndex and X for each series elements
630
645
  const elementSortedX = new Map(
@@ -647,8 +662,10 @@ export function seriesTooltip(
647
662
  return normalizedX - offsetX;
648
663
  };
649
664
 
650
- const indexByFocus = (focus, I, X) => {
651
- 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);
652
669
  const DX = X.filter(defined);
653
670
  const [minX, maxX] = sort([DX[0], DX[DX.length - 1]]);
654
671
  // If only has one element(minX == maxX), show tooltip when hover whole chart
@@ -663,24 +680,33 @@ export function seriesTooltip(
663
680
  return I[i];
664
681
  };
665
682
 
666
- const elementsByFocus = (focus, elements) => {
667
- const index = transposed ? 1 : 0;
668
- const x = focus[index];
669
- const filtered = elements.filter((element) => {
670
- const [min, max] = extent(element);
671
- return x >= min && x <= max;
672
- });
673
- // If closet is true, always find at least one element.
674
- if (!closest || filtered.length > 0) return filtered;
675
-
676
- // Search the closet element to the focus.
677
- const search = bisector((element) => {
678
- const [min, max] = extent(element);
679
- return (min + max) / 2;
680
- }).center;
681
- const i = search(elements, x);
682
- return [elements[i]].filter(defined);
683
- };
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
+ };
684
710
 
685
711
  const seriesData = (element, index) => {
686
712
  const { __data__: data } = element;
@@ -711,7 +737,7 @@ export function seriesTooltip(
711
737
  const selectedSeriesData = [];
712
738
  for (const element of seriesElements) {
713
739
  const [sortedIndex, X] = elementSortedX.get(element);
714
- const index = indexByFocus(focus, sortedIndex, X);
740
+ const index = indexByFocus(event, focus, sortedIndex, X);
715
741
  if (index !== null) {
716
742
  selectedSeriesElements.push(element);
717
743
  const d = seriesData(element, index);
@@ -830,10 +856,17 @@ export function seriesTooltip(
830
856
  });
831
857
  }
832
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
+
833
864
  emitter.emit('tooltip:show', {
834
865
  ...event,
835
866
  nativeEvent: true,
836
- data: { data: { x: invert(scale.x, abstractX(focus), true) } },
867
+ data: {
868
+ data: { x: invert(scale.x, transformedX, true) },
869
+ },
837
870
  });
838
871
  },
839
872
  wait,
@@ -842,20 +875,10 @@ export function seriesTooltip(
842
875
 
843
876
  const hide = (event: MouseEvent) => {
844
877
  hideTooltip({ root, single, emitter, event });
845
- if (crosshairs) {
846
- hideRuleY(root);
847
- hideRuleX(root);
848
- }
849
- if (marker) hideMarker(root);
850
878
  };
851
879
 
852
880
  const destroy = () => {
853
881
  destroyTooltip({ root, single });
854
- if (crosshairs) {
855
- hideRuleY(root);
856
- hideRuleX(root);
857
- }
858
- if (marker) hideMarker(root);
859
882
  };
860
883
 
861
884
  const onTooltipShow = ({ nativeEvent, data }) => {
@@ -867,7 +890,8 @@ export function seriesTooltip(
867
890
  const {
868
891
  min: [minX, minY],
869
892
  } = root.getRenderBounds();
870
- 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 });
871
895
  };
872
896
 
873
897
  const onTooltipHide = () => {
@@ -887,7 +911,11 @@ export function seriesTooltip(
887
911
  if (!disableNative) {
888
912
  root.addEventListener('pointerenter', update);
889
913
  root.addEventListener('pointermove', update);
890
- 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
+ });
891
919
  }
892
920
  };
893
921
 
@@ -927,6 +955,7 @@ export function tooltip(
927
955
  root: DisplayObject,
928
956
  {
929
957
  elements: elementsof,
958
+ coordinate,
930
959
  scale,
931
960
  render,
932
961
  groupName,
@@ -954,11 +983,36 @@ export function tooltip(
954
983
  ) {
955
984
  const elements = elementsof(root);
956
985
  const keyGroup = group(elements, groupKey);
986
+ const inInterval = (d) => d.markType === 'interval';
987
+ const isBar = elements.every(inInterval) && !isPolar(coordinate);
988
+ const xof = (d) => d.__data__.x;
989
+ const scaleX = scale.x;
990
+
991
+ // Sort for bisector search.
992
+ if (isBar) elements.sort((a, b) => xof(a) - xof(b));
993
+
994
+ const findElement = isBar
995
+ ? (event) => {
996
+ const mouse = mousePosition(root, event);
997
+ if (!mouse) return;
998
+ const offsetX = scaleX?.getBandWidth ? scaleX.getBandWidth() / 2 : 0;
999
+ const [normalizedX] = coordinate.invert(mouse);
1000
+ const abstractX = normalizedX - offsetX;
1001
+ const search = bisector(xof).center;
1002
+ const i = search(elements, abstractX);
1003
+ return elements[i];
1004
+ }
1005
+ : (event) => {
1006
+ const { target } = event;
1007
+ return maybeRoot(target, (node) => {
1008
+ if (!node.classList) return false;
1009
+ return node.classList.includes('element');
1010
+ });
1011
+ };
957
1012
 
958
1013
  const pointermove = throttle(
959
1014
  (event) => {
960
- const { target } = event;
961
- const element = findElement(target);
1015
+ const element = findElement(event);
962
1016
  if (!element) {
963
1017
  hideTooltip({ root, single, emitter, event });
964
1018
  return;
@@ -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
  }