import { useState } from 'react' import { Bar, BarChart, CartesianGrid, ErrorBar, LabelList, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts' import { chartRow, INK, ORDER, SERIES } from '@/lib/chart' import { percent } from '@/lib/format' import type { EvalsFile, GroupRow, SystemId } from '@/lib/types' import { cn } from '@/lib/utils' import { Legend, NumbersTable } from './Section' const VIEWS = { docs: { label: 'By documents needed', name: (g: string) => (g === '1' ? '1 document' : `${g} documents`) }, category: { label: 'By question type', name: (g: string) => g }, } as const type View = keyof typeof VIEWS /** Accuracy per group (documents a question needs, or its type), A / B / C side by side, * each bar with its 95% interval. Grouped columns: the job is telling the three systems * apart within each group. Only C's bars carry a value label (the story; labels are * sparing), the tooltip and the table carry the rest. */ export function GroupChart({ data }: { data: EvalsFile }) { const [view, setView] = useState('docs') const groups = view === 'docs' ? data.by_docs : data.by_category const rows = groups.map((g) => chartRow(g, VIEWS[view].name(g.group))) const names = Object.fromEntries(ORDER.map((s) => [s, data.systems[s].name])) as Record return (
{(Object.keys(VIEWS) as View[]).map((v) => ( ))}
percent(v)} tickLine={false} axisLine={false} tick={{ fill: INK.muted, fontSize: 11 }} /> } /> {ORDER.map((s) => ( {s === 'c' && ( percent(Number(v))} style={{ fill: INK.primary, fontSize: 11, fontWeight: 600 }} /> )} ))}
Show the numbers
`System ${s.toUpperCase()} (95% interval)`)]} rows={groups.map((g) => [VIEWS[view].name(g.group), g.n, ...ORDER.map((s) => cell(g, s))])} />
) } function cell(g: GroupRow, s: SystemId): string { const x = g[s] return x ? `${percent(x.accuracy)} (${percent(x.ci[0] ?? 0)}-${percent(x.ci[1] ?? 0)})` : '' } type TooltipProps = { active?: boolean; payload?: { payload: Record }[] } function GroupTooltip({ active, payload }: TooltipProps) { if (!active || !payload?.length) return null const row = payload[0].payload return (

{String(row.name)} ยท {String(row.n)} questions

{ORDER.map((s) => { const ci = row[`${s}_ci`] as [number, number] | undefined return row[s] === undefined ? null : (

System {s.toUpperCase()}: {percent(Number(row[s]))} {ci && ({percent(ci[0])}-{percent(ci[1])})}

) })}
) }