From 52417d301dc28f37100b2b48dd1b94039f5b29b5 Mon Sep 17 00:00:00 2001
From: Runtus <893119806@qq.com>
Date: Tue, 12 Dec 2023 13:43:29 +0800
Subject: [PATCH] fix: fix tooltip shown on one element line chart. (#5917)
* fix: fix the tooltip shown on one element line chart
* fix: change test examples
---
.../tooltip/one-element-line/step0.html | 46 +++++++++++++++++++
__tests__/plots/tooltip/index.ts | 1 +
__tests__/plots/tooltip/one-element-line.ts | 28 +++++++++++
src/interaction/tooltip.ts | 6 ++-
4 files changed, 80 insertions(+), 1 deletion(-)
create mode 100644 __tests__/integration/snapshots/tooltip/one-element-line/step0.html
create mode 100644 __tests__/plots/tooltip/one-element-line.ts
diff --git a/__tests__/integration/snapshots/tooltip/one-element-line/step0.html b/__tests__/integration/snapshots/tooltip/one-element-line/step0.html
new file mode 100644
index 0000000000..6970395bf5
--- /dev/null
+++ b/__tests__/integration/snapshots/tooltip/one-element-line/step0.html
@@ -0,0 +1,46 @@
+
\ No newline at end of file
diff --git a/__tests__/plots/tooltip/index.ts b/__tests__/plots/tooltip/index.ts
index 61f87a81ec..4c530d1acf 100644
--- a/__tests__/plots/tooltip/index.ts
+++ b/__tests__/plots/tooltip/index.ts
@@ -71,3 +71,4 @@ export { mockTooltipClosest } from './mock-tooltip-closest';
export { stateAgesIntervalScrollbar } from './stateages-interval-scrollbar';
export { aaplLineOverflow } from './aapl-line-overflow';
export { moviesIntervalScaleKeyScrollbar } from './movies-interval-scale-key-scrollbar';
+export { oneElementLine } from './one-element-line';
diff --git a/__tests__/plots/tooltip/one-element-line.ts b/__tests__/plots/tooltip/one-element-line.ts
new file mode 100644
index 0000000000..a68d5eff78
--- /dev/null
+++ b/__tests__/plots/tooltip/one-element-line.ts
@@ -0,0 +1,28 @@
+import { G2Spec } from '../../../src';
+import { seriesTooltipSteps } from './utils';
+
+export function oneElementLine(): G2Spec {
+ return {
+ width: 800,
+ type: 'line',
+ data: {
+ value: [
+ {
+ date: '2007-04-23T00:00:00.000Z',
+ close: 93.24,
+ },
+ ],
+ },
+ encode: {
+ x: 'date',
+ y: 'close',
+ size: 10,
+ },
+ tooltip: {
+ title: 'date',
+ items: ['close'],
+ },
+ };
+}
+
+oneElementLine.steps = seriesTooltipSteps([200, 200]);
diff --git a/src/interaction/tooltip.ts b/src/interaction/tooltip.ts
index be47d96f33..52f19ac6c9 100644
--- a/src/interaction/tooltip.ts
+++ b/src/interaction/tooltip.ts
@@ -530,9 +530,13 @@ export function seriesTooltip(
const finalX = abstractX(focus);
const DX = X.filter(defined);
const [minX, maxX] = sort([DX[0], DX[DX.length - 1]]);
+ // If only has one element(minX == maxX), show tooltip when hover whole chart
+ const isOnlyOneElement = minX === maxX;
+
// If closest is true, always find at least one element.
// Otherwise, skip element out of plot area.
- if (!closest && (finalX < minX || finalX > maxX)) return null;
+ if (!closest && (finalX < minX || finalX > maxX) && !isOnlyOneElement)
+ return null;
const search = bisector((i) => X[+i]).center;
const i = search(I, finalX);
return I[i];