"use client"; import * as React from "react"; import { Bar, BarChart, CartesianGrid, Cell, LabelList, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts"; import { AXIS_TICK, CHART_COLORS } from "./chart-theme"; import { ChartTooltip } from "./chart-tooltip"; export type HorizontalBarFormat = "number" | "percent" | "ms" | "usd"; const FORMATTERS: Record string> = { number: (v) => v.toLocaleString("en-US"), percent: (v) => `${v.toFixed(1)}%`, ms: (v) => (v < 1000 ? `${Math.round(v)} ms` : `${(v / 1000).toFixed(2)} s`), usd: (v) => (v < 0.01 && v > 0 ? `$${v.toFixed(4)}` : `$${v.toFixed(2)}`), }; export interface HorizontalBarDatum { label: string; value: number; /** Optional per-entity color (defaults to accent). */ color?: string; /** Optional secondary text shown in the tooltip (e.g. request count). */ hint?: string; } /** * Single-series horizontal bars (one color for all bars unless the entity has a fixed color). * Used for success rate by network and error-code breakdowns. */ export function HorizontalBarChart({ data, color = CHART_COLORS.accent, max, format = "number", rowHeight = 34, labelWidth = 120, }: { data: HorizontalBarDatum[]; color?: string; max?: number; /** Serializable formatter key (server components cannot pass functions to client components). */ format?: HorizontalBarFormat; rowHeight?: number; labelWidth?: number; }) { const valueFormatter = FORMATTERS[format]; const height = Math.max(rowHeight * Math.max(data.length, 1) + 16, 80); return ( String(l)} valueFormatter={(v) => valueFormatter(v)} names={{ value: "Value" }} footer={(p) => { const hint = (p[0]?.payload as HorizontalBarDatum | undefined)?.hint; return hint ? {hint} : null; }} /> } /> {data.map((d) => ( ))} valueFormatter(Number(v))} style={{ fill: "var(--fg-muted)", fontSize: 11.5, fontFamily: "var(--font-mono)" }} /> ); }