// A small line chart for daily store analytics. Reach (impressions, visitors), installs and conversion differ by one // to three orders of magnitude, so on one count axis installs lay flat at the bottom. The chart therefore plots each // line as an index (100 = that line's own average over the days shown, views.ts indexSeries) on one shared axis: every // line uses the full height and a listing change shows as lines moving apart. Hover (or arrow keys when focused) shows // the real values of one day; store changes from git are dashed markers. import { type KeyboardEvent, useId, useState } from 'react'; import { date, num, pct } from '../lib/format'; import { indexSeries } from '../lib/views'; export interface ChartLine { label: string; /** Real daily values (counts, or a 0-1 rate with unit "rate"); plotted as an index, shown as they are on hover. */ values: (number | null)[]; cls: 'l1' | 'l2' | 'l3'; unit: 'count' | 'rate'; } const W = 800, H = 230, L = 46, R = 18, T = 14, B = 26; /** Index axis: a round top above the highest point (at least 150 so 100 sits in the lower two thirds) and 4 ticks. */ export function indexAxis(lines: (number | null)[][]): { max: number; ticks: number[] } { const hi = Math.max(150, ...lines.flat().filter((v): v is number => v !== null)); const step = hi <= 200 ? 50 : hi <= 400 ? 100 : Math.ceil(hi / 4 / 100) * 100; const max = Math.ceil(hi / step) * step; return { max, ticks: Array.from({ length: max / step + 1 }, (_, i) => i * step) }; } export function LineChart({ dates, lines, events, label, eventsLabel, indexLabel }: { dates: string[]; lines: ChartLine[]; events: { date: string; subject: string }[]; label: string; eventsLabel: string; indexLabel: string; }) { const gid = useId().replace(/:/g, ''); const [hover, setHover] = useState(null); const n = dates.length; if (n < 2) return null; const plotted = lines.map(l => indexSeries(l.values)); const { max, ticks } = indexAxis(plotted); const x = (i: number) => L + (i / (n - 1)) * (W - L - R); const y = (v: number) => H - B - (v / max) * (H - T - B); const path = (vs: (number | null)[]) => vs.map((v, i) => (v === null ? '' : `${i && vs[i - 1] !== null ? 'L' : 'M'}${x(i).toFixed(1)} ${y(v).toFixed(1)}`)).join(' '); const onMove = (ev: React.MouseEvent) => { const r = ev.currentTarget.getBoundingClientRect(); const px = ((ev.clientX - r.left) / r.width) * W; setHover(Math.max(0, Math.min(n - 1, Math.round(((px - L) / (W - L - R)) * (n - 1))))); }; const onKey = (ev: KeyboardEvent) => { if (ev.key === 'ArrowRight') { ev.preventDefault(); setHover(h => Math.min(n - 1, (h ?? -1) + 1)); } if (ev.key === 'ArrowLeft') { ev.preventDefault(); setHover(h => Math.max(0, (h ?? n) - 1)); } }; const fmt = (l: ChartLine, v: number | null) => (v === null ? '–' : l.unit === 'rate' ? pct(v, 1) : num(v)); return (
setHover(null)} onKeyDown={onKey} onBlur={() => setHover(null)}> {ticks.map(v => ( {v} ))} {events.map((e, i) => { const k = dates.indexOf(e.date); if (k < 0) return null; return {`${date(e.date)} · ${e.subject}`}; })} {plotted[0] && } {lines.map((l, k) => )} {hover !== null && {lines.map((l, k) => (plotted[k]![hover] === null ? null : ))}} {date(dates[0])} {date(dates[n - 1])}
{indexLabel} {hover !== null && {date(dates[hover])}} {lines.map(l => {l.label}{hover !== null && {fmt(l, l.values[hover] ?? null)}})} {events.length > 0 && {eventsLabel}}
); }