Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tooltip): series enterable #6296

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions __tests__/integration/api-chart-emit-series-tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ describe('chart.emit', () => {
clear();

// chart.emit("tooltip:show", options) should show tooltip.
await expect(canvas).toMatchDOMSnapshot(dir, 'step0', {
expect(canvas).toMatchDOMSnapshot(dir, 'step0', {
fileFormat: 'html',
selector: '.g2-tooltip',
});

// chart.emit("tooltip:hide") should hide tooltip.
chart.emit('tooltip:hide');
await expect(canvas).toMatchDOMSnapshot(dir, 'step1', {
expect(canvas).toMatchDOMSnapshot(dir, 'step1', {
fileFormat: 'html',
selector: '.g2-tooltip',
});
Expand All @@ -51,7 +51,7 @@ describe('chart.emit', () => {
// chart.on("tooltip:hide") should be called when hiding tooltip.
const [tooltipHided, resolveHide] = createPromise();
chart.on('tooltip:hide', receiveExpectData(resolveHide, null));
dispatchPlotEvent(canvas, 'pointerleave');
dispatchPlotEvent(canvas, 'pointerleave', { offsetX: -20, offsetY: -20 });
await tooltipHided;
});

Expand Down
46 changes: 19 additions & 27 deletions __tests__/plots/tooltip/alphabet-interval-enterable.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
import { CustomEvent } from '@antv/g';
import { G2Spec, PLOT_CLASS_NAME } from '../../../src';
import { tooltipSteps } from './utils';
import { G2Spec } from '../../../src';

export function alphabetIntervalEnterable(): G2Spec {
return {
type: 'view',
children: [
{
type: 'interval',
padding: 0,
data: {
type: 'fetch',
value: 'data/alphabet.csv',
},
axis: false,
legend: false,
encode: {
x: 'letter',
y: 'frequency',
color: 'steelblue',
},
interaction: {
tooltip: {
enterable: true,
},
},
type: 'interval',
padding: 0,
data: {
type: 'fetch',
value: 'data/alphabet.csv',
},
axis: false,
legend: false,
encode: {
x: 'letter',
y: 'frequency',
color: 'steelblue',
},
interaction: {
tooltip: {
enterable: true,
},
],
},
};
}

// TOOD: this test case does not test the `enterable` feature.
alphabetIntervalEnterable.steps = tooltipSteps(0);
alphabetIntervalEnterable.skip = true;
26 changes: 26 additions & 0 deletions __tests__/plots/tooltip/alphabet-line-enterable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { G2Spec } from '../../../src';

export function alphabetLineEnterable(): G2Spec {
return {
type: 'line',
padding: 0,
data: {
type: 'fetch',
value: 'data/alphabet.csv',
},
axis: false,
legend: false,
encode: {
x: 'letter',
y: 'frequency',
color: 'steelblue',
},
interaction: {
tooltip: {
enterable: true,
},
},
};
}

alphabetLineEnterable.skip = true;
1 change: 1 addition & 0 deletions __tests__/plots/tooltip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ export { alphabetText } from './alphabet-text';
export { mockLineFlex } from './mock-line-flex';
export { mockTooltipClosestTransposed } from './mock-tooltip-closest-transposed';
export { mockDualBarShareTooltip } from './mock-dual-bar-share-tooltip';
export { alphabetLineEnterable } from './alphabet-line-enterable';
22 changes: 11 additions & 11 deletions src/interaction/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ function hideTooltip({
// Must be clientX, clientY.
tooltipElement.hide(event?.clientX, event?.clientY);
}
hideRuleY(root);
hideRuleX(root);
hideMarker(root);
}

function destroyTooltip({ root, single }) {
Expand All @@ -175,6 +178,9 @@ function destroyTooltip({ root, single }) {
tooltipElement.destroy();
parent.tooltipElement = undefined;
}
hideRuleY(root);
hideRuleX(root);
hideMarker(root);
}

function showUndefined(item) {
Expand Down Expand Up @@ -860,20 +866,10 @@ export function seriesTooltip(

const hide = (event: MouseEvent) => {
hideTooltip({ root, single, emitter, event });
if (crosshairs) {
hideRuleY(root);
hideRuleX(root);
}
if (marker) hideMarker(root);
};

const destroy = () => {
destroyTooltip({ root, single });
if (crosshairs) {
hideRuleY(root);
hideRuleX(root);
}
if (marker) hideMarker(root);
};

const onTooltipShow = ({ nativeEvent, data }) => {
Expand Down Expand Up @@ -905,7 +901,11 @@ export function seriesTooltip(
if (!disableNative) {
root.addEventListener('pointerenter', update);
root.addEventListener('pointermove', update);
root.addEventListener('pointerleave', hide);
// Only emit pointerleave event when the pointer is not in the root area.
root.addEventListener('pointerleave', (e) => {
if (mousePosition(root, e)) return;
hide(e);
});
}
};

Expand Down
Loading