@rise-x/apps-sdk 0.7.0 → 0.8.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/build/ui/components/accordion.d.ts +7 -0
- package/build/ui/components/aspect-ratio.d.ts +4 -0
- package/build/ui/components/button-group.d.ts +7 -0
- package/build/ui/components/carousel.d.ts +18 -0
- package/build/ui/components/chart.d.ts +45 -94
- package/build/ui/components/collapsible.d.ts +6 -0
- package/build/ui/components/combobox.d.ts +19 -0
- package/build/ui/components/command.d.ts +18 -0
- package/build/ui/components/context-menu.d.ts +29 -0
- package/build/ui/components/date-picker.d.ts +16 -0
- package/build/ui/components/direction.d.ts +6 -0
- package/build/ui/components/field.d.ts +25 -0
- package/build/ui/components/hover-card.d.ts +6 -0
- package/build/ui/components/input-group.d.ts +13 -0
- package/build/ui/components/input-otp.d.ts +34 -0
- package/build/ui/components/input.d.ts +1 -3
- package/build/ui/components/item.d.ts +23 -0
- package/build/ui/components/kbd.d.ts +4 -0
- package/build/ui/components/menubar.d.ts +26 -0
- package/build/ui/components/message-scroller.d.ts +15 -0
- package/build/ui/components/navigation-menu.d.ts +13 -0
- package/build/ui/components/page-header.d.ts +7 -1
- package/build/ui/index.d.ts +19 -0
- package/build/ui/reference/components/accordion.tsx +85 -0
- package/build/ui/reference/components/alert-dialog.tsx +1 -1
- package/build/ui/reference/components/aspect-ratio.tsx +28 -0
- package/build/ui/reference/components/button-group.tsx +77 -0
- package/build/ui/reference/components/carousel.tsx +248 -0
- package/build/ui/reference/components/chart.tsx +441 -1131
- package/build/ui/reference/components/collapsible.tsx +43 -0
- package/build/ui/reference/components/combobox.tsx +141 -0
- package/build/ui/reference/components/command.tsx +181 -0
- package/build/ui/reference/components/context-menu.tsx +243 -0
- package/build/ui/reference/components/data-table.tsx +3 -3
- package/build/ui/reference/components/date-picker.tsx +218 -0
- package/build/ui/reference/components/dialog.tsx +2 -2
- package/build/ui/reference/components/direction.tsx +22 -0
- package/build/ui/reference/components/field.tsx +252 -0
- package/build/ui/reference/components/hover-card.tsx +64 -0
- package/build/ui/reference/components/input-group.tsx +157 -0
- package/build/ui/reference/components/input-otp.tsx +106 -0
- package/build/ui/reference/components/input.tsx +4 -32
- package/build/ui/reference/components/item.tsx +188 -0
- package/build/ui/reference/components/kbd.tsx +43 -0
- package/build/ui/reference/components/label.tsx +1 -1
- package/build/ui/reference/components/menubar.tsx +275 -0
- package/build/ui/reference/components/message-scroller.tsx +141 -0
- package/build/ui/reference/components/nav.tsx +6 -0
- package/build/ui/reference/components/navigation-menu.tsx +191 -0
- package/build/ui/reference/components/page-header.tsx +32 -0
- package/build/ui/reference/components/popover.tsx +2 -2
- package/build/ui/reference/components/select.tsx +11 -8
- package/build/ui/reference/components/sheet.tsx +9 -1
- package/build/ui/reference/components/timeline.tsx +7 -0
- package/build/ui/reference/components/toggle.tsx +1 -1
- package/build/ui/reference/index.ts +19 -0
- package/build/ui/standalone.js +167 -28
- package/build/ui/styles.css +1 -1
- package/package.json +6 -1
|
@@ -1,38 +1,45 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
1
3
|
import * as React from 'react';
|
|
4
|
+
import * as RechartsPrimitive from 'recharts';
|
|
5
|
+
import type { TooltipValueType } from 'recharts';
|
|
2
6
|
|
|
3
7
|
import { cn } from '@rise-x/ui/lib/utils';
|
|
4
8
|
|
|
5
9
|
/*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
10
|
+
* CHARTS ARE THE REAL SHADCN LAYER over Recharts — ChartContainer + ChartConfig
|
|
11
|
+
* + ChartTooltipContent + ChartLegendContent, so any shadcn chart example
|
|
12
|
+
* pastes in working. Call sites compose Recharts primitives; there is no
|
|
13
|
+
* LineChart/BarChart kit component to outgrow. Two visual tokens are swapped
|
|
14
|
+
* to the kit's surfaces (tooltip on bg-popover with shadow-pop instead of
|
|
15
|
+
* bg-background with shadow-xl); everything else is upstream verbatim.
|
|
11
16
|
*
|
|
12
|
-
*
|
|
13
|
-
* costs information this product needs. Tick size is NOT one of them — that is
|
|
14
|
-
* shadcn's 12px:
|
|
17
|
+
* TWO HOUSE RULES SURVIVE THE SWITCH:
|
|
15
18
|
*
|
|
16
|
-
* · THE
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
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.
|
|
19
|
+
* · THE PALETTE IS ITS OWN FAMILY — --chart-1..6, sequenced so adjacent series
|
|
20
|
+
* sit far apart in hue. Brand green, berry and pure red are all EXCLUDED, so
|
|
21
|
+
* a chart can never imply success, AI or failure by accident. When the
|
|
22
|
+
* series ARE statuses (matched / in review / failed), use the semantic
|
|
23
|
+
* tokens via `seriesColor(i, tone)` — a failed slice must be red.
|
|
23
24
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
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.
|
|
25
|
+
* · PASS type="monotone", NOT "natural", to Line/Area. Recharts' natural cubic
|
|
26
|
+
* overshoots local extrema and can carry a strictly-positive series below
|
|
27
|
+
* zero — a data error to the reader. The demo's specimens all do this.
|
|
29
28
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
29
|
+
* ALSO PASS isAnimationActive={false} TO EVERY <Line>. Recharts draws a line's
|
|
30
|
+
* entry animation as a dash sweep, and after ChartContainer's initialDimension
|
|
31
|
+
* resize the sweep can be left parked at `stroke-dasharray: 0px, N` — the line
|
|
32
|
+
* is in the DOM, correct, and invisible until something forces a redraw. It
|
|
33
|
+
* reproduces on 3.8 and 3.10 alike, so it is not version drift. Areas, bars and
|
|
34
|
+
* pies animate normally and need nothing.
|
|
32
35
|
*
|
|
33
|
-
* EVERY CHART SHIPS IN A ChartCard. A chart without a title is a decoration:
|
|
34
|
-
* reader has to infer what is being measured and over what period, and two
|
|
36
|
+
* EVERY CHART SHIPS IN A ChartCard. A chart without a title is a decoration:
|
|
37
|
+
* the reader has to infer what is being measured and over what period, and two
|
|
35
38
|
* charts side by side become indistinguishable.
|
|
39
|
+
*
|
|
40
|
+
* Sparkline and ProgressRing stay hand-rolled SVG below — they are micro-
|
|
41
|
+
* visuals for table cells and stat tiles, where a responsive Recharts mount
|
|
42
|
+
* per row would be weight without benefit.
|
|
36
43
|
*/
|
|
37
44
|
|
|
38
45
|
const CHART_SERIES = [
|
|
@@ -58,7 +65,334 @@ function seriesColor(index: number, tone?: SemanticTone) {
|
|
|
58
65
|
return tone ? SEMANTIC_SERIES[tone] : CHART_SERIES[index % CHART_SERIES.length];
|
|
59
66
|
}
|
|
60
67
|
|
|
61
|
-
/* ----
|
|
68
|
+
/* ---- the shadcn chart layer (upstream new-york-v4) ----------------------- */
|
|
69
|
+
|
|
70
|
+
// Format: { THEME_NAME: CSS_SELECTOR }
|
|
71
|
+
const THEMES = { light: '', dark: '.dark' } as const;
|
|
72
|
+
|
|
73
|
+
const INITIAL_DIMENSION = { width: 320, height: 200 } as const;
|
|
74
|
+
type TooltipNameType = number | string;
|
|
75
|
+
|
|
76
|
+
export type ChartConfig = Record<
|
|
77
|
+
string,
|
|
78
|
+
{
|
|
79
|
+
label?: React.ReactNode;
|
|
80
|
+
icon?: React.ComponentType;
|
|
81
|
+
} & (
|
|
82
|
+
| { color?: string; theme?: never }
|
|
83
|
+
| { color?: never; theme: Record<keyof typeof THEMES, string> }
|
|
84
|
+
)
|
|
85
|
+
>;
|
|
86
|
+
|
|
87
|
+
type ChartContextProps = {
|
|
88
|
+
config: ChartConfig;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const ChartContext = React.createContext<ChartContextProps | null>(null);
|
|
92
|
+
|
|
93
|
+
function useChart() {
|
|
94
|
+
const context = React.useContext(ChartContext);
|
|
95
|
+
|
|
96
|
+
if (!context) {
|
|
97
|
+
throw new Error('useChart must be used within a <ChartContainer />');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return context;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function ChartContainer({
|
|
104
|
+
id,
|
|
105
|
+
className,
|
|
106
|
+
children,
|
|
107
|
+
config,
|
|
108
|
+
initialDimension = INITIAL_DIMENSION,
|
|
109
|
+
...props
|
|
110
|
+
}: React.ComponentProps<'div'> & {
|
|
111
|
+
config: ChartConfig;
|
|
112
|
+
children: React.ComponentProps<typeof RechartsPrimitive.ResponsiveContainer>['children'];
|
|
113
|
+
initialDimension?: {
|
|
114
|
+
width: number;
|
|
115
|
+
height: number;
|
|
116
|
+
};
|
|
117
|
+
}) {
|
|
118
|
+
const uniqueId = React.useId();
|
|
119
|
+
const chartId = `chart-${id ?? uniqueId.replace(/:/g, '')}`;
|
|
120
|
+
|
|
121
|
+
return (
|
|
122
|
+
<ChartContext.Provider value={{ config }}>
|
|
123
|
+
<div
|
|
124
|
+
data-slot="chart"
|
|
125
|
+
data-chart={chartId}
|
|
126
|
+
className={cn(
|
|
127
|
+
"flex aspect-video justify-center text-xs [&_.recharts-cartesian-axis-tick_text]:fill-muted-foreground [&_.recharts-cartesian-grid_line[stroke='#ccc']]:stroke-border/50 [&_.recharts-curve.recharts-tooltip-cursor]:stroke-border [&_.recharts-dot[stroke='#fff']]:stroke-transparent [&_.recharts-layer]:outline-hidden [&_.recharts-polar-grid_[stroke='#ccc']]:stroke-border [&_.recharts-radial-bar-background-sector]:fill-muted [&_.recharts-rectangle.recharts-tooltip-cursor]:fill-muted [&_.recharts-reference-line_[stroke='#ccc']]:stroke-border [&_.recharts-sector]:outline-hidden [&_.recharts-sector[stroke='#fff']]:stroke-transparent [&_.recharts-surface]:outline-hidden",
|
|
128
|
+
className,
|
|
129
|
+
)}
|
|
130
|
+
{...props}
|
|
131
|
+
>
|
|
132
|
+
<ChartStyle id={chartId} config={config} />
|
|
133
|
+
<RechartsPrimitive.ResponsiveContainer initialDimension={initialDimension}>
|
|
134
|
+
{children}
|
|
135
|
+
</RechartsPrimitive.ResponsiveContainer>
|
|
136
|
+
</div>
|
|
137
|
+
</ChartContext.Provider>
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const ChartStyle = ({ id, config }: { id: string; config: ChartConfig }) => {
|
|
142
|
+
const colorConfig = Object.entries(config).filter(([, config]) => config.theme ?? config.color);
|
|
143
|
+
|
|
144
|
+
if (!colorConfig.length) {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return (
|
|
149
|
+
<style
|
|
150
|
+
dangerouslySetInnerHTML={{
|
|
151
|
+
__html: Object.entries(THEMES)
|
|
152
|
+
.map(
|
|
153
|
+
([theme, prefix]) => `
|
|
154
|
+
${prefix} [data-chart=${id}] {
|
|
155
|
+
${colorConfig
|
|
156
|
+
.map(([key, itemConfig]) => {
|
|
157
|
+
const color = itemConfig.theme?.[theme as keyof typeof itemConfig.theme] ?? itemConfig.color;
|
|
158
|
+
return color ? ` --color-${key}: ${color};` : null;
|
|
159
|
+
})
|
|
160
|
+
.join('\n')}
|
|
161
|
+
}
|
|
162
|
+
`,
|
|
163
|
+
)
|
|
164
|
+
.join('\n'),
|
|
165
|
+
}}
|
|
166
|
+
/>
|
|
167
|
+
);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const ChartTooltip = RechartsPrimitive.Tooltip;
|
|
171
|
+
|
|
172
|
+
function ChartTooltipContent({
|
|
173
|
+
active,
|
|
174
|
+
payload,
|
|
175
|
+
className,
|
|
176
|
+
indicator = 'dot',
|
|
177
|
+
hideLabel = false,
|
|
178
|
+
hideIndicator = false,
|
|
179
|
+
label,
|
|
180
|
+
labelFormatter,
|
|
181
|
+
labelClassName,
|
|
182
|
+
formatter,
|
|
183
|
+
color,
|
|
184
|
+
nameKey,
|
|
185
|
+
labelKey,
|
|
186
|
+
}: React.ComponentProps<typeof RechartsPrimitive.Tooltip> &
|
|
187
|
+
React.ComponentProps<'div'> & {
|
|
188
|
+
hideLabel?: boolean;
|
|
189
|
+
hideIndicator?: boolean;
|
|
190
|
+
indicator?: 'line' | 'dot' | 'dashed';
|
|
191
|
+
nameKey?: string;
|
|
192
|
+
labelKey?: string;
|
|
193
|
+
} & Omit<
|
|
194
|
+
RechartsPrimitive.DefaultTooltipContentProps<TooltipValueType, TooltipNameType>,
|
|
195
|
+
'accessibilityLayer'
|
|
196
|
+
>) {
|
|
197
|
+
const { config } = useChart();
|
|
198
|
+
|
|
199
|
+
const tooltipLabel = React.useMemo(() => {
|
|
200
|
+
if (hideLabel || !payload?.length) {
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const [item] = payload;
|
|
205
|
+
const key = `${labelKey ?? item?.dataKey ?? item?.name ?? 'value'}`;
|
|
206
|
+
const itemConfig = getPayloadConfigFromPayload(config, item, key);
|
|
207
|
+
const value =
|
|
208
|
+
!labelKey && typeof label === 'string' ? (config[label]?.label ?? label) : itemConfig?.label;
|
|
209
|
+
|
|
210
|
+
if (labelFormatter) {
|
|
211
|
+
return (
|
|
212
|
+
<div className={cn('font-medium', labelClassName)}>{labelFormatter(value, payload)}</div>
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (!value) {
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return <div className={cn('font-medium', labelClassName)}>{value}</div>;
|
|
221
|
+
}, [label, labelFormatter, payload, hideLabel, labelClassName, config, labelKey]);
|
|
222
|
+
|
|
223
|
+
if (!active || !payload?.length) {
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const nestLabel = payload.length === 1 && indicator !== 'dot';
|
|
228
|
+
|
|
229
|
+
return (
|
|
230
|
+
<div
|
|
231
|
+
className={cn(
|
|
232
|
+
// kit pop surface instead of upstream's bg-background + shadow-xl
|
|
233
|
+
'grid min-w-[128px] items-start gap-1.5 rounded-lg border border-border/50 bg-popover px-2.5 py-1.5 text-xs text-popover-foreground shadow-pop',
|
|
234
|
+
className,
|
|
235
|
+
)}
|
|
236
|
+
>
|
|
237
|
+
{!nestLabel ? tooltipLabel : null}
|
|
238
|
+
<div className="grid gap-1.5">
|
|
239
|
+
{payload
|
|
240
|
+
.filter((item) => item.type !== 'none')
|
|
241
|
+
.map((item, index) => {
|
|
242
|
+
const key = `${nameKey ?? item.name ?? item.dataKey ?? 'value'}`;
|
|
243
|
+
const itemConfig = getPayloadConfigFromPayload(config, item, key);
|
|
244
|
+
const indicatorColor = color ?? item.payload?.fill ?? item.color;
|
|
245
|
+
|
|
246
|
+
return (
|
|
247
|
+
<div
|
|
248
|
+
key={index}
|
|
249
|
+
className={cn(
|
|
250
|
+
'flex w-full flex-wrap items-stretch gap-2 [&>svg]:h-2.5 [&>svg]:w-2.5 [&>svg]:text-muted-foreground',
|
|
251
|
+
indicator === 'dot' && 'items-center',
|
|
252
|
+
)}
|
|
253
|
+
>
|
|
254
|
+
{formatter && item?.value !== undefined && item.name ? (
|
|
255
|
+
formatter(item.value, item.name, item, index, item.payload)
|
|
256
|
+
) : (
|
|
257
|
+
<>
|
|
258
|
+
{itemConfig?.icon ? (
|
|
259
|
+
<itemConfig.icon />
|
|
260
|
+
) : (
|
|
261
|
+
!hideIndicator && (
|
|
262
|
+
<div
|
|
263
|
+
className={cn(
|
|
264
|
+
'shrink-0 rounded-[2px] border-(--color-border) bg-(--color-bg)',
|
|
265
|
+
{
|
|
266
|
+
'h-2.5 w-2.5': indicator === 'dot',
|
|
267
|
+
'w-1': indicator === 'line',
|
|
268
|
+
'w-0 border-[1.5px] border-dashed bg-transparent':
|
|
269
|
+
indicator === 'dashed',
|
|
270
|
+
'my-0.5': nestLabel && indicator === 'dashed',
|
|
271
|
+
},
|
|
272
|
+
)}
|
|
273
|
+
style={
|
|
274
|
+
{
|
|
275
|
+
'--color-bg': indicatorColor,
|
|
276
|
+
'--color-border': indicatorColor,
|
|
277
|
+
} as React.CSSProperties
|
|
278
|
+
}
|
|
279
|
+
/>
|
|
280
|
+
)
|
|
281
|
+
)}
|
|
282
|
+
<div
|
|
283
|
+
className={cn(
|
|
284
|
+
'flex flex-1 justify-between leading-none',
|
|
285
|
+
nestLabel ? 'items-end' : 'items-center',
|
|
286
|
+
)}
|
|
287
|
+
>
|
|
288
|
+
<div className="grid gap-1.5">
|
|
289
|
+
{nestLabel ? tooltipLabel : null}
|
|
290
|
+
<span className="text-muted-foreground">
|
|
291
|
+
{itemConfig?.label ?? item.name}
|
|
292
|
+
</span>
|
|
293
|
+
</div>
|
|
294
|
+
{item.value != null && (
|
|
295
|
+
<span className="font-mono font-medium text-foreground tabular-nums">
|
|
296
|
+
{typeof item.value === 'number'
|
|
297
|
+
? item.value.toLocaleString()
|
|
298
|
+
: String(item.value)}
|
|
299
|
+
</span>
|
|
300
|
+
)}
|
|
301
|
+
</div>
|
|
302
|
+
</>
|
|
303
|
+
)}
|
|
304
|
+
</div>
|
|
305
|
+
);
|
|
306
|
+
})}
|
|
307
|
+
</div>
|
|
308
|
+
</div>
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const ChartLegend = RechartsPrimitive.Legend;
|
|
313
|
+
|
|
314
|
+
function ChartLegendContent({
|
|
315
|
+
className,
|
|
316
|
+
hideIcon = false,
|
|
317
|
+
payload,
|
|
318
|
+
verticalAlign = 'bottom',
|
|
319
|
+
nameKey,
|
|
320
|
+
}: React.ComponentProps<'div'> & {
|
|
321
|
+
hideIcon?: boolean;
|
|
322
|
+
nameKey?: string;
|
|
323
|
+
} & RechartsPrimitive.DefaultLegendContentProps) {
|
|
324
|
+
const { config } = useChart();
|
|
325
|
+
|
|
326
|
+
if (!payload?.length) {
|
|
327
|
+
return null;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return (
|
|
331
|
+
<div
|
|
332
|
+
className={cn(
|
|
333
|
+
'flex items-center justify-center gap-4',
|
|
334
|
+
verticalAlign === 'top' ? 'pb-3' : 'pt-3',
|
|
335
|
+
className,
|
|
336
|
+
)}
|
|
337
|
+
>
|
|
338
|
+
{payload
|
|
339
|
+
.filter((item) => item.type !== 'none')
|
|
340
|
+
.map((item, index) => {
|
|
341
|
+
const key = `${nameKey ?? item.dataKey ?? 'value'}`;
|
|
342
|
+
const itemConfig = getPayloadConfigFromPayload(config, item, key);
|
|
343
|
+
|
|
344
|
+
return (
|
|
345
|
+
<div
|
|
346
|
+
key={index}
|
|
347
|
+
className={cn(
|
|
348
|
+
'flex items-center gap-1.5 [&>svg]:h-3 [&>svg]:w-3 [&>svg]:text-muted-foreground',
|
|
349
|
+
)}
|
|
350
|
+
>
|
|
351
|
+
{itemConfig?.icon && !hideIcon ? (
|
|
352
|
+
<itemConfig.icon />
|
|
353
|
+
) : (
|
|
354
|
+
<div
|
|
355
|
+
className="h-2 w-2 shrink-0 rounded-[2px]"
|
|
356
|
+
style={{
|
|
357
|
+
backgroundColor: item.color,
|
|
358
|
+
}}
|
|
359
|
+
/>
|
|
360
|
+
)}
|
|
361
|
+
{itemConfig?.label}
|
|
362
|
+
</div>
|
|
363
|
+
);
|
|
364
|
+
})}
|
|
365
|
+
</div>
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// Helper to extract item config from a payload.
|
|
370
|
+
function getPayloadConfigFromPayload(config: ChartConfig, payload: unknown, key: string) {
|
|
371
|
+
if (typeof payload !== 'object' || payload === null) {
|
|
372
|
+
return undefined;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const payloadPayload =
|
|
376
|
+
'payload' in payload && typeof payload.payload === 'object' && payload.payload !== null
|
|
377
|
+
? payload.payload
|
|
378
|
+
: undefined;
|
|
379
|
+
|
|
380
|
+
let configLabelKey: string = key;
|
|
381
|
+
|
|
382
|
+
if (key in payload && typeof payload[key as keyof typeof payload] === 'string') {
|
|
383
|
+
configLabelKey = payload[key as keyof typeof payload] as string;
|
|
384
|
+
} else if (
|
|
385
|
+
payloadPayload &&
|
|
386
|
+
key in payloadPayload &&
|
|
387
|
+
typeof payloadPayload[key as keyof typeof payloadPayload] === 'string'
|
|
388
|
+
) {
|
|
389
|
+
configLabelKey = payloadPayload[key as keyof typeof payloadPayload] as string;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
return configLabelKey in config ? config[configLabelKey] : config[key];
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/* ---- the card every chart ships in --------------------------------------- */
|
|
62
396
|
|
|
63
397
|
function ChartCard({ className, ...props }: React.ComponentProps<'div'>) {
|
|
64
398
|
return (
|
|
@@ -131,19 +465,12 @@ function ChartCardFooter({ className, ...props }: React.ComponentProps<'div'>) {
|
|
|
131
465
|
);
|
|
132
466
|
}
|
|
133
467
|
|
|
134
|
-
/* ----
|
|
468
|
+
/* ---- micro-visuals (hand-rolled SVG on purpose) --------------------------- */
|
|
135
469
|
|
|
136
470
|
/*
|
|
137
|
-
* Monotone cubic (Fritsch–Carlson) through the points
|
|
138
|
-
*
|
|
139
|
-
*
|
|
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.
|
|
471
|
+
* Monotone cubic (Fritsch–Carlson) through the points — the tangent is zeroed
|
|
472
|
+
* at a local extremum so the curve cannot sail past a peak or trough, and
|
|
473
|
+
* elsewhere clamped to 3× the smaller neighbouring slope.
|
|
147
474
|
*/
|
|
148
475
|
function monotonePath(points: Array<[number, number]>) {
|
|
149
476
|
if (points.length < 2) return '';
|
|
@@ -182,1045 +509,6 @@ function monotonePath(points: Array<[number, number]>) {
|
|
|
182
509
|
return d.join(' ');
|
|
183
510
|
}
|
|
184
511
|
|
|
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
512
|
function Sparkline({
|
|
1225
513
|
values,
|
|
1226
514
|
width = 88,
|
|
@@ -1321,73 +609,95 @@ function ProgressRing({
|
|
|
1321
609
|
);
|
|
1322
610
|
}
|
|
1323
611
|
|
|
1324
|
-
/*
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
style={{ background: seriesColor(index, tone) }}
|
|
1356
|
-
/>
|
|
1357
|
-
{children}
|
|
1358
|
-
</span>
|
|
1359
|
-
);
|
|
1360
|
-
}
|
|
612
|
+
/*
|
|
613
|
+
* THE RECHARTS PRIMITIVES ARE RE-EXPORTED FROM HERE, and a consumer must take
|
|
614
|
+
* them from the kit rather than importing 'recharts' itself.
|
|
615
|
+
*
|
|
616
|
+
* This is a federation constraint, not a style preference. A federated app
|
|
617
|
+
* takes @rise-x/ui from the host share scope (`singleton: true, import: false`),
|
|
618
|
+
* so ChartContainer and ChartTooltip come from the HOST's recharts. An app that
|
|
619
|
+
* also writes `import { BarChart } from 'recharts'` bundles a SECOND copy into
|
|
620
|
+
* its remote, and recharts 3 wires chart state through React context plus an
|
|
621
|
+
* internal store — a Tooltip from one instance is never registered with a chart
|
|
622
|
+
* from the other, so it silently does nothing (and the remote ships a few
|
|
623
|
+
* hundred KB twice). Standalone dev makes it certain: webpack.local.config.js
|
|
624
|
+
* aliases @rise-x/ui to the prebuilt standalone bundle, which has its own copy.
|
|
625
|
+
*
|
|
626
|
+
* Re-exporting keeps every chart on one instance and keeps apps inside the
|
|
627
|
+
* @rise-x/apps-sdk-only contract in AGENTS.md. `no-restricted-imports` in
|
|
628
|
+
* Shell/eslint.config.js enforces it, because a comment did not.
|
|
629
|
+
*
|
|
630
|
+
* THIS LIST IS THE CEILING, so treat it as one. It is everything a caller can
|
|
631
|
+
* draw, and the answer when a primitive is missing is to ADD IT HERE — one line
|
|
632
|
+
* plus a version bump — never to reach for 'recharts' in the app. Recharts'
|
|
633
|
+
* `Label`, `Tooltip` and `Legend` are the only names that cannot come through
|
|
634
|
+
* under their own names: the kit already exports a form `Label`, and `Tooltip`/`Legend`
|
|
635
|
+
* are re-exported ABOVE as `ChartTooltip`/`ChartLegend` — bare aliases, not
|
|
636
|
+
* wrappers. The wrapped pieces are `ChartTooltipContent`/`ChartLegendContent`,
|
|
637
|
+
* which you pass to them as `content`.
|
|
638
|
+
*/
|
|
639
|
+
/* Recharts' Label is aliased: the kit already exports a form Label, and a
|
|
640
|
+
* barrel cannot carry both names. ChartLabel is the one that goes inside a
|
|
641
|
+
* chart (a donut's centre figure, an axis caption). */
|
|
642
|
+
export { Label as ChartLabel } from 'recharts';
|
|
1361
643
|
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
644
|
+
export {
|
|
645
|
+
Area,
|
|
646
|
+
AreaChart,
|
|
647
|
+
Bar,
|
|
648
|
+
BarChart,
|
|
649
|
+
Brush,
|
|
650
|
+
CartesianGrid,
|
|
651
|
+
Cell,
|
|
652
|
+
/* bars and a line on one axis pair — the first thing a mixed chart needs */
|
|
653
|
+
ComposedChart,
|
|
654
|
+
Customized,
|
|
655
|
+
ErrorBar,
|
|
656
|
+
Funnel,
|
|
657
|
+
FunnelChart,
|
|
658
|
+
LabelList,
|
|
659
|
+
Line,
|
|
660
|
+
LineChart,
|
|
661
|
+
Pie,
|
|
662
|
+
PieChart,
|
|
663
|
+
PolarAngleAxis,
|
|
664
|
+
PolarGrid,
|
|
665
|
+
PolarRadiusAxis,
|
|
666
|
+
Radar,
|
|
667
|
+
RadarChart,
|
|
668
|
+
RadialBar,
|
|
669
|
+
RadialBarChart,
|
|
670
|
+
/* Rectangle and Sector carry shadcn's own activeBar / activeShape examples,
|
|
671
|
+
* so "any example from ui.shadcn.com/charts pastes in" needs them. */
|
|
672
|
+
Rectangle,
|
|
673
|
+
ReferenceArea,
|
|
674
|
+
ReferenceDot,
|
|
675
|
+
ReferenceLine,
|
|
676
|
+
ResponsiveContainer,
|
|
677
|
+
Scatter,
|
|
678
|
+
ScatterChart,
|
|
679
|
+
Sector,
|
|
680
|
+
Treemap,
|
|
681
|
+
XAxis,
|
|
682
|
+
YAxis,
|
|
683
|
+
ZAxis,
|
|
684
|
+
} from 'recharts';
|
|
1371
685
|
|
|
1372
686
|
export {
|
|
687
|
+
ChartContainer,
|
|
688
|
+
ChartTooltip,
|
|
689
|
+
ChartTooltipContent,
|
|
690
|
+
ChartLegend,
|
|
691
|
+
ChartLegendContent,
|
|
692
|
+
ChartStyle,
|
|
1373
693
|
ChartCard,
|
|
1374
694
|
ChartCardHeader,
|
|
1375
695
|
ChartCardTitle,
|
|
1376
696
|
ChartCardDescription,
|
|
1377
697
|
ChartCardActions,
|
|
1378
698
|
ChartCardFooter,
|
|
1379
|
-
LineChart,
|
|
1380
|
-
AreaChart,
|
|
1381
|
-
BarChart,
|
|
1382
|
-
PieChart,
|
|
1383
|
-
DonutChart,
|
|
1384
|
-
RadialChart,
|
|
1385
|
-
RadarChart,
|
|
1386
699
|
Sparkline,
|
|
1387
700
|
ProgressRing,
|
|
1388
|
-
ChartLegend,
|
|
1389
|
-
ChartLegendItem,
|
|
1390
|
-
ChartTooltip,
|
|
1391
701
|
CHART_SERIES,
|
|
1392
702
|
seriesColor,
|
|
1393
703
|
type SemanticTone,
|