|
| 1 | +<script lang="ts" module> |
| 2 | + export interface ForestValue { |
| 3 | + est: number; |
| 4 | + lo: number; |
| 5 | + hi: number; |
| 6 | + } |
| 7 | + export interface ForestSeries { |
| 8 | + name: string; |
| 9 | + color: string; |
| 10 | + values: (ForestValue | null)[]; |
| 11 | + } |
| 12 | +</script> |
| 13 | + |
| 14 | +<script lang="ts"> |
| 15 | + /** |
| 16 | + * Estimates with 95% intervals on a log scale around a reference line (rate ratios, #54). |
| 17 | + * One row per term; several series sit side by side within a row. |
| 18 | + */ |
| 19 | + import { registerCsv } from './csv'; |
| 20 | + import { num } from '#lib/format'; |
| 21 | + import Legend from './Legend.svelte'; |
| 22 | + import Tip, { type TipState } from './Tip.svelte'; |
| 23 | + import { linear } from './scale'; |
| 24 | +
|
| 25 | + let { |
| 26 | + rows, |
| 27 | + series, |
| 28 | + ref = 1, |
| 29 | + xLabel = '', |
| 30 | + format = (v: number) => num(v, 2) |
| 31 | + }: { |
| 32 | + rows: string[]; |
| 33 | + series: ForestSeries[]; |
| 34 | + ref?: number; |
| 35 | + xLabel?: string; |
| 36 | + format?: (v: number) => string; |
| 37 | + } = $props(); |
| 38 | +
|
| 39 | + registerCsv(() => ({ |
| 40 | + columns: [ |
| 41 | + 'term', |
| 42 | + ...series.flatMap((s) => |
| 43 | + series.length > 1 |
| 44 | + ? [s.name, `${s.name} ci_low`, `${s.name} ci_high`] |
| 45 | + : ['estimate', 'ci_low', 'ci_high'] |
| 46 | + ) |
| 47 | + ], |
| 48 | + rows: rows.map((r, i) => [ |
| 49 | + r, |
| 50 | + ...series.flatMap((s) => [s.values[i]?.est, s.values[i]?.lo, s.values[i]?.hi]) |
| 51 | + ]) |
| 52 | + })); |
| 53 | +
|
| 54 | + let width = $state(640); |
| 55 | + const CHAR_W = 6.2; |
| 56 | + const longest = $derived(Math.max(0, ...rows.map((c) => c.length))); |
| 57 | + const m = $derived({ |
| 58 | + top: 6, |
| 59 | + right: 16, |
| 60 | + bottom: xLabel ? 42 : 26, |
| 61 | + left: Math.min(Math.max(110, width * 0.45), 12 + longest * CHAR_W) |
| 62 | + }); |
| 63 | + // Labels that don't fit the margin wrap onto a second line (narrow screens). |
| 64 | + const fit = $derived(Math.max(8, Math.floor((m.left - 12) / CHAR_W))); |
| 65 | + const wrap = (s: string, n: number): string[] => { |
| 66 | + if (s.length <= n) return [s]; |
| 67 | + const cut = s.lastIndexOf(' ', n); |
| 68 | + if (cut <= 0) return [`${s.slice(0, n - 1)}…`]; |
| 69 | + const rest = s.slice(cut + 1); |
| 70 | + return [s.slice(0, cut), rest.length > n ? `${rest.slice(0, n - 1)}…` : rest]; |
| 71 | + }; |
| 72 | + const labels = $derived(rows.map((r) => wrap(r, fit))); |
| 73 | + const band = $derived(series.length > 1 || labels.some((l) => l.length > 1) ? 30 : 24); |
| 74 | + const h = $derived(m.top + m.bottom + rows.length * band); |
| 75 | +
|
| 76 | + const all = $derived(series.flatMap((s) => s.values.filter((v): v is ForestValue => v != null))); |
| 77 | + const ext = $derived.by<[number, number]>(() => { |
| 78 | + const lo = Math.min(ref, ...all.map((v) => v.lo)); |
| 79 | + const hi = Math.max(ref, ...all.map((v) => v.hi)); |
| 80 | + const pad = (Math.log(hi) - Math.log(lo)) * 0.06 || 0.1; |
| 81 | + return [Math.exp(Math.log(lo) - pad), Math.exp(Math.log(hi) + pad)]; |
| 82 | + }); |
| 83 | + const sx = $derived(linear(ext, [m.left, Math.max(m.left + 10, width - m.right)], true)); |
| 84 | + const CANDIDATES = [0.05, 0.1, 0.2, 0.25, 0.33, 0.5, 0.67, 0.8, 1, 1.25, 1.5, 2, 3, 4, 5, 10, 20]; |
| 85 | + const xt = $derived.by(() => { |
| 86 | + const inside = CANDIDATES.filter((v) => v >= ext[0] && v <= ext[1]); |
| 87 | + const room = Math.max(3, Math.floor((width - m.left - m.right) / 48)); |
| 88 | + if (inside.length <= room) return inside; |
| 89 | + const keep = [0.1, 0.2, 0.5, 1, 2, 5, 10].filter((v) => v >= ext[0] && v <= ext[1]); |
| 90 | + return keep.length >= 2 ? keep : inside.filter((_, i) => i % 2 === 0); |
| 91 | + }); |
| 92 | + const off = (k: number) => (series.length > 1 ? (k - (series.length - 1) / 2) * 9 : 0); |
| 93 | +
|
| 94 | + let tip = $state<TipState | null>(null); |
| 95 | + function show(e: PointerEvent, i: number) { |
| 96 | + const wrap = (e.currentTarget as Element).closest('.wrap')!.getBoundingClientRect(); |
| 97 | + tip = { |
| 98 | + x: e.clientX - wrap.left, |
| 99 | + y: e.clientY - wrap.top, |
| 100 | + title: rows[i], |
| 101 | + lines: series.map((s) => { |
| 102 | + const v = s.values[i]; |
| 103 | + return { |
| 104 | + label: series.length > 1 ? s.name : undefined, |
| 105 | + value: v ? `${format(v.est)} (${format(v.lo)}–${format(v.hi)})` : '–', |
| 106 | + color: s.color |
| 107 | + }; |
| 108 | + }) |
| 109 | + }; |
| 110 | + } |
| 111 | +</script> |
| 112 | + |
| 113 | +<Legend items={series.map((s) => ({ label: s.name, color: s.color, kind: 'dot' as const }))} /> |
| 114 | +<div class="wrap" bind:clientWidth={width}> |
| 115 | + <svg |
| 116 | + class="chart-svg" |
| 117 | + viewBox={`0 0 ${width} ${h}`} |
| 118 | + {width} |
| 119 | + height={h} |
| 120 | + role="img" |
| 121 | + aria-label={`${xLabel || 'Estimates'} with 95% intervals`} |
| 122 | + onpointerleave={() => (tip = null)} |
| 123 | + > |
| 124 | + {#each xt as t (t)} |
| 125 | + <line x1={sx(t)} x2={sx(t)} y1={m.top} y2={h - m.bottom} stroke="var(--grid)" /> |
| 126 | + <text x={sx(t)} y={h - m.bottom + 15} text-anchor="middle" font-size="11" fill="var(--muted)" |
| 127 | + >{format(t)}</text |
| 128 | + > |
| 129 | + {/each} |
| 130 | + <line |
| 131 | + x1={sx(ref)} |
| 132 | + x2={sx(ref)} |
| 133 | + y1={m.top} |
| 134 | + y2={h - m.bottom} |
| 135 | + stroke="var(--axis)" |
| 136 | + stroke-dasharray="4 3" |
| 137 | + /> |
| 138 | + {#each rows as row, i (i)} |
| 139 | + {@const cy = m.top + band * i + band / 2} |
| 140 | + <g> |
| 141 | + <rect |
| 142 | + x={m.left} |
| 143 | + y={m.top + band * i} |
| 144 | + width={Math.max(0, width - m.left - m.right)} |
| 145 | + height={band} |
| 146 | + fill="transparent" |
| 147 | + role="presentation" |
| 148 | + onpointermove={(e) => show(e, i)} |
| 149 | + /> |
| 150 | + {#each series as s, k (s.name)} |
| 151 | + {@const v = s.values[i]} |
| 152 | + {#if v} |
| 153 | + <line |
| 154 | + x1={sx(v.lo)} |
| 155 | + x2={sx(v.hi)} |
| 156 | + y1={cy + off(k)} |
| 157 | + y2={cy + off(k)} |
| 158 | + stroke={s.color} |
| 159 | + stroke-width="2" |
| 160 | + stroke-linecap="round" |
| 161 | + pointer-events="none" |
| 162 | + /> |
| 163 | + <circle cx={sx(v.est)} cy={cy + off(k)} r="4" fill={s.color} pointer-events="none" /> |
| 164 | + {/if} |
| 165 | + {/each} |
| 166 | + <text x={m.left - 8} y={cy} text-anchor="end" font-size="11.5" fill="var(--ink-2)"> |
| 167 | + {#each labels[i] as line, j (j)} |
| 168 | + <tspan x={m.left - 8} dy={j ? '1.15em' : labels[i].length > 1 ? '-0.25em' : '0.32em'} |
| 169 | + >{line}</tspan |
| 170 | + > |
| 171 | + {/each} |
| 172 | + <title>{row}</title> |
| 173 | + </text> |
| 174 | + </g> |
| 175 | + {/each} |
| 176 | + {#if xLabel} |
| 177 | + <text |
| 178 | + x={(m.left + width - m.right) / 2} |
| 179 | + y={h - 6} |
| 180 | + text-anchor="middle" |
| 181 | + font-size="11" |
| 182 | + fill="var(--ink-2)">{xLabel}</text |
| 183 | + > |
| 184 | + {/if} |
| 185 | + </svg> |
| 186 | + <Tip {tip} {width} /> |
| 187 | +</div> |
| 188 | + |
| 189 | +<style> |
| 190 | + .wrap { |
| 191 | + position: relative; |
| 192 | + width: 100%; |
| 193 | + } |
| 194 | + svg { |
| 195 | + display: block; |
| 196 | + overflow: visible; |
| 197 | + } |
| 198 | +</style> |
0 commit comments