Skip to content

Commit bbde1cb

Browse files
committed
Legend hover improved
1 parent 00a8a2d commit bbde1cb

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

apps/webapp/app/components/code/QueryResultsChart.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ export const QueryResultsChart = memo(function QueryResultsChart({
841841
series={series}
842842
labelFormatter={legendLabelFormatter}
843843
showLegend={showLegend}
844-
maxLegendItems={8}
844+
maxLegendItems={6}
845845
minHeight="300px"
846846
>
847847
<Chart.Bar

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

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,25 +99,23 @@ export function ChartLegendCompound({
9999
}));
100100

101101
if (allItems.length <= maxItems) {
102-
return { visible: allItems, remaining: 0 };
102+
return { visible: allItems, remaining: 0, hoveredHiddenItem: undefined };
103103
}
104104

105105
const visibleItems = allItems.slice(0, maxItems);
106106
const remainingCount = allItems.length - maxItems;
107107

108108
// If we're hovering over an item that's not visible in the legend,
109-
// add it as an extra item instead of showing the "view more" row
109+
// pass it separately to replace the "view more" row
110+
let hoveredHiddenItem: (typeof allItems)[0] | undefined;
110111
if (
111112
highlight.activeBarKey &&
112113
!visibleItems.some((item) => item.dataKey === highlight.activeBarKey)
113114
) {
114-
const hoveredItem = allItems.find((item) => item.dataKey === highlight.activeBarKey);
115-
if (hoveredItem) {
116-
return { visible: [...visibleItems, hoveredItem], remaining: remainingCount - 1 };
117-
}
115+
hoveredHiddenItem = allItems.find((item) => item.dataKey === highlight.activeBarKey);
118116
}
119117

120-
return { visible: visibleItems, remaining: remainingCount };
118+
return { visible: visibleItems, remaining: remainingCount, hoveredHiddenItem };
121119
}, [config, dataKeys, maxItems, highlight.activeBarKey]);
122120

123121
if (hidden || dataKeys.length === 0) {
@@ -187,8 +185,17 @@ export function ChartLegendCompound({
187185
);
188186
})}
189187

190-
{/* View more row */}
191-
{legendItems.remaining > 0 && <ViewAllDataRow remainingCount={legendItems.remaining} />}
188+
{/* View more row - replaced by hovered hidden item when applicable */}
189+
{legendItems.remaining > 0 &&
190+
(legendItems.hoveredHiddenItem ? (
191+
<HoveredHiddenItemRow
192+
item={legendItems.hoveredHiddenItem}
193+
value={currentData[legendItems.hoveredHiddenItem.dataKey] ?? 0}
194+
remainingCount={legendItems.remaining - 1}
195+
/>
196+
) : (
197+
<ViewAllDataRow remainingCount={legendItems.remaining} />
198+
))}
192199
</div>
193200
);
194201
}
@@ -212,3 +219,38 @@ function ViewAllDataRow({ remainingCount }: ViewAllDataRowProps) {
212219
</Button>
213220
);
214221
}
222+
223+
type HoveredHiddenItemRowProps = {
224+
item: { dataKey: string; color?: string; label: React.ReactNode };
225+
value: number;
226+
remainingCount: number;
227+
};
228+
229+
function HoveredHiddenItemRow({ item, value, remainingCount }: HoveredHiddenItemRowProps) {
230+
return (
231+
<div className="relative flex w-full items-center justify-between gap-2 rounded px-2 py-1">
232+
{/* Active highlight background */}
233+
{item.color && (
234+
<div
235+
className="absolute inset-0 rounded opacity-10"
236+
style={{ backgroundColor: item.color }}
237+
/>
238+
)}
239+
<div className="relative flex w-full items-center justify-between gap-2">
240+
<div className="flex items-center gap-1.5">
241+
{item.color && (
242+
<div
243+
className="h-3 w-1 shrink-0 rounded-[2px]"
244+
style={{ backgroundColor: item.color }}
245+
/>
246+
)}
247+
<span className="text-text-bright">{item.label}</span>
248+
{remainingCount > 0 && <span className="text-text-dimmed">+{remainingCount} more</span>}
249+
</div>
250+
<span className="tabular-nums text-text-bright">
251+
<AnimatedNumber value={value} duration={0.25} />
252+
</span>
253+
</div>
254+
</div>
255+
);
256+
}

0 commit comments

Comments
 (0)