"use client"; import * as React from "react"; import { Bar, BarChart, CartesianGrid, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts"; import { formatCompact, formatNumber } from "@/lib/format"; import { AXIS_TICK, CHART_COLORS, CHART_MARGIN, formatBucketLabel, formatTick } from "./chart-theme"; import { ChartTooltip } from "./chart-tooltip"; import { ChartLegend } from "./chart-frame"; export interface RequestsBarPoint { t: string; success: number; failed: number; } /** Stacked success/failed bars per bucket. Legend + 1px surface gap between segments keep the two readable without color. */ export function RequestsBarChart({ data, bucket, height = 240 }: { data: RequestsBarPoint[]; bucket: "hour" | "day"; height?: number }) { const totalSuccess = data.reduce((a, d) => a + d.success, 0); const totalFailed = data.reduce((a, d) => a + d.failed, 0); const tickEvery = Math.max(1, Math.ceil(data.length / (bucket === "hour" ? 8 : 10))); return (
formatTick(v, bucket)} tick={AXIS_TICK} axisLine={false} tickLine={false} interval={tickEvery - 1} minTickGap={16} /> formatCompact(v)} /> formatBucketLabel(String(l), bucket)} names={{ success: "Successful", failed: "Failed" }} footer={(p) => { const total = p.reduce((a, e) => a + Number(e.value ?? 0), 0); return ( Total {formatNumber(total)} ); }} /> } />
); }