@oaiz/app-kit 0.0.0-stage → 0.1.4128
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/LICENSE +21 -0
- package/README.md +143 -2
- package/THIRD_PARTY_NOTICES.md +76 -0
- package/package.json +139 -4
- package/registry.json +1847 -0
- package/src/components/activity-timeline.tsx +227 -0
- package/src/components/app-shell.tsx +622 -0
- package/src/components/bar-list.tsx +47 -0
- package/src/components/board.tsx +700 -0
- package/src/components/calendar-view.tsx +1388 -0
- package/src/components/card-grid.tsx +462 -0
- package/src/components/charts.tsx +701 -0
- package/src/components/chat-markdown.tsx +230 -0
- package/src/components/chat-panel.tsx +760 -0
- package/src/components/code-editor.tsx +276 -0
- package/src/components/collection-controls.tsx +226 -0
- package/src/components/command-palette.tsx +403 -0
- package/src/components/confirm-action.tsx +123 -0
- package/src/components/data-table.tsx +821 -0
- package/src/components/date-input.tsx +429 -0
- package/src/components/empty-state.tsx +31 -0
- package/src/components/error-state.tsx +37 -0
- package/src/components/file-upload.tsx +595 -0
- package/src/components/filter-bar.tsx +415 -0
- package/src/components/grouped-picker.tsx +250 -0
- package/src/components/line-items.tsx +601 -0
- package/src/components/list-view.tsx +491 -0
- package/src/components/metrics.tsx +53 -0
- package/src/components/page-header.tsx +33 -0
- package/src/components/property-picker.tsx +546 -0
- package/src/components/record-detail.tsx +416 -0
- package/src/components/record-form.tsx +295 -0
- package/src/components/record-name.tsx +85 -0
- package/src/components/save-bar.tsx +142 -0
- package/src/components/sign-in.tsx +179 -0
- package/src/components/split-view.tsx +128 -0
- package/src/components/status-dot.tsx +56 -0
- package/src/components/stepper.tsx +153 -0
- package/src/components/text-diff-model.ts +111 -0
- package/src/components/text-diff.tsx +98 -0
- package/src/components/time-zone-picker.tsx +69 -0
- package/src/components/ui/accordion.tsx +138 -0
- package/src/components/ui/alert-dialog.tsx +67 -0
- package/src/components/ui/alert.tsx +65 -0
- package/src/components/ui/avatar-group.tsx +119 -0
- package/src/components/ui/avatar.tsx +55 -0
- package/src/components/ui/badge.tsx +59 -0
- package/src/components/ui/bottom-navigation.tsx +102 -0
- package/src/components/ui/breadcrumb.tsx +117 -0
- package/src/components/ui/button.tsx +71 -0
- package/src/components/ui/card.tsx +95 -0
- package/src/components/ui/checkbox.tsx +31 -0
- package/src/components/ui/collapsible.tsx +27 -0
- package/src/components/ui/context-menu.tsx +270 -0
- package/src/components/ui/count-up.tsx +136 -0
- package/src/components/ui/detail-disclosure-section.tsx +109 -0
- package/src/components/ui/dialog.tsx +157 -0
- package/src/components/ui/dropdown-menu.tsx +273 -0
- package/src/components/ui/form-field.tsx +155 -0
- package/src/components/ui/heading.tsx +126 -0
- package/src/components/ui/hover-card.tsx +45 -0
- package/src/components/ui/input.tsx +50 -0
- package/src/components/ui/label.tsx +30 -0
- package/src/components/ui/orb-spinner.tsx +238 -0
- package/src/components/ui/popover.tsx +59 -0
- package/src/components/ui/progress-ring.tsx +125 -0
- package/src/components/ui/progress.tsx +38 -0
- package/src/components/ui/radio-group.tsx +124 -0
- package/src/components/ui/resizable.tsx +48 -0
- package/src/components/ui/scroll-fade.tsx +98 -0
- package/src/components/ui/segmented-control.tsx +70 -0
- package/src/components/ui/select.tsx +230 -0
- package/src/components/ui/separator.tsx +29 -0
- package/src/components/ui/sheet.tsx +170 -0
- package/src/components/ui/shimmer-text.tsx +87 -0
- package/src/components/ui/shortcut.tsx +39 -0
- package/src/components/ui/skeleton.tsx +15 -0
- package/src/components/ui/slider.tsx +48 -0
- package/src/components/ui/switch.tsx +44 -0
- package/src/components/ui/table.tsx +119 -0
- package/src/components/ui/tabs.tsx +80 -0
- package/src/components/ui/text.tsx +93 -0
- package/src/components/ui/textarea.tsx +51 -0
- package/src/components/ui/toast.tsx +141 -0
- package/src/components/ui/toaster.tsx +40 -0
- package/src/components/ui/tooltip.tsx +38 -0
- package/src/components/ui/top-banner.tsx +108 -0
- package/src/components/ui/tree-view.tsx +215 -0
- package/src/components/ui/use-toast.tsx +184 -0
- package/src/components/ui/voice-orb.tsx +252 -0
- package/src/hooks/oaiz-chat.tsx +530 -0
- package/src/hooks/oaiz-data.tsx +617 -0
- package/src/hooks/oaiz-session.tsx +63 -0
- package/src/hooks/use-arrivals.ts +19 -0
- package/src/hooks/use-breakpoint.ts +100 -0
- package/src/hooks/use-media-query.ts +36 -0
- package/src/hooks/use-theme.ts +67 -0
- package/src/hooks/use-url-state.ts +77 -0
- package/src/lib/breakpoints.ts +81 -0
- package/src/lib/csp-nonce.ts +15 -0
- package/src/lib/format.ts +291 -0
- package/src/lib/speech-level.ts +118 -0
- package/src/lib/utils.ts +109 -0
- package/src/styles/oaiz/app.css +21 -0
- package/src/styles/oaiz/scrollbars.css +20 -0
- package/src/styles/oaiz/tokens.css +952 -0
- package/src/styles/oaiz/voice-orb.css +39 -0
|
@@ -0,0 +1,701 @@
|
|
|
1
|
+
import { Table, TableHeader, TableBody, TableRow, TableHead, TableCell, TableCaption } from './ui/table';
|
|
2
|
+
import { Button } from './ui/button';
|
|
3
|
+
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from './ui/collapsible';
|
|
4
|
+
import {
|
|
5
|
+
useId,
|
|
6
|
+
useLayoutEffect,
|
|
7
|
+
useRef,
|
|
8
|
+
useState,
|
|
9
|
+
type KeyboardEvent,
|
|
10
|
+
type PointerEvent,
|
|
11
|
+
} from 'react';
|
|
12
|
+
|
|
13
|
+
import { cn } from '../lib/utils';
|
|
14
|
+
import { Text } from './ui/text';
|
|
15
|
+
|
|
16
|
+
export type ChartSeries<Row> = {
|
|
17
|
+
key: string;
|
|
18
|
+
label: string;
|
|
19
|
+
/** null/undefined means missing, not zero. Non-finite numbers produce an error state. */
|
|
20
|
+
value: (row: Row) => number | null | undefined;
|
|
21
|
+
};
|
|
22
|
+
export type ChartMessages = {
|
|
23
|
+
viewData: string;
|
|
24
|
+
labelColumn: string;
|
|
25
|
+
missing: string;
|
|
26
|
+
missingNotice: string;
|
|
27
|
+
empty: string;
|
|
28
|
+
invalid: string;
|
|
29
|
+
keyboard: string;
|
|
30
|
+
dismiss: string;
|
|
31
|
+
};
|
|
32
|
+
const MESSAGES: ChartMessages = {
|
|
33
|
+
viewData: 'View data',
|
|
34
|
+
labelColumn: 'Label',
|
|
35
|
+
missing: 'No value',
|
|
36
|
+
missingNotice: 'Some values are missing. View the data table for details.',
|
|
37
|
+
empty: 'No data to display.',
|
|
38
|
+
invalid: 'The chart contains invalid numeric values.',
|
|
39
|
+
keyboard:
|
|
40
|
+
'Use Left and Right arrows to inspect values. Escape dismisses the details.',
|
|
41
|
+
dismiss: 'Dismiss details',
|
|
42
|
+
};
|
|
43
|
+
export type ChartProps<Row> = {
|
|
44
|
+
label: string;
|
|
45
|
+
rows: Row[];
|
|
46
|
+
x: (row: Row) => string;
|
|
47
|
+
series: ChartSeries<Row>[];
|
|
48
|
+
formatValue?: (value: number) => string;
|
|
49
|
+
locale?: string;
|
|
50
|
+
height?: number;
|
|
51
|
+
className?: string;
|
|
52
|
+
messages?: Partial<ChartMessages>;
|
|
53
|
+
};
|
|
54
|
+
const TOP = 8;
|
|
55
|
+
const BOTTOM = 24;
|
|
56
|
+
const LEFT = 56;
|
|
57
|
+
const RIGHT = 16;
|
|
58
|
+
const SERIES_TEXT = [
|
|
59
|
+
'text-chart-1',
|
|
60
|
+
'text-chart-2',
|
|
61
|
+
'text-chart-3',
|
|
62
|
+
'text-chart-4',
|
|
63
|
+
'text-chart-5',
|
|
64
|
+
];
|
|
65
|
+
const DASHES = ['', '8 4', '2 4', '10 3 2 3', '4 3'];
|
|
66
|
+
export function chartColorClass(index: number): string {
|
|
67
|
+
return SERIES_TEXT[index % SERIES_TEXT.length]!;
|
|
68
|
+
}
|
|
69
|
+
function useWidth() {
|
|
70
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
71
|
+
const [width, setWidth] = useState(480);
|
|
72
|
+
useLayoutEffect(() => {
|
|
73
|
+
const element = ref.current;
|
|
74
|
+
if (!element) return;
|
|
75
|
+
const measure = () => {
|
|
76
|
+
if (element.clientWidth > 0) setWidth(element.clientWidth);
|
|
77
|
+
};
|
|
78
|
+
measure();
|
|
79
|
+
const observer = new ResizeObserver(measure);
|
|
80
|
+
observer.observe(element);
|
|
81
|
+
return () => observer.disconnect();
|
|
82
|
+
});
|
|
83
|
+
return [ref, width] as const;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** A signed domain including zero, with five-ish readable intervals. */
|
|
87
|
+
export function chartDomain(values: number[]) {
|
|
88
|
+
let min = 0;
|
|
89
|
+
let max = 0;
|
|
90
|
+
for (const value of values) {
|
|
91
|
+
min = Math.min(min, value);
|
|
92
|
+
max = Math.max(max, value);
|
|
93
|
+
}
|
|
94
|
+
const rough = (max - min || 1) / 4;
|
|
95
|
+
const power = 10 ** Math.floor(Math.log10(rough));
|
|
96
|
+
const step =
|
|
97
|
+
[1, 2, 2.5, 5, 10].map((n) => n * power).find((n) => n >= rough) ??
|
|
98
|
+
power * 10;
|
|
99
|
+
const low = Math.floor(min / step) * step;
|
|
100
|
+
const high = Math.ceil(max / step) * step || (low === 0 ? 1 : 0);
|
|
101
|
+
return { low, high, step };
|
|
102
|
+
}
|
|
103
|
+
function shown(
|
|
104
|
+
value: number | null | undefined,
|
|
105
|
+
format: (value: number) => string,
|
|
106
|
+
messages: ChartMessages,
|
|
107
|
+
) {
|
|
108
|
+
return value == null
|
|
109
|
+
? messages.missing
|
|
110
|
+
: Number.isFinite(value)
|
|
111
|
+
? format(value)
|
|
112
|
+
: messages.invalid;
|
|
113
|
+
}
|
|
114
|
+
function DataTable<Row>({
|
|
115
|
+
label,
|
|
116
|
+
rows,
|
|
117
|
+
x,
|
|
118
|
+
series,
|
|
119
|
+
format,
|
|
120
|
+
messages,
|
|
121
|
+
}: ChartProps<Row> & {
|
|
122
|
+
format: (value: number) => string;
|
|
123
|
+
messages: ChartMessages;
|
|
124
|
+
}) {
|
|
125
|
+
return (
|
|
126
|
+
<Collapsible className="min-w-0">
|
|
127
|
+
<CollapsibleTrigger asChild>
|
|
128
|
+
<Button variant="link" size="sm" className="px-0">
|
|
129
|
+
{messages.viewData}
|
|
130
|
+
</Button>
|
|
131
|
+
</CollapsibleTrigger>
|
|
132
|
+
<CollapsibleContent className="mt-inline max-w-full overflow-x-auto">
|
|
133
|
+
<Table containerClassName="overflow-visible" className="w-full border-collapse text-left text-sm">
|
|
134
|
+
<TableCaption className="sr-only">{label}</TableCaption>
|
|
135
|
+
<TableHeader>
|
|
136
|
+
<TableRow>
|
|
137
|
+
<TableHead scope="col" className="border-b p-inline">
|
|
138
|
+
{messages.labelColumn}
|
|
139
|
+
</TableHead>
|
|
140
|
+
{series.map((s) => (
|
|
141
|
+
<TableHead key={s.key} scope="col" className="border-b p-inline">
|
|
142
|
+
{s.label}
|
|
143
|
+
</TableHead>
|
|
144
|
+
))}
|
|
145
|
+
</TableRow>
|
|
146
|
+
</TableHeader>
|
|
147
|
+
<TableBody>
|
|
148
|
+
{rows.map((row, index) => (
|
|
149
|
+
<TableRow key={index}>
|
|
150
|
+
<TableHead scope="row" className="border-b p-inline font-medium">
|
|
151
|
+
{x(row)}
|
|
152
|
+
</TableHead>
|
|
153
|
+
{series.map((s) => (
|
|
154
|
+
<TableCell key={s.key} className="border-b p-inline tabular-nums">
|
|
155
|
+
{shown(s.value(row), format, messages)}
|
|
156
|
+
</TableCell>
|
|
157
|
+
))}
|
|
158
|
+
</TableRow>
|
|
159
|
+
))}
|
|
160
|
+
</TableBody>
|
|
161
|
+
</Table>
|
|
162
|
+
</CollapsibleContent>
|
|
163
|
+
</Collapsible>
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
function Legend<Row>({
|
|
167
|
+
series,
|
|
168
|
+
patternId,
|
|
169
|
+
}: {
|
|
170
|
+
series: ChartSeries<Row>[];
|
|
171
|
+
patternId?: string;
|
|
172
|
+
}) {
|
|
173
|
+
return (
|
|
174
|
+
<ul className="flex flex-wrap gap-x-stack gap-y-1">
|
|
175
|
+
{series.map((s, index) => (
|
|
176
|
+
<li key={s.key} className="flex items-center gap-1.5">
|
|
177
|
+
<svg
|
|
178
|
+
width="24"
|
|
179
|
+
height="12"
|
|
180
|
+
aria-hidden="true"
|
|
181
|
+
className={chartColorClass(index)}
|
|
182
|
+
>
|
|
183
|
+
<line
|
|
184
|
+
x1="0"
|
|
185
|
+
x2="24"
|
|
186
|
+
y1="6"
|
|
187
|
+
y2="6"
|
|
188
|
+
stroke="currentColor"
|
|
189
|
+
strokeWidth="2"
|
|
190
|
+
strokeDasharray={DASHES[index % DASHES.length]}
|
|
191
|
+
/>
|
|
192
|
+
{patternId && (
|
|
193
|
+
<rect
|
|
194
|
+
x="0"
|
|
195
|
+
y="1"
|
|
196
|
+
width="24"
|
|
197
|
+
height="10"
|
|
198
|
+
fill={`url(#${patternId}-pattern-${index})`}
|
|
199
|
+
/>
|
|
200
|
+
)}
|
|
201
|
+
<text x="9" y="10" className="fill-foreground text-2xs">
|
|
202
|
+
{index + 1}
|
|
203
|
+
</text>
|
|
204
|
+
</svg>
|
|
205
|
+
<Text as="span" size="xs">
|
|
206
|
+
{s.label}
|
|
207
|
+
</Text>
|
|
208
|
+
</li>
|
|
209
|
+
))}
|
|
210
|
+
</ul>
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
function ValueAxis({
|
|
214
|
+
domain,
|
|
215
|
+
width,
|
|
216
|
+
height,
|
|
217
|
+
y,
|
|
218
|
+
format,
|
|
219
|
+
}: {
|
|
220
|
+
domain: ReturnType<typeof chartDomain>;
|
|
221
|
+
width: number;
|
|
222
|
+
height: number;
|
|
223
|
+
y: (value: number) => number;
|
|
224
|
+
format: (value: number) => string;
|
|
225
|
+
}) {
|
|
226
|
+
const count = Math.round((domain.high - domain.low) / domain.step);
|
|
227
|
+
return (
|
|
228
|
+
<g aria-hidden="true">
|
|
229
|
+
{Array.from(
|
|
230
|
+
{ length: Math.min(count + 1, 20) },
|
|
231
|
+
(_, index) => domain.low + index * domain.step,
|
|
232
|
+
).map((value) => (
|
|
233
|
+
<g key={value}>
|
|
234
|
+
<line
|
|
235
|
+
x1={LEFT}
|
|
236
|
+
x2={width - RIGHT}
|
|
237
|
+
y1={y(value)}
|
|
238
|
+
y2={y(value)}
|
|
239
|
+
className={value === 0 ? 'stroke-foreground' : 'stroke-border'}
|
|
240
|
+
strokeDasharray={value === 0 ? undefined : '2 4'}
|
|
241
|
+
/>
|
|
242
|
+
<text
|
|
243
|
+
x={LEFT - 8}
|
|
244
|
+
y={y(value)}
|
|
245
|
+
dy="0.32em"
|
|
246
|
+
textAnchor="end"
|
|
247
|
+
className="fill-muted-foreground text-xs tabular-nums"
|
|
248
|
+
>
|
|
249
|
+
{format(value)}
|
|
250
|
+
</text>
|
|
251
|
+
</g>
|
|
252
|
+
))}
|
|
253
|
+
<path
|
|
254
|
+
d={`M${LEFT},${TOP}V${height - BOTTOM}`}
|
|
255
|
+
className="stroke-border"
|
|
256
|
+
fill="none"
|
|
257
|
+
/>
|
|
258
|
+
</g>
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
function useActiveRow(count: number, indexAt: (offset: number) => number) {
|
|
262
|
+
const [active, setActive] = useState<number | null>(null);
|
|
263
|
+
return {
|
|
264
|
+
active: active !== null && active < count ? active : null,
|
|
265
|
+
clear: () => setActive(null),
|
|
266
|
+
handlers: {
|
|
267
|
+
onPointerMove: (event: PointerEvent<SVGSVGElement>) => {
|
|
268
|
+
if (count)
|
|
269
|
+
setActive(
|
|
270
|
+
indexAt(
|
|
271
|
+
event.clientX - event.currentTarget.getBoundingClientRect().left,
|
|
272
|
+
),
|
|
273
|
+
);
|
|
274
|
+
},
|
|
275
|
+
onFocus: () => {
|
|
276
|
+
if (count) setActive(0);
|
|
277
|
+
},
|
|
278
|
+
onKeyDown: (event: KeyboardEvent<SVGSVGElement>) => {
|
|
279
|
+
if (!count) return;
|
|
280
|
+
if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') {
|
|
281
|
+
event.preventDefault();
|
|
282
|
+
const delta = event.key === 'ArrowRight' ? 1 : -1;
|
|
283
|
+
setActive((current) =>
|
|
284
|
+
Math.min(
|
|
285
|
+
count - 1,
|
|
286
|
+
Math.max(0, (current ?? (delta > 0 ? -1 : count)) + delta),
|
|
287
|
+
),
|
|
288
|
+
);
|
|
289
|
+
} else if (event.key === 'Escape') {
|
|
290
|
+
event.preventDefault();
|
|
291
|
+
setActive(null);
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
},
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
function Details<Row>({
|
|
298
|
+
row,
|
|
299
|
+
x,
|
|
300
|
+
series,
|
|
301
|
+
format,
|
|
302
|
+
messages,
|
|
303
|
+
dismiss,
|
|
304
|
+
}: {
|
|
305
|
+
row: Row;
|
|
306
|
+
x: (row: Row) => string;
|
|
307
|
+
series: ChartSeries<Row>[];
|
|
308
|
+
format: (value: number) => string;
|
|
309
|
+
messages: ChartMessages;
|
|
310
|
+
dismiss: () => void;
|
|
311
|
+
}) {
|
|
312
|
+
return (
|
|
313
|
+
<div
|
|
314
|
+
role="status"
|
|
315
|
+
className="absolute right-0 top-0 z-10 flex max-w-full flex-col gap-1 rounded-md border bg-popover p-inline text-sm text-popover-foreground shadow-md"
|
|
316
|
+
>
|
|
317
|
+
<strong>{x(row)}</strong>
|
|
318
|
+
{series.map((s) => (
|
|
319
|
+
<span key={s.key}>
|
|
320
|
+
{s.label}: {shown(s.value(row), format, messages)}
|
|
321
|
+
</span>
|
|
322
|
+
))}
|
|
323
|
+
<Button variant="plain" size={null}
|
|
324
|
+
type="button"
|
|
325
|
+
onClick={dismiss}
|
|
326
|
+
className="min-h-6 text-left text-xs underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
|
327
|
+
>
|
|
328
|
+
{messages.dismiss}
|
|
329
|
+
</Button>
|
|
330
|
+
</div>
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function Chart<Row>({
|
|
335
|
+
label,
|
|
336
|
+
rows,
|
|
337
|
+
x,
|
|
338
|
+
series,
|
|
339
|
+
formatValue,
|
|
340
|
+
locale,
|
|
341
|
+
height = 240,
|
|
342
|
+
className,
|
|
343
|
+
messages: overrides,
|
|
344
|
+
area = false,
|
|
345
|
+
stacked = false,
|
|
346
|
+
}: ChartProps<Row> & { area?: boolean; stacked?: boolean }) {
|
|
347
|
+
const messages = { ...MESSAGES, ...overrides };
|
|
348
|
+
const format =
|
|
349
|
+
formatValue ??
|
|
350
|
+
new Intl.NumberFormat(locale, { maximumFractionDigits: 2 }).format;
|
|
351
|
+
const [ref, width] = useWidth();
|
|
352
|
+
const id = useId();
|
|
353
|
+
const values = rows.map((row) => series.map((s) => s.value(row)));
|
|
354
|
+
const invalidValues = values.some((row) =>
|
|
355
|
+
row.some((value) => value != null && !Number.isFinite(value)),
|
|
356
|
+
);
|
|
357
|
+
const missing = values.some((row) => row.some((value) => value == null));
|
|
358
|
+
const finite = values
|
|
359
|
+
.flat()
|
|
360
|
+
.filter(
|
|
361
|
+
(value): value is number => value != null && Number.isFinite(value),
|
|
362
|
+
);
|
|
363
|
+
const empty = !rows.length || !series.length || !finite.length;
|
|
364
|
+
const domain = chartDomain(
|
|
365
|
+
stacked
|
|
366
|
+
? values.flatMap((row) => [
|
|
367
|
+
row.reduce<number>((sum, n) => sum + (n != null && n > 0 ? n : 0), 0),
|
|
368
|
+
row.reduce<number>((sum, n) => sum + (n != null && n < 0 ? n : 0), 0),
|
|
369
|
+
])
|
|
370
|
+
: finite,
|
|
371
|
+
);
|
|
372
|
+
const invalid =
|
|
373
|
+
invalidValues ||
|
|
374
|
+
!Number.isFinite(domain.low) ||
|
|
375
|
+
!Number.isFinite(domain.high) ||
|
|
376
|
+
!Number.isFinite(domain.high - domain.low);
|
|
377
|
+
const plotHeight = Math.max(1, height - TOP - BOTTOM);
|
|
378
|
+
const plotWidth = Math.max(1, width - LEFT - RIGHT);
|
|
379
|
+
const y = (value: number) =>
|
|
380
|
+
TOP + ((domain.high - value) / (domain.high - domain.low)) * plotHeight;
|
|
381
|
+
const band = plotWidth / Math.max(1, rows.length);
|
|
382
|
+
const at = (index: number) =>
|
|
383
|
+
stacked
|
|
384
|
+
? LEFT + (index + 0.5) * band
|
|
385
|
+
: LEFT + (index * plotWidth) / Math.max(1, rows.length - 1);
|
|
386
|
+
const pointer = useActiveRow(rows.length, (offset) =>
|
|
387
|
+
Math.min(
|
|
388
|
+
rows.length - 1,
|
|
389
|
+
Math.max(
|
|
390
|
+
0,
|
|
391
|
+
stacked
|
|
392
|
+
? Math.floor((offset - LEFT) / band)
|
|
393
|
+
: Math.round(
|
|
394
|
+
((offset - LEFT) / plotWidth) * Math.max(1, rows.length - 1),
|
|
395
|
+
),
|
|
396
|
+
),
|
|
397
|
+
),
|
|
398
|
+
);
|
|
399
|
+
const active = pointer.active === null ? undefined : rows[pointer.active];
|
|
400
|
+
const every = Math.max(
|
|
401
|
+
1,
|
|
402
|
+
Math.ceil(rows.length / Math.max(1, Math.floor(plotWidth / 72))),
|
|
403
|
+
);
|
|
404
|
+
return (
|
|
405
|
+
<figure
|
|
406
|
+
className={cn('m-0 flex min-w-0 flex-col gap-inline', className)}
|
|
407
|
+
aria-label={label}
|
|
408
|
+
>
|
|
409
|
+
<Legend series={series} patternId={stacked ? id : undefined} />
|
|
410
|
+
{invalid ? (
|
|
411
|
+
<Text role="alert" variant="destructive">
|
|
412
|
+
{messages.invalid}
|
|
413
|
+
</Text>
|
|
414
|
+
) : empty ? (
|
|
415
|
+
<Text role="status" variant="muted">
|
|
416
|
+
{messages.empty}
|
|
417
|
+
</Text>
|
|
418
|
+
) : (
|
|
419
|
+
<div
|
|
420
|
+
ref={ref}
|
|
421
|
+
className="relative w-full"
|
|
422
|
+
onPointerLeave={pointer.clear}
|
|
423
|
+
onBlur={(event) => {
|
|
424
|
+
if (
|
|
425
|
+
!event.currentTarget.contains(event.relatedTarget as Node | null)
|
|
426
|
+
)
|
|
427
|
+
pointer.clear();
|
|
428
|
+
}}
|
|
429
|
+
onKeyDown={(event) => {
|
|
430
|
+
if (event.key === 'Escape') pointer.clear();
|
|
431
|
+
}}
|
|
432
|
+
>
|
|
433
|
+
<span id={`${id}-help`} className="sr-only">
|
|
434
|
+
{messages.keyboard}
|
|
435
|
+
</span>
|
|
436
|
+
<svg
|
|
437
|
+
width={width}
|
|
438
|
+
height={height}
|
|
439
|
+
role="img"
|
|
440
|
+
aria-label={label}
|
|
441
|
+
aria-describedby={`${id}-help`}
|
|
442
|
+
tabIndex={0}
|
|
443
|
+
className="block max-w-full overflow-visible rounded-sm outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
|
444
|
+
{...pointer.handlers}
|
|
445
|
+
>
|
|
446
|
+
<defs>
|
|
447
|
+
{series.map((s, index) => (
|
|
448
|
+
<pattern
|
|
449
|
+
key={s.key}
|
|
450
|
+
id={`${id}-pattern-${index}`}
|
|
451
|
+
width={6 + index * 2}
|
|
452
|
+
height={6 + index * 2}
|
|
453
|
+
patternUnits="userSpaceOnUse"
|
|
454
|
+
patternTransform={`rotate(${index * 30})`}
|
|
455
|
+
className={chartColorClass(index)}
|
|
456
|
+
>
|
|
457
|
+
<rect width="100%" height="100%" fill="currentColor" />
|
|
458
|
+
<line
|
|
459
|
+
x1="0"
|
|
460
|
+
x2="0"
|
|
461
|
+
y1="0"
|
|
462
|
+
y2="20"
|
|
463
|
+
className="stroke-background"
|
|
464
|
+
strokeWidth={index === 0 ? 0 : 2}
|
|
465
|
+
/>
|
|
466
|
+
</pattern>
|
|
467
|
+
))}
|
|
468
|
+
</defs>
|
|
469
|
+
<ValueAxis
|
|
470
|
+
domain={domain}
|
|
471
|
+
width={width}
|
|
472
|
+
height={height}
|
|
473
|
+
y={y}
|
|
474
|
+
format={format}
|
|
475
|
+
/>
|
|
476
|
+
<g aria-hidden="true">
|
|
477
|
+
{rows.map((row, index) =>
|
|
478
|
+
index % every === 0 ? (
|
|
479
|
+
<text
|
|
480
|
+
key={index}
|
|
481
|
+
x={at(index)}
|
|
482
|
+
y={height - 8}
|
|
483
|
+
textAnchor="middle"
|
|
484
|
+
className="fill-muted-foreground text-xs"
|
|
485
|
+
>
|
|
486
|
+
{x(row)}
|
|
487
|
+
</text>
|
|
488
|
+
) : null,
|
|
489
|
+
)}
|
|
490
|
+
</g>
|
|
491
|
+
{stacked
|
|
492
|
+
? rows.map((row, rowIndex) => {
|
|
493
|
+
let positive = 0;
|
|
494
|
+
let negative = 0;
|
|
495
|
+
return (
|
|
496
|
+
<g key={rowIndex}>
|
|
497
|
+
{series.map((s, index) => {
|
|
498
|
+
const value = s.value(row);
|
|
499
|
+
if (value == null || !Number.isFinite(value))
|
|
500
|
+
return null;
|
|
501
|
+
const start = value >= 0 ? positive : negative;
|
|
502
|
+
const end = start + value;
|
|
503
|
+
if (value >= 0) positive = end;
|
|
504
|
+
else negative = end;
|
|
505
|
+
return (
|
|
506
|
+
<rect
|
|
507
|
+
key={s.key}
|
|
508
|
+
data-series={s.key}
|
|
509
|
+
data-value={value}
|
|
510
|
+
x={at(rowIndex) - Math.min(48, band * 0.6) / 2}
|
|
511
|
+
y={Math.min(y(start), y(end))}
|
|
512
|
+
width={Math.min(48, band * 0.6)}
|
|
513
|
+
height={Math.abs(y(start) - y(end))}
|
|
514
|
+
fill={`url(#${id}-pattern-${index})`}
|
|
515
|
+
className={chartColorClass(index)}
|
|
516
|
+
/>
|
|
517
|
+
);
|
|
518
|
+
})}
|
|
519
|
+
</g>
|
|
520
|
+
);
|
|
521
|
+
})
|
|
522
|
+
: series.map((s, index) => {
|
|
523
|
+
const segments: { x: number; y: number; value: number }[][] =
|
|
524
|
+
[];
|
|
525
|
+
let segment: { x: number; y: number; value: number }[] = [];
|
|
526
|
+
rows.forEach((row, rowIndex) => {
|
|
527
|
+
const value = s.value(row);
|
|
528
|
+
if (value == null) {
|
|
529
|
+
if (segment.length) segments.push(segment);
|
|
530
|
+
segment = [];
|
|
531
|
+
} else
|
|
532
|
+
segment.push({ x: at(rowIndex), y: y(value), value });
|
|
533
|
+
});
|
|
534
|
+
if (segment.length) segments.push(segment);
|
|
535
|
+
return (
|
|
536
|
+
<g key={s.key} className={chartColorClass(index)}>
|
|
537
|
+
{segments.map((points, segmentIndex) => {
|
|
538
|
+
const line = `M${points.map((p) => `${p.x},${p.y}`).join('L')}`;
|
|
539
|
+
return (
|
|
540
|
+
<g key={segmentIndex}>
|
|
541
|
+
{area && (
|
|
542
|
+
<path
|
|
543
|
+
d={`${line}L${points[points.length - 1]!.x},${y(0)}L${points[0]!.x},${y(0)}Z`}
|
|
544
|
+
fill="currentColor"
|
|
545
|
+
opacity="0.12"
|
|
546
|
+
/>
|
|
547
|
+
)}
|
|
548
|
+
<path
|
|
549
|
+
data-series={s.key}
|
|
550
|
+
d={line}
|
|
551
|
+
fill="none"
|
|
552
|
+
stroke="currentColor"
|
|
553
|
+
strokeWidth="2"
|
|
554
|
+
strokeDasharray={DASHES[index % DASHES.length]}
|
|
555
|
+
/>
|
|
556
|
+
{points.map((point, pointIndex) =>
|
|
557
|
+
index % 2 === 0 ? (
|
|
558
|
+
<circle
|
|
559
|
+
key={pointIndex}
|
|
560
|
+
data-value={point.value}
|
|
561
|
+
cx={point.x}
|
|
562
|
+
cy={point.y}
|
|
563
|
+
r="3"
|
|
564
|
+
fill="currentColor"
|
|
565
|
+
/>
|
|
566
|
+
) : (
|
|
567
|
+
<rect
|
|
568
|
+
key={pointIndex}
|
|
569
|
+
data-value={point.value}
|
|
570
|
+
x={point.x - 3}
|
|
571
|
+
y={point.y - 3}
|
|
572
|
+
width="6"
|
|
573
|
+
height="6"
|
|
574
|
+
fill="currentColor"
|
|
575
|
+
/>
|
|
576
|
+
),
|
|
577
|
+
)}
|
|
578
|
+
</g>
|
|
579
|
+
);
|
|
580
|
+
})}
|
|
581
|
+
</g>
|
|
582
|
+
);
|
|
583
|
+
})}
|
|
584
|
+
</svg>
|
|
585
|
+
{active !== undefined && (
|
|
586
|
+
<Details
|
|
587
|
+
row={active}
|
|
588
|
+
x={x}
|
|
589
|
+
series={series}
|
|
590
|
+
format={format}
|
|
591
|
+
messages={messages}
|
|
592
|
+
dismiss={() => {
|
|
593
|
+
ref.current?.querySelector('svg')?.focus();
|
|
594
|
+
pointer.clear();
|
|
595
|
+
}}
|
|
596
|
+
/>
|
|
597
|
+
)}
|
|
598
|
+
</div>
|
|
599
|
+
)}
|
|
600
|
+
{missing && !invalid && (
|
|
601
|
+
<Text size="sm" variant="muted">
|
|
602
|
+
{messages.missingNotice}
|
|
603
|
+
</Text>
|
|
604
|
+
)}
|
|
605
|
+
<DataTable
|
|
606
|
+
label={label}
|
|
607
|
+
rows={rows}
|
|
608
|
+
x={x}
|
|
609
|
+
series={series}
|
|
610
|
+
format={format}
|
|
611
|
+
messages={messages}
|
|
612
|
+
/>
|
|
613
|
+
</figure>
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
export function LineChart<Row>(props: ChartProps<Row> & { area?: boolean }) {
|
|
617
|
+
return <Chart {...props} />;
|
|
618
|
+
}
|
|
619
|
+
export function StackedBarChart<Row>(props: ChartProps<Row>) {
|
|
620
|
+
return <Chart {...props} stacked />;
|
|
621
|
+
}
|
|
622
|
+
export type SparklineProps = {
|
|
623
|
+
label: string;
|
|
624
|
+
values: (number | null | undefined)[];
|
|
625
|
+
color?: number;
|
|
626
|
+
height?: number;
|
|
627
|
+
className?: string;
|
|
628
|
+
messages?: Pick<Partial<ChartMessages>, 'empty' | 'invalid'>;
|
|
629
|
+
};
|
|
630
|
+
export function Sparkline({
|
|
631
|
+
label,
|
|
632
|
+
values,
|
|
633
|
+
color = 0,
|
|
634
|
+
height = 32,
|
|
635
|
+
className,
|
|
636
|
+
messages,
|
|
637
|
+
}: SparklineProps) {
|
|
638
|
+
const [ref, width] = useWidth();
|
|
639
|
+
const finite = values.filter(
|
|
640
|
+
(n): n is number => n != null && Number.isFinite(n),
|
|
641
|
+
);
|
|
642
|
+
const min = finite.length ? Math.min(...finite) : 0;
|
|
643
|
+
const range = finite.length ? Math.max(...finite) - min || 1 : 1;
|
|
644
|
+
if (
|
|
645
|
+
values.some((n) => n != null && !Number.isFinite(n)) ||
|
|
646
|
+
!Number.isFinite(range)
|
|
647
|
+
)
|
|
648
|
+
return (
|
|
649
|
+
<Text role="alert" variant="destructive">
|
|
650
|
+
{messages?.invalid ?? MESSAGES.invalid}
|
|
651
|
+
</Text>
|
|
652
|
+
);
|
|
653
|
+
if (!finite.length)
|
|
654
|
+
return (
|
|
655
|
+
<Text role="status" variant="muted">
|
|
656
|
+
{messages?.empty ?? MESSAGES.empty}
|
|
657
|
+
</Text>
|
|
658
|
+
);
|
|
659
|
+
const at = (index: number) =>
|
|
660
|
+
3 + ((width - 6) * index) / Math.max(1, values.length - 1);
|
|
661
|
+
const y = (value: number) => 3 + (height - 6) * (1 - (value - min) / range);
|
|
662
|
+
let newSegment = true;
|
|
663
|
+
const path = values
|
|
664
|
+
.map((value, index) => {
|
|
665
|
+
if (value == null) {
|
|
666
|
+
newSegment = true;
|
|
667
|
+
return '';
|
|
668
|
+
}
|
|
669
|
+
const command = newSegment ? 'M' : 'L';
|
|
670
|
+
newSegment = false;
|
|
671
|
+
return `${command}${at(index)},${y(value)}`;
|
|
672
|
+
})
|
|
673
|
+
.join('');
|
|
674
|
+
return (
|
|
675
|
+
<div ref={ref} className={cn('w-full min-w-16', className)}>
|
|
676
|
+
<svg
|
|
677
|
+
width={width}
|
|
678
|
+
height={height}
|
|
679
|
+
role="img"
|
|
680
|
+
aria-label={label}
|
|
681
|
+
className={cn(
|
|
682
|
+
'block max-w-full overflow-visible',
|
|
683
|
+
chartColorClass(color),
|
|
684
|
+
)}
|
|
685
|
+
>
|
|
686
|
+
<path d={path} fill="none" stroke="currentColor" strokeWidth="1.5" />
|
|
687
|
+
{values.map((value, index) =>
|
|
688
|
+
value == null ? null : (
|
|
689
|
+
<circle
|
|
690
|
+
key={index}
|
|
691
|
+
cx={at(index)}
|
|
692
|
+
cy={y(value)}
|
|
693
|
+
r="2"
|
|
694
|
+
fill="currentColor"
|
|
695
|
+
/>
|
|
696
|
+
),
|
|
697
|
+
)}
|
|
698
|
+
</svg>
|
|
699
|
+
</div>
|
|
700
|
+
);
|
|
701
|
+
}
|