From 90d723756c69ad834c65659770f189c990e1b247 Mon Sep 17 00:00:00 2001 From: luen Date: Tue, 3 Nov 2026 08:45:13 +1000 Subject: [PATCH] Charts: Download CSV on every chart card Each chart component registers a table builder with its ChartCard through Svelte context (charts/csv.ts); the card shows a Download CSV button after hydration and builds the file on click. Explicit ChartCard tables take precedence. Closes #31 Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 1 + src/lib/components/ChartCard.svelte | 53 +++++++++++++++++++++- src/lib/components/charts/BarChart.svelte | 10 ++++ src/lib/components/charts/BoxPlot.svelte | 15 ++++++ src/lib/components/charts/Heatmap.svelte | 6 +++ src/lib/components/charts/Histogram.svelte | 6 +++ src/lib/components/charts/LineChart.svelte | 18 ++++++++ src/lib/components/charts/Polar.svelte | 6 +++ src/lib/components/charts/Scatter.svelte | 6 +++ src/lib/components/charts/Timeline.svelte | 8 ++++ src/lib/components/charts/csv.test.ts | 28 ++++++++++++ src/lib/components/charts/csv.ts | 53 ++++++++++++++++++++++ 12 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 src/lib/components/charts/csv.test.ts create mode 100644 src/lib/components/charts/csv.ts diff --git a/AGENTS.md b/AGENTS.md index a7f56d7..50e34fd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -71,6 +71,7 @@ Drop the workbook in `data/` named like `Physioshark_Shark Log 2026-2027.xlsx` ( ## Charts & design - Follow the dataviz method: categorical slots in fixed order (`--series-1…8`, validated light and dark), sequential = one hue (`--seq-*`), diverging = blue ↔ red with a neutral midpoint, one y-axis only (never dual axes), legends for ≥ 2 series, text in ink tokens (never the series colour), 4 px rounded bar ends, 2 px lines, hover tooltips, data tables via `ChartCard table`. +- Every chart registers its data with the enclosing `ChartCard` (`registerCsv` in `charts/csv.ts`), which shows **Download CSV**. A new chart component must call `registerCsv` too. An explicit `ChartCard table` takes precedence. - Species colours come from `src/lib/species.ts` (blacktip = slot 1, sicklefin lemon = slot 2). - Charts measure their width (`bind:clientWidth`) and carry a `viewBox` so pre-hydration renders scale down instead of overflowing. - Interactive marks (links) inside an SVG → the SVG gets `role="group"`, not `img` (axe `nested-interactive`). Only put `aria-label` on elements that are links. diff --git a/src/lib/components/ChartCard.svelte b/src/lib/components/ChartCard.svelte index 0289486..8ec64a1 100644 --- a/src/lib/components/ChartCard.svelte +++ b/src/lib/components/ChartCard.svelte @@ -1,6 +1,7 @@
@@ -34,6 +60,11 @@ {/if} + {#if canDownload} + + {/if} {#if source}

Source: {source}

{/if}
@@ -73,6 +104,26 @@ margin-top: 10px; font-size: 0.85rem; } + .csv { + align-self: flex-start; + margin-top: 8px; + padding: 0; + background: none; + border: 0; + color: var(--accent-ink); + font: inherit; + font-size: 0.8rem; + cursor: pointer; + text-decoration: underline; + text-underline-offset: 2px; + } + .sr { + position: absolute; + width: 1px; + height: 1px; + overflow: hidden; + clip: rect(0 0 0 0); + } .tbl summary { cursor: pointer; color: var(--accent-ink); diff --git a/src/lib/components/charts/BarChart.svelte b/src/lib/components/charts/BarChart.svelte index 9a4b752..2251b36 100644 --- a/src/lib/components/charts/BarChart.svelte +++ b/src/lib/components/charts/BarChart.svelte @@ -7,6 +7,7 @@