@xemahq/ui-kernel 0.6.0 → 0.6.1
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/dist/lib/biome-host/define-web-biome.d.ts +3 -2
- package/dist/lib/biome-host/define-web-biome.d.ts.map +1 -1
- package/dist/lib/biome-host/define-web-biome.js +23 -1
- package/dist/lib/biome-host/define-web-biome.js.map +1 -1
- package/dist/lib/biome-host/frontend-biome.d.ts +4 -1
- package/dist/lib/biome-host/frontend-biome.d.ts.map +1 -1
- package/dist/lib/biome-host/host-bridge.d.ts +2 -0
- package/dist/lib/biome-host/host-bridge.d.ts.map +1 -1
- package/dist/lib/biome-host/host-bridge.js.map +1 -1
- package/dist/lib/biome-host/i18n-hooks.d.ts +4 -0
- package/dist/lib/biome-host/i18n-hooks.d.ts.map +1 -0
- package/dist/lib/biome-host/i18n-hooks.js +22 -0
- package/dist/lib/biome-host/i18n-hooks.js.map +1 -0
- package/dist/lib/biome-host/i18n-port.d.ts +15 -0
- package/dist/lib/biome-host/i18n-port.d.ts.map +1 -0
- package/dist/lib/biome-host/i18n-port.js +29 -0
- package/dist/lib/biome-host/i18n-port.js.map +1 -0
- package/dist/lib/biome-host/index.d.ts +2 -0
- package/dist/lib/biome-host/index.d.ts.map +1 -1
- package/dist/lib/biome-host/index.js +2 -0
- package/dist/lib/biome-host/index.js.map +1 -1
- package/dist/lib/biome-host/realtime-port.d.ts +1 -1
- package/dist/lib/biome-host/realtime-port.d.ts.map +1 -1
- package/dist/session-kit/approvals/approval-model.d.ts.map +1 -1
- package/dist/session-kit/approvals/approval-model.js.map +1 -1
- package/dist/ui/branding/app-branding-provider.d.ts +17 -0
- package/dist/ui/branding/app-branding-provider.d.ts.map +1 -0
- package/dist/ui/branding/app-branding-provider.js +81 -0
- package/dist/ui/branding/app-branding-provider.js.map +1 -0
- package/dist/ui/branding/sanitize.d.ts +4 -0
- package/dist/ui/branding/sanitize.d.ts.map +1 -0
- package/dist/ui/branding/sanitize.js +47 -0
- package/dist/ui/branding/sanitize.js.map +1 -0
- package/dist/ui/breakpoints.d.ts +2 -0
- package/dist/ui/breakpoints.d.ts.map +1 -0
- package/dist/ui/breakpoints.js +5 -0
- package/dist/ui/breakpoints.js.map +1 -0
- package/dist/ui/charts/bar-chart.d.ts +7 -0
- package/dist/ui/charts/bar-chart.d.ts.map +1 -0
- package/dist/ui/charts/bar-chart.js +47 -0
- package/dist/ui/charts/bar-chart.js.map +1 -0
- package/dist/ui/charts/chart-frame.d.ts +25 -0
- package/dist/ui/charts/chart-frame.d.ts.map +1 -0
- package/dist/ui/charts/chart-frame.js +23 -0
- package/dist/ui/charts/chart-frame.js.map +1 -0
- package/dist/ui/charts/chart-types.d.ts +19 -0
- package/dist/ui/charts/chart-types.d.ts.map +1 -0
- package/dist/ui/charts/chart-types.js +3 -0
- package/dist/ui/charts/chart-types.js.map +1 -0
- package/dist/ui/charts/chart-utils.d.ts +22 -0
- package/dist/ui/charts/chart-utils.d.ts.map +1 -0
- package/dist/ui/charts/chart-utils.js +97 -0
- package/dist/ui/charts/chart-utils.js.map +1 -0
- package/dist/ui/charts/donut-chart.d.ts +7 -0
- package/dist/ui/charts/donut-chart.d.ts.map +1 -0
- package/dist/ui/charts/donut-chart.js +55 -0
- package/dist/ui/charts/donut-chart.js.map +1 -0
- package/dist/ui/charts/index.d.ts +8 -0
- package/dist/ui/charts/index.d.ts.map +1 -0
- package/dist/ui/charts/index.js +21 -0
- package/dist/ui/charts/index.js.map +1 -0
- package/dist/ui/charts/line-chart.d.ts +7 -0
- package/dist/ui/charts/line-chart.d.ts.map +1 -0
- package/dist/ui/charts/line-chart.js +70 -0
- package/dist/ui/charts/line-chart.js.map +1 -0
- package/dist/ui/charts/sparkline.d.ts +13 -0
- package/dist/ui/charts/sparkline.d.ts.map +1 -0
- package/dist/ui/charts/sparkline.js +39 -0
- package/dist/ui/charts/sparkline.js.map +1 -0
- package/dist/ui/hooks/use-is-mobile.d.ts +2 -0
- package/dist/ui/hooks/use-is-mobile.d.ts.map +1 -0
- package/dist/ui/hooks/use-is-mobile.js +19 -0
- package/dist/ui/hooks/use-is-mobile.js.map +1 -0
- package/dist/ui/index.d.ts +6 -0
- package/dist/ui/index.d.ts.map +1 -1
- package/dist/ui/index.js +14 -1
- package/dist/ui/index.js.map +1 -1
- package/dist/ui/pdf/pdf-preview.d.ts +8 -0
- package/dist/ui/pdf/pdf-preview.d.ts.map +1 -0
- package/dist/ui/pdf/pdf-preview.js +34 -0
- package/dist/ui/pdf/pdf-preview.js.map +1 -0
- package/dist/ui/primitives/preview-device-frame.d.ts +9 -0
- package/dist/ui/primitives/preview-device-frame.d.ts.map +1 -0
- package/dist/ui/primitives/preview-device-frame.js +90 -0
- package/dist/ui/primitives/preview-device-frame.js.map +1 -0
- package/package.json +8 -7
- package/src/lib/biome-host/define-web-biome.test.js +93 -0
- package/src/lib/biome-host/define-web-biome.ts +71 -4
- package/src/lib/biome-host/frontend-biome.ts +45 -4
- package/src/lib/biome-host/host-bridge.ts +13 -0
- package/src/lib/biome-host/i18n-hooks.ts +50 -0
- package/src/lib/biome-host/i18n-port.test.ts +50 -0
- package/src/lib/biome-host/i18n-port.ts +96 -0
- package/src/lib/biome-host/index.ts +2 -0
- package/src/lib/biome-host/realtime-port.ts +9 -3
- package/src/lib/system-bus/enums.ts +1 -1
- package/src/lib/system-bus/index.ts +1 -1
- package/src/lib/system-bus/intents.ts +1 -1
- package/src/session-kit/approvals/ApprovalCard.tsx +1 -1
- package/src/session-kit/approvals/ApprovalsCenter.tsx +2 -2
- package/src/session-kit/approvals/CapabilityApprovalStickyBar.tsx +1 -1
- package/src/session-kit/approvals/Obligation.tsx +1 -1
- package/src/session-kit/approvals/ScopedBody.tsx +1 -1
- package/src/session-kit/approvals/approval-model.ts +7 -8
- package/src/session-kit/approvals/index.ts +1 -1
- package/src/session-kit/approvals/obligation-display.ts +1 -1
- package/src/session-kit/approvals/risk-accent.ts +1 -1
- package/src/session-kit/combobox/Combobox.tsx +1 -1
- package/src/session-kit/display/MessageTurn.tsx +1 -1
- package/src/session-kit/display/TokenMeter.tsx +1 -1
- package/src/session-kit/index.ts +3 -3
- package/src/session-kit/lib/enums.ts +1 -1
- package/src/session-kit/lib/status-dot.ts +1 -1
- package/src/session-kit/primitives/RiskPill.tsx +1 -1
- package/src/ui/branding/app-branding-provider.tsx +146 -0
- package/src/ui/branding/sanitize.ts +77 -0
- package/src/ui/breakpoints.ts +14 -0
- package/src/ui/charts/bar-chart.tsx +135 -0
- package/src/ui/charts/chart-frame.tsx +112 -0
- package/src/ui/charts/chart-types.ts +52 -0
- package/src/ui/charts/chart-utils.ts +158 -0
- package/src/ui/charts/charts.test.tsx +204 -0
- package/src/ui/charts/donut-chart.tsx +111 -0
- package/src/ui/charts/index.ts +27 -0
- package/src/ui/charts/line-chart.tsx +180 -0
- package/src/ui/charts/sparkline.tsx +101 -0
- package/src/ui/hooks/use-is-mobile.ts +28 -0
- package/src/ui/index.ts +22 -0
- package/src/ui/pdf/pdf-preview.tsx +122 -0
- package/src/ui/primitives/preview-device-frame.tsx +205 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
2
|
+
// BarChart — categorical bars, pure SVG (Wave 9.2)
|
|
3
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
4
|
+
import type { ReactNode } from 'react';
|
|
5
|
+
|
|
6
|
+
import { ChartEmpty, ChartFrame, type ChartLegendItem } from './chart-frame';
|
|
7
|
+
import type { ChartBaseProps, ChartDatum } from './chart-types';
|
|
8
|
+
import {
|
|
9
|
+
buildAriaSummary,
|
|
10
|
+
chartColorAt,
|
|
11
|
+
defaultFormatValue,
|
|
12
|
+
finitePoints,
|
|
13
|
+
niceScale,
|
|
14
|
+
} from './chart-utils';
|
|
15
|
+
|
|
16
|
+
// Internal coordinate space — the `viewBox`. `width="100%"` makes the SVG scale
|
|
17
|
+
// to its container (D20 fluid), so these are ratios, not device pixels.
|
|
18
|
+
const VIEW_W = 640;
|
|
19
|
+
const VIEW_H = 360;
|
|
20
|
+
const PAD = { top: 12, right: 12, bottom: 44, left: 48 } as const;
|
|
21
|
+
const PLOT_L = PAD.left;
|
|
22
|
+
const PLOT_R = VIEW_W - PAD.right;
|
|
23
|
+
const PLOT_T = PAD.top;
|
|
24
|
+
const PLOT_B = VIEW_H - PAD.bottom;
|
|
25
|
+
const PLOT_W = PLOT_R - PLOT_L;
|
|
26
|
+
const PLOT_H = PLOT_B - PLOT_T;
|
|
27
|
+
|
|
28
|
+
export interface BarChartProps extends ChartBaseProps {
|
|
29
|
+
/** The bars — one per labelled datum. */
|
|
30
|
+
readonly data: readonly ChartDatum[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function BarChart({
|
|
34
|
+
data,
|
|
35
|
+
title,
|
|
36
|
+
ariaLabel,
|
|
37
|
+
formatValue,
|
|
38
|
+
className,
|
|
39
|
+
emptyLabel = 'No data',
|
|
40
|
+
}: BarChartProps): ReactNode {
|
|
41
|
+
const format = formatValue ?? defaultFormatValue;
|
|
42
|
+
const points = finitePoints(data);
|
|
43
|
+
|
|
44
|
+
if (points.length === 0) {
|
|
45
|
+
return <ChartEmpty title={title} label={emptyLabel} className={className} />;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const values = points.map((p) => p.value);
|
|
49
|
+
const scale = niceScale(Math.min(...values), Math.max(...values));
|
|
50
|
+
const span = scale.max - scale.min;
|
|
51
|
+
const yToPx = (v: number): number => PLOT_B - ((v - scale.min) / span) * PLOT_H;
|
|
52
|
+
const baseline = yToPx(0);
|
|
53
|
+
|
|
54
|
+
const band = PLOT_W / points.length;
|
|
55
|
+
const barW = Math.min(band * 0.62, 72);
|
|
56
|
+
|
|
57
|
+
const legend: ChartLegendItem[] = points.map((p, i) => ({
|
|
58
|
+
label: p.label,
|
|
59
|
+
value: format(p.value),
|
|
60
|
+
color: chartColorAt(i, p.color),
|
|
61
|
+
}));
|
|
62
|
+
|
|
63
|
+
const summary = ariaLabel ?? buildAriaSummary(title, points, format, 'bar');
|
|
64
|
+
|
|
65
|
+
const plot = (
|
|
66
|
+
<svg
|
|
67
|
+
role="img"
|
|
68
|
+
aria-label={summary}
|
|
69
|
+
width="100%"
|
|
70
|
+
viewBox={`0 0 ${VIEW_W} ${VIEW_H}`}
|
|
71
|
+
preserveAspectRatio="xMidYMid meet"
|
|
72
|
+
className="block h-auto w-full overflow-visible text-ink-4"
|
|
73
|
+
>
|
|
74
|
+
{/* y gridlines + tick labels */}
|
|
75
|
+
{scale.ticks.map((t) => {
|
|
76
|
+
const y = yToPx(t);
|
|
77
|
+
return (
|
|
78
|
+
<g key={`y-${t}`}>
|
|
79
|
+
<line
|
|
80
|
+
x1={PLOT_L}
|
|
81
|
+
x2={PLOT_R}
|
|
82
|
+
y1={y}
|
|
83
|
+
y2={y}
|
|
84
|
+
stroke="currentColor"
|
|
85
|
+
strokeOpacity={t === 0 ? 0.35 : 0.12}
|
|
86
|
+
/>
|
|
87
|
+
<text
|
|
88
|
+
x={PLOT_L - 8}
|
|
89
|
+
y={y}
|
|
90
|
+
textAnchor="end"
|
|
91
|
+
dominantBaseline="middle"
|
|
92
|
+
fontSize={12}
|
|
93
|
+
fill="currentColor"
|
|
94
|
+
>
|
|
95
|
+
{format(t)}
|
|
96
|
+
</text>
|
|
97
|
+
</g>
|
|
98
|
+
);
|
|
99
|
+
})}
|
|
100
|
+
|
|
101
|
+
{/* bars + x labels */}
|
|
102
|
+
{points.map((p, i) => {
|
|
103
|
+
const cx = PLOT_L + band * i + band / 2;
|
|
104
|
+
const yv = yToPx(p.value);
|
|
105
|
+
const top = Math.min(yv, baseline);
|
|
106
|
+
const h = Math.max(1, Math.abs(baseline - yv));
|
|
107
|
+
return (
|
|
108
|
+
<g key={`bar-${p.label}-${i}`}>
|
|
109
|
+
<rect
|
|
110
|
+
x={cx - barW / 2}
|
|
111
|
+
y={top}
|
|
112
|
+
width={barW}
|
|
113
|
+
height={h}
|
|
114
|
+
rx={3}
|
|
115
|
+
fill={chartColorAt(i, p.color)}
|
|
116
|
+
>
|
|
117
|
+
<title>{`${p.label}: ${format(p.value)}`}</title>
|
|
118
|
+
</rect>
|
|
119
|
+
<text
|
|
120
|
+
x={cx}
|
|
121
|
+
y={PLOT_B + 16}
|
|
122
|
+
textAnchor="middle"
|
|
123
|
+
fontSize={12}
|
|
124
|
+
fill="currentColor"
|
|
125
|
+
>
|
|
126
|
+
{p.label}
|
|
127
|
+
</text>
|
|
128
|
+
</g>
|
|
129
|
+
);
|
|
130
|
+
})}
|
|
131
|
+
</svg>
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
return <ChartFrame plot={plot} legend={legend} className={className} />;
|
|
135
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
2
|
+
// Chart primitives — shared layout frame, legend and empty state (Wave 9.2)
|
|
3
|
+
//
|
|
4
|
+
// `ChartFrame` owns the D20 responsive contract: the plot is a fluid container
|
|
5
|
+
// and the legend sits BESIDE it on wide viewports but MOVES BELOW the plot
|
|
6
|
+
// below `md`. That decision is driven by `useIsMobile()` — which reads the one
|
|
7
|
+
// shared `MOBILE_BREAKPOINT` token — never a second breakpoint.
|
|
8
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
9
|
+
import type { ReactNode } from 'react';
|
|
10
|
+
|
|
11
|
+
import { cn } from '../cn';
|
|
12
|
+
import { useIsMobile } from '../hooks/use-is-mobile';
|
|
13
|
+
|
|
14
|
+
/** One row of the legend: a color swatch + its label + (optional) value. */
|
|
15
|
+
export interface ChartLegendItem {
|
|
16
|
+
readonly label: string;
|
|
17
|
+
readonly color: string;
|
|
18
|
+
readonly value?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface ChartLegendProps {
|
|
22
|
+
readonly items: readonly ChartLegendItem[];
|
|
23
|
+
/** `below` stacks items into a wrapping row; `beside` stacks them vertically. */
|
|
24
|
+
readonly layout: 'beside' | 'below';
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The legend. Meaning is carried by the text label + value — the swatch is a
|
|
29
|
+
* secondary cue, so a color-blind or monochrome reader loses nothing.
|
|
30
|
+
*/
|
|
31
|
+
export function ChartLegend({ items, layout }: ChartLegendProps): ReactNode {
|
|
32
|
+
if (items.length === 0) return null;
|
|
33
|
+
return (
|
|
34
|
+
<ul
|
|
35
|
+
className={cn(
|
|
36
|
+
'm-0 list-none p-0 text-body-2 text-ink-3',
|
|
37
|
+
layout === 'below'
|
|
38
|
+
? 'flex flex-row flex-wrap gap-x-4 gap-y-1.5'
|
|
39
|
+
: 'flex flex-col gap-1.5',
|
|
40
|
+
)}
|
|
41
|
+
>
|
|
42
|
+
{items.map((item, i) => (
|
|
43
|
+
<li key={`${item.label}-${i}`} className="flex items-center gap-2">
|
|
44
|
+
<span
|
|
45
|
+
aria-hidden="true"
|
|
46
|
+
className="inline-block h-2.5 w-2.5 shrink-0 rounded-sm"
|
|
47
|
+
style={{ backgroundColor: item.color }}
|
|
48
|
+
/>
|
|
49
|
+
<span className="text-ink-3">{item.label}</span>
|
|
50
|
+
{item.value !== undefined && (
|
|
51
|
+
<span className="ml-auto font-medium tabular-nums text-ink">{item.value}</span>
|
|
52
|
+
)}
|
|
53
|
+
</li>
|
|
54
|
+
))}
|
|
55
|
+
</ul>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface ChartFrameProps {
|
|
60
|
+
/** The plot itself — a fluid `<svg>` with `width="100%"` + a `viewBox`. */
|
|
61
|
+
readonly plot: ReactNode;
|
|
62
|
+
readonly legend: readonly ChartLegendItem[];
|
|
63
|
+
readonly className?: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Wraps a plot + legend with the responsive D20 layout. Uses `useIsMobile()`
|
|
68
|
+
* so below `md` the legend renders BELOW the plot; at/above `md` it sits to the
|
|
69
|
+
* right. One token (`MOBILE_BREAKPOINT`), no second breakpoint.
|
|
70
|
+
*/
|
|
71
|
+
export function ChartFrame({ plot, legend, className }: ChartFrameProps): ReactNode {
|
|
72
|
+
const isMobile = useIsMobile();
|
|
73
|
+
return (
|
|
74
|
+
<div className={cn('w-full min-w-0 text-ink-3', className)}>
|
|
75
|
+
<div
|
|
76
|
+
className={cn(
|
|
77
|
+
'flex min-w-0 gap-4',
|
|
78
|
+
isMobile ? 'flex-col' : 'flex-row items-start',
|
|
79
|
+
)}
|
|
80
|
+
>
|
|
81
|
+
<div className="min-w-0 flex-1">{plot}</div>
|
|
82
|
+
<ChartLegend items={legend} layout={isMobile ? 'below' : 'beside'} />
|
|
83
|
+
</div>
|
|
84
|
+
</div>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
interface ChartEmptyProps {
|
|
89
|
+
readonly title: string;
|
|
90
|
+
readonly label: string;
|
|
91
|
+
readonly className?: string;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Explicit empty state — rendered instead of a broken/NaN SVG when a series has
|
|
96
|
+
* no finite points. Still carries `role="img"` + an `aria-label` so assistive
|
|
97
|
+
* tech announces the (empty) chart rather than a silent gap.
|
|
98
|
+
*/
|
|
99
|
+
export function ChartEmpty({ title, label, className }: ChartEmptyProps): ReactNode {
|
|
100
|
+
return (
|
|
101
|
+
<div
|
|
102
|
+
role="img"
|
|
103
|
+
aria-label={`${title}: ${label}`}
|
|
104
|
+
className={cn(
|
|
105
|
+
'flex w-full items-center justify-center rounded-lg border border-dashed border-border/60 bg-muted/20 px-4 py-10 text-body-2 text-ink-4',
|
|
106
|
+
className,
|
|
107
|
+
)}
|
|
108
|
+
>
|
|
109
|
+
{label}
|
|
110
|
+
</div>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
2
|
+
// Chart primitives — public typed contracts (Wave 9.2)
|
|
3
|
+
//
|
|
4
|
+
// Pure-SVG chart primitives shipped by the kernel (NO external chart library).
|
|
5
|
+
// These are the closed, typed shapes every chart primitive consumes. No `any`.
|
|
6
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
7
|
+
|
|
8
|
+
/** A single labelled numeric point. The atom of every chart series. */
|
|
9
|
+
export interface ChartDatum {
|
|
10
|
+
/** Human-readable category / x-axis label. Always present (never color-only). */
|
|
11
|
+
readonly label: string;
|
|
12
|
+
/** The numeric value. Non-finite values (NaN/Infinity) are skipped, never plotted. */
|
|
13
|
+
readonly value: number;
|
|
14
|
+
/**
|
|
15
|
+
* Optional per-datum color override — any CSS color, `currentColor`, or a
|
|
16
|
+
* `var(--…)` token. When omitted the themeable palette assigns one by index.
|
|
17
|
+
*/
|
|
18
|
+
readonly color?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A named line series over a shared set of x-labels (LineChart). */
|
|
22
|
+
export interface ChartLineSeries {
|
|
23
|
+
/** Series name — shown in the legend and the aria summary. */
|
|
24
|
+
readonly name: string;
|
|
25
|
+
/** The ordered points of this line. */
|
|
26
|
+
readonly points: readonly ChartDatum[];
|
|
27
|
+
/** Optional color override (CSS color / `currentColor` / `var(--…)`). */
|
|
28
|
+
readonly color?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Formats a raw numeric value for axis ticks, legend and the aria summary. */
|
|
32
|
+
export type ChartValueFormatter = (value: number) => string;
|
|
33
|
+
|
|
34
|
+
/** Props shared by every chart primitive. */
|
|
35
|
+
export interface ChartBaseProps {
|
|
36
|
+
/**
|
|
37
|
+
* Chart title — the spine of the `aria-label` summary and (optionally) a
|
|
38
|
+
* visible caption. Required: an accessible chart always announces what it is.
|
|
39
|
+
*/
|
|
40
|
+
readonly title: string;
|
|
41
|
+
/**
|
|
42
|
+
* Overrides the auto-generated `aria-label`. When omitted, each primitive
|
|
43
|
+
* builds a deterministic summary from `title` + the data.
|
|
44
|
+
*/
|
|
45
|
+
readonly ariaLabel?: string;
|
|
46
|
+
/** Formats values in ticks / legend / summary. Deterministic by contract. */
|
|
47
|
+
readonly formatValue?: ChartValueFormatter;
|
|
48
|
+
/** Extra classes for the outermost wrapper. */
|
|
49
|
+
readonly className?: string;
|
|
50
|
+
/** Label rendered by the empty state when the series has no finite points. */
|
|
51
|
+
readonly emptyLabel?: string;
|
|
52
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
2
|
+
// Chart primitives — deterministic, pure helpers (Wave 9.2)
|
|
3
|
+
//
|
|
4
|
+
// Scale/tick maths, the themeable color palette, value formatting, finite-value
|
|
5
|
+
// filtering and aria-summary construction. Everything here is a pure function:
|
|
6
|
+
// given the same input it returns the same output (no Math.random, no Date.now)
|
|
7
|
+
// so the same data always yields the same SVG.
|
|
8
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
9
|
+
import type { ChartDatum, ChartValueFormatter } from './chart-types';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Themeable categorical palette. Each slot is a CSS custom property with a
|
|
13
|
+
* graceful fallback: a host that defines `--chart-N` (per theme) wins; slot 0
|
|
14
|
+
* additionally follows the app brand via `--app-primary-color`. The fallbacks
|
|
15
|
+
* are the platform's bold, dark-mode-safe primaries — used only when no token
|
|
16
|
+
* is defined, so a chart is never invisible. Data is NEVER conveyed by color
|
|
17
|
+
* alone: every primitive also renders the label + value.
|
|
18
|
+
*/
|
|
19
|
+
export const CHART_PALETTE: readonly string[] = [
|
|
20
|
+
'var(--app-primary-color, var(--chart-1, #00A4EF))',
|
|
21
|
+
'var(--chart-2, #7FBA00)',
|
|
22
|
+
'var(--chart-3, #FFB900)',
|
|
23
|
+
'var(--chart-4, #F25022)',
|
|
24
|
+
'var(--chart-5, #8B5CF6)',
|
|
25
|
+
'var(--chart-6, #EC4899)',
|
|
26
|
+
'var(--chart-7, #06B6D4)',
|
|
27
|
+
'var(--chart-8, #F59E0B)',
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
/** Resolve the color for the datum at `index`: explicit override wins, else palette (wraps). */
|
|
31
|
+
export function chartColorAt(index: number, override?: string): string {
|
|
32
|
+
if (override !== undefined && override.length > 0) return override;
|
|
33
|
+
return CHART_PALETTE[index % CHART_PALETTE.length];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Deterministic default value formatter. Integers render bare; fractions get a
|
|
38
|
+
* fixed 2 decimals (with trailing zeros trimmed). Locale-free on purpose so the
|
|
39
|
+
* SVG is byte-identical across machines/locales.
|
|
40
|
+
*/
|
|
41
|
+
export function defaultFormatValue(value: number): string {
|
|
42
|
+
if (!Number.isFinite(value)) return '—';
|
|
43
|
+
if (Number.isInteger(value)) return String(value);
|
|
44
|
+
return String(Number(value.toFixed(2)));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A datum whose value is a finite number (post-filter). */
|
|
48
|
+
export interface FiniteDatum {
|
|
49
|
+
readonly label: string;
|
|
50
|
+
readonly value: number;
|
|
51
|
+
readonly color?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Keep only points with a finite numeric value — NaN/±Infinity are dropped, never plotted. */
|
|
55
|
+
export function finitePoints(points: readonly ChartDatum[]): FiniteDatum[] {
|
|
56
|
+
const out: FiniteDatum[] = [];
|
|
57
|
+
for (const p of points) {
|
|
58
|
+
if (Number.isFinite(p.value)) {
|
|
59
|
+
out.push({ label: p.label, value: p.value, color: p.color });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ── nice-number axis scale (deterministic) ──────────────────────────────────
|
|
66
|
+
|
|
67
|
+
function niceNum(range: number, round: boolean): number {
|
|
68
|
+
const exponent = Math.floor(Math.log10(range));
|
|
69
|
+
const pow = Math.pow(10, exponent);
|
|
70
|
+
const fraction = range / pow;
|
|
71
|
+
let niceFraction: number;
|
|
72
|
+
if (round) {
|
|
73
|
+
if (fraction < 1.5) niceFraction = 1;
|
|
74
|
+
else if (fraction < 3) niceFraction = 2;
|
|
75
|
+
else if (fraction < 7) niceFraction = 5;
|
|
76
|
+
else niceFraction = 10;
|
|
77
|
+
} else {
|
|
78
|
+
if (fraction <= 1) niceFraction = 1;
|
|
79
|
+
else if (fraction <= 2) niceFraction = 2;
|
|
80
|
+
else if (fraction <= 5) niceFraction = 5;
|
|
81
|
+
else niceFraction = 10;
|
|
82
|
+
}
|
|
83
|
+
return niceFraction * pow;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Trim binary-float dust so ticks like 0.30000000000000004 read as 0.3. */
|
|
87
|
+
function tidy(value: number): number {
|
|
88
|
+
return Number(value.toPrecision(12));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface NiceScale {
|
|
92
|
+
readonly min: number;
|
|
93
|
+
readonly max: number;
|
|
94
|
+
readonly ticks: readonly number[];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface NiceScaleOptions {
|
|
98
|
+
/**
|
|
99
|
+
* Pin the baseline at 0 so heights are proportional (bars, donuts). When
|
|
100
|
+
* `false` the domain hugs the data — better for line charts over a narrow
|
|
101
|
+
* band. Defaults to `true`.
|
|
102
|
+
*/
|
|
103
|
+
readonly pinZero?: boolean;
|
|
104
|
+
readonly maxTicks?: number;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Compute a "nice" axis domain + evenly-spaced ticks for `[dataMin, dataMax]`.
|
|
109
|
+
* With `pinZero` (default) the baseline is pinned at 0 so bar heights are
|
|
110
|
+
* proportional and negatives read below the axis. Fully deterministic; guards
|
|
111
|
+
* the degenerate all-equal / all-zero case instead of producing NaN.
|
|
112
|
+
*/
|
|
113
|
+
export function niceScale(
|
|
114
|
+
dataMin: number,
|
|
115
|
+
dataMax: number,
|
|
116
|
+
options: NiceScaleOptions = {},
|
|
117
|
+
): NiceScale {
|
|
118
|
+
const pinZero = options.pinZero ?? true;
|
|
119
|
+
const maxTicks = options.maxTicks ?? 5;
|
|
120
|
+
let lo = pinZero ? Math.min(0, dataMin) : dataMin;
|
|
121
|
+
let hi = pinZero ? Math.max(0, dataMax) : dataMax;
|
|
122
|
+
if (lo === hi) {
|
|
123
|
+
// Flat series (e.g. all equal): open a unit window so we never divide by 0.
|
|
124
|
+
hi = lo + 1;
|
|
125
|
+
}
|
|
126
|
+
const ticks: number[] = [];
|
|
127
|
+
const range = niceNum(hi - lo, false);
|
|
128
|
+
const step = niceNum(range / Math.max(1, maxTicks - 1), true);
|
|
129
|
+
const niceMin = Math.floor(lo / step) * step;
|
|
130
|
+
const niceMax = Math.ceil(hi / step) * step;
|
|
131
|
+
for (let v = niceMin; v <= niceMax + step * 0.5; v += step) {
|
|
132
|
+
ticks.push(tidy(v));
|
|
133
|
+
}
|
|
134
|
+
return { min: tidy(niceMin), max: tidy(niceMax), ticks };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ── aria summary ────────────────────────────────────────────────────────────
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Build a concise, deterministic aria-label summary from a title and the
|
|
141
|
+
* finite points. Keeps it readable: title, count, then up to `maxItems`
|
|
142
|
+
* "label value" pairs (with an ellipsis when truncated). Color is never the
|
|
143
|
+
* carrier of meaning — this text is.
|
|
144
|
+
*/
|
|
145
|
+
export function buildAriaSummary(
|
|
146
|
+
title: string,
|
|
147
|
+
points: readonly FiniteDatum[],
|
|
148
|
+
format: ChartValueFormatter,
|
|
149
|
+
noun: string,
|
|
150
|
+
maxItems = 8,
|
|
151
|
+
): string {
|
|
152
|
+
if (points.length === 0) return `${title}: no data.`;
|
|
153
|
+
const shown = points.slice(0, maxItems);
|
|
154
|
+
const pairs = shown.map((p) => `${p.label} ${format(p.value)}`).join(', ');
|
|
155
|
+
const suffix = points.length > maxItems ? `, and ${points.length - maxItems} more` : '';
|
|
156
|
+
const count = points.length === 1 ? `1 ${noun}` : `${points.length} ${noun}s`;
|
|
157
|
+
return `${title}: ${count}. ${pairs}${suffix}.`;
|
|
158
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
2
|
+
// Chart primitives — render proofs (Wave 9.2)
|
|
3
|
+
//
|
|
4
|
+
// Repo convention: `renderToStaticMarkup` (no @testing-library, effects DON'T
|
|
5
|
+
// run — so `useIsMobile` stays false and the desktop layout renders). We assert
|
|
6
|
+
// on the produced SVG markup: element counts, the fluid `viewBox` + width,
|
|
7
|
+
// the aria summary, the legend, and — critically — that no coordinate ever
|
|
8
|
+
// serializes as `NaN`.
|
|
9
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
10
|
+
import { renderToStaticMarkup } from 'react-dom/server';
|
|
11
|
+
import { describe, expect, it } from 'vitest';
|
|
12
|
+
|
|
13
|
+
import { BarChart } from './bar-chart';
|
|
14
|
+
import { DonutChart } from './donut-chart';
|
|
15
|
+
import { LineChart } from './line-chart';
|
|
16
|
+
import { Sparkline } from './sparkline';
|
|
17
|
+
|
|
18
|
+
const count = (markup: string, tag: string): number =>
|
|
19
|
+
markup.match(new RegExp(`<${tag}[\\s>]`, 'g'))?.length ?? 0;
|
|
20
|
+
|
|
21
|
+
describe('BarChart', () => {
|
|
22
|
+
const data = [
|
|
23
|
+
{ label: 'Q1', value: 120 },
|
|
24
|
+
{ label: 'Q2', value: 340 },
|
|
25
|
+
{ label: 'Q3', value: 210 },
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
it('renders one bar per datum, fluid SVG, legend and aria summary', () => {
|
|
29
|
+
const markup = renderToStaticMarkup(<BarChart title="Revenue" data={data} />);
|
|
30
|
+
expect(count(markup, 'rect')).toBe(3);
|
|
31
|
+
expect(markup).toContain('viewBox="0 0 640 360"');
|
|
32
|
+
expect(markup).toContain('width="100%"');
|
|
33
|
+
expect(markup).toContain('role="img"');
|
|
34
|
+
// aria summary carries the title + the values (data is never color-only).
|
|
35
|
+
expect(markup).toContain('aria-label="Revenue:');
|
|
36
|
+
expect(markup).toContain('Q2 340');
|
|
37
|
+
// legend beside the plot (desktop: effects don't run so useIsMobile=false).
|
|
38
|
+
expect(count(markup, 'li')).toBe(3);
|
|
39
|
+
expect(markup).not.toContain('NaN');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('renders an explicit empty state (no NaN) for an empty series', () => {
|
|
43
|
+
const markup = renderToStaticMarkup(<BarChart title="Revenue" data={[]} />);
|
|
44
|
+
expect(markup).toContain('role="img"');
|
|
45
|
+
expect(markup).toContain('aria-label="Revenue: No data"');
|
|
46
|
+
expect(count(markup, 'rect')).toBe(0);
|
|
47
|
+
expect(markup).not.toContain('NaN');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('skips a non-finite value instead of emitting NaN coordinates', () => {
|
|
51
|
+
const markup = renderToStaticMarkup(
|
|
52
|
+
<BarChart
|
|
53
|
+
title="Revenue"
|
|
54
|
+
data={[
|
|
55
|
+
{ label: 'Q1', value: 120 },
|
|
56
|
+
{ label: 'Q2', value: Number.NaN },
|
|
57
|
+
{ label: 'Q3', value: Number.POSITIVE_INFINITY },
|
|
58
|
+
]}
|
|
59
|
+
/>,
|
|
60
|
+
);
|
|
61
|
+
// only the one finite bar survives
|
|
62
|
+
expect(count(markup, 'rect')).toBe(1);
|
|
63
|
+
expect(markup).not.toContain('NaN');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('honours a custom value formatter', () => {
|
|
67
|
+
const markup = renderToStaticMarkup(
|
|
68
|
+
<BarChart title="Revenue" data={data} formatValue={(v) => `$${v}`} />,
|
|
69
|
+
);
|
|
70
|
+
expect(markup).toContain('$340');
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe('LineChart', () => {
|
|
75
|
+
const series = [
|
|
76
|
+
{
|
|
77
|
+
name: 'Sessions',
|
|
78
|
+
points: [
|
|
79
|
+
{ label: 'Mon', value: 10 },
|
|
80
|
+
{ label: 'Tue', value: 30 },
|
|
81
|
+
{ label: 'Wed', value: 20 },
|
|
82
|
+
],
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
name: 'Errors',
|
|
86
|
+
points: [
|
|
87
|
+
{ label: 'Mon', value: 2 },
|
|
88
|
+
{ label: 'Tue', value: 5 },
|
|
89
|
+
{ label: 'Wed', value: 1 },
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
it('renders a polyline per series with point markers, fluid + accessible', () => {
|
|
95
|
+
const markup = renderToStaticMarkup(<LineChart title="Traffic" series={series} />);
|
|
96
|
+
expect(count(markup, 'polyline')).toBe(2);
|
|
97
|
+
expect(count(markup, 'circle')).toBe(6);
|
|
98
|
+
expect(markup).toContain('viewBox="0 0 640 360"');
|
|
99
|
+
expect(markup).toContain('width="100%"');
|
|
100
|
+
expect(markup).toContain('role="img"');
|
|
101
|
+
expect(markup).toContain('aria-label="Traffic:');
|
|
102
|
+
// two named series => two legend rows
|
|
103
|
+
expect(count(markup, 'li')).toBe(2);
|
|
104
|
+
expect(markup).not.toContain('NaN');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('breaks the line at a non-finite gap (no NaN, one fewer marker)', () => {
|
|
108
|
+
const markup = renderToStaticMarkup(
|
|
109
|
+
<LineChart
|
|
110
|
+
title="Traffic"
|
|
111
|
+
series={[
|
|
112
|
+
{
|
|
113
|
+
name: 'Sessions',
|
|
114
|
+
points: [
|
|
115
|
+
{ label: 'Mon', value: 10 },
|
|
116
|
+
{ label: 'Tue', value: Number.NaN },
|
|
117
|
+
{ label: 'Wed', value: 20 },
|
|
118
|
+
],
|
|
119
|
+
},
|
|
120
|
+
]}
|
|
121
|
+
/>,
|
|
122
|
+
);
|
|
123
|
+
// gap splits the single line into two 1-point segments; markers only for finite points
|
|
124
|
+
expect(count(markup, 'circle')).toBe(2);
|
|
125
|
+
expect(markup).not.toContain('NaN');
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('renders the empty state when no series has a finite point', () => {
|
|
129
|
+
const markup = renderToStaticMarkup(
|
|
130
|
+
<LineChart title="Traffic" series={[{ name: 'X', points: [] }]} />,
|
|
131
|
+
);
|
|
132
|
+
expect(markup).toContain('aria-label="Traffic: No data"');
|
|
133
|
+
expect(count(markup, 'polyline')).toBe(0);
|
|
134
|
+
expect(markup).not.toContain('NaN');
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
describe('Sparkline', () => {
|
|
139
|
+
it('renders a fluid trend polyline with an aria summary', () => {
|
|
140
|
+
const markup = renderToStaticMarkup(
|
|
141
|
+
<Sparkline title="Latency" values={[5, 8, 6, 9, 7]} />,
|
|
142
|
+
);
|
|
143
|
+
expect(count(markup, 'polyline')).toBe(1);
|
|
144
|
+
expect(markup).toContain('width="100%"');
|
|
145
|
+
expect(markup).toContain('viewBox="0 0 100 28"');
|
|
146
|
+
expect(markup).toContain('role="img"');
|
|
147
|
+
expect(markup).toContain('aria-label="Latency:');
|
|
148
|
+
expect(markup).not.toContain('NaN');
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('renders an empty state for no finite values', () => {
|
|
152
|
+
const markup = renderToStaticMarkup(
|
|
153
|
+
<Sparkline title="Latency" values={[Number.NaN, Number.POSITIVE_INFINITY]} />,
|
|
154
|
+
);
|
|
155
|
+
expect(markup).toContain('aria-label="Latency: No data"');
|
|
156
|
+
expect(count(markup, 'polyline')).toBe(0);
|
|
157
|
+
expect(markup).not.toContain('NaN');
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe('DonutChart', () => {
|
|
162
|
+
const data = [
|
|
163
|
+
{ label: 'Passed', value: 70 },
|
|
164
|
+
{ label: 'Failed', value: 20 },
|
|
165
|
+
{ label: 'Skipped', value: 10 },
|
|
166
|
+
];
|
|
167
|
+
|
|
168
|
+
it('renders one arc path per positive slice, with legend percentages', () => {
|
|
169
|
+
const markup = renderToStaticMarkup(<DonutChart title="Results" data={data} />);
|
|
170
|
+
expect(count(markup, 'path')).toBe(3);
|
|
171
|
+
expect(markup).toContain('viewBox="0 0 200 200"');
|
|
172
|
+
expect(markup).toContain('width="100%"');
|
|
173
|
+
expect(markup).toContain('role="img"');
|
|
174
|
+
expect(markup).toContain('aria-label="Results:');
|
|
175
|
+
// legend shows label + value + percent (never color-only)
|
|
176
|
+
expect(markup).toContain('70%');
|
|
177
|
+
expect(count(markup, 'li')).toBe(3);
|
|
178
|
+
expect(markup).not.toContain('NaN');
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('skips negative / non-finite slices', () => {
|
|
182
|
+
const markup = renderToStaticMarkup(
|
|
183
|
+
<DonutChart
|
|
184
|
+
title="Results"
|
|
185
|
+
data={[
|
|
186
|
+
{ label: 'Passed', value: 70 },
|
|
187
|
+
{ label: 'Bad', value: -5 },
|
|
188
|
+
{ label: 'Worse', value: Number.NaN },
|
|
189
|
+
]}
|
|
190
|
+
/>,
|
|
191
|
+
);
|
|
192
|
+
expect(count(markup, 'path')).toBe(1);
|
|
193
|
+
expect(markup).not.toContain('NaN');
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('renders the empty state when every value is zero', () => {
|
|
197
|
+
const markup = renderToStaticMarkup(
|
|
198
|
+
<DonutChart title="Results" data={[{ label: 'None', value: 0 }]} />,
|
|
199
|
+
);
|
|
200
|
+
expect(markup).toContain('aria-label="Results: No data"');
|
|
201
|
+
expect(count(markup, 'path')).toBe(0);
|
|
202
|
+
expect(markup).not.toContain('NaN');
|
|
203
|
+
});
|
|
204
|
+
});
|