Skip to content

Commit 7737f97

Browse files
fix: handle the case where stepWidth is negative (#6758)
* fix: handle the case where stepWidth is negative * fix: add unit test
1 parent b92a81d commit 7737f97

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<div
2+
xmlns="http://www.w3.org/1999/xhtml"
3+
class="g2-tooltip"
4+
style="pointer-events: none; position: absolute; visibility: visible; z-index: 8; transition: visibility 0.2s cubic-bezier(0.23, 1, 0.32, 1), left 0.4s cubic-bezier(0.23, 1, 0.32, 1), top 0.4s cubic-bezier(0.23, 1, 0.32, 1); background-color: rgba(255, 255, 255, 0.96); box-shadow: 0 6px 12px 0 rgba(0, 0, 0, 0.12); border-radius: 4px; color: rgba(0, 0, 0, 0.65); font-size: 12px; line-height: 20px; padding: 12px; min-width: 120px; max-width: 360px; font-family: sans-serif; left: 242.39007350773517px; top: 432px;"
5+
>
6+
<div
7+
class="g2-tooltip-title"
8+
style="color: rgba(0, 0, 0, 0.45); overflow: hidden; white-space: nowrap; text-overflow: ellipsis;"
9+
>
10+
0
11+
</div>
12+
<ul
13+
class="g2-tooltip-list"
14+
style="margin: 0px; list-style-type: none; padding: 0px;"
15+
>
16+
<li
17+
class="g2-tooltip-list-item"
18+
data-index="0"
19+
style="list-style-type: none; display: flex; line-height: 2em; align-items: center; justify-content: space-between; white-space: nowrap;"
20+
>
21+
<span
22+
class="g2-tooltip-list-item-name"
23+
style="display: flex; align-items: center; max-width: 216px;"
24+
>
25+
<span
26+
class="g2-tooltip-list-item-marker"
27+
style="background: rgb(23, 131, 255); width: 8px; height: 8px; border-radius: 50%; display: inline-block; margin-right: 4px;"
28+
/>
29+
<span
30+
class="g2-tooltip-list-item-name-label"
31+
title="Male"
32+
style="flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;"
33+
>
34+
Male
35+
</span>
36+
</span>
37+
<span
38+
class="g2-tooltip-list-item-value"
39+
title="9735380"
40+
style="display: inline-block; float: right; flex: 1; text-align: right; min-width: 28px; margin-left: 30px; color: rgba(0, 0, 0, 0.85); overflow: hidden; white-space: nowrap; text-overflow: ellipsis;"
41+
>
42+
9735380
43+
</span>
44+
</li>
45+
</ul>
46+
</div>

__tests__/plots/tooltip/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,4 @@ export { itemsCallback } from './items-callback';
8484
export { issue6699 } from './issue-6699';
8585
export { issue6699Improve } from './issue-6699-improve';
8686
export { issue6710 } from './issue-6710';
87+
export { reverseScaleRange } from './reverse-scale-range';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { G2Spec } from '../../../src';
2+
import { tooltipSteps } from './utils';
3+
4+
export function reverseScaleRange(): G2Spec {
5+
return {
6+
type: 'interval',
7+
autoFit: true,
8+
data: {
9+
type: 'fetch',
10+
value: 'data/population.csv',
11+
transform: [{ type: 'filter', callback: (d) => d.year === 2000 }],
12+
},
13+
encode: {
14+
x: 'age',
15+
y: (d) => (d.sex === 1 ? -d.people : d.people),
16+
color: 'sex',
17+
},
18+
scale: { color: { type: 'ordinal' }, x: { range: [1, 0] } },
19+
coordinate: { transform: [{ type: 'transpose' }] },
20+
axis: { y: { labelFormatter: '~s' } },
21+
legend: { color: { labelFormatter: (d) => (d === 1 ? 'Male' : 'Female') } },
22+
tooltip: {
23+
items: [
24+
(d) => ({
25+
value: d.people,
26+
name: d.sex === 1 ? 'Male' : 'Female',
27+
}),
28+
],
29+
},
30+
};
31+
}
32+
33+
reverseScaleRange.steps = tooltipSteps(0);

src/interaction/tooltip.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -1064,10 +1064,18 @@ export function tooltip(
10641064
const i = search(elements, abstractX);
10651065
const target = elements[i];
10661066

1067-
const targetLeftBoundary = xof(target) - stepWidth / 2;
1068-
const targetRightBoundary = xof(target) + stepWidth / 2;
1069-
if (abstractX < targetLeftBoundary || abstractX > targetRightBoundary)
1067+
// Handle the case where stepWidth is negative.
1068+
const isStepWidthPositive = stepWidth > 0;
1069+
const targetLeftBoundary = isStepWidthPositive
1070+
? xof(target) - stepWidth / 2
1071+
: xof(target) + stepWidth / 2;
1072+
const targetRightBoundary = isStepWidthPositive
1073+
? xof(target) + stepWidth / 2
1074+
: xof(target) - stepWidth / 2;
1075+
1076+
if (abstractX < targetLeftBoundary || abstractX > targetRightBoundary) {
10701077
return;
1078+
}
10711079

10721080
if (!shared) {
10731081
// For grouped bar chart without shared options.

0 commit comments

Comments
 (0)