forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

chart-line.tsx217 lines, 10.0 KiBHistoryRawDownload
1import { For, Show, createMemo, type Component } from "solid-js";
2import { cn } from "@/lib/cn";
3import { useT } from "@/stores/preferences-context";
4import { EmptyInline } from "@/components/ui/empty-inline";
5
6export type ChartLineItem = {
7 id: string;
8 label: string;
9 value: number;
10 formattedValue?: string;
11 caption?: string;
12};
13
14export type ChartLineProps = {
15 title?: string;
16 subtitle?: string;
17 items: ChartLineItem[];
18 maxScale?: number;
19 class?: string;
20};
21
22const VIEW_WIDTH = 720;
23const VIEW_HEIGHT = 240;
24const PADDING = { top: 18, right: 18, bottom: 34, left: 38 };
25const Y_TICKS = [0, 25, 50, 75, 100];
26const TOOLTIP_WIDTH = 190;
27const TOOLTIP_HEIGHT = 62;
28
29export const ChartLine: Component<ChartLineProps> = (props) => {
30 const t = useT();
31 const maxValue = () => props.maxScale != null && props.maxScale > 0
32 ? props.maxScale
33 : Math.max(...props.items.map((item) => item.value), 1);
34 const plotWidth = VIEW_WIDTH - PADDING.left - PADDING.right;
35 const plotHeight = VIEW_HEIGHT - PADDING.top - PADDING.bottom;
36 const pointFor = (item: ChartLineItem, index: number) => {
37 const x = props.items.length === 1
38 ? PADDING.left + plotWidth / 2
39 : PADDING.left + (index / (props.items.length - 1)) * plotWidth;
40 const y = PADDING.top + (1 - Math.min(1, Math.max(0, item.value / maxValue()))) * plotHeight;
41 return { x, y };
42 };
43 const points = createMemo(() => props.items.map((item, index) => {
44 const point = pointFor(item, index);
45 return `${point.x},${point.y}`;
46 }).join(" "));
47 const areaPoints = createMemo(() => {
48 if (props.items.length < 2) return "";
49 const baseline = PADDING.top + plotHeight;
50 return `${PADDING.left},${baseline} ${points()} ${PADDING.left + plotWidth},${baseline}`;
51 });
52 const summary = createMemo(() => {
53 if (props.items.length === 0) return null;
54 const values = props.items.map((item) => item.value);
55 return {
56 latest: props.items[props.items.length - 1],
57 minimum: Math.min(...values),
58 maximum: Math.max(...values),
59 average: values.reduce((sum, value) => sum + value, 0) / values.length,
60 };
61 });
62 const xTickIndexes = createMemo(() => {
63 if (props.items.length <= 1) return [0];
64 const step = Math.max(1, Math.ceil((props.items.length - 1) / 5));
65 const indexes = Array.from({ length: props.items.length }, (_, index) => index)
66 .filter((index) => index % step === 0);
67 if (indexes[indexes.length - 1] !== props.items.length - 1) indexes.push(props.items.length - 1);
68 return indexes;
69 });
70 const averageY = () => PADDING.top + (1 - Math.min(1, summary()!.average / maxValue())) * plotHeight;
71
72 return (
73 <div class={cn("flex flex-col gap-4 rounded-xl border border-border-line bg-surface-base p-4", props.class)}>
74 <Show when={props.title}>
75 <div class="border-b border-border-hairline pb-3">
76 <h3 class="text-sm font-semibold tracking-tight text-foreground">{props.title}</h3>
77 <Show when={props.subtitle}>
78 <p class="mt-0.5 text-xs text-muted-foreground">{props.subtitle}</p>
79 </Show>
80 </div>
81 </Show>
82
83 <Show
84 when={summary()}
85 fallback={
86 <EmptyInline illustration="charts" title={t("dashboard.chartEmpty")} hint={t("dashboard.chartEmptyHint")} />
87 }
88 >
89 {(stats) => (
90 <div class="space-y-3">
91 <dl class="grid grid-cols-2 gap-2 sm:grid-cols-4">
92 <div class="rounded-lg bg-surface-overlay px-2.5 py-2">
93 <dt class="text-[11px] text-muted-foreground">{t("dashboard.chartExamCount")}</dt>
94 <dd class="mt-0.5 font-mono text-sm font-semibold tabular-nums">{props.items.length}</dd>
95 </div>
96 <div class="rounded-lg bg-surface-overlay px-2.5 py-2">
97 <dt class="text-[11px] text-muted-foreground">{t("dashboard.chartLatest")}</dt>
98 <dd class="mt-0.5 font-mono text-sm font-semibold tabular-nums">{stats().latest.formattedValue ?? stats().latest.value}</dd>
99 </div>
100 <div class="rounded-lg bg-surface-overlay px-2.5 py-2">
101 <dt class="text-[11px] text-muted-foreground">{t("dashboard.chartAverage")}</dt>
102 <dd class="mt-0.5 font-mono text-sm font-semibold tabular-nums">{stats().average.toFixed(1)}</dd>
103 </div>
104 <div class="rounded-lg bg-surface-overlay px-2.5 py-2">
105 <dt class="text-[11px] text-muted-foreground">{t("dashboard.chartRange")}</dt>
106 <dd class="mt-0.5 font-mono text-sm font-semibold tabular-nums">{stats().minimum.toFixed(1)}–{stats().maximum.toFixed(1)}</dd>
107 </div>
108 </dl>
109
110 <div class="overflow-x-auto pb-1">
111 <svg
112 class="h-60 min-w-[640px] w-full"
113 viewBox={`0 0 ${VIEW_WIDTH} ${VIEW_HEIGHT}`}
114 role="img"
115 aria-label={props.items.map((item) => `${item.label}${item.caption ? ` (${item.caption})` : ""}: ${item.formattedValue ?? item.value}`).join(", ")}
116 >
117 <For each={Y_TICKS}>
118 {(tick) => {
119 const y = () => PADDING.top + (1 - tick / 100) * plotHeight;
120 return (
121 <g>
122 <line x1={PADDING.left} x2={PADDING.left + plotWidth} y1={y()} y2={y()} class="stroke-border/70" stroke-width="1" />
123 <text x={PADDING.left - 8} y={y() + 3} text-anchor="end" class="fill-muted-foreground text-[11px]">{tick}</text>
124 </g>
125 );
126 }}
127 </For>
128
129 <Show when={props.items.length > 1}>
130 <polygon points={areaPoints()} class="fill-primary opacity-10" />
131 <line
132 x1={PADDING.left}
133 x2={PADDING.left + plotWidth}
134 y1={averageY()}
135 y2={averageY()}
136 class="stroke-muted-foreground"
137 stroke-width="1"
138 stroke-dasharray="4 4"
139 />
140 <text x={PADDING.left + 5} y={averageY() - 5} class="fill-muted-foreground text-[11px]">
141 {t("dashboard.chartAverage")}: {stats().average.toFixed(1)}
142 </text>
143 <polyline points={points()} fill="none" class="stroke-primary" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" />
144 </Show>
145
146 <For each={props.items}>
147 {(item, index) => {
148 const point = () => pointFor(item, index());
149 const tooltipX = () => Math.min(
150 VIEW_WIDTH - PADDING.right - TOOLTIP_WIDTH,
151 Math.max(PADDING.left, point().x - TOOLTIP_WIDTH / 2),
152 );
153 const tooltipY = () => {
154 const preferred = point().y - TOOLTIP_HEIGHT - 12;
155 return preferred >= PADDING.top
156 ? preferred
157 : Math.min(point().y + 12, VIEW_HEIGHT - PADDING.bottom - TOOLTIP_HEIGHT);
158 };
159 const detail = () => `${item.label}${item.caption ? `, ${item.caption}` : ""}: ${item.formattedValue ?? item.value}`;
160 return (
161 <g class="group outline-none" tabindex="0" aria-label={detail()}>
162 <circle cx={point().x} cy={point().y} r="9" class="fill-transparent" />
163 <circle
164 cx={point().x}
165 cy={point().y}
166 r="4"
167 class="fill-primary stroke-card transition-[r] group-hover:r-[6px] group-focus:r-[6px]"
168 stroke-width="2"
169 />
170 <foreignObject
171 x={tooltipX()}
172 y={tooltipY()}
173 width={TOOLTIP_WIDTH}
174 height={TOOLTIP_HEIGHT}
175 class="pointer-events-none opacity-0 transition-opacity duration-150 group-hover:opacity-100 group-focus:opacity-100"
176 data-chart-tooltip
177 >
178 <div class="h-full rounded-md border border-border bg-popover px-3 py-2 text-popover-foreground shadow-lg">
179 <p class="truncate text-[11px] font-semibold" title={item.label}>{item.label}</p>
180 <div class="mt-1 flex items-end justify-between gap-3">
181 <span class="truncate text-[11px] text-muted-foreground">{item.caption}</span>
182 <span class="font-mono text-base font-bold leading-none tabular-nums">{item.formattedValue ?? item.value}</span>
183 </div>
184 </div>
185 </foreignObject>
186 </g>
187 );
188 }}
189 </For>
190
191 <For each={xTickIndexes()}>
192 {(index) => {
193 const point = () => pointFor(props.items[index], index);
194 return (
195 <text
196 x={point().x}
197 y={VIEW_HEIGHT - 10}
198 text-anchor={index === 0 ? "start" : index === props.items.length - 1 ? "end" : "middle"}
199 class="fill-muted-foreground text-[11px]"
200 >
201 {props.items[index].caption ?? props.items[index].label}
202 </text>
203 );
204 }}
205 </For>
206 </svg>
207 </div>
208 <p class="truncate text-[11px] text-muted-foreground" title={stats().latest.label}>
209 {t("dashboard.chartLatestExam")}: <span class="font-medium text-foreground/80">{stats().latest.label}</span>
210 <Show when={stats().latest.caption}> · {stats().latest.caption}</Show>
211 </p>
212 </div>
213 )}
214 </Show>
215 </div>
216 );
217};