From 50c62e4a79c1f5e158736f74c64c72084011b49e Mon Sep 17 00:00:00 2001 From: lxfu1 <954055752@qq.com> Date: Tue, 8 Oct 2024 16:50:19 +0800 Subject: [PATCH] fix: tooptip position offset in dodgeX --- __tests__/plots/bugfix/index.ts | 1 + __tests__/plots/bugfix/issue-chart-2719.ts | 41 ++++++++++++++++++++++ src/interaction/tooltip.ts | 10 +++++- 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 __tests__/plots/bugfix/issue-chart-2719.ts diff --git a/__tests__/plots/bugfix/index.ts b/__tests__/plots/bugfix/index.ts index de95724240..ab37e63670 100644 --- a/__tests__/plots/bugfix/index.ts +++ b/__tests__/plots/bugfix/index.ts @@ -1,2 +1,3 @@ export { issue6396 } from './issue-6396'; export { issue6399 } from './issue-6399'; +export { issueChart2719 } from './issue-chart-2719'; diff --git a/__tests__/plots/bugfix/issue-chart-2719.ts b/__tests__/plots/bugfix/issue-chart-2719.ts new file mode 100644 index 0000000000..dbe9de2d13 --- /dev/null +++ b/__tests__/plots/bugfix/issue-chart-2719.ts @@ -0,0 +1,41 @@ +import { Chart } from '../../../src'; + +export function issueChart2719(context) { + const { container, canvas } = context; + const chart = new Chart({ + container: container, + canvas, + }); + + chart.options({ + type: 'interval', + data: { + type: 'fetch', + value: + 'https://gw.alipayobjects.com/os/antfincdn/iPY8JFnxdb/dodge-padding.json', + }, + encode: { + x: '月份', + y: '月均降雨量', + color: 'name', + }, + transform: [ + { + type: 'dodgeX', + padding: 0, + }, + ], + interaction: { + tooltip: { + shared: true, + }, + }, + }); + + const finished = chart.render(); + + return { + chart, + finished, + }; +} diff --git a/src/interaction/tooltip.ts b/src/interaction/tooltip.ts index c5666ce97b..64fa0b6dd7 100644 --- a/src/interaction/tooltip.ts +++ b/src/interaction/tooltip.ts @@ -1001,7 +1001,14 @@ export function tooltip( const scaleSeries = scale.series; const bandWidth = scaleX?.getBandWidth?.() ?? 0; const xof = scaleSeries - ? (d) => d.__data__.x + ? (d) => { + const seriesCount = Math.round(1 / scaleSeries.valueBandWidth); + return ( + d.__data__.x + + d.__data__.series * bandWidth + + bandWidth / (seriesCount * 2) + ); + } : (d) => d.__data__.x + bandWidth / 2; // Sort for bisector search. @@ -1023,6 +1030,7 @@ export function tooltip( const search = bisector(xof).center; const i = search(elements, abstractX); const target = elements[i]; + if (!shared) { // For grouped bar chart without shared options. const isGrouped = elements.find(