CoverageCheck / web /src /components /evals /GroupChart.tsx
TBM99's picture
Deploy coverage-check 3559250
23d418f verified
Raw History Blame Contribute Delete
5.24 kB
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<View>('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<SystemId, string>
return (
<div className="space-y-3">
<div className="flex flex-wrap items-center justify-between gap-2">
<div role="tablist" aria-label="Breakdown" className="inline-flex rounded-lg border p-0.5 text-xs">
{(Object.keys(VIEWS) as View[]).map((v) => (
<button
key={v}
role="tab"
aria-selected={view === v}
onClick={() => setView(v)}
className={cn('rounded-md px-3 py-1', view === v ? 'bg-accent font-medium text-accent-foreground' : 'text-muted-foreground')}
>
{VIEWS[v].label}
</button>
))}
</div>
<Legend names={names} />
</div>
<div className="h-72" role="img" aria-label={`Accuracy ${VIEWS[view].label.toLowerCase()}, systems A, B and C; the table below has every value`}>
<ResponsiveContainer width="100%" height="100%">
<BarChart data={rows} margin={{ top: 20, right: 8, bottom: 0, left: -12 }} barGap={2} barCategoryGap="22%">
<CartesianGrid vertical={false} stroke={INK.grid} />
<XAxis dataKey="name" tickLine={false} axisLine={{ stroke: INK.axis }} tick={{ fill: INK.secondary, fontSize: 12 }} />
<YAxis
domain={[0, 1]}
ticks={[0, 0.25, 0.5, 0.75, 1]}
tickFormatter={(v: number) => percent(v)}
tickLine={false}
axisLine={false}
tick={{ fill: INK.muted, fontSize: 11 }}
/>
<Tooltip cursor={{ fill: 'rgba(148,163,184,0.12)' }} content={<GroupTooltip />} />
{ORDER.map((s) => (
<Bar key={s} dataKey={s} name={`System ${s.toUpperCase()}`} fill={SERIES[s]} maxBarSize={24} radius={[4, 4, 0, 0]} isAnimationActive={false}>
<ErrorBar dataKey={`${s}_err`} width={4} stroke={INK.secondary} strokeWidth={1} />
{s === 'c' && (
<LabelList dataKey="c" position="top" offset={14} formatter={(v) => percent(Number(v))} style={{ fill: INK.primary, fontSize: 11, fontWeight: 600 }} />
)}
</Bar>
))}
</BarChart>
</ResponsiveContainer>
</div>
<details className="border-t pt-3">
<summary className="cursor-pointer text-xs font-medium text-primary">Show the numbers</summary>
<div className="mt-2 overflow-x-auto">
<NumbersTable
head={['Group', 'n', ...ORDER.map((s) => `System ${s.toUpperCase()} (95% interval)`)]}
rows={groups.map((g) => [VIEWS[view].name(g.group), g.n, ...ORDER.map((s) => cell(g, s))])}
/>
</div>
</details>
</div>
)
}
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<string, unknown> }[] }
function GroupTooltip({ active, payload }: TooltipProps) {
if (!active || !payload?.length) return null
const row = payload[0].payload
return (
<div className="rounded-lg border bg-background px-3 py-2 text-xs shadow-md">
<p className="mb-1 font-medium">
{String(row.name)} <span className="font-normal text-muted-foreground">· {String(row.n)} questions</span>
</p>
{ORDER.map((s) => {
const ci = row[`${s}_ci`] as [number, number] | undefined
return row[s] === undefined ? null : (
<p key={s} className="flex items-center gap-1.5 tabular-nums">
<span className="size-2 rounded-sm" style={{ background: SERIES[s] }} aria-hidden />
System {s.toUpperCase()}: <span className="font-medium">{percent(Number(row[s]))}</span>
{ci && <span className="text-muted-foreground">({percent(ci[0])}-{percent(ci[1])})</span>}
</p>
)
})}
</div>
)
}