panelui-native 0.54.0 → 0.56.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 +1 -0
- package/lib/module/components/combobox/index.js +61 -7
- package/lib/module/components/combobox/index.js.map +1 -1
- package/lib/module/components/hex-chart/index.js +765 -0
- package/lib/module/components/hex-chart/index.js.map +1 -0
- package/lib/module/components/sortable/index.js +74 -12
- package/lib/module/components/sortable/index.js.map +1 -1
- package/lib/module/components/steps/index.js +95 -12
- package/lib/module/components/steps/index.js.map +1 -1
- package/lib/module/components/tag-input/index.js +435 -0
- package/lib/module/components/tag-input/index.js.map +1 -0
- package/lib/module/components/tour/index.js +661 -0
- package/lib/module/components/tour/index.js.map +1 -0
- package/lib/module/index.js +3 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/chart.js +209 -0
- package/lib/module/utils/chart.js.map +1 -1
- package/lib/typescript/src/components/combobox/index.d.ts.map +1 -1
- package/lib/typescript/src/components/hex-chart/index.d.ts +277 -0
- package/lib/typescript/src/components/hex-chart/index.d.ts.map +1 -0
- package/lib/typescript/src/components/sortable/index.d.ts.map +1 -1
- package/lib/typescript/src/components/steps/index.d.ts +19 -0
- package/lib/typescript/src/components/steps/index.d.ts.map +1 -1
- package/lib/typescript/src/components/tag-input/index.d.ts +240 -0
- package/lib/typescript/src/components/tag-input/index.d.ts.map +1 -0
- package/lib/typescript/src/components/tour/index.d.ts +168 -0
- package/lib/typescript/src/components/tour/index.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +3 -0
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/utils/chart.d.ts +98 -0
- package/lib/typescript/src/utils/chart.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/components/hex-chart/index.tsx +1009 -0
- package/src/components/steps/index.tsx +103 -8
- package/src/components/tour/index.tsx +933 -0
- package/src/index.ts +22 -0
- package/src/utils/chart.ts +236 -0
|
@@ -0,0 +1,1009 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HexChart — a whole broken into parts, counted out in cells.
|
|
3
|
+
*
|
|
4
|
+
* ```tsx
|
|
5
|
+
* <HexChart data={attribution}>
|
|
6
|
+
* <HexChart.Header title="Attributed revenue" value="$6,750" />
|
|
7
|
+
* <HexChart.Cells />
|
|
8
|
+
* <HexChart.Legend />
|
|
9
|
+
* </HexChart>
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* ## What it is, against the pie beside it
|
|
13
|
+
*
|
|
14
|
+
* Both divide one total. The difference is what the reader has to do to read a
|
|
15
|
+
* share off it. A pie asks them to compare angles, which is the hardest
|
|
16
|
+
* quantity there is to judge by eye; this asks them to compare *counts*, and a
|
|
17
|
+
* count is something anyone can check by looking. A series holding a tenth of
|
|
18
|
+
* the cells looks like a tenth and can be confirmed as one, which is why this
|
|
19
|
+
* is the better shape for a split someone is going to quote.
|
|
20
|
+
*
|
|
21
|
+
* What it gives up is precision at the small end. Every cell is a whole unit,
|
|
22
|
+
* so a series worth half a cell either rounds up to a full one or vanishes. It
|
|
23
|
+
* is a chart for shares of a few percent and up, not for a long tail.
|
|
24
|
+
*
|
|
25
|
+
* ## The field
|
|
26
|
+
*
|
|
27
|
+
* The cells are pointy-top hexagons on an offset grid, so each row nests half a
|
|
28
|
+
* cell into the one above it. The unfilled cells are drawn too, in the muted
|
|
29
|
+
* token: the field is the denominator made visible, and a honeycomb floating on
|
|
30
|
+
* nothing gives the eye no total to read the coloured part against.
|
|
31
|
+
*
|
|
32
|
+
* `shape` decides how the filled cells are arranged. `grid` is reading order,
|
|
33
|
+
* which is the arrangement a reader can actually count off. `blob` grows the
|
|
34
|
+
* series out from the middle of the field instead — the smallest in the centre,
|
|
35
|
+
* each larger one wrapped around it — which counts for nothing but shows the
|
|
36
|
+
* shape of the split at a glance. The blob's edge is ragged by design, and
|
|
37
|
+
* ragged the *same way* every time: the nudge that roughens it is a hash of
|
|
38
|
+
* each cell's own coordinates rather than a random number, so a re-render is
|
|
39
|
+
* not an animation and the same data screenshots twice.
|
|
40
|
+
*
|
|
41
|
+
* Cell counts are apportioned by largest remainder, so they add up to the
|
|
42
|
+
* budget exactly. A honeycomb whose parts came to one less than the whole would
|
|
43
|
+
* have a cell in it that nothing in the data accounts for.
|
|
44
|
+
*
|
|
45
|
+
* ## Drawing and animating
|
|
46
|
+
*
|
|
47
|
+
* Two hundred cells is two hundred nodes if each one is drawn on its own, which
|
|
48
|
+
* is more than this needs to spend. Every cell belonging to a series is
|
|
49
|
+
* concatenated into a *single* path instead, so the whole chart is one node per
|
|
50
|
+
* series plus one for the unfilled field — six or seven, whatever the cell
|
|
51
|
+
* count.
|
|
52
|
+
*
|
|
53
|
+
* That is also why the reveal is a clip rather than a per-cell stagger: the
|
|
54
|
+
* cells are no longer separate things to stagger. An ellipse grows from the
|
|
55
|
+
* centre of the field, in the field's own proportions, so the honeycomb is
|
|
56
|
+
* uncovered in the order the blob grew in — which is the same effect a stagger
|
|
57
|
+
* would have given, for one animated value instead of two hundred. A `grid`
|
|
58
|
+
* wipes across instead, because that is the order its cells were filled in.
|
|
59
|
+
*
|
|
60
|
+
* Touch, not hover: a series is selected by pressing one of its cells, and
|
|
61
|
+
* pressing it again clears the selection. There is no equivalent of a pointer
|
|
62
|
+
* resting somewhere without committing, so a chart that only revealed its
|
|
63
|
+
* numbers on hover would never reveal them at all.
|
|
64
|
+
*
|
|
65
|
+
* Colours come from the `--color-chart-*` tokens, so a chart follows the active
|
|
66
|
+
* theme and is put on brand by overriding those five. Nothing here hardcodes a
|
|
67
|
+
* hex.
|
|
68
|
+
*/
|
|
69
|
+
import {
|
|
70
|
+
Children,
|
|
71
|
+
createContext,
|
|
72
|
+
forwardRef,
|
|
73
|
+
isValidElement,
|
|
74
|
+
useContext,
|
|
75
|
+
useEffect,
|
|
76
|
+
useImperativeHandle,
|
|
77
|
+
useMemo,
|
|
78
|
+
useRef,
|
|
79
|
+
useState,
|
|
80
|
+
type ReactNode,
|
|
81
|
+
} from 'react';
|
|
82
|
+
import {
|
|
83
|
+
Pressable,
|
|
84
|
+
View,
|
|
85
|
+
type LayoutChangeEvent,
|
|
86
|
+
type ViewProps,
|
|
87
|
+
} from 'react-native';
|
|
88
|
+
import Animated, {
|
|
89
|
+
Easing,
|
|
90
|
+
useAnimatedProps,
|
|
91
|
+
useReducedMotion,
|
|
92
|
+
useSharedValue,
|
|
93
|
+
withTiming,
|
|
94
|
+
type SharedValue,
|
|
95
|
+
} from 'react-native-reanimated';
|
|
96
|
+
import Svg, { ClipPath, Defs, Ellipse, G, Path, Rect } from 'react-native-svg';
|
|
97
|
+
import { useCSSVariable } from 'uniwind';
|
|
98
|
+
import { Text } from '../../primitives/text';
|
|
99
|
+
import {
|
|
100
|
+
compactNumber,
|
|
101
|
+
hexAt,
|
|
102
|
+
hexCenter,
|
|
103
|
+
hexFillOrder,
|
|
104
|
+
hexMetrics,
|
|
105
|
+
hexPath,
|
|
106
|
+
hexRadiusFor,
|
|
107
|
+
hexRowsFor,
|
|
108
|
+
shareCounts,
|
|
109
|
+
useSeriesColor,
|
|
110
|
+
type ChartPoint,
|
|
111
|
+
type HexMetrics,
|
|
112
|
+
type HexShape,
|
|
113
|
+
} from '../../utils/chart';
|
|
114
|
+
import { cn } from '../../utils/cn';
|
|
115
|
+
|
|
116
|
+
const AnimatedEllipse = Animated.createAnimatedComponent(Ellipse);
|
|
117
|
+
const AnimatedRect = Animated.createAnimatedComponent(Rect);
|
|
118
|
+
|
|
119
|
+
/** Where a child is drawn: inside the SVG, over it, above it, or under it. */
|
|
120
|
+
type Slot = 'svg' | 'overlay' | 'header' | 'footer';
|
|
121
|
+
|
|
122
|
+
/** Whether the chart is showing data or waiting for it. */
|
|
123
|
+
export type HexChartStatus = 'loading' | 'ready';
|
|
124
|
+
|
|
125
|
+
export type { HexShape };
|
|
126
|
+
|
|
127
|
+
/** One series. Its share is worked out from the others, so there is no maximum. */
|
|
128
|
+
export interface HexDatum {
|
|
129
|
+
/** Name for the legend, the readout and the accessibility label. */
|
|
130
|
+
label: string;
|
|
131
|
+
/** How much of the whole this series is. Negatives are treated as zero. */
|
|
132
|
+
value: number;
|
|
133
|
+
/** Explicit colour, overriding the `--color-chart-*` token. */
|
|
134
|
+
color?: string;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Everything the layout works out once, and every part then reads. */
|
|
138
|
+
interface HexPlan {
|
|
139
|
+
/** One concatenated path per series, index-aligned with the data. */
|
|
140
|
+
paths: string[];
|
|
141
|
+
/** Every cell no series took, as one path. */
|
|
142
|
+
field: string;
|
|
143
|
+
/** Cells per series, index-aligned with the data. */
|
|
144
|
+
counts: number[];
|
|
145
|
+
/**
|
|
146
|
+
* Where a label for each series goes: the middle of its cells across, and
|
|
147
|
+
* the top of them down. A label centred on the cells would cover the ones it
|
|
148
|
+
* is naming, so it is hung above them instead.
|
|
149
|
+
*/
|
|
150
|
+
labelAnchors: ChartPoint[];
|
|
151
|
+
/** Which series owns each cell, by `row * columns + column`. `-1` is unfilled. */
|
|
152
|
+
owners: number[];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const EMPTY_PLAN: HexPlan = {
|
|
156
|
+
paths: [],
|
|
157
|
+
field: '',
|
|
158
|
+
counts: [],
|
|
159
|
+
labelAnchors: [],
|
|
160
|
+
owners: [],
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
interface HexChartContextValue {
|
|
164
|
+
data: HexDatum[];
|
|
165
|
+
/** Everything the values add up to. Zero when there is nothing to show. */
|
|
166
|
+
total: number;
|
|
167
|
+
width: number;
|
|
168
|
+
height: number;
|
|
169
|
+
metrics: HexMetrics;
|
|
170
|
+
columns: number;
|
|
171
|
+
rows: number;
|
|
172
|
+
/** Where the field starts inside the box, after it is centred in it. */
|
|
173
|
+
left: number;
|
|
174
|
+
top: number;
|
|
175
|
+
plan: HexPlan;
|
|
176
|
+
colors: string[];
|
|
177
|
+
status: HexChartStatus;
|
|
178
|
+
clipId: string;
|
|
179
|
+
activeIndex: number;
|
|
180
|
+
setActiveIndex: (index: number) => void;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const HexChartContext = createContext<HexChartContextValue | null>(null);
|
|
184
|
+
|
|
185
|
+
function useChart(component: string): HexChartContextValue {
|
|
186
|
+
const context = useContext(HexChartContext);
|
|
187
|
+
if (!context) {
|
|
188
|
+
throw new Error(`${component} must be used within a <HexChart>`);
|
|
189
|
+
}
|
|
190
|
+
return context;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** The selected series and its share, for something rendered inside the chart. */
|
|
194
|
+
export function useHexChart() {
|
|
195
|
+
const { data, plan, total, activeIndex } = useChart('useHexChart');
|
|
196
|
+
const series = activeIndex >= 0 ? (data[activeIndex] ?? null) : null;
|
|
197
|
+
return {
|
|
198
|
+
activeIndex,
|
|
199
|
+
activeSeries: series,
|
|
200
|
+
/** Its share of the whole, 0 to 1. */
|
|
201
|
+
activeFraction: series && total > 0 ? Math.max(0, series.value) / total : 0,
|
|
202
|
+
/** How many cells it was given, which is what the reader can count. */
|
|
203
|
+
activeCells: activeIndex >= 0 ? (plan.counts[activeIndex] ?? 0) : 0,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export interface HexChartProps extends ViewProps {
|
|
208
|
+
className?: string;
|
|
209
|
+
/** One entry per series. */
|
|
210
|
+
data: HexDatum[];
|
|
211
|
+
/**
|
|
212
|
+
* Cells across the field. The cell size follows from it and the measured
|
|
213
|
+
* width, so this is the one knob for how fine the honeycomb is.
|
|
214
|
+
*
|
|
215
|
+
* More cells resolve a smaller share — twenty-one across a phone is around
|
|
216
|
+
* two hundred and fifty in the field, so roughly a half a percent each — at
|
|
217
|
+
* the cost of every cell getting smaller and harder to press.
|
|
218
|
+
*/
|
|
219
|
+
columns?: number;
|
|
220
|
+
/** Width over height of the field. */
|
|
221
|
+
aspectRatio?: number;
|
|
222
|
+
/**
|
|
223
|
+
* How much of the field the series fill, 0 to 1.
|
|
224
|
+
*
|
|
225
|
+
* Only meaningful with `shape="blob"`, where the unfilled cells are the
|
|
226
|
+
* margin the blob is read against; a `grid` fills every cell, because a
|
|
227
|
+
* waffle with a ragged last row is a waffle that has stopped being countable.
|
|
228
|
+
*/
|
|
229
|
+
density?: number;
|
|
230
|
+
/** How the filled cells are arranged. */
|
|
231
|
+
shape?: HexShape;
|
|
232
|
+
/**
|
|
233
|
+
* The gap between cells, as a share of the cell radius. Given as a share so
|
|
234
|
+
* the field keeps its proportions at whatever size it is measured at.
|
|
235
|
+
*/
|
|
236
|
+
cellGap?: number;
|
|
237
|
+
/** Milliseconds for the honeycomb to fill in. */
|
|
238
|
+
animationDuration?: number;
|
|
239
|
+
/** `loading` draws the field with nothing divided up yet. */
|
|
240
|
+
status?: HexChartStatus;
|
|
241
|
+
/** Selected series. Leave unset to let the chart track it. */
|
|
242
|
+
activeIndex?: number;
|
|
243
|
+
/** Fires with the selected series, or `-1` when the selection is cleared. */
|
|
244
|
+
onActiveIndexChange?: (index: number) => void;
|
|
245
|
+
children?: ReactNode;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Imperative handle: re-run the fill, for a "replay" control. */
|
|
249
|
+
export interface HexChartHandle {
|
|
250
|
+
replay: () => void;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* How far past the field the reveal has to reach to cover its corners.
|
|
255
|
+
*
|
|
256
|
+
* An ellipse with semi-axes `a`, `b` contains the box `2w × 2h` only when
|
|
257
|
+
* `(w/a)² + (h/b)² ≤ 1`; growing both axes in the box's own proportions makes
|
|
258
|
+
* that `2/k² ≤ 1`. Stopping at the edges instead would leave the four corner
|
|
259
|
+
* cells of a `grid` uncovered for good.
|
|
260
|
+
*/
|
|
261
|
+
const CORNER_REACH = Math.SQRT2;
|
|
262
|
+
|
|
263
|
+
const HexChartRoot = forwardRef<HexChartHandle, HexChartProps>(function HexChartRoot(
|
|
264
|
+
{
|
|
265
|
+
className,
|
|
266
|
+
data,
|
|
267
|
+
columns = 21,
|
|
268
|
+
aspectRatio = 1.6,
|
|
269
|
+
density = 0.55,
|
|
270
|
+
shape = 'blob',
|
|
271
|
+
cellGap = 0.14,
|
|
272
|
+
animationDuration = 900,
|
|
273
|
+
status = 'ready',
|
|
274
|
+
activeIndex: activeIndexProp,
|
|
275
|
+
onActiveIndexChange,
|
|
276
|
+
children,
|
|
277
|
+
...props
|
|
278
|
+
},
|
|
279
|
+
ref
|
|
280
|
+
) {
|
|
281
|
+
const [measured, setMeasured] = useState(0);
|
|
282
|
+
const [internalActive, setInternalActive] = useState(-1);
|
|
283
|
+
const reveal = useSharedValue(0);
|
|
284
|
+
const reducedMotion = useReducedMotion();
|
|
285
|
+
const clipId = useRef(
|
|
286
|
+
`panelui-hex-${Math.random().toString(36).slice(2, 9)}`
|
|
287
|
+
).current;
|
|
288
|
+
|
|
289
|
+
const controlled = activeIndexProp !== undefined;
|
|
290
|
+
const activeIndex = controlled ? activeIndexProp : internalActive;
|
|
291
|
+
|
|
292
|
+
const setActiveIndex = useMemo(
|
|
293
|
+
() => (index: number) => {
|
|
294
|
+
if (!controlled) setInternalActive(index);
|
|
295
|
+
onActiveIndexChange?.(index);
|
|
296
|
+
},
|
|
297
|
+
[controlled, onActiveIndexChange]
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
const width = measured;
|
|
301
|
+
const height = aspectRatio > 0 ? width / aspectRatio : 0;
|
|
302
|
+
|
|
303
|
+
const total = useMemo(
|
|
304
|
+
() => data.reduce((sum, series) => sum + Math.max(0, series.value), 0),
|
|
305
|
+
[data]
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
const columnCount = Math.max(1, Math.round(columns));
|
|
309
|
+
const metrics = useMemo(
|
|
310
|
+
() => hexMetrics(hexRadiusFor(width, columnCount)),
|
|
311
|
+
[width, columnCount]
|
|
312
|
+
);
|
|
313
|
+
const rows = useMemo(() => hexRowsFor(height, metrics), [height, metrics]);
|
|
314
|
+
|
|
315
|
+
// The field rarely comes out exactly the size of the box — the rows are a
|
|
316
|
+
// whole number and the height is not — so what is left over is split either
|
|
317
|
+
// side of it rather than left at the bottom.
|
|
318
|
+
const fieldWidth = metrics.stepX * (columnCount + 0.5);
|
|
319
|
+
const fieldHeight = (rows - 1) * metrics.stepY + metrics.height;
|
|
320
|
+
const left = (width - fieldWidth) / 2;
|
|
321
|
+
const top = (height - fieldHeight) / 2;
|
|
322
|
+
|
|
323
|
+
const loading = status === 'loading';
|
|
324
|
+
|
|
325
|
+
const plan = useMemo<HexPlan>(() => {
|
|
326
|
+
if (metrics.radius <= 0 || rows <= 0 || !data.length) return EMPTY_PLAN;
|
|
327
|
+
|
|
328
|
+
const cells = hexFillOrder(columnCount, rows, shape);
|
|
329
|
+
const capacity = columnCount * rows;
|
|
330
|
+
// Loading has no split to show yet, so nothing is taken and the whole field
|
|
331
|
+
// is left for the skeleton to draw.
|
|
332
|
+
const budget = loading
|
|
333
|
+
? 0
|
|
334
|
+
: shape === 'grid'
|
|
335
|
+
? capacity
|
|
336
|
+
: Math.max(0, Math.min(capacity, Math.round(capacity * density)));
|
|
337
|
+
|
|
338
|
+
const counts = shareCounts(
|
|
339
|
+
data.map((series) => series.value),
|
|
340
|
+
budget
|
|
341
|
+
);
|
|
342
|
+
|
|
343
|
+
/*
|
|
344
|
+
* Smallest first for a blob, so the series that is hardest to find gets the
|
|
345
|
+
* middle — where it is most findable — and each larger one wraps around it.
|
|
346
|
+
* Data order for a grid, where reading order is the entire point of the
|
|
347
|
+
* arrangement and reordering it would break the count.
|
|
348
|
+
*/
|
|
349
|
+
const sequence = data.map((_, index) => index);
|
|
350
|
+
if (shape === 'blob') {
|
|
351
|
+
sequence.sort((a, b) => counts[a]! - counts[b]! || a - b);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const radius = metrics.radius * (1 - Math.max(0, Math.min(cellGap, 0.5)));
|
|
355
|
+
const owners = new Array<number>(capacity).fill(-1);
|
|
356
|
+
const paths = data.map(() => '');
|
|
357
|
+
const sums = data.map(() => ({ x: 0, highest: Infinity }));
|
|
358
|
+
|
|
359
|
+
let cursor = 0;
|
|
360
|
+
for (const series of sequence) {
|
|
361
|
+
for (let n = 0; n < counts[series]!; n += 1) {
|
|
362
|
+
const cell = cells[cursor];
|
|
363
|
+
cursor += 1;
|
|
364
|
+
if (!cell) break;
|
|
365
|
+
owners[cell.row * columnCount + cell.column] = series;
|
|
366
|
+
const centre = hexCenter(cell.column, cell.row, metrics, left, top);
|
|
367
|
+
paths[series] += hexPath(centre.x, centre.y, radius);
|
|
368
|
+
sums[series]!.x += centre.x;
|
|
369
|
+
sums[series]!.highest = Math.min(sums[series]!.highest, centre.y);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
let field = '';
|
|
374
|
+
for (let row = 0; row < rows; row += 1) {
|
|
375
|
+
for (let column = 0; column < columnCount; column += 1) {
|
|
376
|
+
if (owners[row * columnCount + column]! >= 0) continue;
|
|
377
|
+
const centre = hexCenter(column, row, metrics, left, top);
|
|
378
|
+
field += hexPath(centre.x, centre.y, radius);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const labelAnchors = sums.map((sum, index) => {
|
|
383
|
+
const count = counts[index] ?? 0;
|
|
384
|
+
if (count <= 0) return { x: left + fieldWidth / 2, y: top };
|
|
385
|
+
// The top edge of the series' highest cell, not its centre — the anchor
|
|
386
|
+
// is what the label is hung *from*, and half a cell of overlap is still
|
|
387
|
+
// overlap.
|
|
388
|
+
return { x: sum.x / count, y: sum.highest - metrics.radius };
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
return { paths, field, counts, labelAnchors, owners };
|
|
392
|
+
}, [
|
|
393
|
+
data,
|
|
394
|
+
metrics,
|
|
395
|
+
rows,
|
|
396
|
+
columnCount,
|
|
397
|
+
shape,
|
|
398
|
+
density,
|
|
399
|
+
cellGap,
|
|
400
|
+
left,
|
|
401
|
+
top,
|
|
402
|
+
fieldWidth,
|
|
403
|
+
fieldHeight,
|
|
404
|
+
loading,
|
|
405
|
+
]);
|
|
406
|
+
|
|
407
|
+
const playReveal = useMemo(
|
|
408
|
+
() => () => {
|
|
409
|
+
if (reducedMotion) {
|
|
410
|
+
reveal.value = 1;
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
reveal.value = 0;
|
|
414
|
+
reveal.value = withTiming(1, {
|
|
415
|
+
duration: animationDuration,
|
|
416
|
+
easing: Easing.bezier(0.85, 0, 0.15, 1),
|
|
417
|
+
});
|
|
418
|
+
},
|
|
419
|
+
[reducedMotion, animationDuration, reveal]
|
|
420
|
+
);
|
|
421
|
+
|
|
422
|
+
const revealed = useRef(false);
|
|
423
|
+
|
|
424
|
+
useEffect(() => {
|
|
425
|
+
if (loading) {
|
|
426
|
+
revealed.current = false;
|
|
427
|
+
reveal.value = 0;
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
if (revealed.current || width <= 0 || !data.length) return;
|
|
431
|
+
revealed.current = true;
|
|
432
|
+
playReveal();
|
|
433
|
+
}, [loading, width, data.length, playReveal, reveal]);
|
|
434
|
+
|
|
435
|
+
useImperativeHandle(ref, () => ({ replay: playReveal }), [playReveal]);
|
|
436
|
+
|
|
437
|
+
// Resolved here rather than inside the cells, so the legend, the header and
|
|
438
|
+
// the tooltip can name a series' colour without drawing one.
|
|
439
|
+
const c1 = useSeriesColor(undefined, 1);
|
|
440
|
+
const c2 = useSeriesColor(undefined, 2);
|
|
441
|
+
const c3 = useSeriesColor(undefined, 3);
|
|
442
|
+
const c4 = useSeriesColor(undefined, 4);
|
|
443
|
+
const c5 = useSeriesColor(undefined, 5);
|
|
444
|
+
const palette = useMemo(() => [c1, c2, c3, c4, c5], [c1, c2, c3, c4, c5]);
|
|
445
|
+
const colors = useMemo(
|
|
446
|
+
() => data.map((series, index) => series.color ?? palette[index % palette.length]!),
|
|
447
|
+
[data, palette]
|
|
448
|
+
);
|
|
449
|
+
|
|
450
|
+
const onLayout = (event: LayoutChangeEvent) => {
|
|
451
|
+
const next = Math.round(event.nativeEvent.layout.width);
|
|
452
|
+
if (next !== measured) setMeasured(next);
|
|
453
|
+
props.onLayout?.(event);
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
const ellipseProps = useAnimatedProps(() => ({
|
|
457
|
+
rx: (fieldWidth / 2) * CORNER_REACH * reveal.value,
|
|
458
|
+
ry: (fieldHeight / 2) * CORNER_REACH * reveal.value,
|
|
459
|
+
}));
|
|
460
|
+
|
|
461
|
+
const rectProps = useAnimatedProps(() => ({ width: width * reveal.value }));
|
|
462
|
+
|
|
463
|
+
const context = useMemo<HexChartContextValue>(
|
|
464
|
+
() => ({
|
|
465
|
+
data,
|
|
466
|
+
total,
|
|
467
|
+
width,
|
|
468
|
+
height,
|
|
469
|
+
metrics,
|
|
470
|
+
columns: columnCount,
|
|
471
|
+
rows,
|
|
472
|
+
left,
|
|
473
|
+
top,
|
|
474
|
+
plan,
|
|
475
|
+
colors,
|
|
476
|
+
status,
|
|
477
|
+
clipId,
|
|
478
|
+
activeIndex,
|
|
479
|
+
setActiveIndex,
|
|
480
|
+
}),
|
|
481
|
+
[
|
|
482
|
+
data,
|
|
483
|
+
total,
|
|
484
|
+
width,
|
|
485
|
+
height,
|
|
486
|
+
metrics,
|
|
487
|
+
columnCount,
|
|
488
|
+
rows,
|
|
489
|
+
left,
|
|
490
|
+
top,
|
|
491
|
+
plan,
|
|
492
|
+
colors,
|
|
493
|
+
status,
|
|
494
|
+
clipId,
|
|
495
|
+
activeIndex,
|
|
496
|
+
setActiveIndex,
|
|
497
|
+
]
|
|
498
|
+
);
|
|
499
|
+
|
|
500
|
+
const slots: Record<Slot, ReactNode[]> = {
|
|
501
|
+
svg: [],
|
|
502
|
+
overlay: [],
|
|
503
|
+
header: [],
|
|
504
|
+
footer: [],
|
|
505
|
+
};
|
|
506
|
+
Children.forEach(children, (child, index) => {
|
|
507
|
+
if (!isValidElement(child)) return;
|
|
508
|
+
const slot = (child.type as { slot?: Slot }).slot ?? 'overlay';
|
|
509
|
+
slots[slot in slots ? slot : 'overlay'].push(
|
|
510
|
+
<ChildSlot key={index}>{child}</ChildSlot>
|
|
511
|
+
);
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
return (
|
|
515
|
+
<HexChartContext.Provider value={context}>
|
|
516
|
+
{/*
|
|
517
|
+
* Two views, because the header is not part of the field. The aspect
|
|
518
|
+
* ratio and the layout measurement belong to the drawing area alone —
|
|
519
|
+
* measured on the outer view they would take in the header too, and the
|
|
520
|
+
* honeycomb would be laid out inside a box taller than the one it is
|
|
521
|
+
* drawn in.
|
|
522
|
+
*/}
|
|
523
|
+
<View {...props} style={props.style} className={cn('w-full', className)}>
|
|
524
|
+
{slots.header}
|
|
525
|
+
<View onLayout={onLayout} style={{ aspectRatio }} className="w-full">
|
|
526
|
+
{width > 0 && height > 0 ? (
|
|
527
|
+
<>
|
|
528
|
+
<Svg width={width} height={height}>
|
|
529
|
+
<Defs>
|
|
530
|
+
<ClipPath id={clipId}>
|
|
531
|
+
{shape === 'blob' ? (
|
|
532
|
+
<AnimatedEllipse
|
|
533
|
+
cx={left + fieldWidth / 2}
|
|
534
|
+
cy={top + fieldHeight / 2}
|
|
535
|
+
animatedProps={ellipseProps}
|
|
536
|
+
/>
|
|
537
|
+
) : (
|
|
538
|
+
<AnimatedRect x={0} y={0} height={height} animatedProps={rectProps} />
|
|
539
|
+
)}
|
|
540
|
+
</ClipPath>
|
|
541
|
+
</Defs>
|
|
542
|
+
{slots.svg}
|
|
543
|
+
</Svg>
|
|
544
|
+
{/*
|
|
545
|
+
* Anything with text or a touch on it goes over the SVG rather
|
|
546
|
+
* than inside it: SVG text ignores the platform's text scaling
|
|
547
|
+
* and the theme's font, and a press cannot be wired to an SVG
|
|
548
|
+
* node with a role attached.
|
|
549
|
+
*/}
|
|
550
|
+
<View
|
|
551
|
+
pointerEvents="box-none"
|
|
552
|
+
style={{ position: 'absolute', width, height }}
|
|
553
|
+
>
|
|
554
|
+
{slots.overlay}
|
|
555
|
+
</View>
|
|
556
|
+
</>
|
|
557
|
+
) : null}
|
|
558
|
+
</View>
|
|
559
|
+
{slots.footer}
|
|
560
|
+
</View>
|
|
561
|
+
</HexChartContext.Provider>
|
|
562
|
+
);
|
|
563
|
+
});
|
|
564
|
+
HexChartRoot.displayName = 'HexChart';
|
|
565
|
+
|
|
566
|
+
function ChildSlot({ children }: { children: ReactNode }) {
|
|
567
|
+
return <>{children}</>;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
export interface HexChartCellsProps {
|
|
571
|
+
/** Colour of the cells no series took. Defaults to the muted token. */
|
|
572
|
+
emptyColor?: string;
|
|
573
|
+
/** Opacity of the series that are not selected, once one is. */
|
|
574
|
+
dimOpacity?: number;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* The honeycomb: the unfilled field, and one path per series over it.
|
|
579
|
+
*
|
|
580
|
+
* One part rather than one per series. Every cell shares a radius, a gap and a
|
|
581
|
+
* grid by definition — a chart where one series' cells could be given a size of
|
|
582
|
+
* their own would be a chart drawing a lie, since the whole claim of the shape
|
|
583
|
+
* is that one cell means the same thing wherever it appears.
|
|
584
|
+
*
|
|
585
|
+
* The field is drawn outside the reveal's clip and the series inside it, so the
|
|
586
|
+
* total is there from the first frame and what fills in against it is the
|
|
587
|
+
* split. Uncovering both together would animate the denominator, which is not
|
|
588
|
+
* something that changed.
|
|
589
|
+
*/
|
|
590
|
+
function HexChartCells({ emptyColor, dimOpacity = 0.25 }: HexChartCellsProps) {
|
|
591
|
+
const { data, plan, total, colors, status, clipId, activeIndex } =
|
|
592
|
+
useChart('HexChart.Cells');
|
|
593
|
+
const token = useCSSVariable('--color-muted');
|
|
594
|
+
const empty = emptyColor ?? (typeof token === 'string' ? token : 'rgba(128,128,128,0.08)');
|
|
595
|
+
|
|
596
|
+
if (status === 'loading') return null;
|
|
597
|
+
|
|
598
|
+
return (
|
|
599
|
+
<G>
|
|
600
|
+
{plan.field ? <Path d={plan.field} fill={empty} /> : null}
|
|
601
|
+
<G clipPath={`url(#${clipId})`}>
|
|
602
|
+
{plan.paths.map((d, index) => {
|
|
603
|
+
const series = data[index];
|
|
604
|
+
if (!d || !series) return null;
|
|
605
|
+
const percent = total > 0 ? Math.round((Math.max(0, series.value) / total) * 100) : 0;
|
|
606
|
+
return (
|
|
607
|
+
<Path
|
|
608
|
+
key={series.label}
|
|
609
|
+
d={d}
|
|
610
|
+
fill={colors[index]}
|
|
611
|
+
fillOpacity={activeIndex >= 0 && activeIndex !== index ? dimOpacity : 1}
|
|
612
|
+
// An SVG node takes a label but not a role, so the series are
|
|
613
|
+
// named without being announced as buttons. `HexChart.Legend` is
|
|
614
|
+
// the properly wired way through the same selection, and the
|
|
615
|
+
// easier target of the two.
|
|
616
|
+
accessibilityLabel={`${series.label}, ${percent} percent`}
|
|
617
|
+
/>
|
|
618
|
+
);
|
|
619
|
+
})}
|
|
620
|
+
</G>
|
|
621
|
+
</G>
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
HexChartCells.displayName = 'HexChart.Cells';
|
|
625
|
+
HexChartCells.slot = 'svg' as const;
|
|
626
|
+
|
|
627
|
+
export interface HexChartSkeletonProps {
|
|
628
|
+
color?: string;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* The loading state: the field, with nothing divided up yet.
|
|
633
|
+
*
|
|
634
|
+
* Deliberately undivided. Placeholder shares would be a made-up split, and a
|
|
635
|
+
* reader has no way to tell an invented one from a real one until it changes
|
|
636
|
+
* under them — which is worse than showing nothing, because it is showing
|
|
637
|
+
* something wrong.
|
|
638
|
+
*/
|
|
639
|
+
function HexChartSkeleton({ color }: HexChartSkeletonProps) {
|
|
640
|
+
const { plan, status } = useChart('HexChart.Skeleton');
|
|
641
|
+
const token = useCSSVariable('--color-skeleton');
|
|
642
|
+
const fill = color ?? (typeof token === 'string' ? token : 'rgba(128,128,128,0.2)');
|
|
643
|
+
|
|
644
|
+
if (status !== 'loading' || !plan.field) return null;
|
|
645
|
+
|
|
646
|
+
return <Path d={plan.field} fill={fill} />;
|
|
647
|
+
}
|
|
648
|
+
HexChartSkeleton.displayName = 'HexChart.Skeleton';
|
|
649
|
+
HexChartSkeleton.slot = 'svg' as const;
|
|
650
|
+
|
|
651
|
+
export interface HexChartTooltipProps {
|
|
652
|
+
/** Format the selected series' value. Defaults to a compact number. */
|
|
653
|
+
formatValue?: (value: number, series: HexDatum) => string;
|
|
654
|
+
/** Show the count of cells beside the share. */
|
|
655
|
+
showCells?: boolean;
|
|
656
|
+
className?: string;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/** Clearance between the label and the cells it is naming. */
|
|
660
|
+
const LABEL_GAP = 6;
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* The press target over the honeycomb, and the label that names what was
|
|
664
|
+
* pressed.
|
|
665
|
+
*
|
|
666
|
+
* The label hangs *above* the selected series rather than on it: it is centred
|
|
667
|
+
* across that series' cells and sits clear of the highest one, so the cells
|
|
668
|
+
* being read are never underneath the thing reading them. Two other places it
|
|
669
|
+
* could go are both worse — under the finger is the one part of the chart
|
|
670
|
+
* nobody can see, and on the middle of the series covers the mass the reader
|
|
671
|
+
* just asked about.
|
|
672
|
+
*
|
|
673
|
+
* A series reaching the top of the field pushes the label back inside it, which
|
|
674
|
+
* is the one case it does overlap. That series is the largest one, and its
|
|
675
|
+
* topmost cells are the part of it a reader is least likely to be counting.
|
|
676
|
+
*
|
|
677
|
+
* A press on an unfilled cell clears the selection, the same as pressing the
|
|
678
|
+
* selected series again. Everything outside the honeycomb is "none of them",
|
|
679
|
+
* and making that gesture do nothing would leave a chart you can select in but
|
|
680
|
+
* not out of.
|
|
681
|
+
*/
|
|
682
|
+
function HexChartTooltip({ formatValue, showCells = false, className }: HexChartTooltipProps) {
|
|
683
|
+
const {
|
|
684
|
+
data,
|
|
685
|
+
plan,
|
|
686
|
+
total,
|
|
687
|
+
colors,
|
|
688
|
+
metrics,
|
|
689
|
+
columns,
|
|
690
|
+
rows,
|
|
691
|
+
left,
|
|
692
|
+
top,
|
|
693
|
+
width,
|
|
694
|
+
height,
|
|
695
|
+
status,
|
|
696
|
+
activeIndex,
|
|
697
|
+
setActiveIndex,
|
|
698
|
+
} = useChart('HexChart.Tooltip');
|
|
699
|
+
|
|
700
|
+
const series = activeIndex >= 0 ? (data[activeIndex] ?? null) : null;
|
|
701
|
+
const format = formatValue ?? ((value: number) => compactNumber(value));
|
|
702
|
+
|
|
703
|
+
const onPress = (x: number, y: number) => {
|
|
704
|
+
// The shared hit test, called straight from the press handler: it is marked
|
|
705
|
+
// as a worklet for the charts that scrub on the UI thread, and a worklet is
|
|
706
|
+
// an ordinary function everywhere else. A press is one event, and the whole
|
|
707
|
+
// point of resolving it is to hand an index back to React anyway.
|
|
708
|
+
const cell = hexAt(x, y, metrics, left, top, columns, rows);
|
|
709
|
+
const owner = cell ? (plan.owners[cell.row * columns + cell.column] ?? -1) : -1;
|
|
710
|
+
setActiveIndex(owner === activeIndex ? -1 : owner);
|
|
711
|
+
};
|
|
712
|
+
|
|
713
|
+
if (status === 'loading' || width <= 0) return null;
|
|
714
|
+
|
|
715
|
+
const anchor = series ? plan.labelAnchors[activeIndex] : null;
|
|
716
|
+
const percent = series && total > 0 ? Math.round((Math.max(0, series.value) / total) * 100) : 0;
|
|
717
|
+
|
|
718
|
+
return (
|
|
719
|
+
<>
|
|
720
|
+
<Pressable
|
|
721
|
+
accessibilityLabel="Select a series"
|
|
722
|
+
onPress={(event) =>
|
|
723
|
+
onPress(event.nativeEvent.locationX, event.nativeEvent.locationY)
|
|
724
|
+
}
|
|
725
|
+
style={{ position: 'absolute', left: 0, top: 0, width, height }}
|
|
726
|
+
/>
|
|
727
|
+
{series && anchor ? (
|
|
728
|
+
// Keyed on the selection so the label remounts, and measures itself
|
|
729
|
+
// again, whenever what it has to say changes length.
|
|
730
|
+
<HexChartLabel
|
|
731
|
+
key={activeIndex}
|
|
732
|
+
anchor={anchor}
|
|
733
|
+
width={width}
|
|
734
|
+
height={height}
|
|
735
|
+
color={colors[activeIndex]}
|
|
736
|
+
value={format(series.value, series)}
|
|
737
|
+
detail={
|
|
738
|
+
showCells
|
|
739
|
+
? `${percent}% · ${plan.counts[activeIndex] ?? 0} cells`
|
|
740
|
+
: `${percent}%`
|
|
741
|
+
}
|
|
742
|
+
className={className}
|
|
743
|
+
/>
|
|
744
|
+
) : null}
|
|
745
|
+
</>
|
|
746
|
+
);
|
|
747
|
+
}
|
|
748
|
+
HexChartTooltip.displayName = 'HexChart.Tooltip';
|
|
749
|
+
HexChartTooltip.slot = 'overlay' as const;
|
|
750
|
+
|
|
751
|
+
/**
|
|
752
|
+
* The label itself, sized by its contents and placed once it knows its size.
|
|
753
|
+
*
|
|
754
|
+
* A fixed box would be simpler to place, and it is what a crosshair label on a
|
|
755
|
+
* time series gets away with — there the text is one formatted number and its
|
|
756
|
+
* width is known within a few points. Here it is a value, a percentage and
|
|
757
|
+
* sometimes a cell count, and a currency total in the millions is half again as
|
|
758
|
+
* wide as a compact one. A box that does not fit its text does not clip it in
|
|
759
|
+
* React Native, it lets it run out of the corner, so the width has to come from
|
|
760
|
+
* the text rather than the other way round.
|
|
761
|
+
*
|
|
762
|
+
* Which means one frame where the size is not known yet. That frame is spent at
|
|
763
|
+
* zero opacity rather than in the wrong place — the same trade the walkthrough
|
|
764
|
+
* card makes, and for the same reason: a label that arrives correct is better
|
|
765
|
+
* than one that arrives early and jumps.
|
|
766
|
+
*/
|
|
767
|
+
function HexChartLabel({
|
|
768
|
+
anchor,
|
|
769
|
+
width,
|
|
770
|
+
height,
|
|
771
|
+
color,
|
|
772
|
+
value,
|
|
773
|
+
detail,
|
|
774
|
+
className,
|
|
775
|
+
}: {
|
|
776
|
+
anchor: ChartPoint;
|
|
777
|
+
width: number;
|
|
778
|
+
height: number;
|
|
779
|
+
color: string | undefined;
|
|
780
|
+
value: string;
|
|
781
|
+
detail: string;
|
|
782
|
+
className?: string;
|
|
783
|
+
}) {
|
|
784
|
+
const [size, setSize] = useState<{ width: number; height: number } | null>(null);
|
|
785
|
+
|
|
786
|
+
const placed = size
|
|
787
|
+
? {
|
|
788
|
+
// Centred across the series and hung above it, then clamped inside the
|
|
789
|
+
// field so one whose cells sit against an edge does not get a label
|
|
790
|
+
// hanging off it.
|
|
791
|
+
left: Math.max(0, Math.min(width - size.width, anchor.x - size.width / 2)),
|
|
792
|
+
top: Math.max(0, Math.min(height - size.height, anchor.y - LABEL_GAP - size.height)),
|
|
793
|
+
}
|
|
794
|
+
: { left: 0, top: 0 };
|
|
795
|
+
|
|
796
|
+
return (
|
|
797
|
+
<View
|
|
798
|
+
pointerEvents="none"
|
|
799
|
+
onLayout={(event) => {
|
|
800
|
+
const { width: w, height: h } = event.nativeEvent.layout;
|
|
801
|
+
setSize((current) =>
|
|
802
|
+
current && Math.abs(current.width - w) < 1 && Math.abs(current.height - h) < 1
|
|
803
|
+
? current
|
|
804
|
+
: { width: w, height: h }
|
|
805
|
+
);
|
|
806
|
+
}}
|
|
807
|
+
style={{
|
|
808
|
+
position: 'absolute',
|
|
809
|
+
...placed,
|
|
810
|
+
opacity: size ? 1 : 0,
|
|
811
|
+
// Never wider than the field, so a very long value wraps or ellipsises
|
|
812
|
+
// inside the label instead of widening it off the edge.
|
|
813
|
+
maxWidth: width,
|
|
814
|
+
}}
|
|
815
|
+
className={cn(
|
|
816
|
+
'flex-row items-center gap-1.5 rounded-lg border border-border bg-overlay px-2 py-1 shadow-md',
|
|
817
|
+
className
|
|
818
|
+
)}
|
|
819
|
+
>
|
|
820
|
+
<View style={{ width: 8, height: 8, borderRadius: 4, backgroundColor: color }} />
|
|
821
|
+
<Text size="xs" weight="medium" numberOfLines={1} className="shrink">
|
|
822
|
+
{value}
|
|
823
|
+
</Text>
|
|
824
|
+
<Text size="xs" muted numberOfLines={1} className="shrink">
|
|
825
|
+
{detail}
|
|
826
|
+
</Text>
|
|
827
|
+
</View>
|
|
828
|
+
);
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
export interface HexChartLegendProps extends ViewProps {
|
|
832
|
+
className?: string;
|
|
833
|
+
/** Show each series' share of the whole beside its name. */
|
|
834
|
+
showValue?: boolean;
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/**
|
|
838
|
+
* A swatch, a name and a share per series, under the chart and across the width
|
|
839
|
+
* of it. Pressable in the same way the cells are — the legend is usually the
|
|
840
|
+
* easier target of the two, and a series worth a couple of percent is a handful
|
|
841
|
+
* of cells that may not be adjacent.
|
|
842
|
+
*
|
|
843
|
+
* It wraps rather than stacking, so five or six entries take two lines instead
|
|
844
|
+
* of six. A key is a lookup table, and a lookup table read down a column of one
|
|
845
|
+
* word each is a column the eye has to walk.
|
|
846
|
+
*/
|
|
847
|
+
function HexChartLegend({ className, showValue = true, ...props }: HexChartLegendProps) {
|
|
848
|
+
const { data, total, colors, activeIndex, setActiveIndex } = useChart('HexChart.Legend');
|
|
849
|
+
|
|
850
|
+
if (!data.length) return null;
|
|
851
|
+
|
|
852
|
+
return (
|
|
853
|
+
<View
|
|
854
|
+
{...props}
|
|
855
|
+
className={cn(
|
|
856
|
+
'w-full flex-row flex-wrap items-center justify-center gap-x-3 gap-y-1.5 pt-3',
|
|
857
|
+
className
|
|
858
|
+
)}
|
|
859
|
+
>
|
|
860
|
+
{data.map((series, index) => {
|
|
861
|
+
const percent = total > 0 ? Math.round((Math.max(0, series.value) / total) * 100) : 0;
|
|
862
|
+
const dimmed = activeIndex >= 0 && activeIndex !== index;
|
|
863
|
+
return (
|
|
864
|
+
<Pressable
|
|
865
|
+
key={series.label}
|
|
866
|
+
accessibilityRole="button"
|
|
867
|
+
accessibilityLabel={`${series.label}, ${percent} percent`}
|
|
868
|
+
onPress={() => setActiveIndex(activeIndex === index ? -1 : index)}
|
|
869
|
+
style={{ opacity: dimmed ? 0.4 : 1 }}
|
|
870
|
+
className="max-w-full flex-row items-center gap-1.5"
|
|
871
|
+
>
|
|
872
|
+
<View
|
|
873
|
+
style={{
|
|
874
|
+
width: 8,
|
|
875
|
+
height: 8,
|
|
876
|
+
borderRadius: 4,
|
|
877
|
+
backgroundColor: colors[index],
|
|
878
|
+
}}
|
|
879
|
+
/>
|
|
880
|
+
<Text size="xs" muted numberOfLines={1} className="shrink">
|
|
881
|
+
{series.label}
|
|
882
|
+
</Text>
|
|
883
|
+
{showValue ? (
|
|
884
|
+
<Text size="xs" weight="medium">
|
|
885
|
+
{percent}%
|
|
886
|
+
</Text>
|
|
887
|
+
) : null}
|
|
888
|
+
</Pressable>
|
|
889
|
+
);
|
|
890
|
+
})}
|
|
891
|
+
</View>
|
|
892
|
+
);
|
|
893
|
+
}
|
|
894
|
+
HexChartLegend.displayName = 'HexChart.Legend';
|
|
895
|
+
HexChartLegend.slot = 'footer' as const;
|
|
896
|
+
|
|
897
|
+
export interface HexChartHeaderProps extends ViewProps {
|
|
898
|
+
className?: string;
|
|
899
|
+
/** Small line above the value — what the chart is of. */
|
|
900
|
+
title?: string;
|
|
901
|
+
/** The readout. The largest thing on the card, and the first thing read. */
|
|
902
|
+
value?: string;
|
|
903
|
+
/** One muted line under the value — a period, a comparison, a caveat. */
|
|
904
|
+
caption?: string;
|
|
905
|
+
/** Prettier names for the series, keyed by their `label`. */
|
|
906
|
+
labels?: Record<string, string>;
|
|
907
|
+
/**
|
|
908
|
+
* Draw a swatch and a name per series along the trailing edge.
|
|
909
|
+
*
|
|
910
|
+
* For two or three short names. Past that use `HexChart.Legend`, which runs
|
|
911
|
+
* under the chart across the full width: a key of five long names crammed
|
|
912
|
+
* into the trailing corner of a header wraps to a column and leaves the title
|
|
913
|
+
* beside it a few points wide.
|
|
914
|
+
*/
|
|
915
|
+
legend?: boolean;
|
|
916
|
+
/** Trailing slot — a control, a badge, a range picker. Wins over `legend`. */
|
|
917
|
+
children?: ReactNode;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
/**
|
|
921
|
+
* The strip above the honeycomb: what the chart is of, what it reads, and what
|
|
922
|
+
* the colours mean.
|
|
923
|
+
*
|
|
924
|
+
* It belongs to the chart rather than to the card around it because it is about
|
|
925
|
+
* the *series* — the number changes as one is selected, and the legend is the
|
|
926
|
+
* list the chart itself is holding. The card's header is a caption on the tray
|
|
927
|
+
* the chart sits in; this is the chart introducing itself.
|
|
928
|
+
*
|
|
929
|
+
* The value is not derived here even though there is a total to derive it from,
|
|
930
|
+
* because the formatting is not the chart's to guess: a total of 6750 is a
|
|
931
|
+
* count, a currency or a percentage depending on what was counted, and only the
|
|
932
|
+
* caller knows which.
|
|
933
|
+
*/
|
|
934
|
+
function HexChartHeader({
|
|
935
|
+
className,
|
|
936
|
+
title,
|
|
937
|
+
value,
|
|
938
|
+
caption,
|
|
939
|
+
labels,
|
|
940
|
+
legend = false,
|
|
941
|
+
children,
|
|
942
|
+
...props
|
|
943
|
+
}: HexChartHeaderProps) {
|
|
944
|
+
const { data, colors } = useChart('HexChart.Header');
|
|
945
|
+
const trailing =
|
|
946
|
+
children ??
|
|
947
|
+
(legend && data.length ? (
|
|
948
|
+
<View className="flex-row flex-wrap items-center justify-end gap-x-3 gap-y-1">
|
|
949
|
+
{data.map((series, index) => (
|
|
950
|
+
<View key={series.label} className="flex-row items-center gap-1.5">
|
|
951
|
+
<View
|
|
952
|
+
style={{
|
|
953
|
+
width: 8,
|
|
954
|
+
height: 8,
|
|
955
|
+
borderRadius: 4,
|
|
956
|
+
backgroundColor: colors[index],
|
|
957
|
+
}}
|
|
958
|
+
/>
|
|
959
|
+
<Text size="xs" muted numberOfLines={1}>
|
|
960
|
+
{labels?.[series.label] ?? series.label}
|
|
961
|
+
</Text>
|
|
962
|
+
</View>
|
|
963
|
+
))}
|
|
964
|
+
</View>
|
|
965
|
+
) : null);
|
|
966
|
+
|
|
967
|
+
return (
|
|
968
|
+
<View
|
|
969
|
+
{...props}
|
|
970
|
+
className={cn('flex-row items-start justify-between gap-3 pb-3', className)}
|
|
971
|
+
>
|
|
972
|
+
<View className="flex-1 gap-0.5">
|
|
973
|
+
{title ? (
|
|
974
|
+
<Text size="xs" muted>
|
|
975
|
+
{title}
|
|
976
|
+
</Text>
|
|
977
|
+
) : null}
|
|
978
|
+
{value ? (
|
|
979
|
+
<Text size="xl" weight="bold">
|
|
980
|
+
{value}
|
|
981
|
+
</Text>
|
|
982
|
+
) : null}
|
|
983
|
+
{caption ? (
|
|
984
|
+
<Text size="xs" muted>
|
|
985
|
+
{caption}
|
|
986
|
+
</Text>
|
|
987
|
+
) : null}
|
|
988
|
+
</View>
|
|
989
|
+
{/*
|
|
990
|
+
* Shrinkable, unlike a view's default in React Native — and capped, which
|
|
991
|
+
* shrinking alone does not achieve. The title column is `flex-1`, so its
|
|
992
|
+
* basis is zero and it lives on what is left over: a wrapping key with
|
|
993
|
+
* nothing stopping it takes the whole row and leaves the title a few
|
|
994
|
+
* points wide, which renders it one letter to a line.
|
|
995
|
+
*/}
|
|
996
|
+
{trailing ? <View className="max-w-[55%] shrink pt-1">{trailing}</View> : null}
|
|
997
|
+
</View>
|
|
998
|
+
);
|
|
999
|
+
}
|
|
1000
|
+
HexChartHeader.displayName = 'HexChart.Header';
|
|
1001
|
+
HexChartHeader.slot = 'header' as const;
|
|
1002
|
+
|
|
1003
|
+
export const HexChart = Object.assign(HexChartRoot, {
|
|
1004
|
+
Header: HexChartHeader,
|
|
1005
|
+
Cells: HexChartCells,
|
|
1006
|
+
Tooltip: HexChartTooltip,
|
|
1007
|
+
Legend: HexChartLegend,
|
|
1008
|
+
Skeleton: HexChartSkeleton,
|
|
1009
|
+
});
|