Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(ui): order tooltip items on dashboard by value desc (#2287)

Richard Krümmel6b7709f8e7a4
diff --git a/web/src/features/dashboard/components/BaseTimeSeriesChart.tsx b/web/src/features/dashboard/components/BaseTimeSeriesChart.tsx
index f8eac0e1ea..942f975fab 100644
--- a/web/src/features/dashboard/components/BaseTimeSeriesChart.tsx
+++ b/web/src/features/dashboard/components/BaseTimeSeriesChart.tsx
@@ -3,6 +3,10 @@ import { getColorsForCategories } from "@/src/features/dashboard/utils/getColors
import { compactNumberFormatter } from "@/src/utils/numbers";
import { cn } from "@/src/utils/tailwind";
import { AreaChart, LineChart } from "@tremor/react";
+import {
+ CurrencyTooltip,
+ NumberTooltip,
+} from "@/src/features/dashboard/components/Tooltip";
export type TimeSeriesChartDataPoint = {
ts: number;
@@ -58,6 +62,9 @@ export function BaseTimeSeriesChart(props: {
};
const ChartComponent = props.chartType === "area" ? AreaChart : LineChart;
+ const TooltipComponent = props.valueFormatter
+ ? CurrencyTooltip
+ : NumberTooltip;
const colors = getColorsForCategories(Array.from(labels));
return (
@@ -74,6 +81,7 @@ export function BaseTimeSeriesChart(props: {
showAnimation={true}
onValueChange={() => {}}
enableLegendSlider={true}
+ customTooltip={TooltipComponent}
/>
);
}
diff --git a/web/src/features/dashboard/components/TabsComponent.tsx b/web/src/features/dashboard/components/TabsComponent.tsx
index b5b66c8aa5..0a645fa8ef 100644
--- a/web/src/features/dashboard/components/TabsComponent.tsx
+++ b/web/src/features/dashboard/components/TabsComponent.tsx
@@ -21,7 +21,7 @@ export const TabComponent = ({ tabs }: TabComponentProps) => {
<select
id="tabs"
name="tabs"
- className="focus:border-primary-accent focus:ring-primary-accent block w-full rounded-md border-border py-2 pl-3 pr-10 text-base focus:outline-none sm:text-sm"
+ className="block w-full rounded-md border-border bg-background py-2 pl-3 pr-10 text-base focus:border-primary-accent focus:outline-none focus:ring-primary-accent sm:text-sm"
defaultValue={0}
onChange={(e) => setSelectedIndex(Number(e.target.selectedIndex))}
>
diff --git a/web/src/features/dashboard/components/Tooltip.tsx b/web/src/features/dashboard/components/Tooltip.tsx
new file mode 100644
index 0000000000..8ecbd02fbd
--- /dev/null
+++ b/web/src/features/dashboard/components/Tooltip.tsx
@@ -0,0 +1,71 @@
+import {
+ ChartTooltipFrame,
+ ChartTooltipRow,
+} from "@tremor/react/dist/components/chart-elements/common/ChartTooltip";
+import { compactNumberFormatter, usdFormatter } from "@/src/utils/numbers";
+import { cn } from "@/src/utils/tailwind";
+import { type CustomTooltipProps } from "@tremor/react";
+import { getRandomColor } from "@/src/features/dashboard/utils/getColorsForCategories";
+
+export const Tooltip = ({
+ payload,
+ active,
+ label,
+ formatter,
+}: CustomTooltipProps & { formatter: (value: number) => string }) => {
+ if (!active || !payload) return null;
+
+ // Filter out duplicates and sort by value in descending order
+ const uniquePayload = Array.from(
+ new Map(payload.map((category) => [category.name, category])).values(),
+ );
+
+ const sortedPayload = uniquePayload.sort(
+ (a, b) => (Number(b.value) ?? 0) - (Number(a.value) ?? 0),
+ );
+
+ return (
+ <ChartTooltipFrame>
+ <div
+ className={cn(
+ // light
+ "border-b border-tremor-border px-4 py-2",
+ // dark
+ "dark:border-dark-tremor-border",
+ )}
+ >
+ <p
+ className={cn(
+ // common
+ "font-medium",
+ // light
+ "text-tremor-content-emphasis",
+ // dark
+ "dark:text-dark-tremor-content-emphasis",
+ )}
+ >
+ {label}
+ </p>
+ </div>
+
+ <div className={cn("space-y-1 px-4 py-2")}>
+ {sortedPayload.map(({ name, value, color }, index) => (
+ <ChartTooltipRow
+ key={`${index}`}
+ value={formatter(Number(value))}
+ name={name?.toString() ?? ""}
+ color={color ?? getRandomColor()}
+ />
+ ))}
+ </div>
+ </ChartTooltipFrame>
+ );
+};
+
+export const NumberTooltip = (props: CustomTooltipProps) => (
+ <Tooltip {...props} formatter={compactNumberFormatter} />
+);
+
+export const CurrencyTooltip = (props: CustomTooltipProps) => (
+ <Tooltip {...props} formatter={usdFormatter} />
+);
diff --git a/web/src/features/dashboard/utils/getColorsForCategories.tsx b/web/src/features/dashboard/utils/getColorsForCategories.tsx
index c38af39241..e562e9d249 100644
--- a/web/src/features/dashboard/utils/getColorsForCategories.tsx
+++ b/web/src/features/dashboard/utils/getColorsForCategories.tsx
@@ -47,7 +47,7 @@ const predefinedColors: Color[] = [
"stone",
];
-function getRandomColor() {
+export function getRandomColor() {
return predefinedColors[Math.floor(Math.random() * predefinedColors.length)];
}