import { For, Show, createMemo, type Component } from "solid-js"; import { cn } from "@/lib/cn"; import { useT } from "@/stores/preferences-context"; 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; }; const VIEW_WIDTH = 720; 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; export const ChartLine: Component = (props) => { const t = useT(); const maxValue = () => props.maxScale != null && props.maxScale > 0 ? props.maxScale : Math.max(...props.items.map((item) => item.value), 1); const plotWidth = VIEW_WIDTH - 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 step = Math.max(1, Math.ceil((props.items.length - 1) / 5)); 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; }); 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")}
{stats().average.toFixed(1)}
{t("dashboard.chartRange")}
{stats().minimum.toFixed(1)}–{stats().maximum.toFixed(1)}
`${item.label}${item.caption ? ` (${item.caption})` : ""}: ${item.formattedValue ?? item.value}`).join(", ")} > {(tick) => { const y = () => PADDING.top + (1 - tick / 100) * plotHeight; return ( {tick} ); }} 1}> {t("dashboard.chartAverage")}: {stats().average.toFixed(1)} {(item, index) => { const point = () => pointFor(item, index()); const tooltipX = () => Math.min( VIEW_WIDTH - 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 ( {props.items[index].caption ?? props.items[index].label} ); }}

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

)}
); };