panelui-native 0.41.0 → 0.43.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/lib/module/components/color-picker/index.js +1395 -0
- package/lib/module/components/color-picker/index.js.map +1 -0
- package/lib/module/components/combobox/index.js +87 -19
- package/lib/module/components/combobox/index.js.map +1 -1
- package/lib/module/components/kpi-chart/index.js +648 -0
- package/lib/module/components/kpi-chart/index.js.map +1 -0
- package/lib/module/components/menu/index.js +40 -10
- package/lib/module/components/menu/index.js.map +1 -1
- package/lib/module/components/radar-chart/index.js +704 -0
- package/lib/module/components/radar-chart/index.js.map +1 -0
- package/lib/module/components/section-rail/index.js +32 -7
- package/lib/module/components/section-rail/index.js.map +1 -1
- package/lib/module/components/slider/index.js +240 -39
- package/lib/module/components/slider/index.js.map +1 -1
- package/lib/module/components/tabs/index.js +194 -10
- package/lib/module/components/tabs/index.js.map +1 -1
- package/lib/module/index.js +4 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/theme/use-theme.js +1 -1
- package/lib/module/utils/chart.js +44 -0
- package/lib/module/utils/chart.js.map +1 -1
- package/lib/module/utils/color.js +268 -0
- package/lib/module/utils/color.js.map +1 -0
- package/lib/typescript/src/components/color-picker/index.d.ts +258 -0
- package/lib/typescript/src/components/color-picker/index.d.ts.map +1 -0
- package/lib/typescript/src/components/combobox/index.d.ts.map +1 -1
- package/lib/typescript/src/components/kpi-chart/index.d.ts +490 -0
- package/lib/typescript/src/components/kpi-chart/index.d.ts.map +1 -0
- package/lib/typescript/src/components/menu/index.d.ts.map +1 -1
- package/lib/typescript/src/components/radar-chart/index.d.ts +214 -0
- package/lib/typescript/src/components/radar-chart/index.d.ts.map +1 -0
- package/lib/typescript/src/components/section-rail/index.d.ts +8 -1
- package/lib/typescript/src/components/section-rail/index.d.ts.map +1 -1
- package/lib/typescript/src/components/slider/index.d.ts +19 -0
- package/lib/typescript/src/components/slider/index.d.ts.map +1 -1
- package/lib/typescript/src/components/tabs/index.d.ts +11 -1
- package/lib/typescript/src/components/tabs/index.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +4 -0
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/utils/chart.d.ts +21 -0
- package/lib/typescript/src/utils/chart.d.ts.map +1 -1
- package/lib/typescript/src/utils/color.d.ts +74 -0
- package/lib/typescript/src/utils/color.d.ts.map +1 -0
- package/package.json +2 -1
- package/src/components/color-picker/index.tsx +1618 -0
- package/src/components/combobox/index.tsx +89 -19
- package/src/components/kpi-chart/index.tsx +719 -0
- package/src/components/menu/index.tsx +51 -6
- package/src/components/radar-chart/index.tsx +910 -0
- package/src/components/section-rail/index.tsx +42 -8
- package/src/components/slider/index.tsx +283 -41
- package/src/components/tabs/index.tsx +263 -8
- package/src/index.ts +60 -0
- package/src/theme/use-theme.ts +1 -1
- package/src/utils/chart.ts +52 -0
- package/src/utils/color.ts +246 -0
- package/theme.css +125 -112
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* RadarChart — several measures of one thing, on one shape.
|
|
5
|
+
*
|
|
6
|
+
* A radar answers a question a bar chart cannot: not "which of these is
|
|
7
|
+
* biggest" but "what shape is this". Five scores read as five bars are five
|
|
8
|
+
* facts; read as a polygon they are a profile, and two profiles laid over each
|
|
9
|
+
* other are comparable at a glance in a way two groups of bars never are.
|
|
10
|
+
*
|
|
11
|
+
* ```tsx
|
|
12
|
+
* <RadarChart data={scores} axisKey="skill">
|
|
13
|
+
* <RadarChart.Header title="Team profile" legend />
|
|
14
|
+
* <RadarChart.Grid />
|
|
15
|
+
* <RadarChart.Axis />
|
|
16
|
+
* <RadarChart.Series dataKey="you" colorIndex={1} />
|
|
17
|
+
* <RadarChart.Series dataKey="team" colorIndex={2} />
|
|
18
|
+
* </RadarChart>
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* That is also the shape's limit, and worth saying out loud: the order of the
|
|
22
|
+
* axes changes the outline, and the outline is what people read. Two datasets
|
|
23
|
+
* are only comparable on one radar if the axes are in the same order, and a
|
|
24
|
+
* radar is the wrong chart for data whose axes have no natural order at all.
|
|
25
|
+
*
|
|
26
|
+
* The reveal grows the polygons out of the centre rather than sweeping across
|
|
27
|
+
* them, because a polar chart has no left-hand edge for a sweep to start at.
|
|
28
|
+
* Everything below the root is drawn on the UI thread.
|
|
29
|
+
*/
|
|
30
|
+
import { Children, createContext, forwardRef, isValidElement, useContext, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
|
|
31
|
+
import { StyleSheet, View } from 'react-native';
|
|
32
|
+
import Svg, { Circle, G, Line, Path, Polygon, Text as SvgText } from 'react-native-svg';
|
|
33
|
+
import Animated, { Easing, useAnimatedProps, useDerivedValue, useReducedMotion, useSharedValue, withTiming } from 'react-native-reanimated';
|
|
34
|
+
import { useCSSVariable } from 'uniwind';
|
|
35
|
+
import { Text } from "../../primitives/text.js";
|
|
36
|
+
import { cn } from "../../utils/cn.js";
|
|
37
|
+
import { polarPoint, radarPath, useSeriesColor } from "../../utils/chart.js";
|
|
38
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
39
|
+
const AnimatedPath = Animated.createAnimatedComponent(Path);
|
|
40
|
+
const AnimatedCircle = Animated.createAnimatedComponent(Circle);
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Room reserved around the rings for the axis labels — wider than it is tall.
|
|
44
|
+
*
|
|
45
|
+
* Labels sit outside the shape rather than along an edge, and they are
|
|
46
|
+
* horizontal text: the one at three o'clock needs its whole width to the right
|
|
47
|
+
* of the ring, while the one at twelve needs a single line's height above it.
|
|
48
|
+
* A square box therefore cannot hold them — either the sides are clipped or
|
|
49
|
+
* the top and bottom are wasted — which is also why a radar drawn in a square
|
|
50
|
+
* ends up so much taller than the wide charts beside it.
|
|
51
|
+
*/
|
|
52
|
+
const LABEL_ROOM = {
|
|
53
|
+
x: 62,
|
|
54
|
+
y: 26
|
|
55
|
+
};
|
|
56
|
+
/** …and with no axis labels asking for it, only enough not to clip the stroke. */
|
|
57
|
+
const BARE_ROOM = {
|
|
58
|
+
x: 6,
|
|
59
|
+
y: 6
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/** How long the polygons take to grow out of the centre. */
|
|
63
|
+
const REVEAL_DURATION = 900;
|
|
64
|
+
/** …and how long one profile takes to travel to the next when the data changes. */
|
|
65
|
+
const MORPH_DURATION = 420;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Ring diameter when nothing says otherwise.
|
|
69
|
+
*
|
|
70
|
+
* The other charts here fill their container, because they are wide and a
|
|
71
|
+
* wider one carries more. A radar filling a panel is as tall as the panel is
|
|
72
|
+
* wide — twice the height of the chart beside it, for the same handful of
|
|
73
|
+
* numbers — so it sizes itself instead, and centres.
|
|
74
|
+
*/
|
|
75
|
+
const DEFAULT_SIZE = 180;
|
|
76
|
+
|
|
77
|
+
/** One row is one axis: its label, and one value per series. */
|
|
78
|
+
|
|
79
|
+
const RadarChartContext = /*#__PURE__*/createContext(null);
|
|
80
|
+
|
|
81
|
+
/** The chart's geometry and data, for a part or a readout beside one. */
|
|
82
|
+
export function useRadarChart() {
|
|
83
|
+
const context = useContext(RadarChartContext);
|
|
84
|
+
if (!context) throw new Error('useRadarChart must be used inside <RadarChart>.');
|
|
85
|
+
return context;
|
|
86
|
+
}
|
|
87
|
+
function useChart(part) {
|
|
88
|
+
const context = useContext(RadarChartContext);
|
|
89
|
+
if (!context) throw new Error(`${part} must be used inside <RadarChart>.`);
|
|
90
|
+
return context;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Which layer a part draws into. */
|
|
94
|
+
|
|
95
|
+
function partition(children) {
|
|
96
|
+
const svg = [];
|
|
97
|
+
const overlay = [];
|
|
98
|
+
const header = [];
|
|
99
|
+
Children.forEach(children, (child, index) => {
|
|
100
|
+
if (! /*#__PURE__*/isValidElement(child)) return;
|
|
101
|
+
const layer = child.type.layer ?? 'svg';
|
|
102
|
+
const slot = /*#__PURE__*/_jsx(ChildSlot, {
|
|
103
|
+
children: child
|
|
104
|
+
}, index);
|
|
105
|
+
(layer === 'header' ? header : layer === 'overlay' ? overlay : svg).push(slot);
|
|
106
|
+
});
|
|
107
|
+
return {
|
|
108
|
+
svg,
|
|
109
|
+
overlay,
|
|
110
|
+
header
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Keeps a child's own key out of the array index the partition gives it. */
|
|
115
|
+
function ChildSlot({
|
|
116
|
+
children
|
|
117
|
+
}) {
|
|
118
|
+
return /*#__PURE__*/_jsx(_Fragment, {
|
|
119
|
+
children: children
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** A theme token as a colour, with the usual guard for a token that is not one. */
|
|
124
|
+
function useToken(variable, fallback) {
|
|
125
|
+
const raw = useCSSVariable(variable);
|
|
126
|
+
return typeof raw === 'string' ? raw : fallback;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/* ------------------------------------------------------------------ *
|
|
130
|
+
* Root.
|
|
131
|
+
* ------------------------------------------------------------------ */
|
|
132
|
+
|
|
133
|
+
/** Imperative handle: re-run the reveal on demand, for a "replay" control. */
|
|
134
|
+
|
|
135
|
+
const RadarChartRoot = /*#__PURE__*/forwardRef(function RadarChartRoot({
|
|
136
|
+
className,
|
|
137
|
+
data,
|
|
138
|
+
axisKey = 'axis',
|
|
139
|
+
status = 'ready',
|
|
140
|
+
size: fixedSize = DEFAULT_SIZE,
|
|
141
|
+
aspectRatio = 1,
|
|
142
|
+
domain,
|
|
143
|
+
animationDuration = REVEAL_DURATION,
|
|
144
|
+
compact = false,
|
|
145
|
+
children,
|
|
146
|
+
...props
|
|
147
|
+
}, ref) {
|
|
148
|
+
const [size, setSize] = useState({
|
|
149
|
+
width: 0,
|
|
150
|
+
height: 0
|
|
151
|
+
});
|
|
152
|
+
const [series, setSeries] = useState([]);
|
|
153
|
+
const reveal = useSharedValue(0);
|
|
154
|
+
const domainMin = useSharedValue(0);
|
|
155
|
+
const domainMax = useSharedValue(0);
|
|
156
|
+
const reducedMotion = useReducedMotion();
|
|
157
|
+
const registerSeries = useMemo(() => (key, color) => setSeries(current => {
|
|
158
|
+
const existing = current.find(([k]) => k === key);
|
|
159
|
+
if (existing?.[1] === color) return current;
|
|
160
|
+
return [...current.filter(([k]) => k !== key), [key, color]];
|
|
161
|
+
}), []);
|
|
162
|
+
const unregisterSeries = useMemo(() => key => setSeries(current => current.filter(([k]) => k !== key)), []);
|
|
163
|
+
const room = compact ? BARE_ROOM : LABEL_ROOM;
|
|
164
|
+
const cx = size.width / 2;
|
|
165
|
+
const cy = size.height / 2;
|
|
166
|
+
// Measured rather than assumed, so the `aspectRatio` path — where the box
|
|
167
|
+
// is whatever the container gave it — lands on the same geometry.
|
|
168
|
+
const radius = Math.max(Math.min(size.width / 2 - room.x, size.height / 2 - room.y), 0);
|
|
169
|
+
const seriesKeys = series.map(([key]) => key).join('|');
|
|
170
|
+
const extent = useMemo(() => {
|
|
171
|
+
if (domain) return domain;
|
|
172
|
+
const keys = seriesKeys ? seriesKeys.split('|') : [];
|
|
173
|
+
let max = -Infinity;
|
|
174
|
+
for (const row of data) {
|
|
175
|
+
for (const key of keys) {
|
|
176
|
+
const value = row[key];
|
|
177
|
+
if (typeof value !== 'number' || Number.isNaN(value)) continue;
|
|
178
|
+
if (value > max) max = value;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (max === -Infinity) return [0, 1];
|
|
182
|
+
/*
|
|
183
|
+
* From zero, always. A radar's rings are read as fractions of the whole —
|
|
184
|
+
* "three quarters of the way out" — and a floor above zero makes a small
|
|
185
|
+
* value look like a large one, which is the failure mode this chart is
|
|
186
|
+
* most prone to.
|
|
187
|
+
*/
|
|
188
|
+
return [0, max === 0 ? 1 : max * 1.05];
|
|
189
|
+
}, [data, domain, seriesKeys]);
|
|
190
|
+
const loading = status === 'loading';
|
|
191
|
+
useEffect(() => {
|
|
192
|
+
if (loading) return;
|
|
193
|
+
const [min, max] = extent;
|
|
194
|
+
domainMin.value = min;
|
|
195
|
+
domainMax.value = max;
|
|
196
|
+
}, [extent, loading, domainMin, domainMax]);
|
|
197
|
+
const revealed = useRef(false);
|
|
198
|
+
const playReveal = useMemo(() => () => {
|
|
199
|
+
if (reducedMotion) {
|
|
200
|
+
reveal.value = 1;
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
reveal.value = 0;
|
|
204
|
+
reveal.value = withTiming(1, {
|
|
205
|
+
duration: animationDuration,
|
|
206
|
+
easing: Easing.bezier(0.85, 0, 0.15, 1)
|
|
207
|
+
});
|
|
208
|
+
}, [reducedMotion, animationDuration, reveal]);
|
|
209
|
+
useEffect(() => {
|
|
210
|
+
if (revealed.current || loading || radius <= 0 || !data.length) return;
|
|
211
|
+
revealed.current = true;
|
|
212
|
+
playReveal();
|
|
213
|
+
}, [loading, radius, data.length, playReveal]);
|
|
214
|
+
useImperativeHandle(ref, () => ({
|
|
215
|
+
replay: playReveal
|
|
216
|
+
}), [playReveal]);
|
|
217
|
+
const onLayout = event => {
|
|
218
|
+
const {
|
|
219
|
+
width,
|
|
220
|
+
height
|
|
221
|
+
} = event.nativeEvent.layout;
|
|
222
|
+
setSize(current => Math.abs(current.width - width) < 1 && Math.abs(current.height - height) < 1 ? current : {
|
|
223
|
+
width,
|
|
224
|
+
height
|
|
225
|
+
});
|
|
226
|
+
props.onLayout?.(event);
|
|
227
|
+
};
|
|
228
|
+
const context = useMemo(() => ({
|
|
229
|
+
data,
|
|
230
|
+
axisKey,
|
|
231
|
+
cx,
|
|
232
|
+
cy,
|
|
233
|
+
radius,
|
|
234
|
+
width: size.width,
|
|
235
|
+
height: size.height,
|
|
236
|
+
status,
|
|
237
|
+
domainMin,
|
|
238
|
+
domainMax,
|
|
239
|
+
reveal,
|
|
240
|
+
series,
|
|
241
|
+
registerSeries,
|
|
242
|
+
unregisterSeries
|
|
243
|
+
}), [data, axisKey, cx, cy, radius, size.width, size.height, status, domainMin, domainMax, reveal, series, registerSeries, unregisterSeries]);
|
|
244
|
+
const {
|
|
245
|
+
svg,
|
|
246
|
+
overlay,
|
|
247
|
+
header
|
|
248
|
+
} = partition(children);
|
|
249
|
+
|
|
250
|
+
/*
|
|
251
|
+
* Two views, because the header is not part of the plot. `aspectRatio` and
|
|
252
|
+
* the layout measurement belong to the drawing area alone — measured on the
|
|
253
|
+
* outer view they would take in the header too, and the rings would lose as
|
|
254
|
+
* much radius as the header took while still claiming the shape asked for.
|
|
255
|
+
*/
|
|
256
|
+
return /*#__PURE__*/_jsx(RadarChartContext.Provider, {
|
|
257
|
+
value: context,
|
|
258
|
+
children: /*#__PURE__*/_jsxs(View, {
|
|
259
|
+
...props,
|
|
260
|
+
style: props.style,
|
|
261
|
+
className: cn('w-full', className),
|
|
262
|
+
children: [header, /*#__PURE__*/_jsx(View, {
|
|
263
|
+
onLayout: onLayout,
|
|
264
|
+
style: fixedSize ? {
|
|
265
|
+
// The ring plus its label room — wider than tall, because
|
|
266
|
+
// the labels are.
|
|
267
|
+
width: fixedSize + room.x * 2,
|
|
268
|
+
height: fixedSize + room.y * 2,
|
|
269
|
+
maxWidth: '100%'
|
|
270
|
+
} : {
|
|
271
|
+
aspectRatio
|
|
272
|
+
},
|
|
273
|
+
className: fixedSize ? 'self-center' : 'w-full',
|
|
274
|
+
children: radius > 0 ? /*#__PURE__*/_jsxs(_Fragment, {
|
|
275
|
+
children: [/*#__PURE__*/_jsx(Svg, {
|
|
276
|
+
width: "100%",
|
|
277
|
+
height: "100%",
|
|
278
|
+
style: StyleSheet.absoluteFill,
|
|
279
|
+
children: svg
|
|
280
|
+
}), overlay]
|
|
281
|
+
}) : null
|
|
282
|
+
})]
|
|
283
|
+
})
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
/* ------------------------------------------------------------------ *
|
|
288
|
+
* Grid — the rings, and the spokes under them.
|
|
289
|
+
* ------------------------------------------------------------------ */
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* The scale, drawn as rings.
|
|
293
|
+
*
|
|
294
|
+
* Polygonal by default rather than circular, because the rings are read
|
|
295
|
+
* against the shape laid over them — a round ring behind an angular polygon
|
|
296
|
+
* gives every axis a different apparent distance to the edge, and the whole
|
|
297
|
+
* point of the rings is to say how far out something is.
|
|
298
|
+
*/
|
|
299
|
+
function RadarChartGrid({
|
|
300
|
+
rings = 4,
|
|
301
|
+
color,
|
|
302
|
+
circular = false,
|
|
303
|
+
spokes = true
|
|
304
|
+
}) {
|
|
305
|
+
const {
|
|
306
|
+
data,
|
|
307
|
+
cx,
|
|
308
|
+
cy,
|
|
309
|
+
radius
|
|
310
|
+
} = useChart('RadarChart.Grid');
|
|
311
|
+
const themed = useToken('--color-border', 'rgba(128,128,128,0.2)');
|
|
312
|
+
const stroke = color ?? themed;
|
|
313
|
+
const count = data.length;
|
|
314
|
+
if (count < 3) return null;
|
|
315
|
+
const levels = Array.from({
|
|
316
|
+
length: rings
|
|
317
|
+
}, (_, index) => (index + 1) / rings * radius);
|
|
318
|
+
return /*#__PURE__*/_jsxs(G, {
|
|
319
|
+
children: [levels.map((r, index) => circular ? /*#__PURE__*/_jsx(Circle, {
|
|
320
|
+
cx: cx,
|
|
321
|
+
cy: cy,
|
|
322
|
+
r: r,
|
|
323
|
+
fill: "none",
|
|
324
|
+
stroke: stroke,
|
|
325
|
+
strokeWidth: 1
|
|
326
|
+
}, index) : /*#__PURE__*/_jsx(Polygon, {
|
|
327
|
+
points: Array.from({
|
|
328
|
+
length: count
|
|
329
|
+
}, (_, spoke) => {
|
|
330
|
+
const point = polarPoint(cx, cy, r, spoke / count);
|
|
331
|
+
return `${point.x},${point.y}`;
|
|
332
|
+
}).join(' '),
|
|
333
|
+
fill: "none",
|
|
334
|
+
stroke: stroke,
|
|
335
|
+
strokeWidth: 1
|
|
336
|
+
}, index)), spokes ? Array.from({
|
|
337
|
+
length: count
|
|
338
|
+
}, (_, index) => {
|
|
339
|
+
const point = polarPoint(cx, cy, radius, index / count);
|
|
340
|
+
return /*#__PURE__*/_jsx(Line, {
|
|
341
|
+
x1: cx,
|
|
342
|
+
y1: cy,
|
|
343
|
+
x2: point.x,
|
|
344
|
+
y2: point.y,
|
|
345
|
+
stroke: stroke,
|
|
346
|
+
strokeWidth: 1
|
|
347
|
+
}, index);
|
|
348
|
+
}) : null]
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/* ------------------------------------------------------------------ *
|
|
353
|
+
* Axis — the labels round the outside.
|
|
354
|
+
* ------------------------------------------------------------------ */
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* The axis names, placed around the rings.
|
|
358
|
+
*
|
|
359
|
+
* Each label is anchored by which side of the circle it is on rather than
|
|
360
|
+
* centred on its point: a label centred at three o'clock overlaps the shape it
|
|
361
|
+
* belongs to, and one centred at nine o'clock overlaps the view's edge.
|
|
362
|
+
*/
|
|
363
|
+
function RadarChartAxis({
|
|
364
|
+
color,
|
|
365
|
+
fontSize = 11,
|
|
366
|
+
offset = 10,
|
|
367
|
+
formatLabel
|
|
368
|
+
}) {
|
|
369
|
+
const {
|
|
370
|
+
data,
|
|
371
|
+
axisKey,
|
|
372
|
+
cx,
|
|
373
|
+
cy,
|
|
374
|
+
radius,
|
|
375
|
+
width,
|
|
376
|
+
height
|
|
377
|
+
} = useChart('RadarChart.Axis');
|
|
378
|
+
const themed = useToken('--color-muted-foreground', '#737373');
|
|
379
|
+
const fill = color ?? themed;
|
|
380
|
+
const count = data.length;
|
|
381
|
+
if (count < 3) return null;
|
|
382
|
+
return /*#__PURE__*/_jsx(G, {
|
|
383
|
+
children: data.map((row, index) => {
|
|
384
|
+
const raw = row[axisKey];
|
|
385
|
+
const label = formatLabel ? formatLabel(String(raw ?? ''), index) : String(raw ?? '');
|
|
386
|
+
const point = polarPoint(cx, cy, radius + offset, index / count);
|
|
387
|
+
|
|
388
|
+
// Within a few points of the vertical it is a top or bottom label and
|
|
389
|
+
// wants centring; either side of that it wants to run away from the
|
|
390
|
+
// chart rather than across it.
|
|
391
|
+
const dx = point.x - cx;
|
|
392
|
+
const anchor = Math.abs(dx) < radius * 0.15 ? 'middle' : dx > 0 ? 'start' : 'end';
|
|
393
|
+
|
|
394
|
+
/*
|
|
395
|
+
* Pull the anchor back inside the view before drawing.
|
|
396
|
+
*
|
|
397
|
+
* SVG clips at its viewport and does not reflow, so a label wider than
|
|
398
|
+
* the room left for it loses its tail with no sign that anything is
|
|
399
|
+
* missing — the reader sees "Accur" and has no reason to think it was
|
|
400
|
+
* ever longer. Nudging the anchor in costs a couple of points of gap
|
|
401
|
+
* between the label and the ring, which is the cheaper of the two.
|
|
402
|
+
*/
|
|
403
|
+
const estimated = label.length * fontSize * 0.55;
|
|
404
|
+
const x = anchor === 'start' ? Math.min(point.x, Math.max(width - estimated, cx)) : anchor === 'end' ? Math.max(point.x, Math.min(estimated, cx)) : point.x;
|
|
405
|
+
|
|
406
|
+
// Text is anchored on its baseline, so a label below the chart needs
|
|
407
|
+
// pushing down by roughly its cap height to look level with one above.
|
|
408
|
+
const dy = point.y < cy ? 0 : fontSize * 0.72;
|
|
409
|
+
const y = Math.min(Math.max(point.y + dy, fontSize), height - 2);
|
|
410
|
+
return /*#__PURE__*/_jsx(SvgText, {
|
|
411
|
+
x: x,
|
|
412
|
+
y: y,
|
|
413
|
+
fill: fill,
|
|
414
|
+
fontSize: fontSize,
|
|
415
|
+
fontWeight: "500",
|
|
416
|
+
textAnchor: anchor,
|
|
417
|
+
children: label
|
|
418
|
+
}, index);
|
|
419
|
+
})
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/* ------------------------------------------------------------------ *
|
|
424
|
+
* Series — one polygon.
|
|
425
|
+
* ------------------------------------------------------------------ */
|
|
426
|
+
|
|
427
|
+
/** One profile. */
|
|
428
|
+
function RadarChartSeries({
|
|
429
|
+
dataKey,
|
|
430
|
+
name,
|
|
431
|
+
color,
|
|
432
|
+
colorIndex = 1,
|
|
433
|
+
strokeWidth = 2,
|
|
434
|
+
fillOpacity = 0.18,
|
|
435
|
+
showDots = false
|
|
436
|
+
}) {
|
|
437
|
+
const {
|
|
438
|
+
data,
|
|
439
|
+
cx,
|
|
440
|
+
cy,
|
|
441
|
+
radius,
|
|
442
|
+
domainMin,
|
|
443
|
+
domainMax,
|
|
444
|
+
reveal,
|
|
445
|
+
status,
|
|
446
|
+
registerSeries,
|
|
447
|
+
unregisterSeries
|
|
448
|
+
} = useChart('RadarChart.Series');
|
|
449
|
+
const stroke = useSeriesColor(color, colorIndex);
|
|
450
|
+
const count = data.length;
|
|
451
|
+
useEffect(() => {
|
|
452
|
+
registerSeries(dataKey, stroke);
|
|
453
|
+
return () => unregisterSeries(dataKey);
|
|
454
|
+
}, [dataKey, stroke, registerSeries, unregisterSeries]);
|
|
455
|
+
const values = useMemo(() => data.map(row => {
|
|
456
|
+
const value = row[dataKey];
|
|
457
|
+
return typeof value === 'number' && !Number.isNaN(value) ? value : null;
|
|
458
|
+
}), [data, dataKey]);
|
|
459
|
+
const loading = status === 'loading';
|
|
460
|
+
|
|
461
|
+
/*
|
|
462
|
+
* Where the shape is coming from and where it is going, and how far between
|
|
463
|
+
* them it currently is.
|
|
464
|
+
*
|
|
465
|
+
* A radar is the chart people put behind a switch — this quarter or last,
|
|
466
|
+
* you or the team — and a polygon that jumps from one profile to the next
|
|
467
|
+
* loses the only thing worth showing at the moment of the switch, which is
|
|
468
|
+
* which axes moved and by how much. Held as two arrays and tweened per
|
|
469
|
+
* vertex, the outline travels and the eye follows the parts of it that
|
|
470
|
+
* travelled furthest.
|
|
471
|
+
*/
|
|
472
|
+
const from = useSharedValue(values);
|
|
473
|
+
const to = useSharedValue(values);
|
|
474
|
+
const morph = useSharedValue(1);
|
|
475
|
+
const settled = useRef(false);
|
|
476
|
+
const reducedMotion = useReducedMotion();
|
|
477
|
+
useEffect(() => {
|
|
478
|
+
// The first values are not a change from anything.
|
|
479
|
+
if (!settled.current) {
|
|
480
|
+
settled.current = true;
|
|
481
|
+
from.value = values;
|
|
482
|
+
to.value = values;
|
|
483
|
+
morph.value = 1;
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
if (reducedMotion) {
|
|
487
|
+
from.value = values;
|
|
488
|
+
to.value = values;
|
|
489
|
+
morph.value = 1;
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// Leave from wherever the last tween had got to, so a switch part way
|
|
494
|
+
// through another one carries on from the shape actually on screen.
|
|
495
|
+
const t = morph.value;
|
|
496
|
+
const previous = from.value;
|
|
497
|
+
const current = to.value;
|
|
498
|
+
from.value = current.map((value, index) => {
|
|
499
|
+
const start = previous[index];
|
|
500
|
+
if (value === null || start === null || start === undefined) return value ?? start ?? null;
|
|
501
|
+
return start + (value - start) * t;
|
|
502
|
+
});
|
|
503
|
+
to.value = values;
|
|
504
|
+
morph.value = 0;
|
|
505
|
+
morph.value = withTiming(1, {
|
|
506
|
+
duration: MORPH_DURATION
|
|
507
|
+
});
|
|
508
|
+
}, [values, reducedMotion, from, to, morph]);
|
|
509
|
+
|
|
510
|
+
/*
|
|
511
|
+
* The polygon is rebuilt on the UI thread every frame the reveal or the
|
|
512
|
+
* morph is running. Scaling the *values* rather than transforming the group
|
|
513
|
+
* is what makes the shape grow along its own axes — a `scale` transform
|
|
514
|
+
* would grow the stroke and the dots with it, and arrive at the wrong
|
|
515
|
+
* stroke width.
|
|
516
|
+
*/
|
|
517
|
+
const animatedProps = useAnimatedProps(() => {
|
|
518
|
+
const span = Math.max(domainMax.value - domainMin.value, 1e-6);
|
|
519
|
+
const target = to.value;
|
|
520
|
+
const start = from.value;
|
|
521
|
+
const scaled = target.map((value, index) => {
|
|
522
|
+
const a = start[index] ?? value;
|
|
523
|
+
const b = value ?? a;
|
|
524
|
+
if (a === null || a === undefined || b === null || b === undefined) return null;
|
|
525
|
+
const mixed = a + (b - a) * morph.value;
|
|
526
|
+
return (mixed - domainMin.value) / span * reveal.value;
|
|
527
|
+
});
|
|
528
|
+
return {
|
|
529
|
+
d: loading ? '' : radarPath(scaled, cx, cy, radius)
|
|
530
|
+
};
|
|
531
|
+
});
|
|
532
|
+
return /*#__PURE__*/_jsxs(G, {
|
|
533
|
+
children: [/*#__PURE__*/_jsx(AnimatedPath, {
|
|
534
|
+
animatedProps: animatedProps,
|
|
535
|
+
fill: stroke,
|
|
536
|
+
fillOpacity: fillOpacity,
|
|
537
|
+
stroke: stroke,
|
|
538
|
+
strokeWidth: strokeWidth,
|
|
539
|
+
strokeLinejoin: "round"
|
|
540
|
+
}), showDots && count >= 3 ? values.map((value, index) => value === null ? null : /*#__PURE__*/_jsx(RadarDot, {
|
|
541
|
+
index: index,
|
|
542
|
+
turn: index / count,
|
|
543
|
+
cx: cx,
|
|
544
|
+
cy: cy,
|
|
545
|
+
radius: radius,
|
|
546
|
+
color: stroke,
|
|
547
|
+
from: from,
|
|
548
|
+
to: to,
|
|
549
|
+
morph: morph,
|
|
550
|
+
domainMin: domainMin,
|
|
551
|
+
domainMax: domainMax,
|
|
552
|
+
reveal: reveal
|
|
553
|
+
}, index)) : null]
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/** One vertex, riding the same reveal and the same morph as its polygon. */
|
|
558
|
+
function RadarDot({
|
|
559
|
+
index,
|
|
560
|
+
turn,
|
|
561
|
+
cx,
|
|
562
|
+
cy,
|
|
563
|
+
radius,
|
|
564
|
+
color,
|
|
565
|
+
from,
|
|
566
|
+
to,
|
|
567
|
+
morph,
|
|
568
|
+
domainMin,
|
|
569
|
+
domainMax,
|
|
570
|
+
reveal
|
|
571
|
+
}) {
|
|
572
|
+
const point = useDerivedValue(() => {
|
|
573
|
+
const span = Math.max(domainMax.value - domainMin.value, 1e-6);
|
|
574
|
+
const target = to.value[index];
|
|
575
|
+
const start = from.value[index] ?? target;
|
|
576
|
+
if (target === null || target === undefined || start === null || start === undefined) {
|
|
577
|
+
return {
|
|
578
|
+
x: cx,
|
|
579
|
+
y: cy
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
const mixed = start + (target - start) * morph.value;
|
|
583
|
+
const fraction = (mixed - domainMin.value) / span * reveal.value;
|
|
584
|
+
return polarPoint(cx, cy, radius * fraction, turn);
|
|
585
|
+
});
|
|
586
|
+
const animatedProps = useAnimatedProps(() => ({
|
|
587
|
+
cx: point.value.x,
|
|
588
|
+
cy: point.value.y
|
|
589
|
+
}));
|
|
590
|
+
return /*#__PURE__*/_jsx(AnimatedCircle, {
|
|
591
|
+
animatedProps: animatedProps,
|
|
592
|
+
r: 3,
|
|
593
|
+
fill: color
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/* ------------------------------------------------------------------ *
|
|
598
|
+
* Header and legend.
|
|
599
|
+
* ------------------------------------------------------------------ */
|
|
600
|
+
|
|
601
|
+
/** The strip above the rings. */
|
|
602
|
+
function RadarChartHeader({
|
|
603
|
+
className,
|
|
604
|
+
title,
|
|
605
|
+
value,
|
|
606
|
+
caption,
|
|
607
|
+
legend = false,
|
|
608
|
+
children
|
|
609
|
+
}) {
|
|
610
|
+
const {
|
|
611
|
+
series
|
|
612
|
+
} = useChart('RadarChart.Header');
|
|
613
|
+
return /*#__PURE__*/_jsxs(View, {
|
|
614
|
+
className: cn('flex-row items-start justify-between gap-3 pb-2', className),
|
|
615
|
+
children: [/*#__PURE__*/_jsxs(View, {
|
|
616
|
+
className: "flex-1",
|
|
617
|
+
children: [title ? /*#__PURE__*/_jsx(Text, {
|
|
618
|
+
size: "sm",
|
|
619
|
+
muted: true,
|
|
620
|
+
children: title
|
|
621
|
+
}) : null, value ? /*#__PURE__*/_jsx(Text, {
|
|
622
|
+
size: "2xl",
|
|
623
|
+
weight: "bold",
|
|
624
|
+
children: value
|
|
625
|
+
}) : null, caption ? /*#__PURE__*/_jsx(Text, {
|
|
626
|
+
size: "sm",
|
|
627
|
+
muted: true,
|
|
628
|
+
children: caption
|
|
629
|
+
}) : null]
|
|
630
|
+
}), legend ? /*#__PURE__*/_jsx(View, {
|
|
631
|
+
className: "items-end gap-1",
|
|
632
|
+
children: series.map(([key, color]) => /*#__PURE__*/_jsxs(View, {
|
|
633
|
+
className: "flex-row items-center gap-1.5",
|
|
634
|
+
children: [/*#__PURE__*/_jsx(View, {
|
|
635
|
+
className: "h-2 w-2 rounded-full",
|
|
636
|
+
style: {
|
|
637
|
+
backgroundColor: color
|
|
638
|
+
}
|
|
639
|
+
}), /*#__PURE__*/_jsx(Text, {
|
|
640
|
+
size: "sm",
|
|
641
|
+
muted: true,
|
|
642
|
+
children: key
|
|
643
|
+
})]
|
|
644
|
+
}, key))
|
|
645
|
+
}) : null, children]
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* The series, named and coloured.
|
|
650
|
+
*
|
|
651
|
+
* In the bottom-left of the plot rather than under it, which on a radar costs
|
|
652
|
+
* nothing: the shape is a circle in a square box, so the corners are empty by
|
|
653
|
+
* construction. Move it with `className`.
|
|
654
|
+
*/
|
|
655
|
+
function RadarChartLegend({
|
|
656
|
+
className,
|
|
657
|
+
formatName
|
|
658
|
+
}) {
|
|
659
|
+
const {
|
|
660
|
+
series
|
|
661
|
+
} = useChart('RadarChart.Legend');
|
|
662
|
+
if (!series.length) return null;
|
|
663
|
+
return /*#__PURE__*/_jsx(View, {
|
|
664
|
+
className: cn('absolute bottom-0 left-0 gap-1', className),
|
|
665
|
+
style: {
|
|
666
|
+
pointerEvents: 'none'
|
|
667
|
+
},
|
|
668
|
+
children: series.map(([key, color]) => /*#__PURE__*/_jsxs(View, {
|
|
669
|
+
className: "flex-row items-center gap-1.5",
|
|
670
|
+
children: [/*#__PURE__*/_jsx(View, {
|
|
671
|
+
className: "h-2 w-2 rounded-full",
|
|
672
|
+
style: {
|
|
673
|
+
backgroundColor: color
|
|
674
|
+
}
|
|
675
|
+
}), /*#__PURE__*/_jsx(Text, {
|
|
676
|
+
size: "sm",
|
|
677
|
+
muted: true,
|
|
678
|
+
children: formatName ? formatName(key) : key
|
|
679
|
+
})]
|
|
680
|
+
}, key))
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
RadarChartRoot.displayName = 'RadarChart';
|
|
684
|
+
RadarChartGrid.displayName = 'RadarChart.Grid';
|
|
685
|
+
RadarChartAxis.displayName = 'RadarChart.Axis';
|
|
686
|
+
RadarChartSeries.displayName = 'RadarChart.Series';
|
|
687
|
+
RadarChartHeader.displayName = 'RadarChart.Header';
|
|
688
|
+
RadarChartLegend.displayName = 'RadarChart.Legend';
|
|
689
|
+
|
|
690
|
+
// Which layer each part draws into. Read by `partition` on the root, so a part
|
|
691
|
+
// can be written in any order and still land in the right place.
|
|
692
|
+
RadarChartGrid.layer = 'svg';
|
|
693
|
+
RadarChartAxis.layer = 'svg';
|
|
694
|
+
RadarChartSeries.layer = 'svg';
|
|
695
|
+
RadarChartHeader.layer = 'header';
|
|
696
|
+
RadarChartLegend.layer = 'overlay';
|
|
697
|
+
export const RadarChart = Object.assign(RadarChartRoot, {
|
|
698
|
+
Header: RadarChartHeader,
|
|
699
|
+
Grid: RadarChartGrid,
|
|
700
|
+
Axis: RadarChartAxis,
|
|
701
|
+
Series: RadarChartSeries,
|
|
702
|
+
Legend: RadarChartLegend
|
|
703
|
+
});
|
|
704
|
+
//# sourceMappingURL=index.js.map
|