import ChartFrame, { type LegendItem } from '../chart/ChartFrame' import type { ReactNode } from 'react' import type { DotsSpec } from '../../utils/stanceGame' import { GROUP_COLORS } from '../../utils/houseTheme' import { countsSentence, specDisplay, topCategories, type CategoryCount } from '../../utils/displayFit' import BoxWaffleChart from './BoxWaffleChart' const fmt = (n: number) => n.toLocaleString('en-US') /** "data center project" โ†’ "data center projects" (the spec's units are all regular). */ const plural = (unit: string, n: number) => (n === 1 ? unit : `${unit}s`) const whereIn = (scope: string) => (scope === 'National' ? 'nationally' : `in ${scope}`) /** The records behind a spec, one row each: name (a link when it has a source), status, place and date. */ export function RecordList({ spec, showStatus }: { spec: DotsSpec; showStatus: boolean }) { return ( ) } /** Sorted horizontal bars labelled with their counts (no percentages); 8 or more categories fold into the top 7 + "Other". */ export function CountBars({ rows, label }: { rows: CategoryCount[]; label: string }) { const top = topCategories(rows) const max = Math.max(1, ...top.map((r) => r.count)) const sum = top.reduce((a, r) => a + r.count, 0) const lead = top[0] // The title leads with the biggest bar; the key says what a bar is and what grey means (#1409). const title = lead && top.length > 1 && sum >= 5 ? `${label}: ${lead.label} leads with ${fmt(lead.count)} of ${fmt(sum)}` : label const legend: LegendItem[] = [ { key: 'bar', label: 'Bar length = how many, count at the right', color: 'var(--teal, #0f766e)' }, ...(top.some((r) => r.other) ? [{ key: 'other', label: 'Other: the smaller groups together', color: 'var(--ink-2)' }] : []), ] return (
    {top.map((r) => (
  1. {r.label}
  2. ))}
) } interface Props { spec: DotsSpec /** "Alabama", "Tuscaloosa, Alabama" or "National". */ scopeLabel: string /** The dot or unit chart, drawn only when the rule picks it (about 20โ€“200 individual items). */ units: ReactNode /** Under the bars (a data table toggle); not shown with a list or a sentence, which already are the data. */ barsExtra?: ReactNode } /** * One panel's figure, in the form the shared rule (utils/displayFit) picks from its count (#1289): a list for a handful of * records, a sentence for a few counts, sorted count bars for more, the unit chart only for about 20โ€“200 records. No panel * picks its own form. */ export default function FitDisplay({ spec, scopeLabel, units, barsExtra }: Props) { const { display, rows, items } = specDisplay(spec) const where = whereIn(scopeLabel) // Without a whole-scope total the list may be capped (the source note says so): never state it as the total. const lead = `${spec.grandTotal == null ? 'At least ' : ''}${fmt(items)} ${plural(spec.unit, items)} on record ${where}` const live = rows.filter((r) => r.count > 0) const records = !spec.countsOnly && spec.groups.some((g) => g.dots.length > 0) switch (display.kind) { case 'empty': return null case 'list': return (
{records ? ( <>

{lead} {live.length > 1 ? `. ${countsSentence(rows)}` : ':'}

) : (

{lead}. {live.length > 1 ? countsSentence(rows) : ''}

)}
) case 'sentence': case 'line': return (

{lead}. {countsSentence(rows)}

{records && (
Show the {fmt(spec.drawn)} {plural(spec.unit, spec.drawn)}
)}
) case 'bars': case 'top-bars': { const waffleRows = topCategories(live) const rowsTotal = waffleRows.reduce((s, r) => s + r.count, 0) const waffleDrawn = rowsTotal >= 20 && waffleRows.length >= 2 return (

{lead}.

{waffleDrawn && ( ({ key: r.label, label: r.label, count: r.count, color: GROUP_COLORS[i % GROUP_COLORS.length]!, }))} title={spec.title} unitLabel={plural(spec.unit, rowsTotal)} replayKey={`${spec.title}|${scopeLabel}|${rowsTotal}`} /> )} {/* One visual per screen (#1415): the boxes carry the counts in their legend chips, so the bars are drawn only when the boxes are not. */} {!waffleDrawn && } {barsExtra}
) } case 'units': return (
{units}
) } }