Skip to content

πŸͺ’ Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(experiments): replace the chart grid with one compact metric strip (#16914)

Nikita Kabardinb6eaa8113c18
diff --git a/web/src/components/metric-strip/MetricStripBand.tsx b/web/src/components/metric-strip/MetricStripBand.tsx
new file mode 100644
index 0000000000..d6c1b25604
--- /dev/null
+++ b/web/src/components/metric-strip/MetricStripBand.tsx
@@ -0,0 +1,114 @@
+import { forwardRef, type ReactNode } from "react";
+import { cn } from "@/src/utils/tailwind";
+
+/**
+ * The band chrome shared by every table strip ("Pulse"): the ruled band, the
+ * measured-width gate, and the loading / empty treatments. Extracted from
+ * `EventsOutlierStrip` so the events, scores and experiments
+ * strips are one band with one set of states instead of three lookalikes.
+ *
+ * What draws INSIDE the band stays with the caller β€” time-bucketed bars on the
+ * events and scores tables, one point per run on the experiments table β€” but
+ * every strip is the same height, ruled the same way, and says the same thing
+ * when it has nothing to show.
+ */
+
+/**
+ * Loading / empty height: they replace the header AND the plot, so they cover
+ * the band's whole content box. A strip whose ready content is taller passes
+ * its own, so the band does not resize the moment its data arrives.
+ */
+const DEFAULT_CONTENT_HEIGHT_CLASS = "h-[76px]";
+
+export type MetricStripStatus = "loading" | "empty" | "ready";
+
+/**
+ * A band-height message. Thin by construction: a strip's "nothing to show"
+ * must never be a box taller than the band, which clips its own text.
+ */
+export function MetricStripMessage({ message }: { message: string }) {
+ return (
+ <div className="text-muted-foreground flex h-full items-center justify-center text-[11px]">
+ {message}
+ </div>
+ );
+}
+
+/** The controls row above the plot: metric dropdown, then its annotations. */
+export function MetricStripHeaderRow({ children }: { children: ReactNode }) {
+ return <div className="flex items-baseline gap-1.5">{children}</div>;
+}
+
+export const MetricStripBand = forwardRef<
+ HTMLDivElement,
+ {
+ /**
+ * False until the wrapper has been measured. First paint renders nothing
+ * inside the band: strips that pick a bucket size from the measured width
+ * would otherwise flash a skeleton sized for nothing. Pass `true` when the
+ * plot needs no measurement.
+ */
+ measured?: boolean;
+ status: MetricStripStatus;
+ /** Shown at `status: "empty"`. */
+ emptyMessage?: string;
+ /**
+ * Height of the loading / empty box, for a strip whose ready content is
+ * taller than the default. Must equal that content's height, or the band β€”
+ * and the table under it β€” jumps when the data arrives.
+ */
+ contentHeightClass?: string;
+ /** Dim held-over data during a refetch: stale must not read as current. */
+ stale?: boolean;
+ /** Rendered above the plot at `status: "ready"` only. */
+ header?: ReactNode;
+ children?: ReactNode;
+ }
+>(
+ (
+ {
+ measured = true,
+ status,
+ emptyMessage = "No Data",
+ contentHeightClass = DEFAULT_CONTENT_HEIGHT_CLASS,
+ stale = false,
+ header,
+ children,
+ },
+ ref,
+ ) => (
+ // Ruled top and bottom: the strip reads as its own band
+ // instead of floating between the toolbar and the table header.
+ <div ref={ref} className="shrink-0 border-y">
+ {!measured ? null : (
+ // pt-2.5 keeps the metric switcher off the top rule; the label then
+ // sits closer to its chart than to the band's edge.
+ <div className="relative px-2 pt-2.5 pb-1">
+ {status === "loading" ? (
+ <div
+ className={cn(
+ "bg-muted animate-pulse rounded",
+ contentHeightClass,
+ )}
+ />
+ ) : status === "empty" ? (
+ <div className={contentHeightClass}>
+ <MetricStripMessage message={emptyMessage} />
+ </div>
+ ) : (
+ <div
+ className={cn(
+ "min-w-0 transition-opacity",
+ stale && "opacity-60",
+ )}
+ >
+ {header}
+ {children}
+ </div>
+ )}
+ </div>
+ )}
+ </div>
+ ),
+);
+MetricStripBand.displayName = "MetricStripBand";
diff --git a/web/src/components/metric-strip/MetricStripTrigger.tsx b/web/src/components/metric-strip/MetricStripTrigger.tsx
new file mode 100644
index 0000000000..f3574dc997
--- /dev/null
+++ b/web/src/components/metric-strip/MetricStripTrigger.tsx
@@ -0,0 +1,71 @@
+import { forwardRef, useRef } from "react";
+import type * as React from "react";
+import { ChevronDown } from "lucide-react";
+import { cn } from "@/src/utils/tailwind";
+
+/**
+ * The bare dropdown triggers in a strip's header row (see `MetricStripBand`):
+ * a bold metric name and, next to it, the aggregation it is drawn with. No
+ * button chrome β€” the band is thin, so the label IS the control.
+ */
+
+export type MetricStripTriggerVariant = "metric" | "aggregation";
+
+/** Shared so a strip whose menu is a different primitive still matches. */
+export const METRIC_STRIP_TRIGGER_CLASS =
+ "flex items-center gap-0.5 text-[13px] leading-none";
+
+export const metricStripTriggerClasses: Record<
+ MetricStripTriggerVariant,
+ string
+> = {
+ metric: "text-foreground hover:text-muted-foreground font-bold",
+ aggregation:
+ "text-muted-foreground hover:text-foreground underline-offset-2 hover:underline",
+};
+
+export const MetricStripTrigger = forwardRef<
+ HTMLButtonElement,
+ {
+ ariaLabel: string;
+ label: string;
+ variant: MetricStripTriggerVariant;
+ } & Omit<React.ComponentPropsWithoutRef<"button">, "aria-label" | "className">
+>(({ ariaLabel, label, variant, ...buttonProps }, ref) => (
+ <button
+ {...buttonProps}
+ ref={ref}
+ type="button"
+ aria-label={ariaLabel}
+ className={cn(
+ METRIC_STRIP_TRIGGER_CLASS,
+ metricStripTriggerClasses[variant],
+ )}
+ >
+ {label}
+ <ChevronDown className="h-2.5 w-2.5" />
+ </button>
+));
+MetricStripTrigger.displayName = "MetricStripTrigger";
+
+/**
+ * Prevent Radix's close-refocus ONLY after a pointer-driven selection β€” the
+ * programmatic refocus renders as a keyboard-style outline on the trigger.
+ * Escape / click-outside / keyboard selection keep the default focus return
+ * so keyboard users aren't dropped onto <body> (mirrors ChatMessages).
+ */
+export const usePointerSelectionFocusGuard = () => {
+ const selectedViaPointerRef = useRef(false);
+ return {
+ markPointerSelection: (event: { detail: number }) => {
+ // Keyboard-synthesized clicks carry detail 0; real pointer clicks β‰₯ 1.
+ if (event.detail > 0) selectedViaPointerRef.current = true;
+ },
+ onCloseAutoFocus: (event: Event) => {
+ if (selectedViaPointerRef.current) {
+ event.preventDefault();
+ selectedViaPointerRef.current = false;
+ }
+ },
+ };
+};
diff --git a/web/src/features/events/components/outlier-strip/EventsOutlierStrip.tsx b/web/src/features/events/components/outlier-strip/EventsOutlierStrip.tsx
index be595cda68..8b0a9380bb 100644
--- a/web/src/features/events/components/outlier-strip/EventsOutlierStrip.tsx
+++ b/web/src/features/events/components/outlier-strip/EventsOutlierStrip.tsx
@@ -1,10 +1,17 @@
-import { forwardRef, useMemo, useRef, useState } from "react";
+import { useMemo, useState } from "react";
import type * as React from "react";
-import { ChevronDown } from "lucide-react";
import { type FilterState, type QueryType } from "@langfuse/shared";
import { api } from "@/src/utils/api";
-import { cn } from "@/src/utils/tailwind";
import { useElementSize } from "@/src/hooks/useElementSize";
+import {
+ MetricStripBand,
+ MetricStripHeaderRow,
+ type MetricStripStatus,
+} from "@/src/components/metric-strip/MetricStripBand";
+import {
+ MetricStripTrigger,
+ usePointerSelectionFocusGuard,
+} from "@/src/components/metric-strip/MetricStripTrigger";
import {
DropdownMenuController,
DropdownMenuItem,
@@ -57,64 +64,6 @@ const MODE_OPTIONS: StripMode[] = ["count", "cost", "latency"];
const modeLabel = (mode: StripMode): string =>
OUTLIER_STRIP_METRICS[mode].shortLabel;
-type OutlierDropdownButtonVariant = "mode" | "aggregation";
-
-const outlierDropdownButtonClasses: Record<
- OutlierDropdownButtonVariant,
- string
-> = {
- mode: "text-foreground hover:text-muted-foreground font-bold",
- aggregation:
- "text-muted-foreground hover:text-foreground underline-offset-2 hover:underline",
-};
-
-const OutlierDropdownButton = forwardRef<
- HTMLButtonElement,
- {
- ariaLabel: string;
- label: string;
- variant: OutlierDropdownButtonVariant;
- } & Omit<React.ComponentPropsWithoutRef<"button">, "aria-label" | "className">
->(({ ariaLabel, label, variant, ...buttonProps }, ref) => (
- <button
- {...buttonProps}
- ref={ref}
- type="button"
- aria-label={ariaLabel}
- className={cn(
- "flex items-center gap-0.5 text-[13px] leading-none",
- outlierDropdownButtonClasses[variant],
- )}
- >
- {label}
- <ChevronDown className="h-2.5 w-2.5" />
- </button>
-));
-OutlierDropdownButton.displayName = "OutlierDropdownButton";
-
-/**
- * Prevent Radix's close-refocus ONLY after a pointer-driven selection β€” the
- * programmatic refocus renders as a keyboard-style outline on the trigger.
- * Escape / click-outside / keyboard selection keep the default focus return
- * so keyboard users aren't dropped onto <body> (mirrors ChatMessages,
- * LFE-6864).
- */
-const usePointerSelectionFocusGuard = () => {
- const selectedViaPointerRef = useRef(false);
- return {
- markPointerSelection: (event: { detail: number }) => {
- // Keyboard-synthesized clicks carry detail 0; real pointer clicks β‰₯ 1.
- if (event.detail > 0) selectedViaPointerRef.current = true;
- },
- onCloseAutoFocus: (event: Event) => {
- if (selectedViaPointerRef.current) {
- event.preventDefault();
- selectedViaPointerRef.current = false;
- }
- },
- };
-};
-
const AggDropdownController = ({
children,
options,
@@ -399,123 +348,99 @@ export function EventsOutlierStrip({
width > 0 &&
((queryResult.isPending && !queryResult.isError) || placeholderMissesGrid);
- // First paint renders nothing inside the measuring wrapper: the bucket size
- // needs the width, so painting before the first measurement would flash a
- // skeleton sized for nothing.
- return (
- // Ruled top and bottom (LFE-14829): the strip reads as its own band
- // instead of floating between the toolbar and the table header.
- <div ref={wrapperRef} className="shrink-0 border-y">
- {size === undefined ? null : (
- // pt-2.5 keeps the metric switcher off the top rule; the label then
- // sits closer to its chart (mt-1.5) than to the band's edge.
- <div className="relative px-2 pt-2.5 pb-1">
- {!canApplyFilters ? (
- <div>
- <div className="flex items-baseline gap-1.5">
- <ModeDropdownController
- options={MODE_OPTIONS}
- onChange={handleModeChange}
- >
- {({ Trigger }) => (
- <Trigger asChild>
- <OutlierDropdownButton
- ariaLabel={`Chart mode: ${modeLabel(mode)}`}
- label={modeLabel(mode)}
- variant="mode"
- />
- </Trigger>
- )}
- </ModeDropdownController>
- </div>
- <OutlierBarStrip
- className="mt-1.5"
- dense={[]}
- maxValue={0}
- ticks={[]}
- stepMs={stepMs}
- metric={mode}
- widthPx={chartWidth}
- disabledReason="Chart unavailable for the current filters"
- />
- </div>
- ) : isLoading || width === 0 ? (
- <div className="bg-muted h-[76px] animate-pulse rounded" />
- ) : queryResult.isError ? (
- <div className="text-muted-foreground flex h-[76px] items-center justify-center text-[11px]">
- No Data
- </div>
- ) : (
- <div
- // Dim held-over bins during a refetch (filter change, saved-view
- // switch, drill-in) β€” stale data must not read as current.
- className={cn(
- "min-w-0 transition-opacity",
- queryResult.isPlaceholderData &&
- queryResult.isFetching &&
- "opacity-60",
- )}
- >
- <div className="flex items-baseline gap-1.5">
- <ModeDropdownController
- options={MODE_OPTIONS}
- onChange={handleModeChange}
- >
- {({ Trigger }) => (
- <Trigger asChild>
- <OutlierDropdownButton
- ariaLabel={`Chart mode: ${modeLabel(mode)}`}
- label={modeLabel(mode)}
- variant="mode"
- />
- </Trigger>
- )}
- </ModeDropdownController>
- {/* The bar's aggregate must be legible where there is a
- choice (p95 vs avg); single-option metrics are
- unambiguous and render no aggregation label. */}
- {aggOptions.length > 1 && (
- <AggDropdownController
- options={aggOptions}
- onChange={(agg) => setAggregation(mode, agg)}
- >
- {({ Trigger }) => (
- <span className="flex items-baseline gap-1">
- <Trigger asChild>
- <OutlierDropdownButton
- ariaLabel={`${def.shortLabel} aggregation: ${aggregation}`}
- label={aggregation}
- variant="aggregation"
- />
- </Trigger>
- </span>
- )}
- </AggDropdownController>
- )}
- </div>
- <OutlierBarStrip
- className="mt-1.5"
- dense={series.dense}
- maxValue={series.maxValue}
- ticks={series.ticks}
- stepMs={stepMs}
- metric={mode}
- widthPx={chartWidth}
- onSelectBucket={handleSelectBucket}
- onPreviewPinned={(trigger) =>
- capture("pulse:preview_pinned", {
- trigger,
- metric: mode,
- isV4: true,
- })
- }
- selection={activeSelection}
- onSelectionChange={handleSelectionChange}
- />
- </div>
+ const status: MetricStripStatus = !canApplyFilters
+ ? "ready"
+ : isLoading || width === 0
+ ? "loading"
+ : queryResult.isError
+ ? "empty"
+ : "ready";
+
+ const header = (
+ <MetricStripHeaderRow>
+ <ModeDropdownController
+ options={MODE_OPTIONS}
+ onChange={handleModeChange}
+ >
+ {({ Trigger }) => (
+ <Trigger asChild>
+ <MetricStripTrigger
+ ariaLabel={`Chart mode: ${modeLabel(mode)}`}
+ label={modeLabel(mode)}
+ variant="metric"
+ />
+ </Trigger>
+ )}
+ </ModeDropdownController>
+ {/* The bar's aggregate must be legible where there is a choice (p95 vs
+ avg); single-option metrics are unambiguous and render no
+ aggregation label. A chart that cannot represent the filters is not
+ drawn with any aggregate, so it shows none. */}
+ {canApplyFilters && aggOptions.length > 1 && (
+ <AggDropdownController
+ options={aggOptions}
+ onChange={(agg) => setAggregation(mode, agg)}
+ >
+ {({ Trigger }) => (
+ <span className="flex items-baseline gap-1">
+ <Trigger asChild>
+ <MetricStripTrigger
+ ariaLabel={`${def.shortLabel} aggregation: ${aggregation}`}
+ label={aggregation}
+ variant="aggregation"
+ />
+ </Trigger>
+ </span>
)}
- </div>
+ </AggDropdownController>
+ )}
+ </MetricStripHeaderRow>
+ );
+
+ return (
+ <MetricStripBand
+ ref={wrapperRef}
+ // The bucket size needs the width, so painting before the first
+ // measurement would flash a skeleton sized for nothing.
+ measured={size !== undefined}
+ status={status}
+ // Dim held-over bins during a refetch (filter change, saved-view
+ // switch, drill-in) β€” stale data must not read as current.
+ stale={queryResult.isPlaceholderData && queryResult.isFetching}
+ header={header}
+ >
+ {!canApplyFilters ? (
+ <OutlierBarStrip
+ className="mt-1.5"
+ dense={[]}
+ maxValue={0}
+ ticks={[]}
+ stepMs={stepMs}
+ metric={mode}
+ widthPx={chartWidth}
+ disabledReason="Chart unavailable for the current filters"
+ />
+ ) : (
+ <OutlierBarStrip
+ className="mt-1.5"
+ dense={series.dense}
+ maxValue={series.maxValue}
+ ticks={series.ticks}
+ stepMs={stepMs}
+ metric={mode}
+ widthPx={chartWidth}
+ onSelectBucket={handleSelectBucket}
+ onPreviewPinned={(trigger) =>
+ capture("pulse:preview_pinned", {
+ trigger,
+ metric: mode,
+ isV4: true,
+ })
+ }
+ selection={activeSelection}
+ onSelectionChange={handleSelectionChange}
+ />
)}
- </div>
+ </MetricStripBand>
);
}
diff --git a/web/src/features/experiments/components/ExperimentChartSlot.tsx b/web/src/features/experiments/components/ExperimentChartSlot.tsx
deleted file mode 100644
index 8c6b651f50..0000000000
--- a/web/src/features/experiments/components/ExperimentChartSlot.tsx
+++ /dev/null
@@ -1,232 +0,0 @@
-import { useMemo } from "react";
-import { X } from "lucide-react";
-import {
- Select,
- SelectContent,
- SelectGroup,
- SelectItem,
- SelectLabel,
- SelectTrigger,
- SelectValue,
-} from "@/src/components/ui/select";
-import { Button } from "@/src/components/ui/button";
-import { WidgetContent } from "@/src/features/widgets/components/InlineWidget";
-import { type QueryType } from "@langfuse/shared/query";
-import type { MetricOption } from "../types/charts";
-import { Skeleton } from "@/src/components/ui/skeleton";
-import { buildWidgetConfigFromId } from "@/src/features/experiments/utils/charts";
-import { NoDataOrLoading } from "@/src/components/NoDataOrLoading";
-
-type ExperimentChartSlotProps = {
- chartIndex: number;
- selectedMetricId: string;
- onMetricChange: (metricId: string) => void;
- onRemove: () => void;
- canDelete: boolean;
- availableMetricOptions: MetricOption[];
- projectId: string;
- experiments: Array<{ id: string; name: string }>;
- fromTimestamp: Date;
- toTimestamp: Date;
- isExternalLoading?: boolean;
-};
-
-/**
- * A single chart slot with an embedded metric dropdown and hover-to-delete.
- */
-export function ExperimentChartSlot({
- chartIndex,
- selectedMetricId,
- onMetricChange,
- onRemove,
- canDelete,
- availableMetricOptions,
- projectId,
- experiments,
- fromTimestamp,
- toTimestamp,
- isExternalLoading = false,
-}: ExperimentChartSlotProps) {
- const { selectedMetricOption, widgetConfig } = useMemo(
- () => ({
- selectedMetricOption: availableMetricOptions.find(
- (opt) => opt.id === selectedMetricId,
- ),
- widgetConfig: buildWidgetConfigFromId(selectedMetricId),
- }),
- [availableMetricOptions, selectedMetricId],
- );
-
- const entityDimensionLabelMap = useMemo(
- () =>
- Object.fromEntries(
- experiments.map((experiment) => [experiment.id, experiment.name]),
- ),
- [experiments],
- );
-
- // Build query from widget config
- const query: QueryType | null = useMemo(() => {
- if (!widgetConfig) return null;
-
- const experimentIds = experiments.map((experiment) => experiment.id);
- const experimentNames = Array.from(
- new Set(
- experiments
- .map((experiment) => experiment.name)
- .filter((name) => name.length > 0),
- ),
- );
- const entityDimensionField = widgetConfig.entityDimension?.field;
-
- return {
- view: widgetConfig.view,
- dimensions: [...widgetConfig.dimensions],
- orderBy: widgetConfig.orderBy ? [...widgetConfig.orderBy] : null,
- timeDimension: widgetConfig.timeDimension,
- entityDimension: widgetConfig.entityDimension,
- metrics: widgetConfig.metrics.map((m) => ({
- measure: m.measure,
- aggregation: m.agg,
- })),
- filters: [
- ...(widgetConfig.filters ?? []),
- ...(entityDimensionField === "experimentName" &&
- experimentNames.length > 0
- ? [
- {
- column: "experimentName" as const,
- operator: "any of" as const,
- value: experimentNames,
- type: "stringOptions" as const,
- },
- {
- column: "experimentId" as const,
- operator: "any of" as const,
- value: experimentIds,
- type: "stringOptions" as const,
- },
- ]
- : []),
- ...(entityDimensionField === "datasetRunId"
- ? [
- {
- column: "datasetRunId" as const,
- operator: "any of" as const,
- value: experimentIds,
- type: "stringOptions" as const,
- },
- ]
- : []),
- ],
- fromTimestamp: fromTimestamp.toISOString(),
- toTimestamp: toTimestamp.toISOString(),
- };
- }, [widgetConfig, experiments, fromTimestamp, toTimestamp]);
-
- // Group options by their group for the dropdown
- const groupedOptions = useMemo(() => {
- const groups = new Map<MetricOption["group"], MetricOption[]>();
-
- for (const option of availableMetricOptions) {
- const existing = groups.get(option.group) ?? [];
- existing.push(option);
- groups.set(option.group, existing);
- }
-
- return groups;
- }, [availableMetricOptions]);
-
- // Get the selected option label for display
- // If not in available options, extract label from ID (for stale selections)
- const selectedLabel = useMemo(() => {
- if (selectedMetricOption) {
- return selectedMetricOption.label;
- }
- // Extract label from ID for stale selections
- if (selectedMetricId.startsWith("base:")) {
- return selectedMetricId === "base:cost" ? "Cost ($)" : "Latency (ms)";
- }
- // Score IDs like "obs-score-numeric:helpfulness" -> "helpfulness"
- const scoreName = selectedMetricId.split(":").pop();
- return scoreName ?? selectedMetricId;
- }, [selectedMetricOption, selectedMetricId]);
-
- // Check if metric is available for current experiments
- const isMetricAvailable = Boolean(selectedMetricOption);
-
- const isEnabled = experiments.length > 0;
-
- return (
- <div className="group relative">
- {/* Hover-to-delete button - fades in on hover */}
- {canDelete && (
- <Button
- variant="ghost"
- size="icon"
- onClick={onRemove}
- className="absolute top-1 right-1 z-10 h-6 w-6 opacity-0 transition-opacity duration-200 group-hover:opacity-100"
- >
- <X className="h-3.5 w-3.5" />
- </Button>
- )}
-
- {/* Header with dropdown */}
- <div className="flex items-center">
- <Select value={selectedMetricId} onValueChange={onMetricChange}>
- <SelectTrigger className="h-7 w-44 text-xs">
- <SelectValue placeholder="Select metric...">
- {selectedLabel}
- </SelectValue>
- </SelectTrigger>
- <SelectContent>
- {Array.from(groupedOptions.entries()).map(([group, options]) => (
- <SelectGroup key={group}>
- <SelectLabel className="text-xs font-bold">{group}</SelectLabel>
- {options.map((option) => (
- <SelectItem key={option.id} value={option.id}>
- {option.label}
- </SelectItem>
- ))}
- </SelectGroup>
- ))}
- </SelectContent>
- </Select>
- </div>
-
- {/* Chart content area */}
- <div className="mt-1 flex h-56 flex-col">
- {isExternalLoading ? (
- <Skeleton className="h-[190px] w-full" />
- ) : !isMetricAvailable || !isEnabled ? (
- <NoDataOrLoading isLoading={false} className="h-[190px]" />
- ) : widgetConfig && query ? (
- <WidgetContent
- projectId={projectId}
- query={query}
- version={widgetConfig.minVersion}
- chartType={widgetConfig.chartType}
- chartConfig={widgetConfig.chartConfig}
- metrics={[...widgetConfig.metrics]}
- dimensions={[...widgetConfig.dimensions]}
- view={widgetConfig.view}
- schedulerId={`chart-slot-${chartIndex}-${selectedMetricId}`}
- isExternalLoading={isExternalLoading}
- layoutHint="compact"
- entityDimensionLabelMap={entityDimensionLabelMap}
- // Experiment names are long and add clutter on the x-axis; show them
- // on hover instead. The axis is always an entity (experiment/run)
- // dimension here.
- hideXAxisLabels={Boolean(widgetConfig.entityDimension)}
- />
- ) : (
- <div className="flex h-full items-center justify-center rounded-lg border-2 border-dashed">
- <span className="text-muted-foreground text-sm">
- Select a metric
- </span>
- </div>
- )}
- </div>
- </div>
- );
-}
diff --git a/web/src/features/experiments/components/ExperimentChartsGrid.clienttest.tsx b/web/src/features/experiments/components/ExperimentChartsGrid.clienttest.tsx
deleted file mode 100644
index 2bdef6ec5c..0000000000
--- a/web/src/features/experiments/components/ExperimentChartsGrid.clienttest.tsx
+++ /dev/null
@@ -1,111 +0,0 @@
-import { fireEvent, render, screen } from "@testing-library/react";
-import { beforeEach, describe, expect, it, vi } from "vitest";
-import { ExperimentChartsGrid } from "./ExperimentChartsGrid";
-
-const h = vi.hoisted(() => ({
- capture: vi.fn(),
- updateChart: vi.fn(),
- charts: ["base:cost"],
-}));
-
-vi.mock("@/src/features/posthog-analytics/usePostHogClientCapture", () => ({
- usePostHogClientCapture: () => h.capture,
-}));
-
-vi.mock(
- "@/src/features/experiments/hooks/useExperimentChartsGridSelection",
- () => ({
- useExperimentChartsGridSelection: () => ({
- charts: h.charts,
- updateChart: h.updateChart,
- addChart: vi.fn(),
- removeChart: vi.fn(),
- canAddChart: false,
- canDeleteChart: false,
- availableMetricOptions: [
- { id: "base:cost", label: "Cost", group: "base" },
- {
- id: "obs-score-numeric:helpfulness",
- label: "helpfulness",
- group: "score",
- },
- ],
- isLoading: false,
- }),
- }),
-);
-
-vi.mock("@/src/features/experiments/components/ExperimentChartSlot", () => ({
- ExperimentChartSlot: ({
- selectedMetricId,
- onMetricChange,
- }: {
- selectedMetricId: string;
- onMetricChange: (id: string) => void;
- }) => (
- <button
- type="button"
- onClick={() => onMetricChange("obs-score-numeric:helpfulness")}
- >
- change-{selectedMetricId}
- </button>
- ),
-}));
-
-describe("ExperimentChartsGrid analytics", () => {
- beforeEach(() => {
- h.capture.mockClear();
- h.updateChart.mockClear();
- h.charts = ["base:cost"];
- });
-
- it("captures chart_metric_changed with metricGroup, not the score name", () => {
- render(
- <ExperimentChartsGrid
- projectId="p1"
- experiments={[{ id: "exp-a", name: "My Experiment A" }]}
- fromTimestamp={new Date("2026-01-01")}
- toTimestamp={new Date("2026-01-02")}
- />,
- );
-
- fireEvent.click(screen.getByRole("button", { name: "change-base:cost" }));
-
- expect(h.capture).toHaveBeenCalledTimes(1);
- expect(h.capture).toHaveBeenCalledWith("experiment:chart_metric_changed", {
- isV4: true,
- tableName: "experiments",
- metricGroup: "score",
- chartIndex: 0,
- slotCount: 1,
- });
- expect(JSON.stringify(h.capture.mock.calls[0][1])).not.toMatch(
- /helpfulness|My Experiment/,
- );
- expect(h.updateChart).toHaveBeenCalledWith(
- 0,
- "obs-score-numeric:helpfulness",
- );
- });
-
- it("does not capture when the metric is unchanged", () => {
- h.charts = ["obs-score-numeric:helpfulness"];
- render(
- <ExperimentChartsGrid
- projectId="p1"
- experiments={[{ id: "exp-a", name: "My Experiment A" }]}
- fromTimestamp={new Date("2026-01-01")}
- toTimestamp={new Date("2026-01-02")}
- />,
- );
-
- fireEvent.click(
- screen.getByRole("button", {
- name: "change-obs-score-numeric:helpfulness",
- }),
- );
-
- expect(h.capture).not.toHaveBeenCalled();
- expect(h.updateChart).not.toHaveBeenCalled();
- });
-});
diff --git a/web/src/features/experiments/components/ExperimentChartsGrid.tsx b/web/src/features/experiments/components/ExperimentChartsGrid.tsx
deleted file mode 100644
index 69e009e799..0000000000
--- a/web/src/features/experiments/components/ExperimentChartsGrid.tsx
+++ /dev/null
@@ -1,127 +0,0 @@
-import { Plus } from "lucide-react";
-import { useMemo } from "react";
-import { ExperimentChartSlot } from "./ExperimentChartSlot";
-import { useExperimentChartsGridSelection } from "../hooks/useExperimentChartsGridSelection";
-import { usePostHogClientCapture } from "@/src/features/posthog-analytics";
-import { chartMetricChangedProps } from "@/src/features/experiments/lib/analytics";
-
-type ExperimentChartsGridProps = {
- projectId: string;
- experiments: Array<{ id: string; name: string }>;
- fromTimestamp: Date;
- toTimestamp: Date;
- isExternalLoading?: boolean;
-};
-
-/**
- * Compact Add Button - small inline button to add a new chart.
- */
-function AddChartButton({ onClick }: { onClick: () => void }) {
- return (
- <button
- onClick={onClick}
- className="border-muted-foreground/25 hover:border-muted-foreground/50 hover:bg-muted/50 flex h-56 w-12 cursor-pointer flex-col items-center justify-center rounded-lg border-2 border-dashed transition-colors duration-200"
- >
- <Plus className="text-muted-foreground/50 h-5 w-5" />
- </button>
- );
-}
-
-/**
- * Get grid template columns based on chart count.
- * Uses responsive layouts with min 300px per chart.
- * - 4 charts: xl+ for 1x4, md+ for 2x2 (always wraps in pairs, never 3+1)
- * - 3 charts + add: lg+ for full row, md for 2-col wrap
- * - 2 charts + add: md+ for full row
- * - 1 chart + add: sm+ for full row
- */
-function getGridClass(chartCount: number): string {
- if (chartCount === 4) {
- // 4 charts: xl+ (1280px) for 1x4, md+ for 2x2, mobile stacked
- // Uses explicit breakpoints to ensure charts wrap in pairs (never 3+1)
- return "grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-4";
- }
- if (chartCount === 3) {
- // 3 charts + add button: lg+ (1024px) for full row, md for 2-col
- return "grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-[1fr_1fr_1fr_auto]";
- }
- if (chartCount === 2) {
- // 2 charts + add button: md+ (768px) for full row
- return "grid grid-cols-1 gap-4 md:grid-cols-[1fr_1fr_auto]";
- }
- // 1 chart + add button: sm+ (640px) for full row
- return "grid grid-cols-1 gap-4 sm:grid-cols-[1fr_auto]";
-}
-
-/**
- * A responsive grid for experiment charts with dynamic add/remove.
- * - Max 4 charts
- * - Layout: adapts based on chart count
- * - Compact add button inline at the end
- * - Hover-to-delete on each chart
- */
-export function ExperimentChartsGrid({
- projectId,
- experiments,
- fromTimestamp,
- toTimestamp,
- isExternalLoading = false,
-}: ExperimentChartsGridProps) {
- const capture = usePostHogClientCapture();
- const experimentIds = useMemo(
- () => experiments.map((experiment) => experiment.id),
- [experiments],
- );
- const {
- charts,
- updateChart,
- addChart,
- removeChart,
- canAddChart,
- canDeleteChart,
- availableMetricOptions,
- isLoading,
- } = useExperimentChartsGridSelection({
- projectId,
- experimentIds,
- });
-
- const gridClass = getGridClass(charts.length);
-
- return (
- <div className={gridClass}>
- {/* Active chart slots */}
- {charts.map((metricId, index) => (
- <ExperimentChartSlot
- key={`${index}-${metricId}`}
- chartIndex={index}
- selectedMetricId={metricId}
- onMetricChange={(newMetricId) => {
- if (newMetricId === metricId) return;
- capture(
- "experiment:chart_metric_changed",
- chartMetricChangedProps({
- tableName: "experiments",
- metricId: newMetricId,
- chartIndex: index,
- slotCount: charts.length,
- }),
- );
- updateChart(index, newMetricId);
- }}
- onRemove={() => removeChart(index)}
- canDelete={canDeleteChart}
- availableMetricOptions={availableMetricOptions}
- projectId={projectId}
- experiments={experiments}
- fromTimestamp={fromTimestamp}
- toTimestamp={toTimestamp}
- isExternalLoading={isExternalLoading || isLoading}
- />
- ))}
-
- {/* Compact Add Button - inline at the end */}
- {canAddChart && <AddChartButton onClick={addChart} />}
- </div>
- );
-}
diff --git a/web/src/features/experiments/components/ExperimentMetricStrip.tsx b/web/src/features/experiments/components/ExperimentMetricStrip.tsx
new file mode 100644
index 0000000000..fc5c05eb79
--- /dev/null
+++ b/web/src/features/experiments/components/ExperimentMetricStrip.tsx
@@ -0,0 +1,371 @@
+import { useMemo } from "react";
+import { ChevronDown } from "lucide-react";
+import {
+ Select,
+ SelectContent,
+ SelectGroup,
+ SelectItem,
+ SelectLabel,
+ SelectTrigger,
+ SelectValue,
+} from "@/src/components/ui/select";
+import DocPopup from "@/src/components/layouts/doc-popup";
+import { ScoreTag, SCORE_LEVEL_LABELS } from "@/src/components/score-tag";
+import { WidgetContent } from "@/src/features/widgets/components/InlineWidget";
+import { type QueryType } from "@langfuse/shared/query";
+import type {
+ MetricOption,
+ ScoreCoverageByLevel,
+} from "@/src/features/experiments/types/charts";
+import { buildWidgetConfigFromId } from "@/src/features/experiments/utils/charts";
+import { SCORE_LEVEL_TAGS } from "@/src/features/experiments/constants/charts";
+import { cn } from "@/src/utils/tailwind";
+import {
+ MetricStripBand,
+ MetricStripHeaderRow,
+ MetricStripMessage,
+ type MetricStripStatus,
+} from "@/src/components/metric-strip/MetricStripBand";
+import {
+ METRIC_STRIP_TRIGGER_CLASS,
+ metricStripTriggerClasses,
+} from "@/src/components/metric-strip/MetricStripTrigger";
+import { useExperimentStripMetric } from "@/src/features/experiments/hooks/useExperimentStripMetric";
+
+/**
+ * Stable node: `Chart` is memoized, so a fresh element on every render would
+ * defeat it. Says which half is missing β€” the metric, not the experiments.
+ */
+const EMPTY_PLOT = <MetricStripMessage message="No values for this metric" />;
+
+/**
+ * The band's shared plot height plus one 20px row for the bar legend beneath
+ * it. Constant, because the chart always draws exactly one row there β€” the
+ * per-experiment legend, or the "hover a bar" note when there are more
+ * experiments than the palette can tell apart β€” so the plot itself stays the
+ * band's 63px and still lines up with the events and scores strips instead of
+ * being squeezed to make room.
+ */
+const PLOT_WITH_LEGEND_HEIGHT_CLASS = "h-[83px]";
+
+/**
+ * What this strip's ready content occupies, for the band's loading and empty
+ * box: the 13px header row (a `leading-none` 13px trigger), the 6px gap under
+ * it, and the plot with its legend. Taller than the band's default, and a
+ * placeholder of the wrong height drops the table by the difference the moment
+ * the data arrives.
+ */
+const CONTENT_HEIGHT_CLASS = "h-[102px]";
+
+const AXIS_EXPLANATION =
+ "One bar per experiment in view, oldest on the left and newest on the right, so a metric that improved over time climbs. The bars are a set of runs in start order, not a timeline β€” nothing is implied between two of them. The table below stays newest-first; filtering it changes which experiments are plotted, not their left-to-right order. Experiments with no value for this metric are left out. The legend below names each bar; past eight experiments the chart palette would give two bars the same colour, so the bars go one colour and hovering one names it. Which metric opens by default is data-driven: the numeric score recorded on the most items across these experiments, with ties settled by name, falling back to cost only when none of them carry a score. Pick any metric from the dropdown and that choice is kept instead.";
+
+/**
+ * Which columns carry the experiment, keyed by the chart's entity dimension.
+ * A score's level decides this: an observation-level score reads the experiment
+ * off its observation, a trace-level score off the scored trace's root event,
+ * and a run-level score is keyed by dataset run id alone.
+ */
+const EXPERIMENT_SCOPE_COLUMNS: Record<
+ string,
+ { nameColumn?: string; idColumn: string }
+> = {
+ experimentName: { nameColumn: "experimentName", idColumn: "experimentId" },
+ datasetRunId: { idColumn: "datasetRunId" },
+};
+
+const buildExperimentScopeFilters = ({
+ entityDimensionField,
+ experimentNames,
+ experimentIds,
+}: {
+ entityDimensionField?: string;
+ experimentNames: string[];
+ experimentIds: string[];
+}) => {
+ const scope = entityDimensionField
+ ? EXPERIMENT_SCOPE_COLUMNS[entityDimensionField]
+ : undefined;
+ if (!scope) return [];
+
+ return [
+ ...(scope.nameColumn && experimentNames.length > 0
+ ? [
+ {
+ column: scope.nameColumn,
+ operator: "any of" as const,
+ value: experimentNames,
+ type: "stringOptions" as const,
+ },
+ ]
+ : []),
+ {
+ column: scope.idColumn,
+ operator: "any of" as const,
+ value: experimentIds,
+ type: "stringOptions" as const,
+ },
+ ];
+};
+
+type ExperimentMetricStripProps = {
+ projectId: string;
+ /** The experiments in view, in any order; the strip re-orders by start time. */
+ experiments: Array<{ id: string; name: string; startTime: Date }>;
+ fromTimestamp: Date;
+ toTimestamp: Date;
+ isExternalLoading?: boolean;
+ /**
+ * Values recorded per score name across the experiments in view, from the
+ * table's own rows. Decides the default metric (`pickDefaultStripMetric`);
+ * undefined until the row metrics land.
+ */
+ scoreCoverage?: ScoreCoverageByLevel;
+};
+
+/**
+ * The compact metric strip above the experiments table β€” one chart in the band
+ * the 4Γ—224px chart grid used to occupy, modelled on the events table's
+ * outlier strip. It plots one metric across the experiments in view, defaulting
+ * to the best-recorded numeric score rather than cost, as one bar per
+ * experiment on a chronological
+ * x-axis (oldest left) so an improving metric climbs. The axis is a set of
+ * discrete runs, not a timeline β€” hence bars, not a line. Long experiment names
+ * stay off the axis; the legend and the hover tooltip carry identity.
+ */
+export function ExperimentMetricStrip({
+ projectId,
+ experiments,
+ fromTimestamp,
+ toTimestamp,
+ isExternalLoading = false,
+ scoreCoverage,
+}: ExperimentMetricStripProps) {
+ // Chronological, oldest first: an improving metric has to read left-to-right.
+ // The table stays newest-first, so this is deliberately not the table order.
+ const orderedExperiments = useMemo(
+ () =>
+ [...experiments].sort(
+ (a, b) => a.startTime.getTime() - b.startTime.getTime(),
+ ),
+ [experiments],
+ );
+
+ const experimentIds = useMemo(
+ () => orderedExperiments.map((experiment) => experiment.id),
+ [orderedExperiments],
+ );
+
+ const { metricId, setMetricId, availableMetricOptions, isLoading } =
+ useExperimentStripMetric({ projectId, experimentIds, scoreCoverage });
+
+ const handleMetricChange = (newMetricId: string) => {
+ if (newMetricId === metricId) return;
+ setMetricId(newMetricId);
+ };
+
+ const { selectedMetricOption, widgetConfig } = useMemo(
+ () => ({
+ selectedMetricOption: availableMetricOptions.find(
+ (option) => option.id === metricId,
+ ),
+ widgetConfig: buildWidgetConfigFromId(metricId),
+ }),
+ [availableMetricOptions, metricId],
+ );
+
+ // Presentation-only names for the entity axis; their insertion order is what
+ // InlineWidget orders the x-axis by, so it carries the chronological order.
+ const entityDimensionLabelMap = useMemo(
+ () =>
+ Object.fromEntries(
+ orderedExperiments.map((experiment) => [
+ experiment.id,
+ experiment.name,
+ ]),
+ ),
+ [orderedExperiments],
+ );
+
+ const query: QueryType | null = useMemo(() => {
+ if (!widgetConfig) return null;
+
+ const experimentNames = Array.from(
+ new Set(
+ orderedExperiments
+ .map((experiment) => experiment.name)
+ .filter((name) => name.length > 0),
+ ),
+ );
+ const entityDimensionField = widgetConfig.entityDimension?.field;
+
+ return {
+ view: widgetConfig.view,
+ dimensions: [...widgetConfig.dimensions],
+ orderBy: widgetConfig.orderBy ? [...widgetConfig.orderBy] : null,
+ timeDimension: widgetConfig.timeDimension,
+ entityDimension: widgetConfig.entityDimension,
+ metrics: widgetConfig.metrics.map((m) => ({
+ measure: m.measure,
+ aggregation: m.agg,
+ })),
+ filters: [
+ ...(widgetConfig.filters ?? []),
+ ...buildExperimentScopeFilters({
+ entityDimensionField,
+ experimentNames,
+ experimentIds,
+ }),
+ ],
+ fromTimestamp: fromTimestamp.toISOString(),
+ toTimestamp: toTimestamp.toISOString(),
+ };
+ }, [
+ widgetConfig,
+ orderedExperiments,
+ experimentIds,
+ fromTimestamp,
+ toTimestamp,
+ ]);
+
+ const groupedOptions = useMemo(() => {
+ const groups = new Map<MetricOption["group"], MetricOption[]>();
+
+ for (const option of availableMetricOptions) {
+ const existing = groups.get(option.group) ?? [];
+ existing.push(option);
+ groups.set(option.group, existing);
+ }
+
+ return groups;
+ }, [availableMetricOptions]);
+
+ // A stored metric can name a score the experiments in view don't carry; show
+ // its name rather than a blank trigger.
+ const selectedLabel =
+ selectedMetricOption?.label ?? metricId.split(":").pop() ?? metricId;
+
+ // Every dropdown row is tagged with its level; the header only needs the tag
+ // when the name alone doesn't say which series is drawn β€” the same score name
+ // can exist at two levels and the chart plots exactly one of them. A
+ // selected name that is unique reads plain, as in the tracing tables'
+ // filter picker.
+ const selectedLevel = selectedMetricOption?.level;
+ const isSelectedNameAmbiguous =
+ Boolean(selectedLevel) &&
+ availableMetricOptions.filter(
+ (option) => option.label === selectedMetricOption?.label,
+ ).length > 1;
+
+ const isChartEnabled =
+ Boolean(selectedMetricOption) && orderedExperiments.length > 0;
+ const status: MetricStripStatus =
+ isExternalLoading || isLoading
+ ? "loading"
+ : isChartEnabled
+ ? "ready"
+ : "empty";
+
+ return (
+ <MetricStripBand
+ status={status}
+ contentHeightClass={CONTENT_HEIGHT_CLASS}
+ // The band's own voice, in place of a 144px dashed card clipped by a
+ // 63px band: say which half is missing.
+ emptyMessage="No experiments in view"
+ header={
+ <MetricStripHeaderRow>
+ <Select value={metricId} onValueChange={handleMetricChange}>
+ <SelectTrigger
+ aria-label={
+ selectedLevel
+ ? `Chart metric: ${selectedLabel} (${SCORE_LEVEL_LABELS[SCORE_LEVEL_TAGS[selectedLevel]]})`
+ : `Chart metric: ${selectedLabel}`
+ }
+ // The band's own bold trigger (`MetricStripTrigger`), on a
+ // Select rather than a DropdownMenu: this list is long (every
+ // score name, at every level), so it needs the scrolling and
+ // type-to-find a Select brings.
+ className={cn(
+ METRIC_STRIP_TRIGGER_CLASS,
+ metricStripTriggerClasses.metric,
+ "h-auto w-auto border-0 bg-transparent p-0 shadow-none focus:ring-0 focus:ring-offset-0",
+ )}
+ hideDownIcon
+ >
+ <SelectValue placeholder="Select metric...">
+ {selectedLabel}
+ </SelectValue>
+ {isSelectedNameAmbiguous && selectedLevel && (
+ <ScoreTag level={SCORE_LEVEL_TAGS[selectedLevel]} />
+ )}
+ <ChevronDown className="h-2.5 w-2.5" />
+ </SelectTrigger>
+ <SelectContent>
+ {Array.from(groupedOptions.entries()).map(([group, options]) => (
+ <SelectGroup key={group}>
+ <SelectLabel className="text-xs font-bold">
+ {group}
+ </SelectLabel>
+ {options.map((option) => (
+ <SelectItem
+ key={option.id}
+ value={option.id}
+ // Type-to-find stays on the score name, so the level tag
+ // can't swallow a keystroke.
+ textValue={option.label}
+ >
+ <span className="flex items-center gap-1.5">
+ {option.label}
+ {option.level && (
+ <ScoreTag level={SCORE_LEVEL_TAGS[option.level]} />
+ )}
+ </span>
+ </SelectItem>
+ ))}
+ </SelectGroup>
+ ))}
+ </SelectContent>
+ </Select>
+ <span className="text-muted-foreground flex items-baseline text-xs leading-none">
+ oldest to newest
+ <DocPopup description={AXIS_EXPLANATION} />
+ </span>
+ </MetricStripHeaderRow>
+ }
+ >
+ <div
+ className={cn("mt-1.5 flex flex-col", PLOT_WITH_LEGEND_HEIGHT_CLASS)}
+ >
+ {query && widgetConfig && (
+ <WidgetContent
+ projectId={projectId}
+ query={query}
+ version={widgetConfig.minVersion}
+ chartType={widgetConfig.chartType}
+ chartConfig={widgetConfig.chartConfig}
+ metrics={[...widgetConfig.metrics]}
+ dimensions={[...widgetConfig.dimensions]}
+ view={widgetConfig.view}
+ schedulerId={`experiments-strip-${metricId}`}
+ isExternalLoading={isExternalLoading}
+ layoutHint="tight"
+ entityDimensionLabelMap={entityDimensionLabelMap}
+ // The band is 63px: the chart's own "No data" card is taller than
+ // that and clips its own text inside it.
+ emptyState={EMPTY_PLOT}
+ // Experiment names are far too long for the axis of a 63px band
+ // (angled category labels cost ~60px of it). Identity moves to the
+ // legend below the plot, which fits in the space the events strip
+ // spends on tick labels, so the band's height is unchanged; the
+ // tooltip carries the exact name either way.
+ hideXAxisLabels
+ colorBarsByCategory
+ // Bars are compared by length, so they have to start at zero.
+ zeroBaseline
+ />
+ )}
+ </div>
+ </MetricStripBand>
+ );
+}
diff --git a/web/src/features/experiments/components/table/ExperimentsTable.tsx b/web/src/features/experiments/components/table/ExperimentsTable.tsx
index 28270bae43..e522dd1fec 100644
--- a/web/src/features/experiments/components/table/ExperimentsTable.tsx
+++ b/web/src/features/experiments/components/table/ExperimentsTable.tsx
@@ -37,7 +37,7 @@ import { useTableDateRange } from "@/src/hooks/useTableDateRange";
import { toAbsoluteTimeRange } from "@/src/utils/date-range-utils";
import { TableHeaderControls } from "@/src/components/table/table-header-controls";
import useColumnOrder from "@/src/features/column-visibility/hooks/useColumnOrder";
-import { ChevronDown, GitCompareArrows, LightbulbIcon } from "lucide-react";
+import { GitCompareArrows, LightbulbIcon } from "lucide-react";
import { createDateTableColumn } from "@/src/components/design-system/table/columns/createDateTableColumn";
import { createNumberTableColumn } from "@/src/components/design-system/table/columns/createNumberTableColumn";
import { createIOTableColumn } from "@/src/components/design-system/table/columns/createIOTableColumn";
@@ -54,6 +54,7 @@ import { useTableViewManager } from "@/src/components/table/table-view-presets/h
import { useRouter } from "next/router";
import { TableSelectionManager } from "@/src/features/table/components/TableSelectionManager";
import { useScoreColumns } from "@/src/features/scores/hooks/useScoreColumns";
+import { collectScoreNameCoverage } from "@/src/features/scores/lib/aggregateScores";
import {
collectPresentScoreKeys,
revealScoreColumns,
@@ -64,10 +65,8 @@ import { useExperimentsTableData } from "../../hooks/useExperimentsTableData";
import { type ExperimentsTableRow, type ExperimentsTableProps } from "./types";
import { useExperimentFilterOptions } from "../../hooks/useExperimentFilterOptions";
import { RunEvaluationDialog } from "@/src/features/batch-actions/components/RunEvaluationDialog";
-import * as AccordionPrimitive from "@radix-ui/react-accordion";
import { useHasProjectAccess } from "@/src/features/rbac";
-import { ExperimentChartsGrid } from "../ExperimentChartsGrid";
-import { useExperimentChartsAccordion } from "../../hooks/useExperimentChartsAccordion";
+import { ExperimentMetricStrip } from "../ExperimentMetricStrip";
import {
createExperimentsTableStore,
type ExperimentsTableStore,
@@ -78,7 +77,6 @@ import { usePostHogClientCapture } from "@/src/features/posthog-analytics";
import {
baselineChangedProps,
comparisonChangedProps,
- chartsSectionToggledProps,
scoreColumnScopeToggledProps,
} from "@/src/features/experiments/lib/analytics";
import { type ColumnGroupTogglePayload } from "@/src/components/table/data-table-column-visibility-filter";
@@ -453,6 +451,18 @@ export default function ExperimentsTable({
};
}, [experiments, metricsLoading]);
+ // Which score the runs in view actually measured, from the same rows and at
+ // the same time as `presentScoreKeys`: the strip opens on the best-recorded
+ // numeric score instead of the alphabetically first one, with no extra query.
+ const scoreCoverage = useMemo(() => {
+ if (metricsLoading || experiments.status !== "success") return undefined;
+ const rows = experiments.rows ?? [];
+ return {
+ obs: collectScoreNameCoverage(rows.map((r) => r.observationItemScores)),
+ experiment: collectScoreNameCoverage(rows.map((r) => r.experimentScores)),
+ };
+ }, [experiments, metricsLoading]);
+
useEffect(() => {
if (experiments.status === "success") {
setDetailPageList(
@@ -821,15 +831,17 @@ export default function ExperimentsTable({
: [];
}, [experiments]);
- // Get experiments from the current query result (for charts)
+ // The strip's series. The strip orders its own x-axis chronologically, which
+ // is deliberately not this table's newest-first order.
const chartExperiments = useMemo(() => {
- return rows.map((row) => ({ id: row.id, name: row.name }));
+ return rows.map((row) => ({
+ id: row.id,
+ name: row.name,
+ startTime: row.startTime,
+ }));
}, [rows]);
- // Charts accordion collapsed state (persisted in session storage)
const capture = usePostHogClientCapture();
- const { accordionValue, setAccordionValue } =
- useExperimentChartsAccordion(projectId);
const datasetIdByExperimentId = useMemo(() => {
const map: Record<string, string> = {};
@@ -853,28 +865,11 @@ export default function ExperimentsTable({
[capture],
);
- const handleChartsAccordionChange = useCallback(
- (value: string) => {
- const isExpanded = value === "charts";
- const wasExpanded = accordionValue === "charts";
- if (isExpanded !== wasExpanded) {
- capture(
- "experiment:charts_section_toggled",
- chartsSectionToggledProps({
- tableName: "experiments",
- isExpanded,
- }),
- );
- }
- setAccordionValue(value);
- },
- [accordionValue, capture, setAccordionValue],
- );
-
// Mirror the visible page's rows into the store (in table order, so
// selectedPageRowIds keeps the first-selected-in-table-order semantics
// the compare baseline relies on).
const pageRowIds = useMemo(() => rows.map((row) => row.id), [rows]);
+
useExperimentsTableSelectionSync({
store: experimentsTableStore,
pageRowIds,
@@ -938,40 +933,6 @@ export default function ExperimentsTable({
/>
</div>
- {/* Charts section - Collapsible Accordion */}
- {tableDateRange && (
- <AccordionPrimitive.Root
- type="single"
- collapsible
- value={accordionValue}
- onValueChange={handleChartsAccordionChange}
- >
- <AccordionPrimitive.Item className="border-t" value="charts">
- <AccordionPrimitive.Header className="flex">
- <AccordionPrimitive.Trigger className="flex flex-1 items-center justify-between px-3 pt-2 pb-1 font-bold transition-all hover:no-underline [&[data-state=open]>svg]:rotate-180">
- <div className="flex items-center gap-2">
- <span className="text-sm font-bold">Charts</span>
- </div>
- <ChevronDown className="h-4 w-4 shrink-0 transition-transform duration-200" />
- </AccordionPrimitive.Trigger>
- </AccordionPrimitive.Header>
- <AccordionPrimitive.Content className="data-[state=closed]:animate-accordion-up data-[state=open]:animate-accordion-down overflow-hidden text-sm transition-all">
- <div className="px-3 pt-1 pb-1">
- <div className="max-h-[40dvh] overflow-x-auto">
- <ExperimentChartsGrid
- projectId={projectId}
- experiments={chartExperiments}
- fromTimestamp={tableDateRange.from}
- toTimestamp={tableDateRange.to}
- isExternalLoading={experiments.status === "loading"}
- />
- </div>
- </div>
- </AccordionPrimitive.Content>
- </AccordionPrimitive.Item>
- </AccordionPrimitive.Root>
- )}
-
{/* Content area with sidebar and table */}
<ResizableFilterLayout>
<DataTableControls
@@ -981,6 +942,19 @@ export default function ExperimentsTable({
/>
<div className="flex flex-1 flex-col overflow-hidden">
+ {/* Table-width, like the events table's pulse strip: inside the
+ layout so the facet sidebar keeps its full height and the
+ strip resizes with the table. */}
+ {tableDateRange && (
+ <ExperimentMetricStrip
+ projectId={projectId}
+ experiments={chartExperiments}
+ fromTimestamp={tableDateRange.from}
+ toTimestamp={tableDateRange.to}
+ isExternalLoading={experiments.status === "loading"}
+ scoreCoverage={scoreCoverage}
+ />
+ )}
<DataTable
key={`experiments-table-${dataUpdatedAt}`}
tableName="experiments"
diff --git a/web/src/features/experiments/constants/charts.ts b/web/src/features/experiments/constants/charts.ts
index 1a92f79eac..763204881a 100644
--- a/web/src/features/experiments/constants/charts.ts
+++ b/web/src/features/experiments/constants/charts.ts
@@ -1,3 +1,4 @@
+import type { ScoreLevel as ScoreTagLevel } from "@/src/components/score-tag";
import type {
ScoreLevel,
ScoreMetricSpec,
@@ -11,32 +12,48 @@ export const BASE_CHART_IDS = {
LATENCY: "base:latency",
} as const;
-export const MAX_CHARTS = 4;
+/**
+ * The level tag shown next to a score name in the metric dropdown, in the
+ * vocabulary the tracing tables' score pickers use (`ScoreTag`).
+ */
+export const SCORE_LEVEL_TAGS: Record<ScoreLevel, ScoreTagLevel> = {
+ obs: "observation",
+ experiment: "experiment",
+};
+
+/**
+ * Where the boolean score names sit per level. Booleans are also listed under
+ * `*_scores_avg` (they are stored as 0/1), so this is what tells a numeric
+ * option apart from a boolean one.
+ */
+export const SCORE_BOOLEAN_FILTER_KEYS: Record<
+ ScoreLevel,
+ "obs_score_booleans" | "experiment_score_booleans"
+> = {
+ obs: "obs_score_booleans",
+ experiment: "experiment_score_booleans",
+};
export const SCORE_METRIC_SPECS: ScoreMetricSpec = {
"obs:numeric": {
level: "obs",
dataType: "numeric",
filterKey: "obs_scores_avg",
- group: "Observation Scores",
},
"obs:categorical": {
level: "obs",
dataType: "categorical",
filterKey: "obs_score_categories",
- group: "Observation Scores",
},
"experiment:numeric": {
level: "experiment",
dataType: "numeric",
filterKey: "experiment_scores_avg",
- group: "Experiment Scores",
},
"experiment:categorical": {
level: "experiment",
dataType: "categorical",
filterKey: "experiment_score_categories",
- group: "Experiment Scores",
},
};
@@ -47,8 +64,12 @@ const BASE_EXPERIMENT_WIDGET_CONFIG = {
// Entity (x-axis) order follows the experiments table order, applied
// client-side in InlineWidget, so no query-side ordering is needed.
orderBy: null,
- chartType: "LINE_TIME_SERIES",
- chartConfig: { type: "LINE_TIME_SERIES" },
+ // Bars, not a line: the axis is a set of discrete experiments, so a segment
+ // drawn between two of them claims a continuity that does not exist. A bar
+ // also makes a metric that only some runs recorded read as absent rather
+ // than as a long line across every other run.
+ chartType: "VERTICAL_BAR",
+ chartConfig: { type: "VERTICAL_BAR" },
timeDimension: null,
entityDimension: { field: "experimentName" },
filters: [],
@@ -93,8 +114,9 @@ export const NUMERIC_SCORE_CHART_CONFIG = {
dimensions: [],
metrics: [{ measure: "value", agg: "avg" }],
filters: [],
- chartType: "LINE_TIME_SERIES",
- chartConfig: { type: "LINE_TIME_SERIES" },
+ // See BASE_EXPERIMENT_WIDGET_CONFIG: one bar per experiment, not a line.
+ chartType: "VERTICAL_BAR",
+ chartConfig: { type: "VERTICAL_BAR" },
} as const;
export const CATEGORICAL_SCORE_CHART_CONFIG = {
diff --git a/web/src/features/experiments/fns/pickDefaultStripMetric.clienttest.ts b/web/src/features/experiments/fns/pickDefaultStripMetric.clienttest.ts
new file mode 100644
index 0000000000..1581bdc841
--- /dev/null
+++ b/web/src/features/experiments/fns/pickDefaultStripMetric.clienttest.ts
@@ -0,0 +1,91 @@
+import { describe, expect, it } from "vitest";
+
+import { pickDefaultStripMetric } from "./pickDefaultStripMetric";
+import { buildMetricOptions } from "@/src/features/experiments/utils/charts";
+
+describe("pickDefaultStripMetric", () => {
+ it("opens on the first numeric score, not on cost", () => {
+ expect(
+ pickDefaultStripMetric(
+ buildMetricOptions({
+ obs_scores_avg: ["groundedness", "accuracy"],
+ obs_score_categories: { verdict: ["pass", "fail"] },
+ }),
+ ),
+ ).toBe("obs-score-numeric:accuracy");
+ });
+
+ it("falls back to a categorical score, then to cost", () => {
+ expect(
+ pickDefaultStripMetric(
+ buildMetricOptions({ obs_score_categories: { verdict: ["pass"] } }),
+ ),
+ ).toBe("obs-score-categorical:verdict");
+ expect(pickDefaultStripMetric(buildMetricOptions({}))).toBe("base:cost");
+ });
+
+ // The reason the rule changed: the alphabetically first score was a language
+ // check recorded on a handful of items, and it opened the page.
+ it("prefers coverage over the alphabetically first name", () => {
+ expect(
+ pickDefaultStripMetric(
+ buildMetricOptions({
+ obs_scores_avg: ["accuracy", "groundedness"],
+ }),
+ {
+ obs: new Map([
+ ["accuracy", 3],
+ ["groundedness", 120],
+ ]),
+ },
+ ),
+ ).toBe("obs-score-numeric:groundedness");
+ });
+
+ it("ranks a boolean score below a numeric one whatever its coverage", () => {
+ expect(
+ pickDefaultStripMetric(
+ buildMetricOptions({
+ obs_scores_avg: ["answered_in_right_language", "groundedness"],
+ obs_score_booleans: ["answered_in_right_language"],
+ }),
+ {
+ obs: new Map([
+ ["answered_in_right_language", 400],
+ ["groundedness", 12],
+ ]),
+ },
+ ),
+ ).toBe("obs-score-numeric:groundedness");
+ });
+
+ // Coverage is per level, and the aggregate keys it is counted under carry
+ // normalized names ("." and "-" become "_").
+ it("resolves coverage per level and through name normalization", () => {
+ const options = buildMetricOptions({
+ obs_scores_avg: ["judge.verdict"],
+ experiment_scores_avg: ["judge.verdict"],
+ });
+ expect(
+ pickDefaultStripMetric(options, {
+ obs: new Map([["judge_verdict", 2]]),
+ experiment: new Map([["judge_verdict", 90]]),
+ }),
+ ).toBe("experiment-score-numeric:judge.verdict");
+ });
+
+ // Without coverage the order must still be total, so the strip does not
+ // switch metrics between renders while the row metrics are in flight.
+ it("is stable when no coverage is known", () => {
+ const options = buildMetricOptions({
+ obs_scores_avg: ["groundedness"],
+ experiment_scores_avg: ["groundedness"],
+ });
+ expect(pickDefaultStripMetric(options)).toBe(
+ "obs-score-numeric:groundedness",
+ );
+ expect(pickDefaultStripMetric(options)).toBe(
+ pickDefaultStripMetric(options),
+ );
+ });
+});
diff --git a/web/src/features/experiments/fns/pickDefaultStripMetric.ts b/web/src/features/experiments/fns/pickDefaultStripMetric.ts
new file mode 100644
index 0000000000..e77a0ae4a1
--- /dev/null
+++ b/web/src/features/experiments/fns/pickDefaultStripMetric.ts
@@ -0,0 +1,65 @@
+import { BASE_CHART_IDS } from "@/src/features/experiments/constants/charts";
+import type {
+ MetricOption,
+ ScoreCoverageByLevel,
+} from "@/src/features/experiments/types/charts";
+import { normalizeScoreName } from "@/src/features/scores/lib/aggregateScores";
+
+/** Score chart ids are `${level}-score-${dataType}:${scoreName}`. */
+const NUMERIC_SCORE_ID = /^(obs|trace|experiment)-score-numeric:/;
+
+/** Numeric first: only a numeric average reads as a bar in a 63px band. */
+const VALUE_KIND_RANK: Record<
+ NonNullable<MetricOption["valueKind"]>,
+ number
+> = {
+ numeric: 0,
+ boolean: 1,
+ categorical: 1,
+};
+
+const valueKindOf = (
+ option: MetricOption,
+): NonNullable<MetricOption["valueKind"]> =>
+ option.valueKind ??
+ (NUMERIC_SCORE_ID.test(option.id) ? "numeric" : "categorical");
+
+/**
+ * Which metric the strip opens on. Coverage-first, so it lands on the score the
+ * runs in view actually measured rather than on whichever name sorts first:
+ *
+ * 1. numeric before boolean/categorical;
+ * 2. then the highest coverage, the score carrying the most recorded values
+ * across the runs in view, counted client-side from the rows already
+ * fetched (`collectScoreNameCoverage`) so no query is added;
+ * 3. then the name, a stable tiebreak so the default cannot flicker between
+ * renders. One name at two levels ties into the option order (observation,
+ * trace, then run level);
+ * 4. Cost only when the runs in view carry no scores at all, so the default
+ * answers "did quality move?" instead of "what did it cost?".
+ *
+ * Coverage is unknown until the row metrics land; until then the order falls
+ * through to the name. A user's explicit pick always wins β€” see
+ * `useExperimentStripMetric`.
+ */
+export function pickDefaultStripMetric(
+ options: MetricOption[],
+ scoreCoverage?: ScoreCoverageByLevel,
+): string {
+ const scores = options.filter((option) => option.group !== "Base Metrics");
+
+ const coverageOf = (option: MetricOption): number =>
+ option.level
+ ? (scoreCoverage?.[option.level]?.get(normalizeScoreName(option.label)) ??
+ 0)
+ : 0;
+
+ const [first] = [...scores].sort(
+ (a, b) =>
+ VALUE_KIND_RANK[valueKindOf(a)] - VALUE_KIND_RANK[valueKindOf(b)] ||
+ coverageOf(b) - coverageOf(a) ||
+ a.label.localeCompare(b.label),
+ );
+
+ return first?.id ?? BASE_CHART_IDS.COST;
+}
diff --git a/web/src/features/experiments/hooks/useExperimentChartsAccordion.ts b/web/src/features/experiments/hooks/useExperimentChartsAccordion.ts
deleted file mode 100644
index 3ca9ff663f..0000000000
--- a/web/src/features/experiments/hooks/useExperimentChartsAccordion.ts
+++ /dev/null
@@ -1,30 +0,0 @@
-import { useCallback } from "react";
-import useSessionStorage from "@/src/components/useSessionStorage";
-
-const CHARTS_ACCORDION_VALUE = "charts";
-
-/**
- * Hook to manage the charts accordion collapsed state with session storage persistence.
- */
-export function useExperimentChartsAccordion(projectId: string) {
- const [accordionValue, setAccordionValue] = useSessionStorage<
- string | undefined
- >(`experiment-charts-accordion-${projectId}`, CHARTS_ACCORDION_VALUE);
-
- const isChartsExpanded = accordionValue === CHARTS_ACCORDION_VALUE;
-
- const setChartsExpanded = useCallback(
- (expanded: boolean) => {
- setAccordionValue(expanded ? CHARTS_ACCORDION_VALUE : undefined);
- },
- [setAccordionValue],
- );
-
- return {
- isChartsExpanded,
- setChartsExpanded,
- // Raw accordion props for direct binding to Accordion component
- accordionValue,
- setAccordionValue,
- };
-}
diff --git a/web/src/features/experiments/hooks/useExperimentChartsGridSelection.ts b/web/src/features/experiments/hooks/useExperimentChartsGridSelection.ts
deleted file mode 100644
index 995020e67a..0000000000
--- a/web/src/features/experiments/hooks/useExperimentChartsGridSelection.ts
+++ /dev/null
@@ -1,143 +0,0 @@
-import { useMemo, useCallback } from "react";
-import useSessionStorage from "@/src/components/useSessionStorage";
-import {
- buildMetricOptions,
- getDefaultCharts,
- isValidChartSelection,
-} from "@/src/features/experiments/utils/charts";
-import { MAX_CHARTS } from "@/src/features/experiments/constants/charts";
-import { api } from "@/src/utils/api";
-import { type ScoreFilterOptions } from "@/src/features/experiments/types/charts";
-
-const processCategoricalScoreOptions = (
- categories: Array<{ label: string; values: string[] }>,
-): Record<string, string[]> =>
- Object.fromEntries(categories.map(({ label, values }) => [label, values]));
-
-/**
- * Hook to manage dynamic experiment chart selection with session storage persistence.
- * Supports add/remove operations with a max of 4 charts.
- */
-export function useExperimentChartsGridSelection({
- projectId,
- experimentIds,
-}: {
- projectId: string;
- experimentIds: string[];
-}) {
- // Default charts
- const defaultCharts = useMemo(() => getDefaultCharts(), []);
-
- const scoreOptions = api.experiments.scoreOptions.useQuery(
- {
- projectId,
- experimentIds,
- },
- { enabled: experimentIds.length > 0 },
- );
-
- // Session storage for chart selections
- const [rawCharts, setCharts] = useSessionStorage<string[]>(
- `experiment-charts-grid-${projectId}`,
- defaultCharts,
- );
-
- // Validate stored data format - fall back to defaults if invalid
- const charts: string[] = useMemo(() => {
- if (isValidChartSelection(rawCharts) && rawCharts.length > 0) {
- return rawCharts;
- }
- // Invalid or empty data - return defaults
- return defaultCharts;
- }, [rawCharts, defaultCharts]);
-
- // Transform API response to ScoreFilterOptions format
- const transformedScoreOptions = useMemo((): ScoreFilterOptions => {
- if (!scoreOptions.data) {
- return {};
- }
- return {
- obs_scores_avg: scoreOptions.data.obs_scores_avg,
- obs_score_categories: processCategoricalScoreOptions(
- scoreOptions.data.obs_score_categories,
- ),
- experiment_scores_avg: scoreOptions.data.experiment_scores_avg,
- experiment_score_categories: processCategoricalScoreOptions(
- scoreOptions.data.experiment_score_categories,
- ),
- };
- }, [scoreOptions.data]);
-
- // Build all available metric options for the dropdowns
- const availableMetricOptions = useMemo(
- () => buildMetricOptions(transformedScoreOptions),
- [transformedScoreOptions],
- );
-
- const availableMetric = useMemo(() => {
- const usedMetrics = new Set(charts);
- return availableMetricOptions.find((opt) => !usedMetrics.has(opt.id));
- }, [charts, availableMetricOptions]);
-
- // Check if we can add more charts
- const canAddChart = charts.length < MAX_CHARTS && Boolean(availableMetric);
-
- // Check if we can delete charts (must have more than 1)
- const canDeleteChart = charts.length > 1;
-
- // Update a specific chart's metric
- const updateChart = useCallback(
- (index: number, metricId: string) => {
- if (index < 0 || index >= charts.length) return;
- const next = [...charts];
- next[index] = metricId;
- setCharts(next);
- },
- [setCharts, charts],
- );
-
- // Add a new chart with the first available metric not already selected
- const addChart = useCallback(() => {
- if (!canAddChart || !availableMetric) return;
-
- setCharts([...charts, availableMetric.id]);
- }, [canAddChart, charts, availableMetric, setCharts]);
-
- // Remove a chart at a specific index
- const removeChart = useCallback(
- (index: number) => {
- if (!canDeleteChart) return;
- if (index < 0 || index >= charts.length) return;
-
- const next = charts.filter((_, i) => i !== index);
- setCharts(next);
- },
- [canDeleteChart, charts, setCharts],
- );
-
- // Reset to defaults
- const resetToDefaults = useCallback(() => {
- setCharts(defaultCharts);
- }, [setCharts, defaultCharts]);
-
- return {
- // Current chart selections (metric IDs)
- charts,
- // Update a chart's metric
- updateChart,
- // Add a new chart
- addChart,
- // Remove a chart
- removeChart,
- // Can add more charts?
- canAddChart,
- // Can delete charts? (more than 1)
- canDeleteChart,
- // Reset to defaults
- resetToDefaults,
- // All available metric options (for dropdown)
- availableMetricOptions,
- // Loading state
- isLoading: scoreOptions.isLoading,
- };
-}
diff --git a/web/src/features/experiments/hooks/useExperimentStripMetric.clienttest.tsx b/web/src/features/experiments/hooks/useExperimentStripMetric.clienttest.tsx
new file mode 100644
index 0000000000..f3a60338eb
--- /dev/null
+++ b/web/src/features/experiments/hooks/useExperimentStripMetric.clienttest.tsx
@@ -0,0 +1,157 @@
+import { render, screen } from "@testing-library/react";
+import { useExperimentStripMetric } from "./useExperimentStripMetric";
+import { type ScoreCoverageByLevel } from "@/src/features/experiments/types/charts";
+
+const scoreOptionsQuery = vi.fn();
+
+vi.mock("@/src/utils/api", () => ({
+ api: {
+ experiments: {
+ scoreOptions: {
+ useQuery: (...args: unknown[]) => scoreOptionsQuery(...args),
+ },
+ },
+ },
+}));
+
+const SCORE_OPTIONS_DATA = {
+ obs_scores_avg: ["groundedness"],
+ obs_score_categories: [],
+ experiment_scores_avg: [],
+ experiment_score_categories: [],
+};
+
+/**
+ * Two observation-level scores as the score-options endpoint returns them:
+ * booleans are listed under `*_scores_avg` too, and only `*_score_columns`
+ * says which they are.
+ */
+const MIXED_SCORE_OPTIONS_DATA = {
+ ...SCORE_OPTIONS_DATA,
+ obs_scores_avg: ["answered_in_right_language", "groundedness"],
+ obs_score_columns: [
+ {
+ name: "answered_in_right_language",
+ dataType: "BOOLEAN",
+ source: "EVAL",
+ },
+ { name: "groundedness", dataType: "NUMERIC", source: "EVAL" },
+ ],
+};
+
+function Harness({
+ experimentIds,
+ scoreCoverage,
+}: {
+ experimentIds: string[];
+ scoreCoverage?: ScoreCoverageByLevel;
+}) {
+ const { metricId, isLoading } = useExperimentStripMetric({
+ projectId: "p1",
+ experimentIds,
+ scoreCoverage,
+ });
+ return (
+ <div data-testid="state">{`${isLoading ? "loading" : "ready"}:${metricId}`}</div>
+ );
+}
+
+const state = () => screen.getByTestId("state").textContent;
+
+describe("useExperimentStripMetric", () => {
+ beforeEach(() => {
+ sessionStorage.clear();
+ scoreOptionsQuery.mockReset();
+ });
+
+ // The bug: a pending options query left the hook "ready" on the Cost
+ // fallback, so a project full of scores opened on "Cost ($)". The fallback
+ // asserts that the experiments in view carry no scores β€” it may only be
+ // reached once the query can say so.
+ it("stays loading while the score options are unknown", () => {
+ scoreOptionsQuery.mockReturnValue({
+ data: undefined,
+ isSuccess: false,
+ isError: false,
+ });
+ render(<Harness experimentIds={["e1"]} />);
+ expect(state()).toBe("loading:base:cost");
+ });
+
+ it("opens on a score once the options arrive", () => {
+ scoreOptionsQuery.mockReturnValue({
+ data: SCORE_OPTIONS_DATA,
+ isSuccess: true,
+ isError: false,
+ });
+ render(<Harness experimentIds={["e1"]} />);
+ expect(state()).toBe("ready:obs-score-numeric:groundedness");
+ });
+
+ // Nothing to ask about: the strip renders its empty band, so it must not be
+ // held on a skeleton by a query that never runs.
+ it("is not loading when no experiments are in view", () => {
+ scoreOptionsQuery.mockReturnValue({
+ data: undefined,
+ isSuccess: false,
+ isError: false,
+ });
+ render(<Harness experimentIds={[]} />);
+ expect(state()).toBe("ready:base:cost");
+ });
+
+ // The reviewer's question: why this score? Because it is the numeric score
+ // with the most values recorded across the runs in view β€” not the first name.
+ it("opens on the best-recorded numeric score", () => {
+ scoreOptionsQuery.mockReturnValue({
+ data: {
+ ...SCORE_OPTIONS_DATA,
+ obs_scores_avg: ["accuracy", "groundedness"],
+ },
+ isSuccess: true,
+ isError: false,
+ });
+ render(
+ <Harness
+ experimentIds={["e1"]}
+ scoreCoverage={{
+ obs: new Map([
+ ["accuracy", 4],
+ ["groundedness", 88],
+ ]),
+ }}
+ />,
+ );
+ expect(state()).toBe("ready:obs-score-numeric:groundedness");
+ });
+
+ it("does not open on a boolean score while a numeric one exists", () => {
+ scoreOptionsQuery.mockReturnValue({
+ data: MIXED_SCORE_OPTIONS_DATA,
+ isSuccess: true,
+ isError: false,
+ });
+ render(
+ <Harness
+ experimentIds={["e1"]}
+ scoreCoverage={{
+ obs: new Map([
+ ["answered_in_right_language", 300],
+ ["groundedness", 30],
+ ]),
+ }}
+ />,
+ );
+ expect(state()).toBe("ready:obs-score-numeric:groundedness");
+ });
+
+ it("falls back to cost when the options query fails", () => {
+ scoreOptionsQuery.mockReturnValue({
+ data: undefined,
+ isSuccess: false,
+ isError: true,
+ });
+ render(<Harness experimentIds={["e1"]} />);
+ expect(state()).toBe("ready:base:cost");
+ });
+});
diff --git a/web/src/features/experiments/hooks/useExperimentStripMetric.ts b/web/src/features/experiments/hooks/useExperimentStripMetric.ts
new file mode 100644
index 0000000000..fe39af0f35
--- /dev/null
+++ b/web/src/features/experiments/hooks/useExperimentStripMetric.ts
@@ -0,0 +1,123 @@
+import { useMemo } from "react";
+import useSessionStorage from "@/src/components/useSessionStorage";
+import { buildMetricOptions } from "@/src/features/experiments/utils/charts";
+import { pickDefaultStripMetric } from "@/src/features/experiments/fns/pickDefaultStripMetric";
+import { api } from "@/src/utils/api";
+import {
+ type ScoreCoverageByLevel,
+ type ScoreFilterOptions,
+} from "@/src/features/experiments/types/charts";
+
+const processCategoricalScoreOptions = (
+ categories: Array<{ label: string; values: string[] }>,
+): Record<string, string[]> =>
+ Object.fromEntries(categories.map(({ label, values }) => [label, values]));
+
+/**
+ * The score names whose values are booleans at this level. Booleans are also
+ * listed as numeric (they are stored as 0/1), so a name that is numeric
+ * anywhere at this level is left out: the strip's default ranks a boolean below
+ * a true numeric, and a share of true reads nothing like an average.
+ */
+const booleanOnlyScoreNames = (
+ columns: Array<{ name: string; dataType: string }> | undefined,
+): string[] => {
+ if (!columns) return [];
+ const numericNames = new Set(
+ columns.filter((c) => c.dataType === "NUMERIC").map((c) => c.name),
+ );
+ return Array.from(
+ new Set(
+ columns
+ .filter((c) => c.dataType === "BOOLEAN" && !numericNames.has(c.name))
+ .map((c) => c.name),
+ ),
+ );
+};
+
+/**
+ * The metric the experiments strip plots, persisted per project in session
+ * storage. Nothing is persisted until the user picks one, so the coverage-first
+ * default (`pickDefaultStripMetric`) keeps applying as the score options
+ * arrive β€” and a stored metric that the experiments in view no longer carry
+ * falls back to it instead of rendering an empty chart.
+ *
+ * `isLoading` covers "we do not know the options yet", not just "a request is
+ * in flight". `pickDefaultStripMetric`'s Cost fallback asserts something β€”
+ * that the experiments in view carry no scores β€” and only the score-options
+ * query can assert it. Until it has, the strip must show its loading state:
+ * reporting ready would put "Cost ($)" in the header of a project full of
+ * scores.
+ */
+export function useExperimentStripMetric({
+ projectId,
+ experimentIds,
+ scoreCoverage,
+}: {
+ projectId: string;
+ experimentIds: string[];
+ /** See `pickDefaultStripMetric`: coverage decides the default metric. */
+ scoreCoverage?: ScoreCoverageByLevel;
+}) {
+ // `projectId` arrives with `router.query` after hydration; without it in the
+ // guard the query can fire with `undefined` and zod rejects it. With no
+ // experiments in view there is nothing to ask about.
+ const canLoadScoreOptions = Boolean(projectId) && experimentIds.length > 0;
+
+ const scoreOptions = api.experiments.scoreOptions.useQuery(
+ { projectId, experimentIds },
+ { enabled: canLoadScoreOptions },
+ );
+
+ const [selectedMetricId, setSelectedMetricId] = useSessionStorage<
+ string | null
+ >(`experiment-strip-metric-${projectId}`, null);
+
+ const transformedScoreOptions = useMemo((): ScoreFilterOptions => {
+ if (!scoreOptions.data) {
+ return {};
+ }
+ return {
+ obs_scores_avg: scoreOptions.data.obs_scores_avg,
+ obs_score_categories: processCategoricalScoreOptions(
+ scoreOptions.data.obs_score_categories,
+ ),
+ obs_score_booleans: booleanOnlyScoreNames(
+ scoreOptions.data.obs_score_columns,
+ ),
+ experiment_scores_avg: scoreOptions.data.experiment_scores_avg,
+ experiment_score_categories: processCategoricalScoreOptions(
+ scoreOptions.data.experiment_score_categories,
+ ),
+ experiment_score_booleans: booleanOnlyScoreNames(
+ scoreOptions.data.experiment_score_columns,
+ ),
+ };
+ }, [scoreOptions.data]);
+
+ const availableMetricOptions = useMemo(
+ () => buildMetricOptions(transformedScoreOptions),
+ [transformedScoreOptions],
+ );
+
+ const metricId = useMemo(() => {
+ if (
+ selectedMetricId &&
+ availableMetricOptions.some((option) => option.id === selectedMetricId)
+ ) {
+ return selectedMetricId;
+ }
+ return pickDefaultStripMetric(availableMetricOptions, scoreCoverage);
+ }, [selectedMetricId, availableMetricOptions, scoreCoverage]);
+
+ return {
+ metricId,
+ setMetricId: setSelectedMetricId,
+ availableMetricOptions,
+ // A disabled query is "pending" forever, so settle on the query's own
+ // outcome rather than on `isLoading`, which a not-yet-started fetch
+ // reports as false.
+ isLoading:
+ canLoadScoreOptions && !scoreOptions.isSuccess && !scoreOptions.isError,
+ };
+}
diff --git a/web/src/features/experiments/types/charts.ts b/web/src/features/experiments/types/charts.ts
index 49d71579db..bb8d22b024 100644
--- a/web/src/features/experiments/types/charts.ts
+++ b/web/src/features/experiments/types/charts.ts
@@ -2,9 +2,30 @@
export type MetricOption = {
id: string;
label: string;
- group: "Base Metrics" | "Observation Scores" | "Experiment Scores";
+ group: "Base Metrics" | "Scores";
+ /**
+ * The level the score was recorded at, absent on base metrics. Presentation
+ * only: the metric still resolves to one level to plot, but the dropdown
+ * tags the entry with the level instead of splitting the list by it β€” the
+ * tracing tables' score facets read the same way.
+ */
+ level?: ScoreLevel;
+ /**
+ * How the score's values read, absent on base metrics. Boolean scores share
+ * the numeric bucket (they are stored as 0/1) but summarise as a share
+ * rather than a magnitude, so they rank below a true numeric as a default.
+ */
+ valueKind?: "numeric" | "boolean" | "categorical";
};
+/**
+ * How many values each score name carries across the runs in view, per level.
+ * Keyed by normalized score name, as `collectScoreNameCoverage` returns it.
+ */
+export type ScoreCoverageByLevel = Partial<
+ Record<ScoreLevel, ReadonlyMap<string, number>>
+>;
+
export type ScoreFilterOptions = {
obs_scores_avg?: string[];
obs_score_categories?: Record<string, string[]>;
@@ -29,6 +50,12 @@ export type ExperimentItemScoreFilterOptions = {
score_name_levels_boolean?: ScoreNameLevels;
};
+/**
+ * Levels the strip can chart. Trace level is deliberately absent: grouping a
+ * trace-attached score by experiment needs a single-row-per-trace join the
+ * query builder's relation model cannot express, so those scores are read from
+ * the score columns and the score matrix instead of plotted here.
+ */
export type ScoreLevel = "obs" | "experiment";
export type ScoreChartDataType = "numeric" | "categorical";
@@ -38,6 +65,5 @@ export type ScoreMetricSpec = Record<
level: ScoreLevel;
dataType: ScoreChartDataType;
filterKey: keyof ScoreFilterOptions;
- group: Exclude<MetricOption["group"], "Base Metrics">;
}
>;
diff --git a/web/src/features/experiments/utils/charts.ts b/web/src/features/experiments/utils/charts.ts
index 697629262a..77dd6d901e 100644
--- a/web/src/features/experiments/utils/charts.ts
+++ b/web/src/features/experiments/utils/charts.ts
@@ -3,10 +3,10 @@ import {
CATEGORICAL_SCORE_CHART_CONFIG,
EXPERIMENT_COST_WIDGET_CONFIG,
EXPERIMENT_LATENCY_WIDGET_CONFIG,
- MAX_CHARTS,
NUMERIC_SCORE_CHART_CONFIG,
SCORE_LEVEL_ENTITY_DIMENSIONS,
SCORE_LEVEL_FILTERS,
+ SCORE_BOOLEAN_FILTER_KEYS,
SCORE_METRIC_SPECS,
} from "@/src/features/experiments/constants/charts";
import type {
@@ -32,7 +32,7 @@ const parseScoreChartId = (
scoreName: string;
} | null => {
const match = chartId.match(
- /^(obs|experiment)-score-(numeric|categorical):(.+)$/,
+ /^(obs|trace|experiment)-score-(numeric|categorical):(.+)$/,
);
if (!match) return null;
return {
@@ -102,35 +102,52 @@ export function buildWidgetConfigFromId(chartId: string) {
});
}
-/**
- * Get smart default charts based on available scores.
- * Starts with Cost and Latency.
- */
-export function getDefaultCharts(): string[] {
- return [BASE_CHART_IDS.COST, BASE_CHART_IDS.LATENCY];
-}
+/** Order two entries for the same score name take: obs, trace, then run. */
+const LEVEL_SORT_ORDER: ScoreLevel[] = ["obs", "experiment"];
/**
* Build all available metric options from score filter options for the dropdown.
+ *
+ * The score metrics are ONE flat, alphabetical list: the level is a tag on the
+ * entry, not a structural division, so the strip reads like the tracing tables'
+ * level-agnostic score facets. A name recorded at two levels yields two
+ * entries β€” each still plots exactly one level β€” sorted next to each other.
*/
export function buildMetricOptions(
scoreFilterOptions: ScoreFilterOptions,
): MetricOption[] {
const scoreOptions = Object.values(SCORE_METRIC_SPECS).flatMap(
- ({ level, dataType, filterKey, group }) => {
+ ({ level, dataType, filterKey }) => {
const scoreNames = getScoreNamesFromFilterOption(
scoreFilterOptions[filterKey],
dataType,
);
+ const booleanNames = new Set(
+ scoreFilterOptions[SCORE_BOOLEAN_FILTER_KEYS[level]] ?? [],
+ );
+
return scoreNames.map((scoreName) => ({
id: buildScoreChartId(level, dataType, scoreName),
label: scoreName,
- group,
+ group: "Scores" as const,
+ level,
+ valueKind:
+ dataType === "categorical"
+ ? ("categorical" as const)
+ : booleanNames.has(scoreName)
+ ? ("boolean" as const)
+ : ("numeric" as const),
}));
},
);
+ scoreOptions.sort(
+ (a, b) =>
+ a.label.localeCompare(b.label) ||
+ LEVEL_SORT_ORDER.indexOf(a.level) - LEVEL_SORT_ORDER.indexOf(b.level),
+ );
+
return [
{
id: BASE_CHART_IDS.COST,
@@ -145,12 +162,3 @@ export function buildMetricOptions(
...scoreOptions,
];
}
-
-/**
- * Validate that stored data is a valid ChartSelection (array of strings, max 4).
- */
-export function isValidChartSelection(data: unknown): data is string[] {
- if (!Array.isArray(data)) return false;
- if (data.length > MAX_CHARTS) return false;
- return data.every((item) => typeof item === "string" && item.length > 0);
-}
diff --git a/web/src/features/scores-chart-view/components/ScoresOutlierStrip.tsx b/web/src/features/scores-chart-view/components/ScoresOutlierStrip.tsx
index a01a314ffe..3d249342c8 100644
--- a/web/src/features/scores-chart-view/components/ScoresOutlierStrip.tsx
+++ b/web/src/features/scores-chart-view/components/ScoresOutlierStrip.tsx
@@ -5,8 +5,11 @@ import {
type ViewVersion,
} from "@langfuse/shared/query";
import { api } from "@/src/utils/api";
-import { cn } from "@/src/utils/tailwind";
import { useElementSize } from "@/src/hooks/useElementSize";
+import {
+ MetricStripBand,
+ type MetricStripStatus,
+} from "@/src/components/metric-strip/MetricStripBand";
import {
canReuseOutlierPlaceholder,
pickChartGranularity,
@@ -285,76 +288,63 @@ export function ScoresOutlierStrip({
(valueQueryResult.isPending && !valueQueryResult.isError) ||
placeholderMissesGrid);
+ const status: MetricStripStatus = !canApplyFilters
+ ? "ready"
+ : isLoading || width === 0
+ ? "loading"
+ : countQueryResult.isError || valueQueryResult.isError
+ ? "empty"
+ : "ready";
+
return (
- // Ruled top and bottom: the strip reads as its own band instead of
- // floating between the toolbar and the table header.
- <div ref={wrapperRef} className="shrink-0 border-y">
- {size === undefined ? null : (
- <div className="relative px-2 pt-2.5 pb-1">
- {!canApplyFilters ? (
- <div>
- <ScoreOutlierStripHeader
- mode={mode}
- onModeChange={handleModeChange}
- aggregation={aggregation}
- aggOptions={aggOptions}
- onAggregationChange={setAggregation}
- />
- <ScoreOutlierBarStrip
- dense={[]}
- maxValue={0}
- ticks={[]}
- stepMs={stepMs}
- metric={mode}
- widthPx={chartWidth}
- disabledReason="Chart unavailable for the current filters"
- />
- </div>
- ) : isLoading || width === 0 ? (
- <div className="bg-muted h-[76px] animate-pulse rounded" />
- ) : countQueryResult.isError || valueQueryResult.isError ? (
- <div className="text-muted-foreground flex h-[76px] items-center justify-center text-[11px]">
- No Data
- </div>
- ) : (
- <div
- className={cn("min-w-0 transition-opacity", {
- "opacity-60":
- (countQueryResult.isPlaceholderData &&
- countQueryResult.isFetching) ||
- (valueQueryResult.isPlaceholderData &&
- valueQueryResult.isFetching),
- })}
- >
- <ScoreOutlierStripHeader
- mode={mode}
- onModeChange={handleModeChange}
- aggregation={aggregation}
- aggOptions={aggOptions}
- onAggregationChange={setAggregation}
- />
- <ScoreOutlierBarStrip
- dense={series.dense}
- maxValue={series.maxValue}
- ticks={series.ticks}
- stepMs={stepMs}
- metric={mode}
- widthPx={chartWidth}
- onSelectBucket={handleSelectBucket}
- onPreviewPinned={(trigger) =>
- capture("pulse:preview_pinned", {
- trigger,
- metric: mode,
- surface: "scores",
- })
- }
- selection={activeSelection}
- onSelectionChange={handleSelectionChange}
- />
- </div>
- )}
- </div>
+ <MetricStripBand
+ ref={wrapperRef}
+ measured={size !== undefined}
+ status={status}
+ stale={
+ (countQueryResult.isPlaceholderData && countQueryResult.isFetching) ||
+ (valueQueryResult.isPlaceholderData && valueQueryResult.isFetching)
+ }
+ header={
+ <ScoreOutlierStripHeader
+ mode={mode}
+ onModeChange={handleModeChange}
+ aggregation={aggregation}
+ aggOptions={aggOptions}
+ onAggregationChange={setAggregation}
+ />
+ }
+ >
+ {!canApplyFilters ? (
+ <ScoreOutlierBarStrip
+ dense={[]}
+ maxValue={0}
+ ticks={[]}
+ stepMs={stepMs}
+ metric={mode}
+ widthPx={chartWidth}
+ disabledReason="Chart unavailable for the current filters"
+ />
+ ) : (
+ <ScoreOutlierBarStrip
+ dense={series.dense}
+ maxValue={series.maxValue}
+ ticks={series.ticks}
+ stepMs={stepMs}
+ metric={mode}
+ widthPx={chartWidth}
+ onSelectBucket={handleSelectBucket}
+ onPreviewPinned={(trigger) =>
+ capture("pulse:preview_pinned", {
+ trigger,
+ metric: mode,
+ surface: "scores",
+ })
+ }
+ selection={activeSelection}
+ onSelectionChange={handleSelectionChange}
+ />
)}
- </div>
+ </MetricStripBand>
);
}
diff --git a/web/src/features/scores/lib/aggregateScores.ts b/web/src/features/scores/lib/aggregateScores.ts
index e08a839873..db61d533b3 100644
--- a/web/src/features/scores/lib/aggregateScores.ts
+++ b/web/src/features/scores/lib/aggregateScores.ts
@@ -46,6 +46,28 @@ export const decomposeAggregateScoreKey = (
};
};
+/**
+ * How many values each score name carries across the aggregates handed in β€”
+ * the counting sibling of `collectPresentScoreKeys`, which only answers
+ * whether a score is present at all. Keyed by normalized score name and summed
+ * across sources and data types, so a surface can prefer the best-recorded
+ * score over whichever name sorts first. Client-side, from data already
+ * fetched.
+ */
+export const collectScoreNameCoverage = (
+ aggregates: (ScoreAggregate | null | undefined)[],
+): Map<string, number> => {
+ const coverage = new Map<string, number>();
+ for (const aggregate of aggregates) {
+ if (!aggregate) continue;
+ for (const [key, value] of Object.entries(aggregate)) {
+ const { name } = decomposeAggregateScoreKey(key);
+ coverage.set(name, (coverage.get(name) ?? 0) + value.values.length);
+ }
+ }
+ return coverage;
+};
+
export const getScoreLabelFromKey = (key: string): string => {
const { name, source, dataType } = decomposeAggregateScoreKey(key);
return `${getScoreDataTypeIcon(dataType)} ${name} (${source.toLowerCase()})`;
diff --git a/web/src/features/widgets/chart-library/Chart.tsx b/web/src/features/widgets/chart-library/Chart.tsx
index d2c7c27ab8..46c41bc2f9 100644
--- a/web/src/features/widgets/chart-library/Chart.tsx
+++ b/web/src/features/widgets/chart-library/Chart.tsx
@@ -1,4 +1,4 @@
-import React, { useState, useMemo } from "react";
+import React, { useState, useMemo, type ReactNode } from "react";
import {
type FormatMetricOptions,
type MetricFormatterFunction,
@@ -64,6 +64,9 @@ const ChartComponent = ({
thresholds,
missingValue,
hideXAxisLabels,
+ colorBarsByCategory,
+ zeroBaseline,
+ emptyState,
}: {
chartType: DashboardWidgetChartType;
data: DataPoint[];
@@ -102,6 +105,19 @@ const ChartComponent = ({
* dataset-compare charts.
*/
hideXAxisLabels?: boolean;
+ /**
+ * Colour each bar of a categorical axis and name it in a legend below the
+ * plot; see {@link ChartProps.colorBarsByCategory}. Consumed by VERTICAL_BAR.
+ */
+ colorBarsByCategory?: boolean;
+ /** See {@link ChartProps.zeroBaseline}. Consumed by VERTICAL_BAR. */
+ zeroBaseline?: boolean;
+ /**
+ * Replaces the default "No data" card when the chart has nothing to draw.
+ * For a chart in a band too short for that card (the table strips), where it
+ * would clip its own text. Pass a stable node so the memo still holds.
+ */
+ emptyState?: ReactNode;
}) => {
const [forceRender, setForceRender] = useState(overrideWarning);
const shouldWarn = data.length > 2000 && !forceRender;
@@ -148,12 +164,15 @@ const ChartComponent = ({
// isChartDataEmpty); skip while loading so a first paint doesn't flash
// "No data" before the real result arrives. (LFE-14333, manifesto
// principle 8)
+ // A caller that supplied its own empty state (a table strip) gets it for
+ // any chart type: its band is too short for the default card, whatever the
+ // mark.
if (
- EMPTY_STATE_CHART_TYPES.has(chartType) &&
+ (EMPTY_STATE_CHART_TYPES.has(chartType) || emptyState !== undefined) &&
!isLoading &&
isChartDataEmpty(data)
) {
- return <NoDataOrLoading isLoading={false} />;
+ return emptyState ?? <NoDataOrLoading isLoading={false} />;
}
switch (chartType) {
@@ -221,6 +240,9 @@ const ChartComponent = ({
config={resolvedConfig}
metricFormatter={metricFormatter}
subtleFill={chartConfig?.subtle_fill}
+ hideXAxisLabels={hideXAxisLabels}
+ colorBarsByCategory={colorBarsByCategory}
+ zeroBaseline={zeroBaseline}
/>
);
case "PIE":
diff --git a/web/src/features/widgets/chart-library/VerticalBarChart.tsx b/web/src/features/widgets/chart-library/VerticalBarChart.tsx
index b820328174..2ac56d8e85 100644
--- a/web/src/features/widgets/chart-library/VerticalBarChart.tsx
+++ b/web/src/features/widgets/chart-library/VerticalBarChart.tsx
@@ -1,4 +1,4 @@
-import React from "react";
+import React, { useMemo } from "react";
import {
ChartContainer,
ChartTooltip,
@@ -10,6 +10,40 @@ import {
formatMetric,
toFullMetricString,
} from "@/src/features/widgets/chart-library/utils";
+import { useChartTickBudget } from "@/src/features/widgets/chart-library/useChartTickBudget";
+import {
+ prepareCategoryBars,
+ type CategoryBarLegendItem,
+} from "@/src/features/widgets/chart-library/prepareCategoryBars";
+import { cn } from "@/src/utils/tailwind";
+
+/**
+ * Names every bar, in bar order, in ONE row that never wraps. This legend is
+ * built for a table strip's 63px band, where the wrapping/scrolling legend the
+ * time-series charts use would crowd out the plot it is explaining. Names share
+ * the row and ellipsize (full name in `title`), which still identifies runs
+ * whose names differ early β€” as model/prompt variants do.
+ */
+function CategoryBarLegend({ items }: { items: CategoryBarLegendItem[] }) {
+ return (
+ <div className="mt-1 flex h-4 min-w-0 shrink-0 items-center gap-x-3 overflow-hidden">
+ {items.map((item) => (
+ <div key={item.category} className="flex min-w-0 items-center gap-1.5">
+ <div
+ className="h-2 w-2 shrink-0 rounded-[2px]"
+ style={{ backgroundColor: item.color }}
+ />
+ <span
+ className="text-muted-foreground truncate text-xs leading-4"
+ title={item.category}
+ >
+ {item.category}
+ </span>
+ </div>
+ ))}
+ </div>
+ );
+}
/**
* VerticalBarChart component
@@ -30,53 +64,106 @@ export const VerticalBarChart: React.FC<ChartProps> = ({
accessibilityLayer = true,
metricFormatter = (value, options) => formatMetric(value, options),
subtleFill = false,
+ hideXAxisLabels = false,
+ colorBarsByCategory = false,
+ zeroBaseline = false,
}) => {
+ const { ref: containerRef, maxYTicks } = useChartTickBudget();
+
+ const categoryBars = useMemo(
+ () => (colorBarsByCategory ? prepareCategoryBars(data) : null),
+ [colorBarsByCategory, data],
+ );
+ const rows = categoryBars?.rows ?? data;
+ const legend = categoryBars?.legend ?? [];
+
const formatValue = (value: number) =>
toFullMetricString(metricFormatter(value, { style: "compact" }));
return (
- <ChartContainer
- config={config}
- className="[&_.recharts-bar-rectangle:hover]:opacity-30 dark:[&_.recharts-bar-rectangle:hover]:opacity-100 dark:[&_.recharts-bar-rectangle:hover]:brightness-[3]"
- >
- <BarChart accessibilityLayer={accessibilityLayer} data={data}>
- <XAxis
- type="category"
- dataKey="dimension"
- stroke="hsl(var(--chart-grid))"
- fontSize={12}
- tickLine={false}
- axisLine={false}
- niceTicks="auto"
- />
- <YAxis
- type="number"
- stroke="hsl(var(--chart-grid))"
- fontSize={12}
- tickLine={false}
- axisLine={false}
- tickFormatter={(value) => formatValue(Number(value))}
- />
- <Bar
- dataKey="metric"
- radius={[4, 4, 0, 0]}
- className="fill-(--color-metric)"
- fillOpacity={subtleFill ? 0.3 : 1}
- isAnimationActive={false}
- />
- <ChartTooltip
- cursor={false}
- contentStyle={{ backgroundColor: "hsl(var(--background))" }}
- content={({ active, payload, label }) => (
- <ChartTooltipContent
- active={active}
- payload={payload}
- label={label}
- valueFormatter={(v) => formatValue(Number(v))}
- />
- )}
- />
- </BarChart>
- </ChartContainer>
+ <div className="flex h-full w-full min-w-0 flex-col">
+ <ChartContainer
+ ref={containerRef}
+ config={config}
+ className="min-h-0 flex-1 [&_.recharts-bar-rectangle:hover]:opacity-30 dark:[&_.recharts-bar-rectangle:hover]:opacity-100 dark:[&_.recharts-bar-rectangle:hover]:brightness-[3]"
+ >
+ <BarChart accessibilityLayer={accessibilityLayer} data={rows}>
+ <XAxis
+ type="category"
+ dataKey="dimension"
+ stroke="hsl(var(--chart-grid))"
+ fontSize={12}
+ tickLine={false}
+ axisLine={false}
+ niceTicks="auto"
+ // Same treatment prepareTimeAxis gives a hidden categorical axis:
+ // every tick drawn, label-less, on a slim axis. The category stays
+ // in the legend and the tooltip.
+ {...(hideXAxisLabels
+ ? { interval: 0, tickFormatter: () => "", height: 8 }
+ : {})}
+ />
+ <YAxis
+ type="number"
+ stroke="hsl(var(--chart-grid))"
+ fontSize={12}
+ tickLine={false}
+ axisLine={false}
+ niceTicks="auto"
+ // `min(0, dataMin)` rather than a flat 0 so a negative series (a
+ // delta) still fits, and the max keeps `"auto"`, which is what lets
+ // niceTicks round the top of the scale.
+ {...(zeroBaseline
+ ? {
+ domain: [
+ (dataMin: number) => Math.min(0, dataMin),
+ "auto" as const,
+ ] as const,
+ }
+ : {})}
+ // Ask for only as many ticks as the measured height fits, so
+ // recharts never has to drop colliding labels β€” and so the zero
+ // tick, the one that says where a bar is measured from, survives in
+ // a short band.
+ tickCount={maxYTicks}
+ tickFormatter={(value) => formatValue(Number(value))}
+ />
+ <Bar
+ dataKey="metric"
+ radius={[4, 4, 0, 0]}
+ // A CSS `fill` beats the per-row `fill` attribute the preparer set,
+ // so the single-colour class only applies when there are no
+ // per-category colours.
+ className={cn(legend.length === 0 && "fill-(--color-metric)")}
+ fillOpacity={subtleFill ? 0.3 : 1}
+ isAnimationActive={false}
+ />
+ <ChartTooltip
+ cursor={false}
+ contentStyle={{ backgroundColor: "hsl(var(--background))" }}
+ content={({ active, payload, label }) => (
+ <ChartTooltipContent
+ active={active}
+ payload={payload}
+ label={label}
+ valueFormatter={(v) => formatValue(Number(v))}
+ />
+ )}
+ />
+ </BarChart>
+ </ChartContainer>
+ {legend.length > 0 ? (
+ <CategoryBarLegend items={legend} />
+ ) : (
+ // No legend and no axis labels: say where identity lives rather than
+ // leaving anonymous bars (manifesto principle 8).
+ colorBarsByCategory &&
+ hideXAxisLabels && (
+ <div className="text-muted-foreground mt-1 h-4 shrink-0 text-right text-[11px] leading-4">
+ Hover a bar to name it
+ </div>
+ )
+ )}
+ </div>
);
};
diff --git a/web/src/features/widgets/chart-library/chart-props.ts b/web/src/features/widgets/chart-library/chart-props.ts
index 75249022e6..ba5f316b75 100644
--- a/web/src/features/widgets/chart-library/chart-props.ts
+++ b/web/src/features/widgets/chart-library/chart-props.ts
@@ -150,4 +150,24 @@ export interface ChartProps {
* affects a categorical axis β€” a temporal axis keeps its timestamp labels.
*/
hideXAxisLabels?: boolean;
+ /**
+ * Give each bar of a categorical (entity) axis its own palette colour, plus a
+ * legend below the plot that names it. Off by default: a dashboard bar chart
+ * shows its categories on the axis and needs neither. Opt in where the axis
+ * labels are hidden and the bars are entities rather than buckets β€” the
+ * experiments strip, one bar per run. Colours and legend both come from
+ * `prepareCategoryBars`, which also decides when there are too many bars for
+ * the bounded palette to identify any of them.
+ */
+ colorBarsByCategory?: boolean;
+ /**
+ * Measure the bars from zero instead of from recharts' fitted domain. A bar
+ * encodes its value as a length, so this is what makes two bars comparable at
+ * all β€” on a fitted domain 0.80/0.87/1.00 draw as short/medium/full.
+ *
+ * Off by default only because it is a visible change to the bar charts that
+ * already ship without it (the score-analytics dashboard widget), which are
+ * out of scope here. Every vertical bar chart ought to have it.
+ */
+ zeroBaseline?: boolean;
}
diff --git a/web/src/features/widgets/chart-library/prepareCategoryBars.clienttest.ts b/web/src/features/widgets/chart-library/prepareCategoryBars.clienttest.ts
new file mode 100644
index 0000000000..e796bab979
--- /dev/null
+++ b/web/src/features/widgets/chart-library/prepareCategoryBars.clienttest.ts
@@ -0,0 +1,48 @@
+import { describe, expect, it } from "vitest";
+import {
+ CATEGORY_COLOR_LIMIT,
+ prepareCategoryBars,
+} from "@/src/features/widgets/chart-library/prepareCategoryBars";
+import { type DataPoint } from "@/src/features/widgets/chart-library/chart-props";
+
+const bars = (count: number): DataPoint[] =>
+ Array.from({ length: count }, (_, index) => ({
+ time_dimension: `run-${index}`,
+ dimension: `run-${index}`,
+ metric: index,
+ }));
+
+describe("prepareCategoryBars", () => {
+ it("colours every bar and names it while the palette can identify", () => {
+ const prepared = prepareCategoryBars(bars(CATEGORY_COLOR_LIMIT));
+
+ expect(prepared.legend).toHaveLength(CATEGORY_COLOR_LIMIT);
+ expect(new Set(prepared.legend.map((item) => item.color)).size).toBe(
+ CATEGORY_COLOR_LIMIT,
+ );
+ // The swatch is the bar's fill, not a parallel lookup.
+ expect(prepared.rows.map((row) => row.fill)).toEqual(
+ prepared.legend.map((item) => item.color),
+ );
+ });
+
+ it("drops the legend once the palette would repeat a colour", () => {
+ const prepared = prepareCategoryBars(bars(CATEGORY_COLOR_LIMIT + 1));
+
+ expect(prepared.legend).toEqual([]);
+ expect(prepared.total).toBe(CATEGORY_COLOR_LIMIT + 1);
+ expect(prepared.rows.every((row) => row.fill === undefined)).toBe(true);
+ });
+
+ it("keys colour by category, so repeated categories match", () => {
+ const prepared = prepareCategoryBars([
+ { time_dimension: "a", dimension: "a", metric: 1 },
+ { time_dimension: "b", dimension: "b", metric: 2 },
+ { time_dimension: "a", dimension: "a", metric: 3 },
+ ]);
+
+ expect(prepared.total).toBe(2);
+ expect(prepared.rows[0].fill).toBe(prepared.rows[2].fill);
+ expect(prepared.rows[0].fill).not.toBe(prepared.rows[1].fill);
+ });
+});
diff --git a/web/src/features/widgets/chart-library/prepareCategoryBars.ts b/web/src/features/widgets/chart-library/prepareCategoryBars.ts
new file mode 100644
index 0000000000..45c3dfcbb4
--- /dev/null
+++ b/web/src/features/widgets/chart-library/prepareCategoryBars.ts
@@ -0,0 +1,67 @@
+import { type DataPoint } from "@/src/features/widgets/chart-library/chart-props";
+import { seriesColor } from "@/src/features/widgets/chart-library/TimeSeriesLegend";
+
+/**
+ * How many bars a colour can still IDENTIFY. The chart palette holds 8 slots and
+ * `seriesColor` cycles them, so from the ninth bar on two bars share a colour
+ * and a swatch names nothing β€” a legend built on it would be actively wrong
+ * about which bar is which. Past the limit we colour every bar alike and leave
+ * identity to the tooltip, rather than shipping a legend that lies.
+ * (ARCHITECTURE.md V6: colour is identity, and the palette is bounded.)
+ */
+export const CATEGORY_COLOR_LIMIT = 8;
+
+type CategoryBarRow = DataPoint & { fill?: string };
+
+export type CategoryBarLegendItem = { category: string; color: string };
+
+export type CategoryBars = {
+ /**
+ * Rows for recharts, each carrying its own `fill` when colour can identify.
+ * A per-row `fill` is the recharts idiom the tooltip already reads back for
+ * its swatch (see `getFillColor`), so bar, legend and tooltip cannot diverge.
+ */
+ rows: CategoryBarRow[];
+ /** One entry per bar, in bar order β€” empty when colour cannot identify. */
+ legend: CategoryBarLegendItem[];
+ /** Distinct categories, so a caller can say so when there is no legend. */
+ total: number;
+};
+
+/**
+ * Preparer (data -> visualiser seam): decides whether a bar chart's categorical
+ * axis can be identified by colour, and if so assigns the colours and the
+ * legend that reads them back β€” one derivation, so swatch and fill are the same
+ * value rather than two lookups that agree by luck.
+ *
+ * Colour is keyed by a category's FIRST appearance, not by row index, so two
+ * rows of the same category always share a colour.
+ *
+ * Pure and side-effect free β€” see ARCHITECTURE.md.
+ */
+export function prepareCategoryBars(data: DataPoint[]): CategoryBars {
+ const order = new Map<string, number>();
+ for (const point of data) {
+ const category = point.dimension;
+ if (category === undefined || order.has(category)) continue;
+ order.set(category, order.size);
+ }
+
+ const total = order.size;
+ if (total === 0 || total > CATEGORY_COLOR_LIMIT) {
+ return { rows: data, legend: [], total };
+ }
+
+ return {
+ rows: data.map((point) =>
+ point.dimension === undefined
+ ? point
+ : { ...point, fill: seriesColor(order.get(point.dimension) ?? 0) },
+ ),
+ legend: Array.from(order, ([category, index]) => ({
+ category,
+ color: seriesColor(index),
+ })),
+ total,
+ };
+}
diff --git a/web/src/features/widgets/chart-library/useChartTickBudget.ts b/web/src/features/widgets/chart-library/useChartTickBudget.ts
index 0e75672e00..be845457cb 100644
--- a/web/src/features/widgets/chart-library/useChartTickBudget.ts
+++ b/web/src/features/widgets/chart-library/useChartTickBudget.ts
@@ -5,29 +5,47 @@ import { useEffect, useRef, useState } from "react";
const APPROX_LABEL_PX = 64;
const AXIS_GUTTER_PX = 56;
+// Vertical room one y-axis label needs. Below it recharts silently drops
+// colliding labels and which ones survive is arbitrary β€” a 63px band came out
+// labelled "1" and "0.25", with no zero.
+const APPROX_Y_LABEL_PX = 28;
+
+/** Recharts' own YAxis default; the budget only ever thins below it. */
+const DEFAULT_Y_TICK_COUNT = 5;
+
/**
- * Measures the chart's width and returns how many x-axis ticks comfortably fit
- * (`maxTicks`). The preparer (`prepareTimeAxis`) turns that budget into the
- * actual tick interval + labels β€” this hook only measures. (LFE-10549)
+ * Measures the chart's box and returns how many ticks comfortably fit on each
+ * axis (`maxTicks` across, `maxYTicks` down). The preparer (`prepareTimeAxis`)
+ * turns the x budget into the actual tick interval + labels β€” this hook only
+ * measures.
*/
export function useChartTickBudget() {
const ref = useRef<HTMLDivElement>(null);
- const [width, setWidth] = useState(0);
+ const [size, setSize] = useState({ width: 0, height: 0 });
useEffect(() => {
const element = ref.current;
if (!element) return;
const observer = new ResizeObserver((entries) => {
- setWidth(entries[0]?.contentRect.width ?? 0);
+ const box = entries[0]?.contentRect;
+ setSize({ width: box?.width ?? 0, height: box?.height ?? 0 });
});
observer.observe(element);
return () => observer.disconnect();
}, []);
const maxTicks =
- width > 0
- ? Math.max(2, Math.floor((width - AXIS_GUTTER_PX) / APPROX_LABEL_PX))
+ size.width > 0
+ ? Math.max(2, Math.floor((size.width - AXIS_GUTTER_PX) / APPROX_LABEL_PX))
: 6;
- return { ref, maxTicks };
+ const maxYTicks =
+ size.height > 0
+ ? Math.min(
+ DEFAULT_Y_TICK_COUNT,
+ Math.max(2, Math.floor(size.height / APPROX_Y_LABEL_PX)),
+ )
+ : DEFAULT_Y_TICK_COUNT;
+
+ return { ref, maxTicks, maxYTicks };
}
diff --git a/web/src/features/widgets/components/InlineWidget.tsx b/web/src/features/widgets/components/InlineWidget.tsx
index 1cffdc8c11..0edf47479a 100644
--- a/web/src/features/widgets/components/InlineWidget.tsx
+++ b/web/src/features/widgets/components/InlineWidget.tsx
@@ -1,5 +1,5 @@
/* eslint-disable @repo/no-style-props */
-import { useMemo, useState, useCallback } from "react";
+import { useMemo, useState, useCallback, type ReactNode } from "react";
import { type DashboardWidgetChartType } from "@langfuse/shared/src/db";
import { type OrderByState } from "@langfuse/shared";
import {
@@ -82,6 +82,23 @@ export interface WidgetContentProps {
* charts (experiments) whose long names clutter the axis.
*/
hideXAxisLabels?: boolean;
+ /**
+ * Colour each bar of a categorical (entity) axis and name it in a legend
+ * below the plot. Off by default; opt in on an entity-dimension bar chart
+ * whose axis labels are hidden (the experiments strip). See
+ * `prepareCategoryBars`.
+ */
+ colorBarsByCategory?: boolean;
+ /**
+ * Measure bars from zero rather than from a fitted domain. Off by default;
+ * see `ChartProps.zeroBaseline`.
+ */
+ zeroBaseline?: boolean;
+ /**
+ * Replaces the chart's default "No data" card β€” for a widget in a band too
+ * short for it. Pass a stable node (see `Chart`).
+ */
+ emptyState?: ReactNode;
}
// ============================================================================
@@ -128,6 +145,9 @@ export function WidgetContent({
className,
entityDimensionLabelMap,
hideXAxisLabels,
+ colorBarsByCategory,
+ zeroBaseline,
+ emptyState,
}: WidgetContentProps) {
// Transport-only: `version` is a prop here, so an unresolved session can
// never change WHAT is queried β€” only whether it streams (SSE) or not.
@@ -233,9 +253,21 @@ export function WidgetContent({
};
}
+ // A non-time-series chart draws `dimension` on its categorical axis, and
+ // on an entity query the ENTITY *is* that category β€” so hand it over as
+ // the dimension rather than the metric's name, which would collapse every
+ // entity into one bar. Only when the query has no breakdown of its own:
+ // that breakdown is the real series (categorical scores).
+ const entityIsCategory =
+ !isTimeSeries &&
+ item["entity_dimension"] !== undefined &&
+ dimensions.length === 0;
+
// Handle series dimension (for legend)
let seriesDimension: string;
- if (dimensionValue !== undefined) {
+ if (entityIsCategory) {
+ seriesDimension = xAxisValue ?? "Unknown";
+ } else if (dimensionValue !== undefined) {
const val = dimensionValue;
// Empty first: "" is a string, so the order matters. (LFE-10694)
if (val === null || val === undefined || val === "") {
@@ -267,8 +299,9 @@ export function WidgetContent({
// Entity-dimension charts have no meaningful query-side order (the server
// falls back to first-metric DESC, which differs per chart). Order the
- // x-axis to match the experiments table order provided via
- // entityDimensionLabelMap so the same entity lines up across chart slots.
+ // x-axis by the caller's entityDimensionLabelMap insertion order, so the
+ // caller decides what left-to-right means (the experiments strip makes it
+ // chronological). Entities the map doesn't know sort last.
if (
chartType !== "PIVOT_TABLE" &&
entityDimensionLabelMap &&
@@ -283,8 +316,8 @@ export function WidgetContent({
.slice()
.sort(
(a, b) =>
- (order.get(b.time_dimension ?? "") ?? Number.MAX_SAFE_INTEGER) -
- (order.get(a.time_dimension ?? "") ?? Number.MAX_SAFE_INTEGER),
+ (order.get(a.time_dimension ?? "") ?? Number.MAX_SAFE_INTEGER) -
+ (order.get(b.time_dimension ?? "") ?? Number.MAX_SAFE_INTEGER),
);
}
@@ -392,6 +425,9 @@ export function WidgetContent({
metricFormatter={chartPresentation?.metricFormatter}
missingValue={getWidgetMissingBucketValue(metrics[0]?.agg ?? "count")}
hideXAxisLabels={hideXAxisLabels}
+ colorBarsByCategory={colorBarsByCategory}
+ zeroBaseline={zeroBaseline}
+ emptyState={emptyState}
/>
<ChartLoadingState
isLoading={chartLoadingState.isLoading}
diff --git a/web/src/features/widgets/utils.ts b/web/src/features/widgets/utils.ts
index 6cb282c881..755a071606 100644
--- a/web/src/features/widgets/utils.ts
+++ b/web/src/features/widgets/utils.ts
@@ -19,6 +19,9 @@ export type WidgetChartConfig = {
type: DashboardWidgetChartType;
row_limit?: number;
bins?: number;
+ /** Draw a dot per sample (line/area). Off by default β€” chart junk on a dense
+ * series, but the only thing that shows where a sparse one has data. */
+ show_data_point_dots?: boolean;
defaultSort?: {
column: string;
order: "ASC" | "DESC";