Skip to content

Commit bb92113

Browse files
committed
Fix for stale data when hovering
1 parent eb0f963 commit bb92113

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
@@ -74,12 +74,12 @@ export function ChartLegendCompound({
7474
const currentTotal = useMemo((): number | null => {
7575
if (!activePayload?.length) return grandTotal;
7676

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

82-
// Filter to non-null values only
8383
const values = rawValues
8484
.filter((v): v is number => v != null)
8585
.map((v) => Number(v) || 0);
@@ -88,7 +88,6 @@ export function ChartLegendCompound({
8888
if (values.length === 0) return null;
8989

9090
if (!aggregation) {
91-
// Default: sum
9291
return values.reduce((a, b) => a + b, 0);
9392
}
9493
return aggregateValues(values, aggregation);
@@ -113,24 +112,24 @@ export function ChartLegendCompound({
113112
const currentData = useMemo((): Record<string, number | null> => {
114113
if (!activePayload?.length) return totals;
115114

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

128-
// Return a merged object - totals for keys not in the hover data
129128
return {
130129
...totals,
131130
...hoverData,
132131
};
133-
}, [activePayload, totals]);
132+
}, [activePayload, totals, dataKeys]);
134133

135134
// Prepare legend items with capped display
136135
const legendItems = useMemo(() => {

0 commit comments

Comments
 (0)