@umituz/web-dashboard 8.10.0 → 9.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -7
- package/package.json +6 -2
- package/src/config/DashboardConfig.ts +4 -8
- package/src/domains/analytics/components/AnalyticsLayout.test.tsx +85 -0
- package/src/domains/analytics/components/AnalyticsLayout.tsx +103 -105
- package/src/domains/analytics/components/index.ts +0 -3
- package/src/domains/analytics/hooks/index.ts +1 -1
- package/src/domains/analytics/hooks/useAnalytics.test.tsx +125 -0
- package/src/domains/analytics/hooks/useAnalytics.ts +37 -56
- package/src/domains/analytics/index.ts +9 -26
- package/src/domains/analytics/services/index.ts +0 -1
- package/src/domains/analytics/types/analytics.ts +32 -149
- package/src/domains/analytics/types/index.ts +3 -15
- package/src/domains/analytics/utils/analytics.test.ts +82 -13
- package/src/domains/analytics/utils/analytics.ts +52 -75
- package/src/domains/analytics/utils/index.ts +5 -8
- package/src/domains/charts/charts.css +455 -0
- package/src/domains/charts/components/ActivityHeatmap.test.tsx +48 -0
- package/src/domains/charts/components/ActivityHeatmap.tsx +58 -0
- package/src/domains/charts/components/BarChart.test.tsx +91 -0
- package/src/domains/charts/components/BarChart.tsx +143 -0
- package/src/domains/charts/components/ChartCard.tsx +39 -0
- package/src/domains/charts/components/ChartPrimitives.test.tsx +90 -0
- package/src/domains/charts/components/ChartTip.tsx +18 -0
- package/src/domains/charts/components/Donut.test.tsx +51 -0
- package/src/domains/charts/components/Donut.tsx +97 -0
- package/src/domains/charts/components/HBars.tsx +31 -0
- package/src/domains/charts/components/Legend.tsx +21 -0
- package/src/domains/charts/components/LineChart.test.tsx +81 -0
- package/src/domains/charts/components/LineChart.tsx +244 -0
- package/src/domains/charts/components/Sparkline.tsx +35 -0
- package/src/domains/charts/components/index.ts +16 -0
- package/src/domains/charts/index.ts +16 -2
- package/src/domains/charts/ssr.test.tsx +35 -0
- package/src/domains/charts/types/index.ts +8 -0
- package/src/domains/charts/utils.ts +15 -0
- package/src/domains/permissions/AccessDenied.tsx +10 -6
- package/src/domains/permissions/PermissionsContext.test.tsx +10 -3
- package/src/domains/permissions/PermissionsContext.tsx +5 -3
- package/src/domains/tour/tourEngine.ts +3 -3
- package/src/domains/tour/types.ts +1 -1
- package/src/index.ts +6 -23
- package/src/styles/chrome.css +10 -490
- package/src/styles/dashboard.contract.test.ts +20 -5
- package/src/styles/dashboard.css +1 -0
- package/src/domains/analytics/components/AnalyticsCard.tsx +0 -87
- package/src/domains/analytics/components/AnalyticsChart.tsx +0 -387
- package/src/domains/analytics/components/MetricCard.tsx +0 -42
- package/src/domains/analytics/services/AnalyticsEngineService.ts +0 -300
- package/src/domains/charts/components.test.tsx +0 -141
- package/src/domains/charts/components.tsx +0 -683
|
@@ -1,683 +0,0 @@
|
|
|
1
|
-
import { useId, useMemo, useState } from 'react';
|
|
2
|
-
import type { ReactNode } from 'react';
|
|
3
|
-
import { PALETTE, gridTicks, niceMax, type TipState } from './utils';
|
|
4
|
-
|
|
5
|
-
export function ChartTip({ tip }: { tip: TipState }) {
|
|
6
|
-
return (
|
|
7
|
-
<div className="udb-charttip" style={{ left: tip.x, top: tip.y }}>
|
|
8
|
-
<div className="udb-charttip__title">{tip.title}</div>
|
|
9
|
-
|
|
10
|
-
{tip.rows.map((r, i) => (
|
|
11
|
-
<div key={i} className="udb-charttip__row">
|
|
12
|
-
<span className="udb-charttip__label">
|
|
13
|
-
{r.color && <span className="udb-chartlegend__dot" style={{ background: r.color }} />}
|
|
14
|
-
{r.label}
|
|
15
|
-
</span>
|
|
16
|
-
|
|
17
|
-
<b>{r.value}</b>
|
|
18
|
-
|
|
19
|
-
</div>
|
|
20
|
-
|
|
21
|
-
))}
|
|
22
|
-
</div>
|
|
23
|
-
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface ChartCardProps {
|
|
28
|
-
title: string;
|
|
29
|
-
subtitle?: string;
|
|
30
|
-
legend?: ReactNode;
|
|
31
|
-
actions?: ReactNode;
|
|
32
|
-
children: ReactNode;
|
|
33
|
-
tip: TipState | null;
|
|
34
|
-
hostRef?: React.Ref<HTMLDivElement>;
|
|
35
|
-
onMouseLeave?: () => void;
|
|
36
|
-
bare?: boolean;
|
|
37
|
-
className?: string;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function ChartCard({ title, subtitle, legend, actions, children, tip, hostRef, onMouseLeave, bare, className }: ChartCardProps) {
|
|
41
|
-
return (
|
|
42
|
-
<div className={['udb-chartcard', className].filter(Boolean).join(' ')}>
|
|
43
|
-
<div className="udb-chartcard__head">
|
|
44
|
-
<div className="udb-chartcard__heading">
|
|
45
|
-
<div className="udb-chartcard__title">{title}</div>
|
|
46
|
-
|
|
47
|
-
{subtitle && <div className="udb-chartcard__sub">{subtitle}</div>}
|
|
48
|
-
|
|
49
|
-
</div>
|
|
50
|
-
|
|
51
|
-
<div className="udb-chartcard__tools">
|
|
52
|
-
{legend}
|
|
53
|
-
{actions}
|
|
54
|
-
</div>
|
|
55
|
-
|
|
56
|
-
</div>
|
|
57
|
-
|
|
58
|
-
<div className="udb-chartcard__body" style={bare ? { padding: 0 } : undefined}>
|
|
59
|
-
<div ref={hostRef} className="udb-chartcard__plot" onMouseLeave={onMouseLeave}>
|
|
60
|
-
{children}
|
|
61
|
-
{tip && <ChartTip tip={tip} />}
|
|
62
|
-
</div>
|
|
63
|
-
|
|
64
|
-
</div>
|
|
65
|
-
|
|
66
|
-
</div>
|
|
67
|
-
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export interface LegendItem {
|
|
72
|
-
label: string;
|
|
73
|
-
color: string;
|
|
74
|
-
value?: string;
|
|
75
|
-
hint?: string;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function Legend({ items }: { items: LegendItem[] }) {
|
|
79
|
-
return (
|
|
80
|
-
<div className="udb-chartlegend">
|
|
81
|
-
{items.map((i) => (
|
|
82
|
-
<span key={i.label} className="udb-chartlegend__item">
|
|
83
|
-
<span className="udb-chartlegend__dot" style={{ background: i.color }} />
|
|
84
|
-
{i.label}
|
|
85
|
-
{i.value && <span className="udb-chartlegend__value">{i.value}</span>}
|
|
86
|
-
|
|
87
|
-
{i.hint && <span className="udb-chartlegend__hint">{i.hint}</span>}
|
|
88
|
-
|
|
89
|
-
</span>
|
|
90
|
-
|
|
91
|
-
))}
|
|
92
|
-
</div>
|
|
93
|
-
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export function Sparkline({
|
|
98
|
-
data,
|
|
99
|
-
height = 34,
|
|
100
|
-
width = 110,
|
|
101
|
-
color = 'currentColor',
|
|
102
|
-
}: {
|
|
103
|
-
data: readonly number[];
|
|
104
|
-
height?: number;
|
|
105
|
-
width?: number;
|
|
106
|
-
color?: string;
|
|
107
|
-
}) {
|
|
108
|
-
const id = useId();
|
|
109
|
-
if (data.length < 2) return null;
|
|
110
|
-
const min = Math.min(...data);
|
|
111
|
-
const max = Math.max(...data);
|
|
112
|
-
const span = max - min || 1;
|
|
113
|
-
const pts = data.map((v, i) => [(i / (data.length - 1)) * width, height - 3 - ((v - min) / span) * (height - 8)]);
|
|
114
|
-
const line = pts.map((p, i) => `${i === 0 ? 'M' : 'L'}${p[0]!.toFixed(1)},${p[1]!.toFixed(1)}`).join(' ');
|
|
115
|
-
const area = `${line} L${width},${height} L0,${height} Z`;
|
|
116
|
-
return (
|
|
117
|
-
<svg width={width} height={height} viewBox={`0 0 ${width} ${height}`} aria-hidden>
|
|
118
|
-
<defs>
|
|
119
|
-
<linearGradient id={`sp${id}`} x1="0" y1="0" x2="0" y2="1">
|
|
120
|
-
<stop offset="0%" stopColor={color} stopOpacity="0.28" />
|
|
121
|
-
<stop offset="100%" stopColor={color} stopOpacity="0" />
|
|
122
|
-
</linearGradient>
|
|
123
|
-
|
|
124
|
-
</defs>
|
|
125
|
-
|
|
126
|
-
<path d={area} fill={`url(#sp${id})`} />
|
|
127
|
-
<path d={line} fill="none" stroke={color} strokeWidth="1.6" strokeLinejoin="round" strokeLinecap="round" />
|
|
128
|
-
<circle cx={pts.at(-1)![0]} cy={pts.at(-1)![1]} r="2.4" fill={color} />
|
|
129
|
-
</svg>
|
|
130
|
-
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export interface HBarRow {
|
|
135
|
-
label: ReactNode;
|
|
136
|
-
sub?: ReactNode;
|
|
137
|
-
value: number;
|
|
138
|
-
tone?: string;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export function HBars({ rows, formatValue = (n: number) => String(n) }: { rows: HBarRow[]; formatValue?: (n: number) => string }) {
|
|
142
|
-
const max = Math.max(1, ...rows.map((r) => r.value));
|
|
143
|
-
return (
|
|
144
|
-
<div className="udb-hbars">
|
|
145
|
-
{rows.map((r, i) => (
|
|
146
|
-
<div key={i} className="udb-hbars__row">
|
|
147
|
-
<div className="udb-hbars__label">
|
|
148
|
-
<div className="udb-hbars__name">{r.label}</div>
|
|
149
|
-
|
|
150
|
-
{r.sub && <div className="udb-hbars__sub">{r.sub}</div>}
|
|
151
|
-
|
|
152
|
-
</div>
|
|
153
|
-
|
|
154
|
-
<div className="udb-hbars__track">
|
|
155
|
-
<div
|
|
156
|
-
className="udb-hbars__fill"
|
|
157
|
-
style={{ width: `${(r.value / max) * 100}%`, ...(r.tone ? { background: r.tone } : {}) }}
|
|
158
|
-
/>
|
|
159
|
-
</div>
|
|
160
|
-
|
|
161
|
-
<span className="udb-hbars__value">{formatValue(r.value)}</span>
|
|
162
|
-
|
|
163
|
-
</div>
|
|
164
|
-
|
|
165
|
-
))}
|
|
166
|
-
</div>
|
|
167
|
-
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
export interface BarSeries {
|
|
172
|
-
key?: string;
|
|
173
|
-
label: string;
|
|
174
|
-
values?: readonly number[];
|
|
175
|
-
data?: readonly number[];
|
|
176
|
-
tone?: string;
|
|
177
|
-
color?: string;
|
|
178
|
-
/** Per-bar color override (index-aligned with labels); undefined falls back to the series tone. */
|
|
179
|
-
accents?: readonly (string | undefined)[];
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
export interface BarChartGoalLine {
|
|
183
|
-
value: number;
|
|
184
|
-
label?: string;
|
|
185
|
-
tone?: string;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
export function BarChart({
|
|
189
|
-
labels,
|
|
190
|
-
series,
|
|
191
|
-
height = 130,
|
|
192
|
-
formatValue = (v: number) => String(v),
|
|
193
|
-
formatLabel = (l: string) => l,
|
|
194
|
-
goalLine,
|
|
195
|
-
}: {
|
|
196
|
-
labels: readonly string[];
|
|
197
|
-
series: BarSeries[];
|
|
198
|
-
height?: number;
|
|
199
|
-
formatValue?: (v: number) => string;
|
|
200
|
-
formatLabel?: (l: string) => string;
|
|
201
|
-
goalLine?: BarChartGoalLine;
|
|
202
|
-
}) {
|
|
203
|
-
const [hover, setHover] = useState<number | null>(null);
|
|
204
|
-
const vals = (s: BarSeries) => s.values ?? s.data ?? [];
|
|
205
|
-
const max = Math.max(1, ...series.flatMap((s) => vals(s)), goalLine?.value ?? 0);
|
|
206
|
-
const toneOf = (s: BarSeries, i: number) => s.tone ?? s.color ?? PALETTE[i % PALETTE.length]!;
|
|
207
|
-
const accentOf = (s: BarSeries, si: number, i: number) => s.accents?.[i] ?? toneOf(s, si);
|
|
208
|
-
const goalPct = goalLine !== undefined ? Math.min(100, (goalLine.value / max) * 100) : null;
|
|
209
|
-
return (
|
|
210
|
-
<div className="udb-barchart" style={{ height }}>
|
|
211
|
-
{goalLine !== undefined && goalPct !== null && (
|
|
212
|
-
<div
|
|
213
|
-
className="udb-barchart__goal"
|
|
214
|
-
style={{
|
|
215
|
-
bottom: `${goalPct}%`,
|
|
216
|
-
...(goalLine.tone !== undefined ? { borderTopColor: goalLine.tone } : {}),
|
|
217
|
-
}}
|
|
218
|
-
title={goalLine.label ?? formatValue(goalLine.value)}
|
|
219
|
-
aria-hidden="true"
|
|
220
|
-
>
|
|
221
|
-
{goalLine.label !== undefined && (
|
|
222
|
-
<span
|
|
223
|
-
className="udb-barchart__goallabel"
|
|
224
|
-
style={
|
|
225
|
-
goalLine.tone !== undefined
|
|
226
|
-
? { color: goalLine.tone, borderColor: goalLine.tone }
|
|
227
|
-
: undefined
|
|
228
|
-
}
|
|
229
|
-
>
|
|
230
|
-
{goalLine.label}
|
|
231
|
-
</span>
|
|
232
|
-
|
|
233
|
-
)}
|
|
234
|
-
</div>
|
|
235
|
-
|
|
236
|
-
)}
|
|
237
|
-
{labels.map((label, i) => (
|
|
238
|
-
<div
|
|
239
|
-
key={label}
|
|
240
|
-
className="udb-barchart__col"
|
|
241
|
-
onMouseEnter={() => setHover(i)}
|
|
242
|
-
onMouseLeave={() => setHover(null)}
|
|
243
|
-
>
|
|
244
|
-
<div className="udb-barchart__bars">
|
|
245
|
-
{series.map((s, si) => (
|
|
246
|
-
<div
|
|
247
|
-
key={s.key ?? s.label}
|
|
248
|
-
className="udb-barchart__bar"
|
|
249
|
-
style={{
|
|
250
|
-
height: `${Math.max(2, ((vals(s)[i] ?? 0) / max) * 100)}%`,
|
|
251
|
-
background: accentOf(s, si, i),
|
|
252
|
-
opacity: hover === null || hover === i ? 1 : 0.4,
|
|
253
|
-
}}
|
|
254
|
-
/>
|
|
255
|
-
))}
|
|
256
|
-
</div>
|
|
257
|
-
|
|
258
|
-
{hover === i && (
|
|
259
|
-
<div className="udb-barchart__tip">
|
|
260
|
-
<div className="udb-barchart__tiptitle">{formatLabel(label)}</div>
|
|
261
|
-
|
|
262
|
-
{series.map((s, si) => (
|
|
263
|
-
<div key={s.key ?? s.label} className="udb-barchart__tiprow">
|
|
264
|
-
<span className="udb-barchart__tiplabel">
|
|
265
|
-
<span className="udb-chartlegend__dot" style={{ background: accentOf(s, si, i) }} />
|
|
266
|
-
{s.label}
|
|
267
|
-
</span>
|
|
268
|
-
|
|
269
|
-
<b>{formatValue(vals(s)[i] ?? 0)}</b>
|
|
270
|
-
|
|
271
|
-
</div>
|
|
272
|
-
|
|
273
|
-
))}
|
|
274
|
-
</div>
|
|
275
|
-
|
|
276
|
-
)}
|
|
277
|
-
</div>
|
|
278
|
-
|
|
279
|
-
))}
|
|
280
|
-
</div>
|
|
281
|
-
|
|
282
|
-
);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
export interface DonutSegment {
|
|
286
|
-
label: string;
|
|
287
|
-
value: number;
|
|
288
|
-
tone?: string;
|
|
289
|
-
color?: string;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
export function Donut({
|
|
293
|
-
segments,
|
|
294
|
-
size = 148,
|
|
295
|
-
thickness = 18,
|
|
296
|
-
centerValue,
|
|
297
|
-
centerLabel,
|
|
298
|
-
formatValue = (v: number) => String(v),
|
|
299
|
-
}: {
|
|
300
|
-
segments: DonutSegment[];
|
|
301
|
-
size?: number;
|
|
302
|
-
thickness?: number;
|
|
303
|
-
centerValue?: string;
|
|
304
|
-
centerLabel?: string;
|
|
305
|
-
formatValue?: (v: number) => string;
|
|
306
|
-
}) {
|
|
307
|
-
const [active, setActive] = useState<number | null>(null);
|
|
308
|
-
const total = segments.reduce((sum, seg) => sum + seg.value, 0) || 1;
|
|
309
|
-
const radius = (size - thickness) / 2;
|
|
310
|
-
const circumference = 2 * Math.PI * radius;
|
|
311
|
-
let offset = 0;
|
|
312
|
-
return (
|
|
313
|
-
<div className="udb-donut">
|
|
314
|
-
<div className="udb-donut__wrap" style={{ width: size, height: size }}>
|
|
315
|
-
<svg width={size} height={size} style={{ transform: 'rotate(-90deg)' }}>
|
|
316
|
-
<circle
|
|
317
|
-
cx={size / 2}
|
|
318
|
-
cy={size / 2}
|
|
319
|
-
r={radius}
|
|
320
|
-
fill="none"
|
|
321
|
-
stroke="var(--color-muted)"
|
|
322
|
-
strokeWidth={thickness}
|
|
323
|
-
/>
|
|
324
|
-
{segments.map((seg, i) => {
|
|
325
|
-
const dash = (seg.value / total) * circumference;
|
|
326
|
-
const el = (
|
|
327
|
-
<circle
|
|
328
|
-
key={seg.label}
|
|
329
|
-
cx={size / 2}
|
|
330
|
-
cy={size / 2}
|
|
331
|
-
r={radius}
|
|
332
|
-
fill="none"
|
|
333
|
-
stroke={seg.tone ?? seg.color ?? PALETTE[i % PALETTE.length]!}
|
|
334
|
-
strokeWidth={active === i ? thickness + 3 : thickness}
|
|
335
|
-
strokeDasharray={`${dash} ${circumference - dash}`}
|
|
336
|
-
strokeDashoffset={-offset}
|
|
337
|
-
strokeLinecap="butt"
|
|
338
|
-
opacity={active === null || active === i ? 1 : 0.35}
|
|
339
|
-
className="udb-donut__seg"
|
|
340
|
-
onMouseEnter={() => setActive(i)}
|
|
341
|
-
onMouseLeave={() => setActive(null)}
|
|
342
|
-
/>
|
|
343
|
-
);
|
|
344
|
-
offset += dash;
|
|
345
|
-
return el;
|
|
346
|
-
})}
|
|
347
|
-
</svg>
|
|
348
|
-
|
|
349
|
-
<div className="udb-donut__center">
|
|
350
|
-
<span className="udb-donut__value">
|
|
351
|
-
{active !== null ? formatValue(segments[active]!.value) : centerValue}
|
|
352
|
-
</span>
|
|
353
|
-
|
|
354
|
-
<span className="udb-donut__label">{active !== null ? segments[active]!.label : centerLabel}</span>
|
|
355
|
-
|
|
356
|
-
</div>
|
|
357
|
-
|
|
358
|
-
</div>
|
|
359
|
-
|
|
360
|
-
<div className="udb-donut__list">
|
|
361
|
-
{segments.map((seg, i) => (
|
|
362
|
-
<div
|
|
363
|
-
key={seg.label}
|
|
364
|
-
className="udb-donut__item"
|
|
365
|
-
onMouseEnter={() => setActive(i)}
|
|
366
|
-
onMouseLeave={() => setActive(null)}
|
|
367
|
-
>
|
|
368
|
-
<span className="udb-donut__itemlabel">
|
|
369
|
-
<span className="udb-donut__dot" style={{ background: seg.tone ?? seg.color ?? PALETTE[i % PALETTE.length]! }} />
|
|
370
|
-
{seg.label}
|
|
371
|
-
</span>
|
|
372
|
-
|
|
373
|
-
<span className="udb-donut__itemvalue">
|
|
374
|
-
{formatValue(seg.value)}
|
|
375
|
-
<span className="udb-donut__pct">{Math.round((seg.value / total) * 100)}%</span>
|
|
376
|
-
|
|
377
|
-
</span>
|
|
378
|
-
|
|
379
|
-
</div>
|
|
380
|
-
|
|
381
|
-
))}
|
|
382
|
-
</div>
|
|
383
|
-
|
|
384
|
-
</div>
|
|
385
|
-
|
|
386
|
-
);
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
export interface LineSeries {
|
|
390
|
-
key?: string;
|
|
391
|
-
label: string;
|
|
392
|
-
values?: readonly number[];
|
|
393
|
-
data?: readonly number[];
|
|
394
|
-
tone?: string;
|
|
395
|
-
color?: string;
|
|
396
|
-
kind?: 'line' | 'bars';
|
|
397
|
-
axis?: 'left' | 'right';
|
|
398
|
-
dashed?: boolean;
|
|
399
|
-
area?: boolean;
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
export interface LineChartPoint {
|
|
403
|
-
label: string;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
export function LineChart({
|
|
407
|
-
points,
|
|
408
|
-
labels,
|
|
409
|
-
series,
|
|
410
|
-
band,
|
|
411
|
-
height = 220,
|
|
412
|
-
formatLeft,
|
|
413
|
-
formatRight,
|
|
414
|
-
formatValue,
|
|
415
|
-
yLabelLeft,
|
|
416
|
-
yLabelRight,
|
|
417
|
-
}: {
|
|
418
|
-
points?: readonly LineChartPoint[];
|
|
419
|
-
labels?: readonly string[];
|
|
420
|
-
series: LineSeries[];
|
|
421
|
-
band?: { from: number; to: number };
|
|
422
|
-
height?: number;
|
|
423
|
-
formatLeft?: (v: number) => string;
|
|
424
|
-
formatRight?: (v: number) => string;
|
|
425
|
-
formatValue?: (v: number) => string;
|
|
426
|
-
yLabelLeft?: string;
|
|
427
|
-
yLabelRight?: string;
|
|
428
|
-
}) {
|
|
429
|
-
const [hover, setHover] = useState<number | null>(null);
|
|
430
|
-
const pts = points ?? (labels ?? []).map((label) => ({ label }));
|
|
431
|
-
const fmtLeft = formatLeft ?? formatValue ?? ((v: number) => String(Math.round(v)));
|
|
432
|
-
const fmtRight = formatRight ?? formatValue ?? ((v: number) => String(Math.round(v)));
|
|
433
|
-
const vals = (s: LineSeries) => s.values ?? s.data ?? [];
|
|
434
|
-
const w = 720;
|
|
435
|
-
const padLeft = 52;
|
|
436
|
-
const padRight = series.some((s) => s.axis === 'right') ? 46 : 14;
|
|
437
|
-
const padTop = 12;
|
|
438
|
-
const padBottom = 24;
|
|
439
|
-
const innerW = w - padLeft - padRight;
|
|
440
|
-
const innerH = height - padTop - padBottom;
|
|
441
|
-
const n = pts.length;
|
|
442
|
-
|
|
443
|
-
const toneOf = (s: LineSeries, i: number) => s.tone ?? s.color ?? PALETTE[i % PALETTE.length]!;
|
|
444
|
-
|
|
445
|
-
const leftMax = useMemo(
|
|
446
|
-
() => niceMax(Math.max(1, ...series.filter((s) => s.axis !== 'right').flatMap((s) => vals(s)), ...(band ? [band.from, band.to] : []))),
|
|
447
|
-
[series, band],
|
|
448
|
-
);
|
|
449
|
-
const rightMax = useMemo(
|
|
450
|
-
() => niceMax(Math.max(1, ...series.filter((s) => s.axis === 'right').flatMap((s) => vals(s)))),
|
|
451
|
-
[series],
|
|
452
|
-
);
|
|
453
|
-
const leftTicks = useMemo(() => gridTicks(Math.max(...series.filter((s) => s.axis !== 'right').flatMap((s) => vals(s)), ...(band ? [band.from, band.to] : []), 1)), [series, band]);
|
|
454
|
-
|
|
455
|
-
const xAt = (i: number) => padLeft + (n <= 1 ? innerW / 2 : (i / (n - 1)) * innerW);
|
|
456
|
-
const yLeft = (v: number) => padTop + innerH - (v / leftMax) * innerH;
|
|
457
|
-
const yRight = (v: number) => padTop + innerH - (v / rightMax) * innerH;
|
|
458
|
-
const labelEvery = Math.max(1, Math.ceil(n / 8));
|
|
459
|
-
|
|
460
|
-
if (n === 0) return null;
|
|
461
|
-
|
|
462
|
-
return (
|
|
463
|
-
<div className="udb-linechart">
|
|
464
|
-
<svg
|
|
465
|
-
viewBox={`0 0 ${w} ${height}`}
|
|
466
|
-
className="udb-linechart__svg"
|
|
467
|
-
style={{ height }}
|
|
468
|
-
onMouseLeave={() => setHover(null)}
|
|
469
|
-
onMouseMove={(e) => {
|
|
470
|
-
const rect = e.currentTarget.getBoundingClientRect();
|
|
471
|
-
const x = ((e.clientX - rect.left) / rect.width) * w;
|
|
472
|
-
const idx = Math.round(((x - padLeft) / innerW) * (n - 1));
|
|
473
|
-
setHover(Math.max(0, Math.min(n - 1, idx)));
|
|
474
|
-
}}
|
|
475
|
-
>
|
|
476
|
-
{leftTicks.map((v, i) => (
|
|
477
|
-
<g key={i}>
|
|
478
|
-
<line x1={padLeft} x2={w - padRight} y1={yLeft(v)} y2={yLeft(v)} className="udb-linechart__grid" />
|
|
479
|
-
<text x={padLeft - 6} y={yLeft(v) + 3} textAnchor="end" className="udb-linechart__axis">
|
|
480
|
-
{fmtLeft(v)}
|
|
481
|
-
</text>
|
|
482
|
-
|
|
483
|
-
</g>
|
|
484
|
-
|
|
485
|
-
))}
|
|
486
|
-
{band && (
|
|
487
|
-
<rect
|
|
488
|
-
x={padLeft}
|
|
489
|
-
y={yLeft(band.to)}
|
|
490
|
-
width={innerW}
|
|
491
|
-
height={Math.max(0, yLeft(band.from) - yLeft(band.to))}
|
|
492
|
-
rx={2}
|
|
493
|
-
className="udb-linechart__band"
|
|
494
|
-
/>
|
|
495
|
-
)}
|
|
496
|
-
{series.some((s) => s.axis === 'right') && (
|
|
497
|
-
<g>
|
|
498
|
-
{leftTicks.map((v, i) => (
|
|
499
|
-
<text key={i} x={w - padRight + 6} y={yLeft(v) + 3} textAnchor="start" className="udb-linechart__axis">
|
|
500
|
-
{fmtRight((v / leftMax) * rightMax)}
|
|
501
|
-
</text>
|
|
502
|
-
|
|
503
|
-
))}
|
|
504
|
-
</g>
|
|
505
|
-
|
|
506
|
-
)}
|
|
507
|
-
{pts.map((p, i) =>
|
|
508
|
-
i % labelEvery === 0 ? (
|
|
509
|
-
<text key={i} x={xAt(i)} y={height - 8} textAnchor="middle" className="udb-linechart__axis">
|
|
510
|
-
{p.label}
|
|
511
|
-
</text>
|
|
512
|
-
|
|
513
|
-
) : null,
|
|
514
|
-
)}
|
|
515
|
-
{series.map((s, si) => {
|
|
516
|
-
const yFn = s.axis === 'right' ? yRight : yLeft;
|
|
517
|
-
if (s.kind === 'bars') {
|
|
518
|
-
const barW = Math.max(3, (innerW / Math.max(1, n - 1)) * 0.5);
|
|
519
|
-
return (
|
|
520
|
-
<g key={s.key ?? s.label}>
|
|
521
|
-
{vals(s).slice(0, n).map((v, i) => (
|
|
522
|
-
<rect
|
|
523
|
-
key={i}
|
|
524
|
-
x={xAt(i) - barW / 2}
|
|
525
|
-
y={yFn(v)}
|
|
526
|
-
width={barW}
|
|
527
|
-
height={Math.max(0, padTop + innerH - yFn(v))}
|
|
528
|
-
rx={1.5}
|
|
529
|
-
fill={toneOf(s, si)}
|
|
530
|
-
opacity={hover === null || hover === i ? 0.85 : 0.35}
|
|
531
|
-
/>
|
|
532
|
-
))}
|
|
533
|
-
</g>
|
|
534
|
-
|
|
535
|
-
);
|
|
536
|
-
}
|
|
537
|
-
const path = vals(s)
|
|
538
|
-
.slice(0, n)
|
|
539
|
-
.map((v, i) => `${i === 0 ? 'M' : 'L'}${xAt(i).toFixed(1)},${yFn(v).toFixed(1)}`)
|
|
540
|
-
.join(' ');
|
|
541
|
-
const areaPath = `${path} L${xAt(n - 1).toFixed(1)},${padTop + innerH} L${xAt(0).toFixed(1)},${padTop + innerH} Z`;
|
|
542
|
-
return (
|
|
543
|
-
<g key={s.key ?? s.label}>
|
|
544
|
-
{s.area !== false && !s.dashed && <path d={areaPath} fill={toneOf(s, si)} opacity={0.07} />}
|
|
545
|
-
<path
|
|
546
|
-
d={path}
|
|
547
|
-
fill="none"
|
|
548
|
-
stroke={toneOf(s, si)}
|
|
549
|
-
strokeWidth={2}
|
|
550
|
-
strokeLinejoin="round"
|
|
551
|
-
strokeLinecap="round"
|
|
552
|
-
strokeDasharray={s.dashed ? '4 4' : undefined}
|
|
553
|
-
/>
|
|
554
|
-
{vals(s).slice(0, n).map((v, i) => (
|
|
555
|
-
<circle
|
|
556
|
-
key={i}
|
|
557
|
-
cx={xAt(i)}
|
|
558
|
-
cy={yFn(v)}
|
|
559
|
-
r={hover === i ? 3.4 : 0}
|
|
560
|
-
fill="var(--color-card)"
|
|
561
|
-
stroke={toneOf(s, si)}
|
|
562
|
-
strokeWidth={2}
|
|
563
|
-
/>
|
|
564
|
-
))}
|
|
565
|
-
</g>
|
|
566
|
-
|
|
567
|
-
);
|
|
568
|
-
})}
|
|
569
|
-
{hover !== null && (
|
|
570
|
-
<line
|
|
571
|
-
x1={xAt(hover)}
|
|
572
|
-
x2={xAt(hover)}
|
|
573
|
-
y1={padTop}
|
|
574
|
-
y2={padTop + innerH}
|
|
575
|
-
className="udb-linechart__crosshair"
|
|
576
|
-
/>
|
|
577
|
-
)}
|
|
578
|
-
{yLabelLeft && (
|
|
579
|
-
<text x={4} y={10} className="udb-linechart__axis">
|
|
580
|
-
{yLabelLeft}
|
|
581
|
-
</text>
|
|
582
|
-
|
|
583
|
-
)}
|
|
584
|
-
{yLabelRight && (
|
|
585
|
-
<text x={w - 4} y={10} textAnchor="end" className="udb-linechart__axis">
|
|
586
|
-
{yLabelRight}
|
|
587
|
-
</text>
|
|
588
|
-
|
|
589
|
-
)}
|
|
590
|
-
</svg>
|
|
591
|
-
|
|
592
|
-
{hover !== null && (
|
|
593
|
-
<div
|
|
594
|
-
className="udb-linechart__tip"
|
|
595
|
-
style={{
|
|
596
|
-
left: `${(xAt(hover) / w) * 100}%`,
|
|
597
|
-
transform: hover > n / 2 ? 'translateX(calc(-100% - 10px))' : 'translateX(10px)',
|
|
598
|
-
}}
|
|
599
|
-
>
|
|
600
|
-
<div className="udb-linechart__tiptitle">{pts[hover]!.label}</div>
|
|
601
|
-
|
|
602
|
-
{series.map((s, si) => (
|
|
603
|
-
<div key={s.key ?? s.label} className="udb-linechart__tiprow">
|
|
604
|
-
<span>
|
|
605
|
-
<span className="udb-linechart__swatch" style={{ background: toneOf(s, si) }} />
|
|
606
|
-
{s.label}
|
|
607
|
-
</span>
|
|
608
|
-
|
|
609
|
-
<b>{(s.axis === 'right' ? fmtRight : fmtLeft)(vals(s)[hover] ?? 0)}</b>
|
|
610
|
-
|
|
611
|
-
</div>
|
|
612
|
-
|
|
613
|
-
))}
|
|
614
|
-
</div>
|
|
615
|
-
|
|
616
|
-
)}
|
|
617
|
-
<div className="udb-linechart__legendhost">
|
|
618
|
-
<Legend items={series.map((s, si) => ({ label: s.label, color: toneOf(s, si) }))} />
|
|
619
|
-
</div>
|
|
620
|
-
|
|
621
|
-
</div>
|
|
622
|
-
|
|
623
|
-
);
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
export interface HeatmapCell {
|
|
627
|
-
day: string;
|
|
628
|
-
value: number;
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
export interface ActivityHeatmapProps {
|
|
632
|
-
cells: HeatmapCell[];
|
|
633
|
-
max?: number;
|
|
634
|
-
titleFor?: (cell: HeatmapCell) => string;
|
|
635
|
-
ariaLabel?: string;
|
|
636
|
-
className?: string;
|
|
637
|
-
levelClassNames?: string[];
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
const DEFAULT_HEATMAP_LEVELS = [
|
|
641
|
-
'bg-muted/40 dark:bg-muted/30',
|
|
642
|
-
'bg-primary/20 text-primary',
|
|
643
|
-
'bg-primary/40 text-primary',
|
|
644
|
-
'bg-primary/70 text-primary-foreground',
|
|
645
|
-
'bg-primary text-primary-foreground',
|
|
646
|
-
];
|
|
647
|
-
|
|
648
|
-
export function ActivityHeatmap({
|
|
649
|
-
cells,
|
|
650
|
-
max,
|
|
651
|
-
titleFor,
|
|
652
|
-
ariaLabel = 'Activity heatmap',
|
|
653
|
-
className = '',
|
|
654
|
-
levelClassNames = DEFAULT_HEATMAP_LEVELS,
|
|
655
|
-
}: ActivityHeatmapProps) {
|
|
656
|
-
const peak = Math.max(1, max ?? Math.max(0, ...cells.map((cell) => cell.value)));
|
|
657
|
-
const maxIntensity = levelClassNames.length - 1;
|
|
658
|
-
|
|
659
|
-
return (
|
|
660
|
-
<div
|
|
661
|
-
className={`grid grid-flow-col grid-rows-7 gap-1 ${className}`.trim()}
|
|
662
|
-
role="img"
|
|
663
|
-
aria-label={ariaLabel}
|
|
664
|
-
>
|
|
665
|
-
{cells.map((cell, index) => {
|
|
666
|
-
const intensity =
|
|
667
|
-
cell.value <= 0 ? 0 : Math.min(maxIntensity, Math.ceil((cell.value / peak) * maxIntensity));
|
|
668
|
-
const cellClass = levelClassNames[intensity] ?? levelClassNames[0];
|
|
669
|
-
const cellTitle = titleFor ? titleFor(cell) : `${cell.day}: ${cell.value}`;
|
|
670
|
-
|
|
671
|
-
return (
|
|
672
|
-
<span
|
|
673
|
-
key={`${cell.day}-${index}`}
|
|
674
|
-
className={`h-3.5 w-3.5 rounded-[3px] transition-colors ${cellClass}`}
|
|
675
|
-
title={cellTitle}
|
|
676
|
-
>
|
|
677
|
-
<span className="sr-only">{cellTitle}</span>
|
|
678
|
-
</span>
|
|
679
|
-
);
|
|
680
|
-
})}
|
|
681
|
-
</div>
|
|
682
|
-
);
|
|
683
|
-
}
|