Skip to content

Commit 427bee8

Browse files
committed
feat: Shift HoverAnnotationDot to top when needed
1 parent 28690bd commit 427bee8

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

app/charts/shared/interaction/hover-annotation-dot.tsx

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AreasState } from "@/charts/area/areas-state";
22
import { StackedBarsState } from "@/charts/bar/bars-stacked-state";
33
import { StackedColumnsState } from "@/charts/column/columns-stacked-state";
4+
import { ColumnsState } from "@/charts/column/columns-state";
45
import { LinesState } from "@/charts/line/lines-state";
56
import { PieState } from "@/charts/pie/pie-state";
67
import { ScatterplotState } from "@/charts/scatterplot/scatterplot-state";
@@ -13,6 +14,7 @@ export const HoverAnnotationDot = () => {
1314
const [interaction] = useInteraction();
1415
const {
1516
chartType,
17+
segments,
1618
getTooltipInfo,
1719
bounds: { margins },
1820
getSegmentLabel,
@@ -22,6 +24,7 @@ export const HoverAnnotationDot = () => {
2224
| PieState
2325
| ScatterplotState
2426
| StackedBarsState
27+
| ColumnsState
2528
| StackedColumnsState;
2629

2730
if (
@@ -42,7 +45,13 @@ export const HoverAnnotationDot = () => {
4245
}
4346

4447
const value = values?.find((v) => v.label === segmentLabel) ?? datum;
45-
const { x, y } = getPosition({ chartType, xAnchor, yAnchor, value });
48+
const { x, y } = getPosition({
49+
chartType,
50+
shiftToTop: segments.length <= 1,
51+
xAnchor,
52+
yAnchor,
53+
value,
54+
});
4655

4756
if (!value || value.hide || x === undefined || y === undefined) {
4857
return null;
@@ -60,25 +69,41 @@ export const HoverAnnotationDot = () => {
6069

6170
const getPosition = ({
6271
chartType,
72+
shiftToTop,
6373
xAnchor,
6474
yAnchor,
6575
value,
6676
}: {
6777
chartType: "area" | "bar" | "column" | "line" | "pie" | "scatterplot";
78+
shiftToTop?: boolean;
6879
xAnchor: number;
6980
yAnchor: number | undefined;
7081
value: TooltipValue | undefined;
7182
}) => {
83+
const axisOffset = shiftToTop ? -16 : 0;
84+
7285
switch (chartType) {
7386
case "area":
7487
case "column":
75-
case "line":
76-
return { x: xAnchor, y: value?.axisOffset };
88+
case "line": {
89+
const y = value?.axisOffset ?? yAnchor;
90+
91+
return {
92+
x: xAnchor,
93+
y: y !== undefined ? y + axisOffset : y,
94+
};
95+
}
7796
case "bar":
78-
return { x: value?.axisOffset, y: yAnchor };
97+
return {
98+
x: (value?.axisOffset ?? xAnchor) + axisOffset,
99+
y: yAnchor,
100+
};
79101
case "pie":
80102
case "scatterplot":
81-
return { x: xAnchor, y: yAnchor };
103+
return {
104+
x: xAnchor,
105+
y: yAnchor,
106+
};
82107
default:
83108
const _exhaustiveCheck: never = chartType;
84109
return _exhaustiveCheck;

0 commit comments

Comments
 (0)