Skip to content

Commit 11fc976

Browse files
moayuisuda林舸
andauthored
fix: tooltip-shared with annotation (#7255)
* fix: tooltip-shared with annotation * fix: tooltip shared not work for series mark --------- Co-authored-by: 林舸 <anhaohui.ahh@antgroup.com>
1 parent 4d5c134 commit 11fc976

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/interaction/tooltip.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import { angle, sub, dist } from '../utils/vector';
1414
import { invert } from '../utils/scale';
1515
import { BBox } from '../runtime';
1616
import { CALLBACK_ITEM_SYMBOL } from '../runtime/transform';
17-
import { G2_CLASS_PREFIX, g2Selector } from '../component/constant';
17+
import {
18+
G2_CLASS_PREFIX,
19+
g2Selector,
20+
ANNOTATION_MARKS,
21+
} from '../component/constant';
1822
import {
1923
selectG2Elements,
2024
createXKey,
@@ -649,7 +653,10 @@ export function findSingleElement({
649653
shared,
650654
}): DisplayObject | undefined {
651655
const inInterval = (d) => d.markType === 'interval';
652-
const isBar = elements.every(inInterval) && !isPolar(coordinate);
656+
const inAnnotation = (d) => ANNOTATION_MARKS.includes(d.markType);
657+
const markEls = elements.filter((d) => !inAnnotation(d));
658+
const isBar =
659+
markEls.length > 0 && markEls.every(inInterval) && !isPolar(coordinate);
653660
const scaleX = scale.x;
654661
const isEqualWidth = equalBandWidth(scale);
655662
const scaleSeries = scale.series;
@@ -667,7 +674,7 @@ export function findSingleElement({
667674
: (d) => d.__data__.x + bandWidth / 2;
668675

669676
// Sort for bisector search.
670-
if (isBar) elements.sort((a, b) => xof(a) - xof(b));
677+
if (isBar) markEls.sort((a, b) => xof(a) - xof(b));
671678
const findElementByTarget = (event) => {
672679
const { target = last(elements) } = event;
673680
return maybeRoot(target, (node) => {
@@ -693,13 +700,13 @@ export function findSingleElement({
693700
const [abstractX] = coordinate.invert(mouse);
694701
const search = bisector(xof).center;
695702
const i = isEqualWidth
696-
? search(elements, abstractX)
703+
? search(markEls, abstractX)
697704
: findNearestElementIndex(scaleX, abstractX);
698-
const target = elements[i];
705+
const target = markEls[i];
699706

700707
if (!shared) {
701708
// For grouped bar chart without shared options.
702-
const isGrouped = elements.find(
709+
const isGrouped = markEls.find(
703710
(d) => d !== target && xof(d) === xof(target),
704711
);
705712
if (isGrouped) return findElementByTarget(event) || isGrouped;
@@ -751,6 +758,7 @@ export function findSeriesElement({
751758
const seriesElements = [];
752759
const itemElements = [];
753760
for (const element of elements) {
761+
if (ANNOTATION_MARKS.includes(element.markType)) continue;
754762
const { __data__: data } = element;
755763
const { seriesX, title, items } = data;
756764
if (seriesX) seriesElements.push(element);
@@ -783,7 +791,7 @@ export function findSeriesElement({
783791
};
784792

785793
// Sort itemElements for bisector search.
786-
if (isBar) elements.sort((a, b) => xof(a) - xof(b));
794+
if (isBar) itemElements.sort((a, b) => xof(a) - xof(b));
787795
else {
788796
itemElements.sort((a, b) => {
789797
const [minA, maxA] = extent(a);

0 commit comments

Comments
 (0)