From 4f8519545009c8569655f6a2ad14837199a6d6bf Mon Sep 17 00:00:00 2001 From: luen Date: Tue, 3 Nov 2026 09:05:51 +1000 Subject: [PATCH] Charts: overlay mode for histograms Histogram takes mode='stack' | 'overlay'. Overlay draws each series from zero with a translucent fill and a 2 px stepped outline, so distributions can be compared directly. Used for high vs low tide times (/tides) and dark vs bright nights (/moon). Closes #80 Co-Authored-By: Claude Opus 5.5 --- src/lib/components/charts/Histogram.svelte | 41 ++++++++++++++++++++-- src/routes/moon/+page.svelte | 1 + src/routes/tides/+page.svelte | 1 + 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/lib/components/charts/Histogram.svelte b/src/lib/components/charts/Histogram.svelte index 3fbf97d..d11131f 100644 --- a/src/lib/components/charts/Histogram.svelte +++ b/src/lib/components/charts/Histogram.svelte @@ -11,7 +11,8 @@ xLabel = '', yLabel = 'Count', xFormat = compact, - markers = [] + markers = [], + mode = 'stack' }: { /** Bin edges (n + 1 values). */ edges: number[]; @@ -21,6 +22,8 @@ yLabel?: string; xFormat?: (v: number) => string; markers?: { x: number; label: string }[]; + /** 'stack' adds series up; 'overlay' draws each from zero (translucent fill + outline). */ + mode?: 'stack' | 'overlay'; } = $props(); let width = $state(640); @@ -33,7 +36,10 @@ const totals = $derived( edges.slice(0, -1).map((_, i) => series.reduce((a, s) => a + (s.counts[i] ?? 0), 0)) ); - const yExt = $derived(nice(0, Math.max(1, ...totals), 5, true)); + const peak = $derived( + mode === 'overlay' ? Math.max(1, ...series.flatMap((s) => s.counts)) : Math.max(1, ...totals) + ); + const yExt = $derived(nice(0, peak, 5, true)); const sx = $derived( linear( [edges[0] ?? 0, edges[edges.length - 1] ?? 1], @@ -44,6 +50,15 @@ const xt = $derived( ticks(edges[0] ?? 0, edges[edges.length - 1] ?? 1, Math.max(3, Math.floor(width / 80))) ); + /** Stepped outline along the top of one series' bars (overlay mode). */ + function outline(counts: number[]): string { + let d = ''; + edges.slice(0, -1).forEach((e0, i) => { + const y = sy(counts[i] ?? 0); + d += `${i ? 'L' : 'M'}${sx(e0)},${y}L${sx(edges[i + 1])},${y}`; + }); + return d; + } let tip = $state(null); function show(e: PointerEvent, i: number) { const wrap = (e.currentTarget as Element).closest('.wrap')!.getBoundingClientRect(); @@ -85,7 +100,7 @@ {#each edges.slice(0, -1) as e0, i (i)} {@const x0 = sx(e0) + 1} {@const x1 = sx(edges[i + 1]) - 1} - {#each series as s, k (s.name)} + {#each mode === 'overlay' ? [] : series as s, k (s.name)} {@const below = series.slice(0, k).reduce((a, q) => a + (q.counts[i] ?? 0), 0)} {@const v = s.counts[i] ?? 0} {#if v > 0} @@ -109,6 +124,26 @@ onpointermove={(e) => show(e, i)} /> {/each} + {#if mode === 'overlay'} + {#each series as s (s.name)} + + {#each edges.slice(0, -1) as e0, i (i)} + {@const v = s.counts[i] ?? 0} + {#if v > 0} + + {/if} + {/each} + + + {/each} + {/if}