import { For, Show, createMemo, createSignal, onCleanup, type Component } from "solid-js"; import { cn } from "@/lib/cn"; import { usePreferences } from "@/stores/preferences-context"; import { formatDecimal } from "@/lib/format"; import { EmptyInline } from "@/components/ui/empty-inline"; export type ChartLineItem = { id: string; label: string; value: number; formattedValue?: string; caption?: string; }; export type ChartLineProps = { title?: string; subtitle?: string; items: ChartLineItem[]; maxScale?: number; class?: string; }; // The drawing is laid out in real pixels: the viewBox follows the measured // width, so a wide card is filled edge to edge (a fixed 720-wide viewBox in a // 240px-tall box letterboxed it) and axis text keeps its 11px size. const DEFAULT_VIEW_WIDTH = 720; const MIN_VIEW_WIDTH = 260; // Room one x-axis caption needs, so a phone gets fewer ticks, not overlaps. const TICK_SLOT = 96; const VIEW_HEIGHT = 240; const PADDING = { top: 18, right: 18, bottom: 34, left: 38 }; const Y_TICKS = [0, 25, 50, 75, 100]; const TOOLTIP_WIDTH = 190; const TOOLTIP_HEIGHT = 62; const TICK_MAX_CHARS = 14; export const ChartLine: Component = (props) => { const { t, locale } = usePreferences(); const decimal = (value: number) => formatDecimal(value, locale()); const maxValue = () => props.maxScale != null && props.maxScale > 0 ? props.maxScale : Math.max(...props.items.map((item) => item.value), 1); const [viewWidth, setViewWidth] = createSignal(DEFAULT_VIEW_WIDTH); const observeWidth = (el: HTMLDivElement) => { if (typeof ResizeObserver === "undefined") return; const observer = new ResizeObserver((entries) => { const width = Math.round(entries[0]?.contentRect.width ?? 0); if (width > 0) setViewWidth(Math.max(MIN_VIEW_WIDTH, width)); }); observer.observe(el); onCleanup(() => observer.disconnect()); }; const plotWidth = () => viewWidth() - PADDING.left - PADDING.right; const plotHeight = VIEW_HEIGHT - PADDING.top - PADDING.bottom; const pointFor = (item: ChartLineItem, index: number) => { const x = props.items.length === 1 ? PADDING.left + plotWidth() / 2 : PADDING.left + (index / (props.items.length - 1)) * plotWidth(); const y = PADDING.top + (1 - Math.min(1, Math.max(0, item.value / maxValue()))) * plotHeight; return { x, y }; }; const points = createMemo(() => props.items.map((item, index) => { const point = pointFor(item, index); return `${point.x},${point.y}`; }).join(" ")); const areaPoints = createMemo(() => { if (props.items.length < 2) return ""; const baseline = PADDING.top + plotHeight; return `${PADDING.left},${baseline} ${points()} ${PADDING.left + plotWidth()},${baseline}`; }); const summary = createMemo(() => { if (props.items.length === 0) return null; const values = props.items.map((item) => item.value); return { latest: props.items[props.items.length - 1], minimum: Math.min(...values), maximum: Math.max(...values), average: values.reduce((sum, value) => sum + value, 0) / values.length, }; }); const xTickIndexes = createMemo(() => { if (props.items.length <= 1) return [0]; const slots = Math.max(1, Math.min(5, Math.floor(plotWidth() / TICK_SLOT))); const step = Math.max(1, Math.ceil((props.items.length - 1) / slots)); const indexes = Array.from({ length: props.items.length }, (_, index) => index) .filter((index) => index % step === 0); if (indexes[indexes.length - 1] !== props.items.length - 1) indexes.push(props.items.length - 1); return indexes; }); // An axis tick has ~1/6 of the width, so it carries the item's caption (a // short date) or, when there is none, its position. Full labels overlap // each other and are already in the tooltip and the "latest" line. const tickLabel = (index: number) => { const caption = props.items[index].caption; if (!caption) return String(index + 1); return caption.length > TICK_MAX_CHARS ? `${caption.slice(0, TICK_MAX_CHARS - 1)}…` : caption; }; const averageY = () => PADDING.top + (1 - Math.min(1, summary()!.average / maxValue())) * plotHeight; return (

{props.title}

{props.subtitle}

} > {(stats) => (
{t("dashboard.chartExamCount")}
{props.items.length}
{t("dashboard.chartLatest")}
{stats().latest.formattedValue ?? stats().latest.value}
{t("dashboard.chartAverage")}
{decimal(stats().average)}
{t("dashboard.chartRange")}
{decimal(stats().minimum)}–{decimal(stats().maximum)}
{/* Fits any width: the viewBox follows the box and a narrow screen gets fewer x ticks, so a phone needs no sideways scroller (a 480px floor cut the line with no scroll cue). */}
`${item.label}${item.caption ? ` (${item.caption})` : ""}: ${item.formattedValue ?? item.value}`).join(", ")} > {(tick) => { const y = () => PADDING.top + (1 - tick / 100) * plotHeight; return ( {tick} ); }} 1}> {(item, index) => { const point = () => pointFor(item, index()); const tooltipX = () => Math.min( viewWidth() - PADDING.right - TOOLTIP_WIDTH, Math.max(PADDING.left, point().x - TOOLTIP_WIDTH / 2), ); const tooltipY = () => { const preferred = point().y - TOOLTIP_HEIGHT - 12; return preferred >= PADDING.top ? preferred : Math.min(point().y + 12, VIEW_HEIGHT - PADDING.bottom - TOOLTIP_HEIGHT); }; const detail = () => `${item.label}${item.caption ? `, ${item.caption}` : ""}: ${item.formattedValue ?? item.value}`; return (

{item.label}

{item.caption} {item.formattedValue ?? item.value}
); }}
{(index) => { const point = () => pointFor(props.items[index], index); return ( {tickLabel(index)} ); }}

{t("dashboard.chartLatestExam")}: {stats().latest.label} · {stats().latest.caption}

)}
); };