Repository: antvis/G2 (2025-08-14T05:59:14Z)

Issue:
折线图通过监听事件chart.on('tooltip:show', (event) => {})回调返回的数据是上个日期，应该是当前选中的日期
### 问题描述

折线图通过监听事件chart.on('tooltip:show', (event) => {})回调返回的数据是上个日期，应该是当前选中的日期

### 重现链接

    chart.on('tooltip:show', (event) => {       const { x } = event?.data?.data || {}       console.log('tooltip:show x: ', x)     })

### 重现步骤

    chart.on('tooltip:show', (event) => {
      const { x } = event?.data?.data || {}
      console.log('tooltip:show x: ', x)
    })

### 预期行为

期望是当前选中的日期，但是回调返回的上个日期

### 平台

- 操作系统: [macOS, Windows, Linux, React Native ...]
- 网页浏览器: [Google Chrome, Safari, Firefox]


### 屏幕截图或视频（可选）

![企业微信截图_eb1af7ec-3c92-47b8-bba4-0de9a58ddecd](https://github.com/user-attachments/assets/3a974749-aead-4de7-8194-fdfcdf32a331)


### 补充说明（可选）

_No response_


Discussion:
nice issue.



Patch:
diff --git a/src/utils/scale.ts b/src/utils/scale.ts
index f2f46ab01e..fa4c11f4b2 100644
--- a/src/utils/scale.ts
+++ b/src/utils/scale.ts
@@ -11,6 +11,9 @@ export function isOrdinalScale(scale) {
 export function invert(scale, x, start) {
   if (!isOrdinalScale(scale)) return scale.invert(x);
   const { adjustedRange } = scale;
+  if (adjustedRange.includes(x)) {
+    return scale.invert(x);
+  }
   const { domain } = scale.getOptions();
   const offset = start ? -1 : 0;
   const step = scale.getStep();


Test patch:
diff --git a/__tests__/integration/api-chart-emit-tooltip-show.spec.ts b/__tests__/integration/api-chart-emit-tooltip-show.spec.ts
new file mode 100644
index 0000000000..810228557b
--- /dev/null
+++ b/__tests__/integration/api-chart-emit-tooltip-show.spec.ts
@@ -0,0 +1,25 @@
+import { chartEmitTooltipShow as render } from '../plots/api/chart-emit-tooltip-show';
+import { createNodeGCanvas } from './utils/createNodeGCanvas';
+
+describe('chart.emit tooltip with line', () => {
+  const canvas = createNodeGCanvas(800, 400);
+  const container = document.createElement('div');
+  const { finished, chart } = render({
+    canvas,
+    container,
+  });
+
+  it('chart.on("tooltip:show") should return expected data X.', async () => {
+    await finished;
+    const tooltipData: HTMLElement | null =
+      container.querySelector('.tooltip-content');
+
+    expect(tooltipData?.innerText).toEqual(
+      `{"title":"1997","items":[{"value":7,"color":"#1783FF","name":"value"}],"data":{"x":"1997"}}`,
+    );
+  });
+
+  afterAll(() => {
+    canvas?.destroy();
+  });
+});
diff --git a/__tests__/plots/api/chart-emit-tooltip-show.ts b/__tests__/plots/api/chart-emit-tooltip-show.ts
new file mode 100644
index 0000000000..b3f3e06a50
--- /dev/null
+++ b/__tests__/plots/api/chart-emit-tooltip-show.ts
@@ -0,0 +1,57 @@
+import { CustomEvent } from '@antv/g';
+import { Chart } from '../../../src';
+
+export function chartEmitTooltipShow(context) {
+  const { container, canvas } = context;
+
+  // wrapperDiv
+  const wrapperDiv = document.createElement('div');
+  container.appendChild(wrapperDiv);
+
+  // p
+  const p = document.createElement('p');
+  p.innerText = '';
+  p.className = 'tooltip-content';
+  container.appendChild(p);
+
+  const chart = new Chart({
+    container: wrapperDiv,
+    canvas,
+  });
+
+  chart
+    .line()
+    .data([
+      { year: '1991', value: 3 },
+      { year: '1992', value: 4 },
+      { year: '1993', value: 3.5 },
+      { year: '1994', value: 5 },
+      { year: '1995', value: 4.9 },
+      { year: '1996', value: 6 },
+      { year: '1997', value: 7 },
+      { year: '1998', value: 9 },
+      { year: '1999', value: 13 },
+    ])
+    .encode('x', 'year')
+    .encode('y', 'value');
+
+  const finished = chart.render();
+
+  finished.then((chart) => {
+    const { canvas: C } = chart.getContext();
+    const { document } = C as any;
+    const element = document.getElementsByClassName('plot')[0];
+    element.dispatchEvent(
+      new CustomEvent('pointermove', { offsetX: 500, offsetY: 300 }),
+    );
+  });
+
+  chart.on('tooltip:show', ({ data }) => {
+    p.innerText = JSON.stringify(data);
+  });
+
+  return {
+    chart,
+    finished,
+  };
+}
diff --git a/__tests__/plots/api/index.ts b/__tests__/plots/api/index.ts
index 44f4b399a4..0b9e52d691 100644
--- a/__tests__/plots/api/index.ts
+++ b/__tests__/plots/api/index.ts
@@ -61,3 +61,4 @@ export { chartEmitHeatmapTooltip } from './chart-emit-heatmap-tooltip';
 export { chartEmitItemTooltipWithFlex } from './chart-emit-item-tooltip-with-flex';
 export { chartEmitShowTooltipWithResize } from './chart-emit-show-tooltip-with-resize';
 export { chartEmitShowTooltipWithNull } from './chart-emit-show-tooltip-with-null';
+export { chartEmitTooltipShow } from './chart-emit-tooltip-show';