Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions src/lib/components/charts/Histogram.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
xLabel = '',
yLabel = 'Count',
xFormat = compact,
markers = []
markers = [],
mode = 'stack'
}: {
/** Bin edges (n + 1 values). */
edges: number[];
Expand All @@ -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);
Expand All @@ -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],
Expand All @@ -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<TipState | null>(null);
function show(e: PointerEvent, i: number) {
const wrap = (e.currentTarget as Element).closest('.wrap')!.getBoundingClientRect();
Expand Down Expand Up @@ -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}
Expand All @@ -109,6 +124,26 @@
onpointermove={(e) => show(e, i)}
/>
{/each}
{#if mode === 'overlay'}
{#each series as s (s.name)}
<g pointer-events="none">
{#each edges.slice(0, -1) as e0, i (i)}
{@const v = s.counts[i] ?? 0}
{#if v > 0}
<rect
x={sx(e0)}
width={Math.max(0, sx(edges[i + 1]) - sx(e0))}
y={sy(v)}
height={Math.max(0, sy(0) - sy(v))}
fill={s.color}
fill-opacity="0.22"
/>
{/if}
{/each}
<path d={outline(s.counts)} fill="none" stroke={s.color} stroke-width="2" />
</g>
{/each}
{/if}
<line
x1={m.left}
x2={width - m.right}
Expand Down
1 change: 1 addition & 0 deletions src/routes/moon/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
note={`Median ${num(t.dark)} min after sunset on dark nights (n = ${num(t.nDark)}) vs ${num(t.bright)} min on bright nights (n = ${num(t.nBright)}); Welch ${pValue(t.p)}.`}
>
<Histogram
mode="overlay"
edges={t.edges}
series={[
{ name: 'Dark nights', color: 'var(--series-1)', counts: t.histDark },
Expand Down
1 change: 1 addition & 0 deletions src/routes/tides/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
subtitle="Clock time of every high and low tide since 2013"
>
<Histogram
mode="overlay"
edges={data.highsEdges}
series={[
{ name: 'High tides', color: 'var(--series-1)', counts: data.highsHist },
Expand Down
Loading