@rise-x/apps-sdk 0.6.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/LICENSE +1 -0
- package/README.md +553 -0
- package/build/_internal.d.ts +2 -0
- package/build/_internal.js +23 -0
- package/build/accessors.d.ts +8 -0
- package/build/accessors.js +40 -0
- package/build/cli/index.d.ts +13 -0
- package/build/cli/index.js +54 -0
- package/build/cli/init.d.ts +6 -0
- package/build/cli/init.js +179 -0
- package/build/cli/templating.d.ts +2 -0
- package/build/cli/templating.js +84 -0
- package/build/connectors/agents.d.ts +85 -0
- package/build/connectors/agents.js +404 -0
- package/build/connectors/assets.d.ts +76 -0
- package/build/connectors/assets.js +204 -0
- package/build/connectors/deps.d.ts +4 -0
- package/build/connectors/deps.js +2 -0
- package/build/connectors/errors.d.ts +11 -0
- package/build/connectors/errors.js +67 -0
- package/build/connectors/flows.d.ts +37 -0
- package/build/connectors/flows.js +191 -0
- package/build/connectors/index.d.ts +8 -0
- package/build/connectors/index.js +50 -0
- package/build/connectors/sse.d.ts +12 -0
- package/build/connectors/sse.js +157 -0
- package/build/connectors/types.d.ts +285 -0
- package/build/connectors/types.js +419 -0
- package/build/connectors/work.d.ts +84 -0
- package/build/connectors/work.js +224 -0
- package/build/hooks.d.ts +4 -0
- package/build/hooks.js +28 -0
- package/build/index.d.ts +6 -0
- package/build/index.js +34 -0
- package/build/lifecycle.d.ts +23 -0
- package/build/lifecycle.js +6 -0
- package/build/mockShell.d.ts +10 -0
- package/build/mockShell.js +55 -0
- package/build/query/agents.d.ts +80 -0
- package/build/query/agents.js +143 -0
- package/build/query/assets.d.ts +73 -0
- package/build/query/assets.js +152 -0
- package/build/query/client.d.ts +5 -0
- package/build/query/client.js +68 -0
- package/build/query/context.d.ts +2 -0
- package/build/query/context.js +34 -0
- package/build/query/flows.d.ts +54 -0
- package/build/query/flows.js +81 -0
- package/build/query/index.d.ts +10 -0
- package/build/query/index.js +85 -0
- package/build/query/keys.d.ts +51 -0
- package/build/query/keys.js +79 -0
- package/build/query/layer.d.ts +175 -0
- package/build/query/layer.js +35 -0
- package/build/query/shared.d.ts +43 -0
- package/build/query/shared.js +116 -0
- package/build/query/work.d.ts +75 -0
- package/build/query/work.js +172 -0
- package/build/shell/index.d.ts +1 -0
- package/build/shell/index.js +6 -0
- package/build/shell/installShellBridge.d.ts +11 -0
- package/build/shell/installShellBridge.js +104 -0
- package/build/shellBridge.d.ts +36 -0
- package/build/shellBridge.js +10 -0
- package/build/topBar.d.ts +25 -0
- package/build/topBar.js +17 -0
- package/build/ui/components/ai.d.ts +49 -0
- package/build/ui/components/alert-dialog.d.ts +27 -0
- package/build/ui/components/alert.d.ts +14 -0
- package/build/ui/components/avatar.d.ts +17 -0
- package/build/ui/components/badge.d.ts +19 -0
- package/build/ui/components/button.d.ts +13 -0
- package/build/ui/components/calendar.d.ts +21 -0
- package/build/ui/components/card.d.ts +16 -0
- package/build/ui/components/chart.d.ts +124 -0
- package/build/ui/components/checkbox.d.ts +4 -0
- package/build/ui/components/data-table.d.ts +70 -0
- package/build/ui/components/dialog.d.ts +17 -0
- package/build/ui/components/dropdown-menu.d.ts +29 -0
- package/build/ui/components/empty-state.d.ts +7 -0
- package/build/ui/components/file-upload.d.ts +17 -0
- package/build/ui/components/input.d.ts +8 -0
- package/build/ui/components/label.d.ts +4 -0
- package/build/ui/components/list.d.ts +24 -0
- package/build/ui/components/map.d.ts +26 -0
- package/build/ui/components/message.d.ts +24 -0
- package/build/ui/components/nav.d.ts +12 -0
- package/build/ui/components/page-header.d.ts +27 -0
- package/build/ui/components/popover.d.ts +10 -0
- package/build/ui/components/progress.d.ts +4 -0
- package/build/ui/components/radio.d.ts +6 -0
- package/build/ui/components/resizable.d.ts +7 -0
- package/build/ui/components/scroll-area.d.ts +5 -0
- package/build/ui/components/segmented.d.ts +6 -0
- package/build/ui/components/select.d.ts +15 -0
- package/build/ui/components/separator.d.ts +4 -0
- package/build/ui/components/sheet.d.ts +14 -0
- package/build/ui/components/skeleton.d.ts +3 -0
- package/build/ui/components/slider.d.ts +15 -0
- package/build/ui/components/snackbar.d.ts +3 -0
- package/build/ui/components/sonner.d.ts +4 -0
- package/build/ui/components/spinner.d.ts +5 -0
- package/build/ui/components/statistic.d.ts +19 -0
- package/build/ui/components/stepper.d.ts +12 -0
- package/build/ui/components/switch.d.ts +6 -0
- package/build/ui/components/table.d.ts +24 -0
- package/build/ui/components/textarea.d.ts +3 -0
- package/build/ui/components/timeline.d.ts +15 -0
- package/build/ui/components/toggle-group.d.ts +10 -0
- package/build/ui/components/toggle.d.ts +9 -0
- package/build/ui/components/tooltip.d.ts +7 -0
- package/build/ui/index.d.ts +46 -0
- package/build/ui/index.js +8 -0
- package/build/ui/lib/utils.d.ts +2 -0
- package/build/ui/reference/README.md +11 -0
- package/build/ui/reference/components/ai.tsx +291 -0
- package/build/ui/reference/components/alert-dialog.tsx +208 -0
- package/build/ui/reference/components/alert.tsx +112 -0
- package/build/ui/reference/components/avatar.tsx +158 -0
- package/build/ui/reference/components/badge.tsx +160 -0
- package/build/ui/reference/components/button.tsx +100 -0
- package/build/ui/reference/components/calendar.tsx +119 -0
- package/build/ui/reference/components/card.tsx +144 -0
- package/build/ui/reference/components/chart.tsx +1394 -0
- package/build/ui/reference/components/checkbox.tsx +44 -0
- package/build/ui/reference/components/data-table.tsx +480 -0
- package/build/ui/reference/components/dialog.tsx +172 -0
- package/build/ui/reference/components/dropdown-menu.tsx +245 -0
- package/build/ui/reference/components/empty-state.tsx +63 -0
- package/build/ui/reference/components/file-upload.tsx +116 -0
- package/build/ui/reference/components/input.tsx +114 -0
- package/build/ui/reference/components/label.tsx +19 -0
- package/build/ui/reference/components/list.tsx +191 -0
- package/build/ui/reference/components/map.css +251 -0
- package/build/ui/reference/components/map.tsx +294 -0
- package/build/ui/reference/components/message.tsx +171 -0
- package/build/ui/reference/components/nav.tsx +113 -0
- package/build/ui/reference/components/page-header.tsx +213 -0
- package/build/ui/reference/components/popover.tsx +76 -0
- package/build/ui/reference/components/progress.tsx +31 -0
- package/build/ui/reference/components/radio.tsx +65 -0
- package/build/ui/reference/components/resizable.tsx +40 -0
- package/build/ui/reference/components/scroll-area.tsx +55 -0
- package/build/ui/reference/components/segmented.tsx +45 -0
- package/build/ui/reference/components/select.tsx +185 -0
- package/build/ui/reference/components/separator.tsx +26 -0
- package/build/ui/reference/components/sheet.tsx +173 -0
- package/build/ui/reference/components/skeleton.tsx +15 -0
- package/build/ui/reference/components/slider.tsx +161 -0
- package/build/ui/reference/components/snackbar.tsx +27 -0
- package/build/ui/reference/components/sonner.tsx +43 -0
- package/build/ui/reference/components/spinner.tsx +27 -0
- package/build/ui/reference/components/statistic.tsx +122 -0
- package/build/ui/reference/components/stepper.tsx +138 -0
- package/build/ui/reference/components/switch.tsx +47 -0
- package/build/ui/reference/components/table.tsx +233 -0
- package/build/ui/reference/components/textarea.tsx +28 -0
- package/build/ui/reference/components/timeline.tsx +94 -0
- package/build/ui/reference/components/toggle-group.tsx +86 -0
- package/build/ui/reference/components/toggle.tsx +46 -0
- package/build/ui/reference/components/tooltip.tsx +53 -0
- package/build/ui/reference/index.ts +49 -0
- package/build/ui/reference/lib/utils.ts +34 -0
- package/build/ui/standalone-stub.js +5 -0
- package/build/ui/standalone.js +135 -0
- package/build/ui/styles.css +3 -0
- package/connectors/package.json +8 -0
- package/package.json +114 -0
- package/query/package.json +8 -0
- package/shell/package.json +8 -0
- package/template/AGENTS.md +70 -0
- package/template/CLAUDE.md +4 -0
- package/template/README.md +102 -0
- package/template/_gitignore +6 -0
- package/template/commitlint.config.js +12 -0
- package/template/package.json +31 -0
- package/template/public/index.html +13 -0
- package/template/src/App.tsx +171 -0
- package/template/src/bootstrap.tsx +13 -0
- package/template/src/index.tsx +1 -0
- package/template/src/lifecycle.ts +20 -0
- package/template/tsconfig.json +31 -0
- package/template/webpack.config.js +112 -0
- package/template/webpack.local.config.js +68 -0
- package/ui/package.json +8 -0
|
@@ -0,0 +1,1394 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
import { cn } from '@rise-x/ui/lib/utils';
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* Charts, hand-rolled in SVG. The visual contract comes from shadcn's chart
|
|
7
|
+
* registry rather than from eyeballing its screenshots: axis ticks in
|
|
8
|
+
* `muted-foreground` (AXIS_LABEL), a half-strength dashed grid (GRID_OPACITY),
|
|
9
|
+
* horizontal gridlines only, an 8px tick margin (TICK_GAP), and no axis or tick
|
|
10
|
+
* lines. 1.75px strokes are ours.
|
|
11
|
+
*
|
|
12
|
+
* THREE DELIBERATE DEVIATIONS, all of them cases where the library's choice
|
|
13
|
+
* costs information this product needs. Tick size is NOT one of them — that is
|
|
14
|
+
* shadcn's 12px:
|
|
15
|
+
*
|
|
16
|
+
* · THE Y AXIS IS ON. shadcn omits `<YAxis>` and keeps the grid, which reads
|
|
17
|
+
* beautifully and cannot be quoted from. `yAxis={false}` gives that look.
|
|
18
|
+
* · AREAS ARE ALWAYS GRADIENT (0.8 → 0.1). An area chart is about the shape
|
|
19
|
+
* under the line, and shadcn's flat `fillOpacity: 0.4` flattens exactly that.
|
|
20
|
+
* · THE CURVE IS TRULY MONOTONE. shadcn passes Recharts `type="natural"`, a
|
|
21
|
+
* natural cubic that overshoots local extrema and can carry a strictly-positive
|
|
22
|
+
* series below zero. This one is a bug fix, not a preference.
|
|
23
|
+
*
|
|
24
|
+
* THE PALETTE IS ITS OWN FAMILY — indigo, teal, tango, pink, amber, then slate
|
|
25
|
+
* for the remainder — sequenced so adjacent series sit far apart in hue. Brand
|
|
26
|
+
* green, berry and pure red are all EXCLUDED, so a chart can never imply
|
|
27
|
+
* success, AI or failure by accident. If a chart of revenue renders its worst
|
|
28
|
+
* quarter in the company colour, the colour has stopped meaning anything.
|
|
29
|
+
*
|
|
30
|
+
* THE ONE EXCEPTION: when the series ARE statuses (matched / in review /
|
|
31
|
+
* failed), pass `tone` to use the semantic colours — a failed slice must be red.
|
|
32
|
+
*
|
|
33
|
+
* EVERY CHART SHIPS IN A ChartCard. A chart without a title is a decoration: the
|
|
34
|
+
* reader has to infer what is being measured and over what period, and two
|
|
35
|
+
* charts side by side become indistinguishable.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
const CHART_SERIES = [
|
|
39
|
+
'var(--chart-1)',
|
|
40
|
+
'var(--chart-2)',
|
|
41
|
+
'var(--chart-3)',
|
|
42
|
+
'var(--chart-4)',
|
|
43
|
+
'var(--chart-5)',
|
|
44
|
+
'var(--chart-6)',
|
|
45
|
+
] as const;
|
|
46
|
+
|
|
47
|
+
const SEMANTIC_SERIES = {
|
|
48
|
+
success: 'var(--success)',
|
|
49
|
+
warning: 'var(--warning)',
|
|
50
|
+
error: 'var(--error)',
|
|
51
|
+
info: 'var(--info)',
|
|
52
|
+
neutral: 'var(--neutral)',
|
|
53
|
+
} as const;
|
|
54
|
+
|
|
55
|
+
type SemanticTone = keyof typeof SEMANTIC_SERIES;
|
|
56
|
+
|
|
57
|
+
function seriesColor(index: number, tone?: SemanticTone) {
|
|
58
|
+
return tone ? SEMANTIC_SERIES[tone] : CHART_SERIES[index % CHART_SERIES.length];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/* ---- card ---------------------------------------------------------------- */
|
|
62
|
+
|
|
63
|
+
function ChartCard({ className, ...props }: React.ComponentProps<'div'>) {
|
|
64
|
+
return (
|
|
65
|
+
<div
|
|
66
|
+
data-slot="chart-card"
|
|
67
|
+
className={cn('flex flex-col rounded-xl bg-card p-3.5 shadow-card', className)}
|
|
68
|
+
{...props}
|
|
69
|
+
/>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function ChartCardHeader({ className, ...props }: React.ComponentProps<'div'>) {
|
|
74
|
+
return (
|
|
75
|
+
<div
|
|
76
|
+
data-slot="chart-card-header"
|
|
77
|
+
className={cn(
|
|
78
|
+
'mb-3 flex items-start gap-3 [&>[data-slot=chart-card-actions]]:ml-auto',
|
|
79
|
+
className,
|
|
80
|
+
)}
|
|
81
|
+
{...props}
|
|
82
|
+
/>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function ChartCardTitle({ className, ...props }: React.ComponentProps<'div'>) {
|
|
87
|
+
return (
|
|
88
|
+
<div
|
|
89
|
+
data-slot="chart-card-title"
|
|
90
|
+
className={cn('text-ui leading-tight font-medium', className)}
|
|
91
|
+
{...props}
|
|
92
|
+
/>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/*
|
|
97
|
+
* A SUBTITLE, so it is explicitly regular weight in the secondary tone. It was
|
|
98
|
+
* already computing to 400 — nothing was setting a weight — but it read as heavy
|
|
99
|
+
* because `--muted-foreground` is the BODY tone (#5c5c5c, 6.69:1), the same
|
|
100
|
+
* darkness as the copy it sits above, at only 1px less than the title. Weight
|
|
101
|
+
* pinned here so a caller's `font-medium` cannot leak in by inheritance.
|
|
102
|
+
*/
|
|
103
|
+
function ChartCardDescription({ className, ...props }: React.ComponentProps<'div'>) {
|
|
104
|
+
return (
|
|
105
|
+
<div
|
|
106
|
+
data-slot="chart-card-description"
|
|
107
|
+
className={cn('mt-0.5 text-xs font-normal text-subtle', className)}
|
|
108
|
+
{...props}
|
|
109
|
+
/>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function ChartCardActions({ className, ...props }: React.ComponentProps<'div'>) {
|
|
114
|
+
return (
|
|
115
|
+
<div
|
|
116
|
+
data-slot="chart-card-actions"
|
|
117
|
+
className={cn('flex shrink-0 items-center gap-2', className)}
|
|
118
|
+
{...props}
|
|
119
|
+
/>
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/* Footer carries the takeaway, not a repeat of the axis labels. */
|
|
124
|
+
function ChartCardFooter({ className, ...props }: React.ComponentProps<'div'>) {
|
|
125
|
+
return (
|
|
126
|
+
<div
|
|
127
|
+
data-slot="chart-card-footer"
|
|
128
|
+
className={cn('mt-3 flex flex-col gap-1 text-xs', className)}
|
|
129
|
+
{...props}
|
|
130
|
+
/>
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/* ---- geometry helpers ---------------------------------------------------- */
|
|
135
|
+
|
|
136
|
+
/*
|
|
137
|
+
* Monotone cubic (Fritsch–Carlson) through the points.
|
|
138
|
+
*
|
|
139
|
+
* Two things make it monotone, and this used to have neither — it was a plain
|
|
140
|
+
* Catmull-Rom with a fixed 0.28 tension, which overshoots exactly the way the
|
|
141
|
+
* comment here claimed it didn't:
|
|
142
|
+
* · the tangent is ZEROED at a local extremum, so the curve cannot sail past
|
|
143
|
+
* a peak or trough — that overshoot is what dips a strictly-positive series
|
|
144
|
+
* below zero and reads as a data error;
|
|
145
|
+
* · elsewhere the tangent is clamped to 3× the smaller neighbouring slope,
|
|
146
|
+
* which keeps each segment inside the box its two endpoints define.
|
|
147
|
+
*/
|
|
148
|
+
function monotonePath(points: Array<[number, number]>) {
|
|
149
|
+
if (points.length < 2) return '';
|
|
150
|
+
const n = points.length;
|
|
151
|
+
const slope = (i: number) =>
|
|
152
|
+
(points[i + 1][1] - points[i][1]) / (points[i + 1][0] - points[i][0] || 1);
|
|
153
|
+
|
|
154
|
+
const m: number[] = [];
|
|
155
|
+
for (let i = 0; i < n; i += 1) {
|
|
156
|
+
if (i === 0) {
|
|
157
|
+
m.push(slope(0));
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (i === n - 1) {
|
|
161
|
+
m.push(slope(n - 2));
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const s0 = slope(i - 1);
|
|
165
|
+
const s1 = slope(i);
|
|
166
|
+
if (s0 * s1 <= 0) {
|
|
167
|
+
m.push(0);
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
const avg = (s0 + s1) / 2;
|
|
171
|
+
const limit = 3 * Math.min(Math.abs(s0), Math.abs(s1));
|
|
172
|
+
m.push(Math.sign(avg) * Math.min(Math.abs(avg), limit));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const d: string[] = [`M ${points[0][0]} ${points[0][1]}`];
|
|
176
|
+
for (let i = 0; i < n - 1; i += 1) {
|
|
177
|
+
const [x1, y1] = points[i];
|
|
178
|
+
const [x2, y2] = points[i + 1];
|
|
179
|
+
const dx = (x2 - x1) / 3;
|
|
180
|
+
d.push(`C ${x1 + dx} ${y1 + m[i] * dx}, ${x2 - dx} ${y2 - m[i + 1] * dx}, ${x2} ${y2}`);
|
|
181
|
+
}
|
|
182
|
+
return d.join(' ');
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/* ---- accessible names ----------------------------------------------------
|
|
186
|
+
* `role="img"` REQUIRES an accessible name; without one a screen reader
|
|
187
|
+
* announces a nameless graphic and the chart contributes nothing at all. The
|
|
188
|
+
* ChartCard title is a sibling div, so it cannot do this job. Each name is
|
|
189
|
+
* generated from what the chart already knows and rendered as a `<title>`, which
|
|
190
|
+
* must be the svg's FIRST child to name it.
|
|
191
|
+
* ------------------------------------------------------------------------- */
|
|
192
|
+
function axisName(series: { label: string }[], labels: string[]) {
|
|
193
|
+
const span = labels.length ? `, ${labels[0]} to ${labels[labels.length - 1]}` : '';
|
|
194
|
+
return `${series.map((s) => s.label).join(', ')}${span}`;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function partsName(parts: { label: string; value: number }[], format: (v: number) => string) {
|
|
198
|
+
return parts.map((p) => `${p.label} ${format(p.value)}`).join(', ');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/*
|
|
202
|
+
* AXIS LABELS — one class, used by every axis in the file.
|
|
203
|
+
*
|
|
204
|
+
* `text-xs` (12px), which is shadcn's value — its ChartContainer sets `text-xs`
|
|
205
|
+
* on the chart and `fill-muted-foreground` on the ticks. These started at 10px,
|
|
206
|
+
* which was off-scale entirely (11px `micro` is the floor of the scale).
|
|
207
|
+
*
|
|
208
|
+
* The colour does most of the legibility work: `--muted-foreground` instead of
|
|
209
|
+
* `--text-subtle` takes the numbers from 6.69:1 to 8.65:1 in dark and
|
|
210
|
+
* 5.33:1 to 6.69:1 in light. Tabular figures because a column of right-aligned
|
|
211
|
+
* numbers with proportional digits reads as ragged — shadcn gets this free from
|
|
212
|
+
* Recharts' own tick layout.
|
|
213
|
+
*/
|
|
214
|
+
const AXIS_LABEL = 'fill-muted-foreground text-xs tabular-nums';
|
|
215
|
+
|
|
216
|
+
/* shadcn draws the grid at `stroke-border/50` — half-strength, so the gridline
|
|
217
|
+
* never competes with the series drawn over it. */
|
|
218
|
+
const GRID_STROKE = 'var(--border)';
|
|
219
|
+
const GRID_OPACITY = 0.5;
|
|
220
|
+
|
|
221
|
+
function linearPath(points: Array<[number, number]>) {
|
|
222
|
+
return points.map(([x, y], i) => `${i ? 'L' : 'M'} ${x} ${y}`).join(' ');
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/* Step: hold the value until the next reading. Correct for anything sampled
|
|
226
|
+
* rather than continuous — a smooth curve between two daily readings invents
|
|
227
|
+
* values that were never measured. */
|
|
228
|
+
function stepPath(points: Array<[number, number]>) {
|
|
229
|
+
const d: string[] = [];
|
|
230
|
+
points.forEach(([x, y], i) => {
|
|
231
|
+
if (!i) {
|
|
232
|
+
d.push(`M ${x} ${y}`);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const [, py] = points[i - 1];
|
|
236
|
+
d.push(`L ${x} ${py}`, `L ${x} ${y}`);
|
|
237
|
+
});
|
|
238
|
+
return d.join(' ');
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/* ---- hover -------------------------------------------------------------------
|
|
242
|
+
* EVERY CHART IS HOVERABLE BY DEFAULT. A chart that cannot be interrogated is a
|
|
243
|
+
* picture: the reader can see the shape but not read a value, so the first thing
|
|
244
|
+
* anyone asks of it ("what was March?") needs a different screen to answer.
|
|
245
|
+
*
|
|
246
|
+
* Axis charts use one full-height hit band per x index rather than a hit target
|
|
247
|
+
* per point — points are 3px and a line chart with six of them would be almost
|
|
248
|
+
* unhittable, and the band also lets a single hover report every series at that
|
|
249
|
+
* x, which is the comparison the reader actually wants.
|
|
250
|
+
* -------------------------------------------------------------------------- */
|
|
251
|
+
|
|
252
|
+
interface HoverPoint {
|
|
253
|
+
index: number;
|
|
254
|
+
label: string;
|
|
255
|
+
/** Left offset within the chart container, as a percentage. */
|
|
256
|
+
left: number;
|
|
257
|
+
rows: { label: string; value: number; color: string }[];
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function HoverCard({
|
|
261
|
+
point,
|
|
262
|
+
formatValue,
|
|
263
|
+
}: {
|
|
264
|
+
point: HoverPoint;
|
|
265
|
+
formatValue: (v: number) => string;
|
|
266
|
+
}) {
|
|
267
|
+
/* Flip the anchor near the edges so the card never hangs outside the card it
|
|
268
|
+
* belongs to. */
|
|
269
|
+
const align = point.left < 22 ? 'left-0' : point.left > 78 ? 'right-0' : '-translate-x-1/2';
|
|
270
|
+
return (
|
|
271
|
+
<div
|
|
272
|
+
role="tooltip"
|
|
273
|
+
className={cn(
|
|
274
|
+
'pointer-events-none absolute top-1 z-10 min-w-[132px] rounded-lg bg-card p-2.5 text-xs shadow-pop',
|
|
275
|
+
align,
|
|
276
|
+
)}
|
|
277
|
+
style={align === '-translate-x-1/2' ? { left: `${point.left}%` } : undefined}
|
|
278
|
+
>
|
|
279
|
+
<div className="mb-1.5 text-micro text-subtle">{point.label}</div>
|
|
280
|
+
{point.rows.map((r) => (
|
|
281
|
+
<div key={r.label} className="flex items-center gap-[7px] py-0.5">
|
|
282
|
+
<i className="size-2 shrink-0 rounded-[2px]" style={{ background: r.color }} />
|
|
283
|
+
<span className="text-muted-foreground">{r.label}</span>
|
|
284
|
+
<span className="ml-auto font-medium tabular-nums">{formatValue(r.value)}</span>
|
|
285
|
+
</div>
|
|
286
|
+
))}
|
|
287
|
+
</div>
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/*
|
|
292
|
+
* Real SVG arc paths, not a dash-array trick.
|
|
293
|
+
*
|
|
294
|
+
* The previous pie drew each slice as a stroked circle at half the radius with a
|
|
295
|
+
* stroke as thick as the radius, then halved every dash offset to compensate.
|
|
296
|
+
* That happens to look right for a donut and is visibly wrong for a pie — the
|
|
297
|
+
* boundaries land at the wrong angles and slices develop a step where the stroke
|
|
298
|
+
* caps meet. Arcs are the same amount of code and are simply correct.
|
|
299
|
+
*
|
|
300
|
+
* `rInner = 0` gives a pie; any inner radius gives a donut.
|
|
301
|
+
*/
|
|
302
|
+
function arcPath(cx: number, cy: number, rOuter: number, rInner: number, a0: number, a1: number) {
|
|
303
|
+
const p = (r: number, a: number) => [cx + r * Math.cos(a), cy + r * Math.sin(a)] as const;
|
|
304
|
+
const large = a1 - a0 > Math.PI ? 1 : 0;
|
|
305
|
+
const [x0, y0] = p(rOuter, a0);
|
|
306
|
+
const [x1, y1] = p(rOuter, a1);
|
|
307
|
+
if (rInner <= 0) {
|
|
308
|
+
return `M ${cx} ${cy} L ${x0} ${y0} A ${rOuter} ${rOuter} 0 ${large} 1 ${x1} ${y1} Z`;
|
|
309
|
+
}
|
|
310
|
+
const [x2, y2] = p(rInner, a1);
|
|
311
|
+
const [x3, y3] = p(rInner, a0);
|
|
312
|
+
return `M ${x0} ${y0} A ${rOuter} ${rOuter} 0 ${large} 1 ${x1} ${y1} L ${x2} ${y2} A ${rInner} ${rInner} 0 ${large} 0 ${x3} ${y3} Z`;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export interface ChartSeries {
|
|
316
|
+
label: string;
|
|
317
|
+
values: number[];
|
|
318
|
+
tone?: SemanticTone;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
interface AxisChartProps extends Omit<React.ComponentProps<'div'>, 'children'> {
|
|
322
|
+
series: ChartSeries[];
|
|
323
|
+
labels: string[];
|
|
324
|
+
height?: number;
|
|
325
|
+
gridLines?: number;
|
|
326
|
+
formatValue?: (value: number) => string;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const W = 720;
|
|
330
|
+
/* `l` is only the FLOOR for the left gutter — the real width is derived per
|
|
331
|
+
* chart by gutter() below. `r` is shadcn's `margin={{ right: 12 }}`, and `b`
|
|
332
|
+
* holds a 12px label plus the library's `tickMargin={8}`. */
|
|
333
|
+
const PAD = { l: 40, r: 12, t: 10, b: 26 };
|
|
334
|
+
/* shadcn's `tickMargin={8}` — the gap between the plot edge and the tick text. */
|
|
335
|
+
const TICK_GAP = 8;
|
|
336
|
+
|
|
337
|
+
/* The tick values an axis will label, top to bottom. Shared so the gutter is
|
|
338
|
+
* measured from exactly the strings that get rendered. */
|
|
339
|
+
function tickValues(min: number, max: number, count: number) {
|
|
340
|
+
return Array.from({ length: count + 1 }, (_, i) => {
|
|
341
|
+
const v = max - (i / count) * (max - min);
|
|
342
|
+
/* Round to whole numbers only when whole numbers can tell the ticks apart.
|
|
343
|
+
* Rounding unconditionally labelled every line of a 0–1 scale "0", and did
|
|
344
|
+
* it BEFORE the caller's formatValue ran, so no formatter could recover the
|
|
345
|
+
* value. */
|
|
346
|
+
return max - min >= count ? Math.round(v) : Math.round(v * 1000) / 1000;
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/*
|
|
351
|
+
* The left gutter is DERIVED from the widest tick label, never fixed.
|
|
352
|
+
*
|
|
353
|
+
* A hardcoded 38px fitted "1284" and clipped "€21,400" clean off the left edge
|
|
354
|
+
* of the viewBox — and because the labels were 10px, the clipping was just
|
|
355
|
+
* subtle enough to survive review. Tabular figures make the advance predictable:
|
|
356
|
+
* CHAR_W per character, plus the 7px gap to the gridline and 4px of margin.
|
|
357
|
+
*/
|
|
358
|
+
/* Advance of one character of 12px tabular text. Both measured, not guessed, and
|
|
359
|
+
* rounded UP so the estimate over-reserves rather than clipping. */
|
|
360
|
+
const CHAR_W = 7.1;
|
|
361
|
+
/* `tabular-nums` normalises DIGITS, and nothing else. `%` renders ~1.4× a digit
|
|
362
|
+
* and currency glyphs are wider still — counting characters put "100%" 2px
|
|
363
|
+
* outside the viewBox even though every digit-only label fitted. */
|
|
364
|
+
const WIDE_W = 11.5;
|
|
365
|
+
|
|
366
|
+
function labelWidth(label: string) {
|
|
367
|
+
let w = 0;
|
|
368
|
+
for (const ch of label) w += /[%€$£¥₽]/.test(ch) ? WIDE_W : CHAR_W;
|
|
369
|
+
return w;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function gutter(ticks: string[]) {
|
|
373
|
+
const widest = ticks.reduce((n, t) => Math.max(n, labelWidth(t)), 0);
|
|
374
|
+
return Math.max(PAD.l, Math.ceil(widest) + TICK_GAP + 4);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function Gridlines({
|
|
378
|
+
count,
|
|
379
|
+
min,
|
|
380
|
+
max,
|
|
381
|
+
height,
|
|
382
|
+
format,
|
|
383
|
+
padL,
|
|
384
|
+
yAxis,
|
|
385
|
+
}: {
|
|
386
|
+
count: number;
|
|
387
|
+
min: number;
|
|
388
|
+
max: number;
|
|
389
|
+
height: number;
|
|
390
|
+
format: (v: number) => string;
|
|
391
|
+
padL: number;
|
|
392
|
+
/** Tick LABELS. The gridlines are drawn either way — shadcn's default chart
|
|
393
|
+
* keeps `<CartesianGrid>` and omits `<YAxis>`. */
|
|
394
|
+
yAxis: boolean;
|
|
395
|
+
}) {
|
|
396
|
+
const plotH = height - PAD.t - PAD.b;
|
|
397
|
+
const ticks = tickValues(min, max, count);
|
|
398
|
+
return (
|
|
399
|
+
<>
|
|
400
|
+
{ticks.map((v, i) => {
|
|
401
|
+
const gy = PAD.t + (i / count) * plotH;
|
|
402
|
+
const zero = Math.abs(v) < 1e-9 && min < 0;
|
|
403
|
+
return (
|
|
404
|
+
<g key={i}>
|
|
405
|
+
<line
|
|
406
|
+
x1={padL}
|
|
407
|
+
x2={W - PAD.r}
|
|
408
|
+
y1={gy}
|
|
409
|
+
y2={gy}
|
|
410
|
+
stroke={zero ? 'var(--border-strong)' : GRID_STROKE}
|
|
411
|
+
strokeOpacity={zero ? 1 : GRID_OPACITY}
|
|
412
|
+
strokeWidth="1"
|
|
413
|
+
strokeDasharray={zero ? undefined : '3 3'}
|
|
414
|
+
/>
|
|
415
|
+
{yAxis ? (
|
|
416
|
+
<text x={padL - TICK_GAP} y={gy + 4} textAnchor="end" className={AXIS_LABEL}>
|
|
417
|
+
{format(v)}
|
|
418
|
+
</text>
|
|
419
|
+
) : null}
|
|
420
|
+
</g>
|
|
421
|
+
);
|
|
422
|
+
})}
|
|
423
|
+
</>
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function XLabels({ labels, height, padL }: { labels: string[]; height: number; padL: number }) {
|
|
428
|
+
const step = (W - padL - PAD.r) / Math.max(1, labels.length - 1);
|
|
429
|
+
return (
|
|
430
|
+
<>
|
|
431
|
+
{labels.map((label, i) => (
|
|
432
|
+
<text
|
|
433
|
+
key={label + i}
|
|
434
|
+
x={padL + i * step}
|
|
435
|
+
/* plot floor + tickMargin + the cap height of 12px text */
|
|
436
|
+
y={height - PAD.b + TICK_GAP + 9}
|
|
437
|
+
textAnchor={i === 0 ? 'start' : i === labels.length - 1 ? 'end' : 'middle'}
|
|
438
|
+
className={AXIS_LABEL}
|
|
439
|
+
>
|
|
440
|
+
{label}
|
|
441
|
+
</text>
|
|
442
|
+
))}
|
|
443
|
+
</>
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/* ---- line / area -------------------------------------------------------- */
|
|
448
|
+
|
|
449
|
+
function LineChart({
|
|
450
|
+
series,
|
|
451
|
+
labels,
|
|
452
|
+
height = 176,
|
|
453
|
+
gridLines = 4,
|
|
454
|
+
formatValue = (v) => String(v),
|
|
455
|
+
area = false,
|
|
456
|
+
curve = 'monotone',
|
|
457
|
+
dots = false,
|
|
458
|
+
stacked = false,
|
|
459
|
+
stackOffset = 'none',
|
|
460
|
+
fill = 'gradient',
|
|
461
|
+
yAxis = true,
|
|
462
|
+
hover = true,
|
|
463
|
+
className,
|
|
464
|
+
...props
|
|
465
|
+
}: AxisChartProps & {
|
|
466
|
+
area?: boolean;
|
|
467
|
+
curve?: 'monotone' | 'linear' | 'step';
|
|
468
|
+
dots?: boolean;
|
|
469
|
+
/** Stack the series so the top line is the total. */
|
|
470
|
+
stacked?: boolean;
|
|
471
|
+
/** `expand` normalises each x to 100% — shadcn's `stackOffset="expand"`. Only
|
|
472
|
+
* meaningful with `stacked`, since it is a share of the stack. */
|
|
473
|
+
stackOffset?: 'none' | 'expand';
|
|
474
|
+
/** `gradient`, ramping 0.8 → 0.1, is the default: an area chart is about the
|
|
475
|
+
* shape under the line, and a flat block of colour flattens exactly that.
|
|
476
|
+
* `flat` is shadcn's `fillOpacity={0.4}`, kept for a single-colour export
|
|
477
|
+
* where a gradient would band. */
|
|
478
|
+
fill?: 'gradient' | 'flat';
|
|
479
|
+
/** Y tick LABELS. ON by default: these charts are read for MAGNITUDE, and a
|
|
480
|
+
* tooltip only answers for the point under the pointer, so a chart someone
|
|
481
|
+
* has to quote a number from needs the scale on the page. Not strict —
|
|
482
|
+
* `yAxis={false}` gives shadcn's axis-less look and reclaims the gutter. */
|
|
483
|
+
yAxis?: boolean;
|
|
484
|
+
/** Hover bands + tooltip. On by default — see the note above HoverCard. */
|
|
485
|
+
hover?: boolean;
|
|
486
|
+
}) {
|
|
487
|
+
const uid = React.useId().replace(/:/g, '');
|
|
488
|
+
const [active, setActive] = React.useState<number | null>(null);
|
|
489
|
+
const plotH = height - PAD.t - PAD.b;
|
|
490
|
+
|
|
491
|
+
/* Stacking has to happen before the scale is chosen, or the top series runs
|
|
492
|
+
* off the top of the plot. */
|
|
493
|
+
const expand = stacked && stackOffset === 'expand';
|
|
494
|
+
const stacks: number[][] = [];
|
|
495
|
+
if (stacked) {
|
|
496
|
+
const totals = labels.map((_, i) => series.reduce((s, sr) => s + (sr.values[i] ?? 0), 0));
|
|
497
|
+
let running = labels.map(() => 0);
|
|
498
|
+
series.forEach((sr) => {
|
|
499
|
+
running = running.map((acc, i) => {
|
|
500
|
+
const v = sr.values[i] ?? 0;
|
|
501
|
+
/* expand: each value becomes its share of that x's total, so every
|
|
502
|
+
* stack tops out at 1 and the chart reads as composition over time. */
|
|
503
|
+
return acc + (expand ? v / (totals[i] || 1) : v);
|
|
504
|
+
});
|
|
505
|
+
stacks.push([...running]);
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
const all = stacked ? stacks.flat() : series.flatMap((sr) => sr.values);
|
|
509
|
+
const max = expand ? 1 : Math.max(...all, 1);
|
|
510
|
+
const min = Math.min(...all, 0);
|
|
511
|
+
/* Expanded stacks are SHARES, so the axis is a percentage while the tooltip
|
|
512
|
+
* still reports the underlying value — the axis answers "how much of the
|
|
513
|
+
* whole", the tooltip answers "how many". */
|
|
514
|
+
const axisFormat = expand ? (v: number) => `${Math.round(v * 100)}%` : formatValue;
|
|
515
|
+
/* The gutter has to be known before any x arithmetic — it defines where the
|
|
516
|
+
* plot starts. With no tick labels there is nothing to reserve for, so the
|
|
517
|
+
* plot uses the same 12px inset as the right edge. */
|
|
518
|
+
const padL = yAxis ? gutter(tickValues(min, max, gridLines).map(axisFormat)) : PAD.r;
|
|
519
|
+
const stepX = (W - padL - PAD.r) / Math.max(1, labels.length - 1);
|
|
520
|
+
const y = (v: number) => PAD.t + (1 - (v - min) / (max - min || 1)) * plotH;
|
|
521
|
+
const draw = curve === 'linear' ? linearPath : curve === 'step' ? stepPath : monotonePath;
|
|
522
|
+
const xAt = (i: number) => padL + i * stepX;
|
|
523
|
+
|
|
524
|
+
const point: HoverPoint | null =
|
|
525
|
+
hover && active !== null
|
|
526
|
+
? {
|
|
527
|
+
index: active,
|
|
528
|
+
label: labels[active],
|
|
529
|
+
left: (xAt(active) / W) * 100,
|
|
530
|
+
rows: series.map((sr, i) => ({
|
|
531
|
+
label: sr.label,
|
|
532
|
+
value: sr.values[active] ?? 0,
|
|
533
|
+
color: seriesColor(i, sr.tone),
|
|
534
|
+
})),
|
|
535
|
+
}
|
|
536
|
+
: null;
|
|
537
|
+
|
|
538
|
+
return (
|
|
539
|
+
<div data-slot="chart" className={cn('relative w-full', className)} {...props}>
|
|
540
|
+
<svg viewBox={`0 0 ${W} ${height}`} className="h-auto w-full" role="img">
|
|
541
|
+
<title>{axisName(series, labels)}</title>
|
|
542
|
+
{/* shadcn's gradient variant, verbatim: 5% at 0.8 → 95% at 0.1. */}
|
|
543
|
+
<defs>
|
|
544
|
+
{series.map((sr, i) => (
|
|
545
|
+
<linearGradient key={sr.label} id={`${uid}-g${i}`} x1="0" y1="0" x2="0" y2="1">
|
|
546
|
+
<stop offset="5%" stopColor={seriesColor(i, sr.tone)} stopOpacity="0.8" />
|
|
547
|
+
<stop offset="95%" stopColor={seriesColor(i, sr.tone)} stopOpacity="0.1" />
|
|
548
|
+
</linearGradient>
|
|
549
|
+
))}
|
|
550
|
+
</defs>
|
|
551
|
+
<Gridlines
|
|
552
|
+
count={gridLines}
|
|
553
|
+
min={min}
|
|
554
|
+
max={max}
|
|
555
|
+
height={height}
|
|
556
|
+
format={axisFormat}
|
|
557
|
+
padL={padL}
|
|
558
|
+
yAxis={yAxis}
|
|
559
|
+
/>
|
|
560
|
+
{/* the guide sits under the series so it never crosses a line */}
|
|
561
|
+
{point ? (
|
|
562
|
+
<line
|
|
563
|
+
x1={xAt(point.index)}
|
|
564
|
+
x2={xAt(point.index)}
|
|
565
|
+
y1={PAD.t}
|
|
566
|
+
y2={PAD.t + plotH}
|
|
567
|
+
stroke="var(--border-strong)"
|
|
568
|
+
strokeWidth="1"
|
|
569
|
+
/>
|
|
570
|
+
) : null}
|
|
571
|
+
{series
|
|
572
|
+
.map((sr, i) => {
|
|
573
|
+
const vals = stacked ? stacks[i] : sr.values;
|
|
574
|
+
const pts = vals.map((v, xi) => [xAt(xi), y(v)] as [number, number]);
|
|
575
|
+
const path = draw(pts);
|
|
576
|
+
const base = stacked && i > 0 ? stacks[i - 1] : null;
|
|
577
|
+
const floor = base
|
|
578
|
+
? [...base]
|
|
579
|
+
.reverse()
|
|
580
|
+
.map((v, xi) => [xAt(base.length - 1 - xi), y(v)] as [number, number])
|
|
581
|
+
: null;
|
|
582
|
+
return (
|
|
583
|
+
<g key={sr.label}>
|
|
584
|
+
{area && (
|
|
585
|
+
<path
|
|
586
|
+
d={
|
|
587
|
+
floor
|
|
588
|
+
? `${path} ${draw(floor).replace(/^M/, 'L')} Z`
|
|
589
|
+
: `${path} L ${pts[pts.length - 1][0]} ${y(min)} L ${pts[0][0]} ${y(min)} Z`
|
|
590
|
+
}
|
|
591
|
+
fill={fill === 'flat' ? seriesColor(i, sr.tone) : `url(#${uid}-g${i})`}
|
|
592
|
+
fillOpacity={fill === 'flat' ? 0.4 : undefined}
|
|
593
|
+
/>
|
|
594
|
+
)}
|
|
595
|
+
<path
|
|
596
|
+
d={path}
|
|
597
|
+
fill="none"
|
|
598
|
+
stroke={seriesColor(i, sr.tone)}
|
|
599
|
+
strokeWidth="1.75"
|
|
600
|
+
strokeLinecap="round"
|
|
601
|
+
strokeLinejoin="round"
|
|
602
|
+
/>
|
|
603
|
+
{pts.map(([px, py], di) =>
|
|
604
|
+
dots || (point && point.index === di) ? (
|
|
605
|
+
<circle
|
|
606
|
+
key={di}
|
|
607
|
+
cx={px}
|
|
608
|
+
cy={py}
|
|
609
|
+
r={point && point.index === di ? 4 : 3}
|
|
610
|
+
fill="var(--card)"
|
|
611
|
+
stroke={seriesColor(i, sr.tone)}
|
|
612
|
+
strokeWidth="1.75"
|
|
613
|
+
/>
|
|
614
|
+
) : null,
|
|
615
|
+
)}
|
|
616
|
+
</g>
|
|
617
|
+
);
|
|
618
|
+
})
|
|
619
|
+
.reverse()}
|
|
620
|
+
<XLabels labels={labels} height={height} padL={padL} />
|
|
621
|
+
{/* One full-height hit band per index, painted last so it wins the
|
|
622
|
+
pointer. A 3px dot is not a hit target. */}
|
|
623
|
+
{hover
|
|
624
|
+
? labels.map((label, i) => (
|
|
625
|
+
<rect
|
|
626
|
+
key={label + i}
|
|
627
|
+
x={xAt(i) - stepX / 2}
|
|
628
|
+
y={PAD.t}
|
|
629
|
+
width={stepX}
|
|
630
|
+
height={plotH}
|
|
631
|
+
fill="transparent"
|
|
632
|
+
onMouseEnter={() => setActive(i)}
|
|
633
|
+
onMouseLeave={() => setActive(null)}
|
|
634
|
+
/>
|
|
635
|
+
))
|
|
636
|
+
: null}
|
|
637
|
+
</svg>
|
|
638
|
+
{point ? <HoverCard point={point} formatValue={formatValue} /> : null}
|
|
639
|
+
</div>
|
|
640
|
+
);
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
function AreaChart(props: React.ComponentProps<typeof LineChart>) {
|
|
644
|
+
return <LineChart {...props} area />;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/* ---- bar ---------------------------------------------------------------- */
|
|
648
|
+
|
|
649
|
+
function BarChart({
|
|
650
|
+
series,
|
|
651
|
+
labels,
|
|
652
|
+
height = 176,
|
|
653
|
+
gridLines = 4,
|
|
654
|
+
stacked = false,
|
|
655
|
+
horizontal = false,
|
|
656
|
+
radius = 3,
|
|
657
|
+
yAxis = true,
|
|
658
|
+
hover = true,
|
|
659
|
+
formatValue = (v) => String(v),
|
|
660
|
+
className,
|
|
661
|
+
...props
|
|
662
|
+
}: AxisChartProps & {
|
|
663
|
+
stacked?: boolean;
|
|
664
|
+
horizontal?: boolean;
|
|
665
|
+
radius?: number;
|
|
666
|
+
/** Y tick labels, on by default — see LineChart's note. Grid is unaffected. */
|
|
667
|
+
yAxis?: boolean;
|
|
668
|
+
hover?: boolean;
|
|
669
|
+
}) {
|
|
670
|
+
const [active, setActive] = React.useState<number | null>(null);
|
|
671
|
+
/* A stacked bar's extent is the positive sum ABOVE zero and the negative sum
|
|
672
|
+
* BELOW it — not the net total, which cancels the two out and clips whichever
|
|
673
|
+
* side is larger. */
|
|
674
|
+
const sumBy = (pick: (v: number) => number) =>
|
|
675
|
+
labels.map((_, i) => series.reduce((sum, sr) => sum + pick(sr.values[i] ?? 0), 0));
|
|
676
|
+
const pool = stacked
|
|
677
|
+
? [...sumBy((v) => Math.max(0, v)), ...sumBy((v) => Math.min(0, v))]
|
|
678
|
+
: series.flatMap((sr) => sr.values);
|
|
679
|
+
const max = Math.max(...pool, 1);
|
|
680
|
+
/* Negative values need the baseline off the floor, and the zero line drawn
|
|
681
|
+
* solid — a dashed zero reads as just another gridline. */
|
|
682
|
+
const min = Math.min(...pool, 0);
|
|
683
|
+
const span = max - min || 1;
|
|
684
|
+
|
|
685
|
+
const rows = (i: number) =>
|
|
686
|
+
series.map((sr, si) => ({
|
|
687
|
+
label: sr.label,
|
|
688
|
+
value: sr.values[i] ?? 0,
|
|
689
|
+
color: seriesColor(si, sr.tone),
|
|
690
|
+
}));
|
|
691
|
+
|
|
692
|
+
if (horizontal) {
|
|
693
|
+
const rowH = 30;
|
|
694
|
+
const H = labels.length * rowH + PAD.t + 6;
|
|
695
|
+
const padL = 92;
|
|
696
|
+
const x = (v: number) => padL + ((v - min) / span) * (W - padL - PAD.r);
|
|
697
|
+
return (
|
|
698
|
+
<div data-slot="chart" className={cn('relative w-full', className)} {...props}>
|
|
699
|
+
<svg viewBox={`0 0 ${W} ${H}`} className="h-auto w-full" role="img">
|
|
700
|
+
<title>{axisName(series, labels)}</title>
|
|
701
|
+
{labels.map((label, i) => {
|
|
702
|
+
const cy = PAD.t + i * rowH;
|
|
703
|
+
return (
|
|
704
|
+
<g
|
|
705
|
+
key={label}
|
|
706
|
+
onMouseEnter={hover ? () => setActive(i) : undefined}
|
|
707
|
+
onMouseLeave={hover ? () => setActive(null) : undefined}
|
|
708
|
+
>
|
|
709
|
+
<rect x={0} y={cy} width={W} height={rowH} fill="transparent" />
|
|
710
|
+
{active === i ? (
|
|
711
|
+
<rect
|
|
712
|
+
x={padL}
|
|
713
|
+
y={cy}
|
|
714
|
+
width={W - padL - PAD.r}
|
|
715
|
+
height={rowH}
|
|
716
|
+
fill="var(--fill-0)"
|
|
717
|
+
/>
|
|
718
|
+
) : null}
|
|
719
|
+
<text x={padL - 10} y={cy + rowH / 2 + 3} textAnchor="end" className={AXIS_LABEL}>
|
|
720
|
+
{label}
|
|
721
|
+
</text>
|
|
722
|
+
{series.map((sr, si) => {
|
|
723
|
+
const v = sr.values[i] ?? 0;
|
|
724
|
+
const bh = Math.min(16, (rowH - 8) / series.length);
|
|
725
|
+
return (
|
|
726
|
+
<rect
|
|
727
|
+
key={sr.label}
|
|
728
|
+
x={x(Math.min(0, v))}
|
|
729
|
+
y={cy + (rowH - bh * series.length) / 2 + si * bh}
|
|
730
|
+
width={Math.max(1, Math.abs(x(v) - x(0)))}
|
|
731
|
+
height={bh - 1}
|
|
732
|
+
rx={radius}
|
|
733
|
+
fill={seriesColor(si, sr.tone)}
|
|
734
|
+
opacity={active === null || active === i ? 1 : 0.45}
|
|
735
|
+
/>
|
|
736
|
+
);
|
|
737
|
+
})}
|
|
738
|
+
</g>
|
|
739
|
+
);
|
|
740
|
+
})}
|
|
741
|
+
</svg>
|
|
742
|
+
{hover && active !== null ? (
|
|
743
|
+
<HoverCard
|
|
744
|
+
point={{ index: active, label: labels[active], left: 50, rows: rows(active) }}
|
|
745
|
+
formatValue={formatValue}
|
|
746
|
+
/>
|
|
747
|
+
) : null}
|
|
748
|
+
</div>
|
|
749
|
+
);
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
const plotH = height - PAD.t - PAD.b;
|
|
753
|
+
const padL = yAxis ? gutter(tickValues(min, max, gridLines).map(formatValue)) : PAD.r;
|
|
754
|
+
const slot = (W - padL - PAD.r) / labels.length;
|
|
755
|
+
const barW = stacked ? Math.min(30, slot * 0.5) : Math.min(20, (slot * 0.64) / series.length);
|
|
756
|
+
const y = (v: number) => PAD.t + (1 - (v - min) / span) * plotH;
|
|
757
|
+
|
|
758
|
+
return (
|
|
759
|
+
<div data-slot="chart" className={cn('relative w-full', className)} {...props}>
|
|
760
|
+
<svg viewBox={`0 0 ${W} ${height}`} className="h-auto w-full" role="img">
|
|
761
|
+
<title>{axisName(series, labels)}</title>
|
|
762
|
+
<Gridlines
|
|
763
|
+
count={gridLines}
|
|
764
|
+
min={min}
|
|
765
|
+
max={max}
|
|
766
|
+
height={height}
|
|
767
|
+
format={formatValue}
|
|
768
|
+
padL={padL}
|
|
769
|
+
yAxis={yAxis}
|
|
770
|
+
/>
|
|
771
|
+
{labels.map((label, i) => {
|
|
772
|
+
const centre = padL + slot * (i + 0.5);
|
|
773
|
+
/* Two cursors, because a stack can grow in both directions: negatives
|
|
774
|
+
* hang below the baseline instead of being piled on top of the
|
|
775
|
+
* positives as if their sign didn't exist. */
|
|
776
|
+
let up = y(0);
|
|
777
|
+
let down = y(0);
|
|
778
|
+
const dim = active !== null && active !== i;
|
|
779
|
+
return (
|
|
780
|
+
<g
|
|
781
|
+
key={label}
|
|
782
|
+
onMouseEnter={hover ? () => setActive(i) : undefined}
|
|
783
|
+
onMouseLeave={hover ? () => setActive(null) : undefined}
|
|
784
|
+
>
|
|
785
|
+
{/* the band is the hit target AND the hover affordance */}
|
|
786
|
+
<rect
|
|
787
|
+
x={padL + slot * i}
|
|
788
|
+
y={PAD.t}
|
|
789
|
+
width={slot}
|
|
790
|
+
height={plotH}
|
|
791
|
+
fill={active === i ? 'var(--fill-0)' : 'transparent'}
|
|
792
|
+
/>
|
|
793
|
+
{series.map((sr, si) => {
|
|
794
|
+
const v = sr.values[i] ?? 0;
|
|
795
|
+
if (stacked) {
|
|
796
|
+
const h = Math.abs(y(v) - y(0));
|
|
797
|
+
let top: number;
|
|
798
|
+
if (v < 0) {
|
|
799
|
+
top = down;
|
|
800
|
+
down += h;
|
|
801
|
+
} else {
|
|
802
|
+
up -= h;
|
|
803
|
+
top = up;
|
|
804
|
+
}
|
|
805
|
+
return (
|
|
806
|
+
<rect
|
|
807
|
+
key={sr.label}
|
|
808
|
+
x={centre - barW / 2}
|
|
809
|
+
y={top}
|
|
810
|
+
width={barW}
|
|
811
|
+
height={Math.max(0, h)}
|
|
812
|
+
rx={radius}
|
|
813
|
+
fill={seriesColor(si, sr.tone)}
|
|
814
|
+
opacity={dim ? 0.45 : 1}
|
|
815
|
+
/>
|
|
816
|
+
);
|
|
817
|
+
}
|
|
818
|
+
const groupW = barW * series.length + 2 * (series.length - 1);
|
|
819
|
+
return (
|
|
820
|
+
<rect
|
|
821
|
+
key={sr.label}
|
|
822
|
+
x={centre - groupW / 2 + si * (barW + 2)}
|
|
823
|
+
y={v >= 0 ? y(v) : y(0)}
|
|
824
|
+
width={barW}
|
|
825
|
+
height={Math.max(1, Math.abs(y(v) - y(0)))}
|
|
826
|
+
rx={radius}
|
|
827
|
+
fill={seriesColor(si, sr.tone)}
|
|
828
|
+
opacity={dim ? 0.45 : 1}
|
|
829
|
+
/>
|
|
830
|
+
);
|
|
831
|
+
})}
|
|
832
|
+
<text x={centre} y={height - 6} textAnchor="middle" className={AXIS_LABEL}>
|
|
833
|
+
{label}
|
|
834
|
+
</text>
|
|
835
|
+
</g>
|
|
836
|
+
);
|
|
837
|
+
})}
|
|
838
|
+
</svg>
|
|
839
|
+
{hover && active !== null ? (
|
|
840
|
+
<HoverCard
|
|
841
|
+
point={{
|
|
842
|
+
index: active,
|
|
843
|
+
label: labels[active],
|
|
844
|
+
left: ((padL + slot * (active + 0.5)) / W) * 100,
|
|
845
|
+
rows: rows(active),
|
|
846
|
+
}}
|
|
847
|
+
formatValue={formatValue}
|
|
848
|
+
/>
|
|
849
|
+
) : null}
|
|
850
|
+
</div>
|
|
851
|
+
);
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
/* ---- pie / donut -------------------------------------------------------- */
|
|
855
|
+
|
|
856
|
+
export interface PieSlice {
|
|
857
|
+
label: string;
|
|
858
|
+
value: number;
|
|
859
|
+
tone?: SemanticTone;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
/*
|
|
863
|
+
* Circumference arithmetic, not rounded degrees: a 0.5° rounding error leaves a
|
|
864
|
+
* visible seam between slices.
|
|
865
|
+
*
|
|
866
|
+
* The frame is sized to the SVG, NOT full width — an absolutely-positioned
|
|
867
|
+
* centre label inside a `w-full` wrapper centres on the container rather than on
|
|
868
|
+
* the ring, which puts the total next to the donut instead of inside it.
|
|
869
|
+
*/
|
|
870
|
+
function PieChart({
|
|
871
|
+
slices,
|
|
872
|
+
size = 168,
|
|
873
|
+
/** 0 renders a solid pie; any thickness renders a donut. */
|
|
874
|
+
thickness = 0,
|
|
875
|
+
centerLabel,
|
|
876
|
+
centerValue,
|
|
877
|
+
hover = true,
|
|
878
|
+
formatValue = (v) => String(v),
|
|
879
|
+
className,
|
|
880
|
+
...props
|
|
881
|
+
}: Omit<React.ComponentProps<'div'>, 'children'> & {
|
|
882
|
+
slices: PieSlice[];
|
|
883
|
+
size?: number;
|
|
884
|
+
thickness?: number;
|
|
885
|
+
centerLabel?: React.ReactNode;
|
|
886
|
+
centerValue?: React.ReactNode;
|
|
887
|
+
hover?: boolean;
|
|
888
|
+
formatValue?: (v: number) => string;
|
|
889
|
+
}) {
|
|
890
|
+
const [active, setActive] = React.useState<number | null>(null);
|
|
891
|
+
const total = slices.reduce((sum, sl) => sum + sl.value, 0) || 1;
|
|
892
|
+
const donut = thickness > 0;
|
|
893
|
+
const cx = size / 2;
|
|
894
|
+
const cy = size / 2;
|
|
895
|
+
/* Room for the hovered slice to lift without clipping. */
|
|
896
|
+
const lift = hover ? 4 : 0;
|
|
897
|
+
const rOuter = size / 2 - lift;
|
|
898
|
+
const rInner = donut ? rOuter - thickness : 0;
|
|
899
|
+
|
|
900
|
+
let a = -Math.PI / 2;
|
|
901
|
+
const wedges = slices.map((sl, i) => {
|
|
902
|
+
const sweep = (sl.value / total) * Math.PI * 2;
|
|
903
|
+
const mid = a + sweep / 2;
|
|
904
|
+
const w = { slice: sl, i, a0: a, a1: a + sweep, mid };
|
|
905
|
+
a += sweep;
|
|
906
|
+
return w;
|
|
907
|
+
});
|
|
908
|
+
|
|
909
|
+
/* A slice holding the whole total degenerates as an arc — its start and end
|
|
910
|
+
* points coincide, so the path paints nothing — and is drawn as a ring/disc
|
|
911
|
+
* instead. This is NOT the same as `slices.length === 1`: a status breakdown
|
|
912
|
+
* on a clean day still passes three slices, two of them zero. */
|
|
913
|
+
const full = wedges.find((w) => w.a1 - w.a0 >= Math.PI * 2 - 1e-9);
|
|
914
|
+
|
|
915
|
+
return (
|
|
916
|
+
<div
|
|
917
|
+
data-slot="chart"
|
|
918
|
+
style={{ width: size, height: size }}
|
|
919
|
+
className={cn('relative shrink-0', className)}
|
|
920
|
+
{...props}
|
|
921
|
+
>
|
|
922
|
+
<svg width={size} height={size} viewBox={`0 0 ${size} ${size}`} role="img">
|
|
923
|
+
<title>{partsName(slices, formatValue)}</title>
|
|
924
|
+
{full ? (
|
|
925
|
+
<circle
|
|
926
|
+
cx={cx}
|
|
927
|
+
cy={cy}
|
|
928
|
+
r={donut ? (rOuter + rInner) / 2 : rOuter}
|
|
929
|
+
fill={donut ? 'none' : seriesColor(full.i, full.slice.tone)}
|
|
930
|
+
stroke={donut ? seriesColor(full.i, full.slice.tone) : undefined}
|
|
931
|
+
strokeWidth={donut ? thickness : undefined}
|
|
932
|
+
/>
|
|
933
|
+
) : (
|
|
934
|
+
wedges.map(({ slice: sl, i, a0, a1, mid }) => {
|
|
935
|
+
const on = active === i;
|
|
936
|
+
return (
|
|
937
|
+
<path
|
|
938
|
+
key={sl.label}
|
|
939
|
+
d={arcPath(cx, cy, rOuter, rInner, a0, a1)}
|
|
940
|
+
fill={seriesColor(i, sl.tone)}
|
|
941
|
+
opacity={active === null || on ? 1 : 0.45}
|
|
942
|
+
transform={
|
|
943
|
+
on ? `translate(${Math.cos(mid) * lift} ${Math.sin(mid) * lift})` : undefined
|
|
944
|
+
}
|
|
945
|
+
onMouseEnter={hover ? () => setActive(i) : undefined}
|
|
946
|
+
onMouseLeave={hover ? () => setActive(null) : undefined}
|
|
947
|
+
style={{ transition: 'opacity .15s var(--ease)' }}
|
|
948
|
+
/>
|
|
949
|
+
);
|
|
950
|
+
})
|
|
951
|
+
)}
|
|
952
|
+
</svg>
|
|
953
|
+
{/* On a donut the centre is free, so hovering swaps the total for the
|
|
954
|
+
hovered slice — the reader gets the detail without a card covering the
|
|
955
|
+
chart. A pie has no centre to write in, hence the card. */}
|
|
956
|
+
{donut && (centerLabel || centerValue) ? (
|
|
957
|
+
<div className="pointer-events-none absolute inset-0 grid place-content-center text-center">
|
|
958
|
+
{active !== null ? (
|
|
959
|
+
<>
|
|
960
|
+
<div className="text-[19px] leading-none font-semibold tracking-[-0.02em] tabular-nums">
|
|
961
|
+
{formatValue(slices[active].value)}
|
|
962
|
+
</div>
|
|
963
|
+
<div className="mt-1 max-w-[86px] text-micro text-subtle">{slices[active].label}</div>
|
|
964
|
+
</>
|
|
965
|
+
) : (
|
|
966
|
+
<>
|
|
967
|
+
{centerValue ? (
|
|
968
|
+
<div className="text-[22px] leading-none font-semibold tracking-[-0.02em] tabular-nums">
|
|
969
|
+
{centerValue}
|
|
970
|
+
</div>
|
|
971
|
+
) : null}
|
|
972
|
+
{centerLabel ? (
|
|
973
|
+
<div className="mt-1 text-micro text-subtle">{centerLabel}</div>
|
|
974
|
+
) : null}
|
|
975
|
+
</>
|
|
976
|
+
)}
|
|
977
|
+
</div>
|
|
978
|
+
) : null}
|
|
979
|
+
{!donut && hover && active !== null ? (
|
|
980
|
+
<HoverCard
|
|
981
|
+
point={{
|
|
982
|
+
index: active,
|
|
983
|
+
label: slices[active].label,
|
|
984
|
+
left: 50,
|
|
985
|
+
rows: [
|
|
986
|
+
{
|
|
987
|
+
label: `${Math.round((slices[active].value / total) * 100)}% of total`,
|
|
988
|
+
value: slices[active].value,
|
|
989
|
+
color: seriesColor(active, slices[active].tone),
|
|
990
|
+
},
|
|
991
|
+
],
|
|
992
|
+
}}
|
|
993
|
+
formatValue={formatValue}
|
|
994
|
+
/>
|
|
995
|
+
) : null}
|
|
996
|
+
</div>
|
|
997
|
+
);
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
/** Donut is a pie with a hole — same arithmetic, one default changed. */
|
|
1001
|
+
function DonutChart({ thickness = 22, ...props }: React.ComponentProps<typeof PieChart>) {
|
|
1002
|
+
return <PieChart thickness={thickness} {...props} />;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
/* ---- radial ------------------------------------------------------------- */
|
|
1006
|
+
|
|
1007
|
+
/* Concentric arcs, one per series. Reads well for a handful of parts-of-whole
|
|
1008
|
+
* comparisons; useless beyond about four, because the inner radius shrinks
|
|
1009
|
+
* until the arc lengths are no longer comparable. */
|
|
1010
|
+
function RadialChart({
|
|
1011
|
+
series,
|
|
1012
|
+
size = 168,
|
|
1013
|
+
thickness = 14,
|
|
1014
|
+
gap = 4,
|
|
1015
|
+
max = 100,
|
|
1016
|
+
hover = true,
|
|
1017
|
+
centerLabel,
|
|
1018
|
+
centerValue,
|
|
1019
|
+
/* `max` is configurable, so the readout cannot hardcode a percent sign — on a
|
|
1020
|
+
* 0–250 scale "180%" is simply wrong. Percent stays the default because the
|
|
1021
|
+
* default scale is 0–100. */
|
|
1022
|
+
formatValue = (v) => (max === 100 ? `${v}%` : String(v)),
|
|
1023
|
+
className,
|
|
1024
|
+
...props
|
|
1025
|
+
}: Omit<React.ComponentProps<'div'>, 'children'> & {
|
|
1026
|
+
series: { label: string; value: number; tone?: SemanticTone }[];
|
|
1027
|
+
size?: number;
|
|
1028
|
+
thickness?: number;
|
|
1029
|
+
gap?: number;
|
|
1030
|
+
max?: number;
|
|
1031
|
+
centerLabel?: React.ReactNode;
|
|
1032
|
+
centerValue?: React.ReactNode;
|
|
1033
|
+
hover?: boolean;
|
|
1034
|
+
formatValue?: (value: number) => string;
|
|
1035
|
+
}) {
|
|
1036
|
+
const [active, setActive] = React.useState<number | null>(null);
|
|
1037
|
+
return (
|
|
1038
|
+
<div
|
|
1039
|
+
data-slot="chart"
|
|
1040
|
+
style={{ width: size, height: size }}
|
|
1041
|
+
className={cn('relative shrink-0', className)}
|
|
1042
|
+
{...props}
|
|
1043
|
+
>
|
|
1044
|
+
<svg width={size} height={size} viewBox={`0 0 ${size} ${size}`} role="img">
|
|
1045
|
+
<title>{partsName(series, formatValue)}</title>
|
|
1046
|
+
<g transform={`rotate(-90 ${size / 2} ${size / 2})`}>
|
|
1047
|
+
{series.map((s, i) => {
|
|
1048
|
+
const r = (size - thickness) / 2 - i * (thickness + gap);
|
|
1049
|
+
if (r <= thickness / 2) return null;
|
|
1050
|
+
const C = 2 * Math.PI * r;
|
|
1051
|
+
const len = (Math.max(0, Math.min(max, s.value)) / max) * C;
|
|
1052
|
+
return (
|
|
1053
|
+
<g key={s.label}>
|
|
1054
|
+
<circle
|
|
1055
|
+
cx={size / 2}
|
|
1056
|
+
cy={size / 2}
|
|
1057
|
+
r={r}
|
|
1058
|
+
fill="none"
|
|
1059
|
+
stroke="var(--fill-1)"
|
|
1060
|
+
strokeWidth={thickness}
|
|
1061
|
+
/>
|
|
1062
|
+
<circle
|
|
1063
|
+
cx={size / 2}
|
|
1064
|
+
cy={size / 2}
|
|
1065
|
+
r={r}
|
|
1066
|
+
fill="none"
|
|
1067
|
+
stroke={seriesColor(i, s.tone)}
|
|
1068
|
+
strokeWidth={thickness}
|
|
1069
|
+
strokeLinecap="round"
|
|
1070
|
+
strokeDasharray={`${len} ${C - len}`}
|
|
1071
|
+
opacity={active === null || active === i ? 1 : 0.45}
|
|
1072
|
+
style={{ transition: 'opacity .15s var(--ease)' }}
|
|
1073
|
+
/>
|
|
1074
|
+
{/* transparent full ring as the hit target: the arc itself only
|
|
1075
|
+
covers its own sweep, so a 30% ring would be unhoverable
|
|
1076
|
+
across most of its circumference */}
|
|
1077
|
+
<circle
|
|
1078
|
+
cx={size / 2}
|
|
1079
|
+
cy={size / 2}
|
|
1080
|
+
r={r}
|
|
1081
|
+
fill="none"
|
|
1082
|
+
stroke="transparent"
|
|
1083
|
+
strokeWidth={thickness}
|
|
1084
|
+
onMouseEnter={hover ? () => setActive(i) : undefined}
|
|
1085
|
+
onMouseLeave={hover ? () => setActive(null) : undefined}
|
|
1086
|
+
/>
|
|
1087
|
+
</g>
|
|
1088
|
+
);
|
|
1089
|
+
})}
|
|
1090
|
+
</g>
|
|
1091
|
+
</svg>
|
|
1092
|
+
{(centerLabel || centerValue) && (
|
|
1093
|
+
<div className="pointer-events-none absolute inset-0 grid place-content-center text-center">
|
|
1094
|
+
{active !== null ? (
|
|
1095
|
+
<>
|
|
1096
|
+
<div className="text-[19px] leading-none font-semibold tracking-[-0.02em] tabular-nums">
|
|
1097
|
+
{formatValue(series[active].value)}
|
|
1098
|
+
</div>
|
|
1099
|
+
<div className="mt-1 max-w-[80px] text-micro text-subtle">{series[active].label}</div>
|
|
1100
|
+
</>
|
|
1101
|
+
) : (
|
|
1102
|
+
<>
|
|
1103
|
+
{centerValue ? (
|
|
1104
|
+
<div className="text-[19px] leading-none font-semibold tracking-[-0.02em] tabular-nums">
|
|
1105
|
+
{centerValue}
|
|
1106
|
+
</div>
|
|
1107
|
+
) : null}
|
|
1108
|
+
{centerLabel ? (
|
|
1109
|
+
<div className="mt-1 text-micro text-subtle">{centerLabel}</div>
|
|
1110
|
+
) : null}
|
|
1111
|
+
</>
|
|
1112
|
+
)}
|
|
1113
|
+
</div>
|
|
1114
|
+
)}
|
|
1115
|
+
</div>
|
|
1116
|
+
);
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
/* ---- radar -------------------------------------------------------------- */
|
|
1120
|
+
|
|
1121
|
+
/* For comparing a few entities across the SAME handful of dimensions. The web
|
|
1122
|
+
* (not concentric rings) is the grid, because the shape is what gets compared. */
|
|
1123
|
+
function RadarChart({
|
|
1124
|
+
axes,
|
|
1125
|
+
series,
|
|
1126
|
+
size = 200,
|
|
1127
|
+
rings = 4,
|
|
1128
|
+
hover = true,
|
|
1129
|
+
className,
|
|
1130
|
+
...props
|
|
1131
|
+
}: Omit<React.ComponentProps<'div'>, 'children'> & {
|
|
1132
|
+
axes: string[];
|
|
1133
|
+
series: { label: string; values: number[]; tone?: SemanticTone }[];
|
|
1134
|
+
size?: number;
|
|
1135
|
+
rings?: number;
|
|
1136
|
+
hover?: boolean;
|
|
1137
|
+
}) {
|
|
1138
|
+
const [active, setActive] = React.useState<number | null>(null);
|
|
1139
|
+
const cx = size / 2;
|
|
1140
|
+
const cy = size / 2;
|
|
1141
|
+
/*
|
|
1142
|
+
* THE WEB IS INSET BY WHAT THE LABELS NEED, not by a constant.
|
|
1143
|
+
*
|
|
1144
|
+
* The axis labels sit OUTSIDE the web at 1.16r, so the box has to hold the web
|
|
1145
|
+
* plus the widest label. A fixed inset (this was `size/2 - 22`) clips as soon
|
|
1146
|
+
* as a label gets long — and tuning the constant until one dataset fits just
|
|
1147
|
+
* moves the clipping to whichever axis is longest next time.
|
|
1148
|
+
*
|
|
1149
|
+
* With five axes the side labels sit at |cos| = 0.95, so treating them as
|
|
1150
|
+
* fully horizontal over-reserves by ~5% and never under-reserves. If the
|
|
1151
|
+
* result leaves the web too small to compare shapes in, the fix is a bigger
|
|
1152
|
+
* `size` — not a smaller label.
|
|
1153
|
+
*/
|
|
1154
|
+
const widestLabel = axes.reduce((n, a) => Math.max(n, labelWidth(a)), 0);
|
|
1155
|
+
const r = Math.max(28, size / 2 - (widestLabel + TICK_GAP) / 1.16);
|
|
1156
|
+
const max = Math.max(...series.flatMap((s) => s.values), 1);
|
|
1157
|
+
const pt = (i: number, frac: number): [number, number] => {
|
|
1158
|
+
const a = (Math.PI * 2 * i) / axes.length - Math.PI / 2;
|
|
1159
|
+
return [cx + Math.cos(a) * r * frac, cy + Math.sin(a) * r * frac];
|
|
1160
|
+
};
|
|
1161
|
+
|
|
1162
|
+
return (
|
|
1163
|
+
<div
|
|
1164
|
+
data-slot="chart"
|
|
1165
|
+
style={{ width: size, height: size }}
|
|
1166
|
+
className={cn('relative shrink-0', className)}
|
|
1167
|
+
{...props}
|
|
1168
|
+
>
|
|
1169
|
+
<svg width={size} height={size} viewBox={`0 0 ${size} ${size}`} role="img">
|
|
1170
|
+
<title>{`${series.map((s) => s.label).join(', ')} across ${axes.join(', ')}`}</title>
|
|
1171
|
+
{Array.from({ length: rings }, (_, ri) => (
|
|
1172
|
+
<polygon
|
|
1173
|
+
key={ri}
|
|
1174
|
+
points={axes.map((_, i) => pt(i, (ri + 1) / rings).join(',')).join(' ')}
|
|
1175
|
+
fill="none"
|
|
1176
|
+
stroke="var(--border)"
|
|
1177
|
+
strokeWidth="1"
|
|
1178
|
+
strokeDasharray="3 3"
|
|
1179
|
+
/>
|
|
1180
|
+
))}
|
|
1181
|
+
{axes.map((_, i) => {
|
|
1182
|
+
const [x, y] = pt(i, 1);
|
|
1183
|
+
return (
|
|
1184
|
+
<line key={i} x1={cx} y1={cy} x2={x} y2={y} stroke="var(--border)" strokeWidth="1" />
|
|
1185
|
+
);
|
|
1186
|
+
})}
|
|
1187
|
+
{series.map((s, si) => (
|
|
1188
|
+
<polygon
|
|
1189
|
+
key={s.label}
|
|
1190
|
+
points={s.values.map((v, i) => pt(i, v / max).join(',')).join(' ')}
|
|
1191
|
+
fill={seriesColor(si, s.tone)}
|
|
1192
|
+
fillOpacity={active === si ? 0.3 : active === null ? 0.18 : 0.06}
|
|
1193
|
+
stroke={seriesColor(si, s.tone)}
|
|
1194
|
+
strokeWidth={active === si ? 2.25 : 1.75}
|
|
1195
|
+
strokeLinejoin="round"
|
|
1196
|
+
onMouseEnter={hover ? () => setActive(si) : undefined}
|
|
1197
|
+
onMouseLeave={hover ? () => setActive(null) : undefined}
|
|
1198
|
+
style={{ transition: 'fill-opacity .15s var(--ease)' }}
|
|
1199
|
+
/>
|
|
1200
|
+
))}
|
|
1201
|
+
{axes.map((label, i) => {
|
|
1202
|
+
const [x, y] = pt(i, 1.16);
|
|
1203
|
+
return (
|
|
1204
|
+
<text
|
|
1205
|
+
key={label}
|
|
1206
|
+
x={x}
|
|
1207
|
+
y={y + 3}
|
|
1208
|
+
textAnchor={x > cx + 4 ? 'start' : x < cx - 4 ? 'end' : 'middle'}
|
|
1209
|
+
className={AXIS_LABEL}
|
|
1210
|
+
>
|
|
1211
|
+
{label}
|
|
1212
|
+
</text>
|
|
1213
|
+
);
|
|
1214
|
+
})}
|
|
1215
|
+
</svg>
|
|
1216
|
+
</div>
|
|
1217
|
+
);
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
/* ---- sparkline & ring --------------------------------------------------- */
|
|
1221
|
+
|
|
1222
|
+
/* Answers "which way" and nothing else — adding a scale to a 24px-tall chart is
|
|
1223
|
+
* false precision. */
|
|
1224
|
+
function Sparkline({
|
|
1225
|
+
values,
|
|
1226
|
+
width = 88,
|
|
1227
|
+
height = 24,
|
|
1228
|
+
tone,
|
|
1229
|
+
className,
|
|
1230
|
+
...props
|
|
1231
|
+
}: Omit<React.ComponentProps<'div'>, 'children'> & {
|
|
1232
|
+
values: number[];
|
|
1233
|
+
width?: number;
|
|
1234
|
+
height?: number;
|
|
1235
|
+
tone?: SemanticTone;
|
|
1236
|
+
}) {
|
|
1237
|
+
const max = Math.max(...values, 1);
|
|
1238
|
+
const min = Math.min(...values, 0);
|
|
1239
|
+
const span = max - min || 1;
|
|
1240
|
+
const pts = values.map(
|
|
1241
|
+
(v, i) =>
|
|
1242
|
+
[
|
|
1243
|
+
(i / Math.max(1, values.length - 1)) * (width - 2) + 1,
|
|
1244
|
+
height - 2 - ((v - min) / span) * (height - 4),
|
|
1245
|
+
] as [number, number],
|
|
1246
|
+
);
|
|
1247
|
+
return (
|
|
1248
|
+
<div
|
|
1249
|
+
data-slot="chart"
|
|
1250
|
+
className={cn('inline-block shrink-0 leading-none', className)}
|
|
1251
|
+
{...props}
|
|
1252
|
+
>
|
|
1253
|
+
<svg width={width} height={height} viewBox={`0 0 ${width} ${height}`} role="img">
|
|
1254
|
+
<title>{`Trend, ${values[0]} to ${values[values.length - 1]}`}</title>
|
|
1255
|
+
<path
|
|
1256
|
+
d={monotonePath(pts)}
|
|
1257
|
+
fill="none"
|
|
1258
|
+
stroke={seriesColor(0, tone)}
|
|
1259
|
+
strokeWidth="1.5"
|
|
1260
|
+
strokeLinecap="round"
|
|
1261
|
+
/>
|
|
1262
|
+
</svg>
|
|
1263
|
+
</div>
|
|
1264
|
+
);
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
function ProgressRing({
|
|
1268
|
+
value,
|
|
1269
|
+
size = 44,
|
|
1270
|
+
thickness = 4,
|
|
1271
|
+
tone,
|
|
1272
|
+
showValue = true,
|
|
1273
|
+
className,
|
|
1274
|
+
...props
|
|
1275
|
+
}: Omit<React.ComponentProps<'div'>, 'children'> & {
|
|
1276
|
+
value: number;
|
|
1277
|
+
size?: number;
|
|
1278
|
+
thickness?: number;
|
|
1279
|
+
tone?: SemanticTone;
|
|
1280
|
+
showValue?: boolean;
|
|
1281
|
+
}) {
|
|
1282
|
+
const r = (size - thickness) / 2;
|
|
1283
|
+
const C = 2 * Math.PI * r;
|
|
1284
|
+
const len = (Math.max(0, Math.min(100, value)) / 100) * C;
|
|
1285
|
+
return (
|
|
1286
|
+
<div
|
|
1287
|
+
data-slot="chart"
|
|
1288
|
+
style={{ width: size, height: size }}
|
|
1289
|
+
className={cn('relative shrink-0', className)}
|
|
1290
|
+
{...props}
|
|
1291
|
+
>
|
|
1292
|
+
<svg width={size} height={size} viewBox={`0 0 ${size} ${size}`} role="img">
|
|
1293
|
+
<title>{`${Math.round(value)}%`}</title>
|
|
1294
|
+
<circle
|
|
1295
|
+
cx={size / 2}
|
|
1296
|
+
cy={size / 2}
|
|
1297
|
+
r={r}
|
|
1298
|
+
fill="none"
|
|
1299
|
+
stroke="var(--fill-2)"
|
|
1300
|
+
strokeWidth={thickness}
|
|
1301
|
+
/>
|
|
1302
|
+
<g transform={`rotate(-90 ${size / 2} ${size / 2})`}>
|
|
1303
|
+
<circle
|
|
1304
|
+
cx={size / 2}
|
|
1305
|
+
cy={size / 2}
|
|
1306
|
+
r={r}
|
|
1307
|
+
fill="none"
|
|
1308
|
+
stroke={seriesColor(0, tone)}
|
|
1309
|
+
strokeWidth={thickness}
|
|
1310
|
+
strokeLinecap="round"
|
|
1311
|
+
strokeDasharray={`${len} ${C - len}`}
|
|
1312
|
+
/>
|
|
1313
|
+
</g>
|
|
1314
|
+
</svg>
|
|
1315
|
+
{showValue ? (
|
|
1316
|
+
<span className="absolute inset-0 grid place-content-center text-micro font-medium tabular-nums">
|
|
1317
|
+
{Math.round(value)}%
|
|
1318
|
+
</span>
|
|
1319
|
+
) : null}
|
|
1320
|
+
</div>
|
|
1321
|
+
);
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
/* ---- legend & tooltip --------------------------------------------------- */
|
|
1325
|
+
|
|
1326
|
+
function ChartLegend({ className, ...props }: React.ComponentProps<'div'>) {
|
|
1327
|
+
return (
|
|
1328
|
+
<div
|
|
1329
|
+
data-slot="chart-legend"
|
|
1330
|
+
className={cn(
|
|
1331
|
+
'flex flex-wrap items-center gap-3 text-micro text-muted-foreground',
|
|
1332
|
+
className,
|
|
1333
|
+
)}
|
|
1334
|
+
{...props}
|
|
1335
|
+
/>
|
|
1336
|
+
);
|
|
1337
|
+
}
|
|
1338
|
+
|
|
1339
|
+
function ChartLegendItem({
|
|
1340
|
+
className,
|
|
1341
|
+
index = 0,
|
|
1342
|
+
tone,
|
|
1343
|
+
children,
|
|
1344
|
+
...props
|
|
1345
|
+
}: React.ComponentProps<'span'> & { index?: number; tone?: SemanticTone }) {
|
|
1346
|
+
return (
|
|
1347
|
+
<span
|
|
1348
|
+
data-slot="chart-legend-item"
|
|
1349
|
+
className={cn('inline-flex items-center gap-1.5', className)}
|
|
1350
|
+
{...props}
|
|
1351
|
+
>
|
|
1352
|
+
<i
|
|
1353
|
+
aria-hidden
|
|
1354
|
+
className="size-2 shrink-0 rounded-[2px]"
|
|
1355
|
+
style={{ background: seriesColor(index, tone) }}
|
|
1356
|
+
/>
|
|
1357
|
+
{children}
|
|
1358
|
+
</span>
|
|
1359
|
+
);
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
function ChartTooltip({ className, ...props }: React.ComponentProps<'div'>) {
|
|
1363
|
+
return (
|
|
1364
|
+
<div
|
|
1365
|
+
data-slot="chart-tooltip"
|
|
1366
|
+
className={cn('min-w-[148px] rounded-lg bg-card p-2.5 text-xs shadow-pop', className)}
|
|
1367
|
+
{...props}
|
|
1368
|
+
/>
|
|
1369
|
+
);
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
export {
|
|
1373
|
+
ChartCard,
|
|
1374
|
+
ChartCardHeader,
|
|
1375
|
+
ChartCardTitle,
|
|
1376
|
+
ChartCardDescription,
|
|
1377
|
+
ChartCardActions,
|
|
1378
|
+
ChartCardFooter,
|
|
1379
|
+
LineChart,
|
|
1380
|
+
AreaChart,
|
|
1381
|
+
BarChart,
|
|
1382
|
+
PieChart,
|
|
1383
|
+
DonutChart,
|
|
1384
|
+
RadialChart,
|
|
1385
|
+
RadarChart,
|
|
1386
|
+
Sparkline,
|
|
1387
|
+
ProgressRing,
|
|
1388
|
+
ChartLegend,
|
|
1389
|
+
ChartLegendItem,
|
|
1390
|
+
ChartTooltip,
|
|
1391
|
+
CHART_SERIES,
|
|
1392
|
+
seriesColor,
|
|
1393
|
+
type SemanticTone,
|
|
1394
|
+
};
|