/** Graphiques SVG QC26 (rendu serveur possible, sans dépendance). */ import type { ReactNode } from "react"; import type { TrendPoint } from "@/lib/api"; import { OTHERS, PARTY_IDS, partyColor, partyLabel } from "@/lib/parties"; import { cls } from "@/lib/format"; // -------------------------------------------------------------- SeatMeter /** Barre de sièges empilée, seuil de majorité marqué (dérivé des données, jamais codé en dur). */ export function SeatMeter({ seats, total, majority, height = 22, showLabels = true, className, title }: { seats: Record; total: number; majority: number; height?: number; showLabels?: boolean; className?: string; title?: string; }) { const entries = Object.entries(seats).filter(([id]) => id !== "aut").sort((a, b) => b[1] - a[1]); const others = seats["aut"] ?? 0; let x = 0; const majX = (majority / total) * 100; return (
{entries.map(([id, n]) => { const w = (n / total) * 100; const el =
; x += w; return el; })} {others > 0 &&
}
{majority} · Majorité
{showLabels && (
{entries.map(([id, n]) => ( {partyLabel(id)} {n} ))} {others > 0 && Autres {others}}
)}
); } // ------------------------------------------------------- SeatDistribution /** Distribution des sièges d'un parti (histogramme Monte Carlo) avec seuil de majorité et intervalles. */ export function SeatDistribution({ dist, color, majority, median, lo80, hi80, lo95, hi95, height = 120, className, total }: { dist: number[]; color: string; majority: number; median: number; lo80: number; hi80: number; lo95: number; hi95: number; height?: number; className?: string; total: number; }) { const n = dist.length; const max = Math.max(...dist, 1); const W = 640, H = height, pad = 18; const bw = (W - 2 * pad) / n; const xs = (i: number) => pad + i * bw; const vis = dist.map((v, i) => [i, v] as const).filter(([, v]) => v > 0); const minI = Math.max(0, (vis[0]?.[0] ?? 0) - 2), maxI = Math.min(n - 1, (vis[vis.length - 1]?.[0] ?? n - 1) + 2); const span = Math.max(1, maxI - minI); const xz = (i: number) => pad + ((i - minI) / span) * (W - 2 * pad); const bwz = (W - 2 * pad) / span; return ( {dist.map((v, i) => (i < minI || i > maxI || v === 0) ? null : ( = majority ? 1 : 0.55} rx={1} /> ))} {majority >= minI && majority <= maxI && ( <> {majority} · majorité )} {[minI, Math.round((minI + maxI) / 2), maxI].map((i) => ( {i} ))} {median} {`Sur ${total} sièges`} ); } // ----------------------------------------------------------------- Trend export function TrendChart({ trend, polls, from, height = 300, showBand = true, ids = PARTY_IDS, className, markers = [], election2022 }: { trend: TrendPoint[]; polls?: { date: string; values: Record; id?: string }[]; from?: string; height?: number; showBand?: boolean; ids?: string[]; className?: string; markers?: { date: string; label: string }[]; election2022?: Record | null; }) { const pts = from ? trend.filter((t) => t.date >= from) : trend; if (pts.length < 2) return
Pas assez de données.
; const W = 900, H = height, pl = 34, pr = 78, pt = 12, pb = 28; const t0 = new Date(pts[0].date).getTime(), t1 = new Date(pts[pts.length - 1].date).getTime(); const x = (d: string) => pl + ((new Date(d).getTime() - t0) / Math.max(1, t1 - t0)) * (W - pl - pr); const allVals = pts.flatMap((p) => ids.map((id) => p.mean[id] ?? 0)); const pollVals = (polls ?? []).filter((p) => !from || p.date >= from).flatMap((p) => ids.map((id) => p.values[id] ?? 0)); const ymax = Math.min(100, Math.ceil((Math.max(...allVals, ...pollVals, 10) + 4) / 5) * 5); const y = (v: number) => pt + (1 - v / ymax) * (H - pt - pb); const ticks = Array.from({ length: Math.floor(ymax / 10) + 1 }, (_, i) => i * 10); // repères temporels : premier jour de chaque mois (ou trimestre si long) const months: { d: Date; label: string }[] = []; const spanDays = (t1 - t0) / 86400000; const step = spanDays > 900 ? 6 : spanDays > 400 ? 3 : 1; const cur = new Date(t0); cur.setDate(1); cur.setMonth(cur.getMonth() + 1); while (cur.getTime() <= t1) { if (cur.getMonth() % step === 0) months.push({ d: new Date(cur), label: cur.toLocaleDateString("fr-CA", { month: "short", year: spanDays > 200 ? "2-digit" : undefined }) }); cur.setMonth(cur.getMonth() + 1); } const path = (id: string) => pts.map((p, i) => `${i ? "L" : "M"}${x(p.date).toFixed(1)},${y(p.mean[id] ?? 0).toFixed(1)}`).join(" "); const band = (id: string) => { const up = pts.map((p) => `${x(p.date).toFixed(1)},${y((p.mean[id] ?? 0) + 1.28 * (p.sd[id] ?? 0)).toFixed(1)}`); const dn = [...pts].reverse().map((p) => `${x(p.date).toFixed(1)},${y((p.mean[id] ?? 0) - 1.28 * (p.sd[id] ?? 0)).toFixed(1)}`); return `M${up.join(" L")} L${dn.join(" L")} Z`; }; const last = pts[pts.length - 1]; const labels = ids.map((id) => ({ id, v: last.mean[id] ?? 0 })).sort((a, b) => b.v - a.v); // évite les chevauchements d'étiquettes let prevY = -Infinity; const labelY = labels.map((l) => { let yy = y(l.v); if (yy - prevY < 13) yy = prevY + 13; prevY = yy; return yy; }); return ( {ticks.map((t) => ( {t} ))} {months.map((m, i) => ( {m.label} ))} {markers.filter((m) => new Date(m.date).getTime() >= t0 && new Date(m.date).getTime() <= t1).map((m, i) => ( {m.label} ))} {polls?.filter((p) => !from || p.date >= from).map((p, i) => ids.map((id) => p.values[id] === undefined ? null : ( )))} {showBand && ids.map((id) => )} {ids.map((id) => )} {election2022 && ids.map((id) => election2022[id] !== undefined && ( ))} {labels.map((l, i) => ( {partyLabel(l.id)} {l.v.toFixed(1).replace(".", ",")} ))} ); } // ---------------------------------------------------------------- VoteBars export function VoteBars({ values, ci, max, className, order }: { values: Record; ci?: Record; max?: number; className?: string; order?: string[] }) { const ids = order ?? Object.keys(values).filter((k) => k !== "aut").sort((a, b) => values[b] - values[a]); const m = max ?? Math.max(...ids.map((i) => ci?.[i]?.[1] ?? values[i]), 10) * 1.08; return (
{ids.map((id) => (
{partyLabel(id)}
{ci?.[id] &&
}
{values[id].toFixed(1).replace(".", ",")} %
))}
); } export function Legend({ ids, className }: { ids: string[]; className?: string }) { return (
{ids.map((id) => ( {partyLabel(id)} ))}
); } export function Sparkline({ values, color, width = 120, height = 32, className }: { values: number[]; color: string; width?: number; height?: number; className?: string }) { if (values.length < 2) return null; const min = Math.min(...values), max = Math.max(...values), span = Math.max(0.001, max - min); const pts = values.map((v, i) => `${(i / (values.length - 1)) * width},${height - ((v - min) / span) * (height - 4) - 2}`).join(" "); return ; } export function Note({ children }: { children: ReactNode }) { return

{children}

; }