Skip to content

Commit 2ceaf28

Browse files
committed
Fix for stale data when hovering
1 parent 91ddc8e commit 2ceaf28

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

apps/webapp/app/components/primitives/charts/ChartLegendCompound.tsx

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ export function ChartLegendCompound({
7777
const currentTotal = useMemo((): number | null => {
7878
if (!activePayload?.length) return grandTotal;
7979

80-
// Collect all series values from the hovered data point, preserving nulls
81-
const rawValues = activePayload
82-
.filter((item) => item.value !== undefined && dataKeys.includes(item.dataKey as string))
83-
.map((item) => item.value);
80+
// Use the full data row so the total covers ALL dataKeys, not just visibleSeries
81+
const dataRow = activePayload[0]?.payload;
82+
if (!dataRow) return grandTotal;
83+
84+
const rawValues = dataKeys.map((key) => dataRow[key]);
8485

85-
// Filter to non-null values only
8686
const values = rawValues
8787
.filter((v): v is number => v != null)
8888
.map((v) => Number(v) || 0);
@@ -91,7 +91,6 @@ export function ChartLegendCompound({
9191
if (values.length === 0) return null;
9292

9393
if (!aggregation) {
94-
// Default: sum
9594
return values.reduce((a, b) => a + b, 0);
9695
}
9796
return aggregateValues(values, aggregation);
@@ -116,24 +115,24 @@ export function ChartLegendCompound({
116115
const currentData = useMemo((): Record<string, number | null> => {
117116
if (!activePayload?.length) return totals;
118117

119-
// If we have activePayload data from hovering over a bar/line
120-
const hoverData = activePayload.reduce(
121-
(acc, item) => {
122-
if (item.dataKey && item.value !== undefined) {
123-
// Preserve null for gap-filled points instead of coercing to 0
124-
acc[item.dataKey] = item.value != null ? Number(item.value) || 0 : null;
125-
}
126-
return acc;
127-
},
128-
{} as Record<string, number | null>
129-
);
118+
// Use the full data row so ALL dataKeys are resolved from the hovered point,
119+
// not just the visibleSeries present in activePayload.
120+
const dataRow = activePayload[0]?.payload;
121+
if (!dataRow) return totals;
122+
123+
const hoverData: Record<string, number | null> = {};
124+
for (const key of dataKeys) {
125+
const value = dataRow[key];
126+
if (value !== undefined) {
127+
hoverData[key] = value != null ? Number(value) || 0 : null;
128+
}
129+
}
130130

131-
// Return a merged object - totals for keys not in the hover data
132131
return {
133132
...totals,
134133
...hoverData,
135134
};
136-
}, [activePayload, totals]);
135+
}, [activePayload, totals, dataKeys]);
137136

138137
// Prepare legend items with capped display
139138
const legendItems = useMemo(() => {

0 commit comments

Comments
 (0)