import { useMemo, useRef } from 'react' import { censusMetricRankDirection } from '../../utils/censusDataDictionary' import { niceScale } from '../../utils/matrixMapMath' import { changeTone } from '../../utils/matrixTable' import { compareStory } from '../../utils/matrixStory' import { signedChange } from './data' import { useElementWidth, useSection } from './hooks' import { Empty, Seg } from './parts' import SectionShell from './SectionShell' import { Tooltip, useTooltip } from './Tooltip' import type { MatrixCtx, Row } from './types' type SortBy = 'latest' | 'change' | 'base' interface Pt { r: Row a: number | null c: number | null d: number | null } /** Compare (dumbbell): the earliest published vintage against the selected one, per geography. */ export default function CompareSection({ ctx }: { ctx: MatrixCtx }) { const { vintage, vintages, metricSlug: slug, fmt } = ctx const base = vintages[0] ?? '' const hasPair = !!base && base !== vintage const baseRows = hasPair ? ctx.rowsByVintage[base] : undefined const sortBy: SortBy = ctx.sortRaw === 'change' ? 'change' : ctx.sortRaw === 'base' ? 'base' : 'latest' const lowerBetter = censusMetricRankDirection(slug) === 'lower' const pts: Pt[] = useMemo(() => { if (!baseRows) return [] const byFips = new Map(baseRows.map((r) => [r.fips, r])) const list = ctx.rows.map((r) => { const a = byFips.get(r.fips)?.vals[slug] ?? null const c = r.vals[slug] ?? null return { r, a, c, d: a != null && c != null ? c - a : null } }) const dir = ctx.sortDir === 'asc' ? 1 : -1 const key = (p: Pt) => (sortBy === 'change' ? p.d : sortBy === 'base' ? p.a : p.c) return list.sort((x, y) => { const kx = key(x) const ky = key(y) if (kx == null && ky == null) return x.r.name.localeCompare(y.r.name) if (kx == null) return 1 if (ky == null) return -1 return (kx - ky) * dir || x.r.name.localeCompare(y.r.name) }) }, [baseRows, ctx.rows, slug, sortBy, ctx.sortDir]) const set = (v: SortBy) => ctx.setMany({ sort: v === 'latest' ? null : v }) const withBoth = pts.filter((p) => p.d != null) const ups = withBoth.filter((p) => p.d! > 0).length const downs = withBoth.filter((p) => p.d! < 0).length const name = hasPair ? `${ctx.metricLabel}, ${base} to ${vintage}` : `${ctx.metricLabel}, ${vintage}` const story = hasPair && baseRows === undefined ? { headline: `${ctx.metricLabel}, ${base} against ${vintage}.`, lead: null } : compareStory({ metricLabel: ctx.metricLabel, base: hasPair ? base : null, vintage, pairs: pts.map((p) => ({ name: p.r.name, from: p.a, to: p.c })), fmt: (v) => fmt(slug, v), fmtChange: (a, b) => signedChange(slug, a, b, ctx.metrics, fmt), }) let body: React.ReactNode if (!hasPair) { body = ( No data available — this needs two vintages. {vintages.length < 2 ? 'Only one vintage is published, so there is nothing to compare.' : `Choose a vintage later than ${base} to compare with it.`} ) } else if (baseRows === undefined) body = Loading {base}… else if (baseRows === null) body = No data available — the {base} state file could not be loaded, so there is nothing to compare with. else if (!pts.some((p) => p.a != null || p.c != null)) body = No data available for {ctx.metricLabel}. else body = return (

{hasPair ? `Each row is one geography: a hollow dot for ${base} and a filled dot for ${vintage}, joined by a line that grows from the first to the second. ` : 'Each row would be one geography, with a hollow dot for the earlier year and a filled dot for the selected one. '} The right-hand column is the change, in the metric's own unit; it reads “—” when either vintage is missing.

{hasPair && baseRows && (

{withBoth.length} geographies have both readings: {ups} {lowerBetter ? 'rose' : 'higher'}, {downs} {lowerBetter ? 'fell' : 'lower'}. Sort by {vintage}, by change or by {base}; again to reverse.

)} } controls={ hasPair ? ( set(v as SortBy)} options={[ { value: 'latest', label: `By ${vintage}` }, { value: 'change', label: 'By change' }, { value: 'base', label: `By ${base}` }, ]} /> ) : undefined } caption={ <> {name}, one row per geography. Source: ACS 5-year, {hasPair ? `${base} and ${vintage}` : vintage}. } table={() => hasPair && baseRows ? { columns: [{ label: 'Geography' }, { label: base, numeric: true }, { label: vintage, numeric: true }, { label: 'Change', numeric: true }], rows: pts.map((p) => [p.r.name, fmt(slug, p.a), fmt(slug, p.c), signedChange(slug, p.a, p.c, ctx.metrics, fmt)]), } : null } > {body}
) } const ROW_H = 26 const TOP = 34 function Dumbbell({ ctx, pts, base, lowerBetter, title }: { ctx: MatrixCtx; pts: Pt[]; base: string; lowerBetter: boolean; title: string }) { const { seen } = useSection() const host = useRef(null) const w = useElementWidth(host) const tt = useTooltip() const slug = ctx.metricSlug const revealed = seen || ctx.reduced const compact = w < 460 const labelW = compact ? 0 : Math.min(120, Math.round(w * 0.24)) const deltaW = compact ? 0 : 74 const plotL = compact ? 10 : labelW const plotR = compact ? w - 14 : w - deltaW const rH = compact ? 44 : ROW_H const all = pts.flatMap((p) => [p.a, p.c]).filter((x): x is number => x != null) const sc = niceScale(Math.min(...all), Math.max(...all), compact ? 3 : 5) const x = (v: number) => plotL + ((v - sc.min) / (sc.max - sc.min || 1)) * (plotR - plotL) const h = TOP + pts.length * rH + 30 const good = 'var(--violet)' const bad = 'var(--bad)' const dirColor = (d: number) => (changeTone(d, lowerBetter) === 'good' ? good : bad) const dirInk = (d: number) => (changeTone(d, lowerBetter) === 'good' ? 'var(--violet-ink)' : 'var(--bad-ink)') return (

{title}

{base} (base) {ctx.vintage} (latest) {lowerBetter ? ' better (lower is better for this metric)' : ' higher'} {lowerBetter ? ' worse' : ' lower'}
{sc.ticks.map((t) => ( {ctx.fmt(slug, t)} ))} {pts.map((p, idx) => { const y = TOP + idx * rH const trackY = compact ? 30 : rH / 2 const xa = p.a != null ? x(p.a) : null const xc = p.c != null ? x(p.c) : null const delay = ctx.reduced ? 0 : idx * 60 const info = { cat: p.r.name, val: `${ctx.fmt(slug, p.a)} → ${ctx.fmt(slug, p.c)}`, sub: `Change: ${signedChange(slug, p.a, p.c, ctx.metrics, ctx.fmt)} (${base} to ${ctx.vintage})` } return ( {p.r.name} {xa != null && xc != null && ( )} {xa != null && ( )} {xc != null && ( )} {signedChange(slug, p.a, p.c, ctx.metrics, ctx.fmt)} ) })}
) }