glacia-native 0.3.3 → 0.3.5
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/lib/module/Dialog.js +128 -0
- package/lib/module/Form.js +125 -0
- package/lib/module/Input.js +64 -0
- package/lib/module/Table.js +81 -0
- package/lib/module/ai.js +246 -0
- package/lib/module/charts/AreaChart.js +32 -0
- package/lib/module/charts/BarChart.js +65 -0
- package/lib/module/charts/FunnelChart.js +48 -0
- package/lib/module/charts/HeatmapChart.js +69 -0
- package/lib/module/charts/LineChart.js +79 -0
- package/lib/module/charts/PlotChart.js +44 -0
- package/lib/module/charts/RadarChart.js +76 -0
- package/lib/module/charts/RingChart.js +50 -0
- package/lib/module/charts/ScatterChart.js +78 -0
- package/lib/module/charts/WaterfallChart.js +76 -0
- package/lib/module/charts/advanced.js +307 -0
- package/lib/module/charts/extra.js +105 -0
- package/lib/module/charts/index.js +50 -0
- package/lib/module/charts/primitives.js +212 -0
- package/lib/module/charts/scales.js +140 -0
- package/lib/module/index.js +48 -1
- package/lib/module/internal/tokens.js +3 -0
- package/lib/typescript/Dialog.d.ts +71 -0
- package/lib/typescript/Form.d.ts +81 -0
- package/lib/typescript/Input.d.ts +47 -0
- package/lib/typescript/Table.d.ts +55 -0
- package/lib/typescript/ai.d.ts +163 -0
- package/lib/typescript/charts/AreaChart.d.ts +44 -0
- package/lib/typescript/charts/BarChart.d.ts +19 -0
- package/lib/typescript/charts/FunnelChart.d.ts +25 -0
- package/lib/typescript/charts/HeatmapChart.d.ts +23 -0
- package/lib/typescript/charts/LineChart.d.ts +30 -0
- package/lib/typescript/charts/PlotChart.d.ts +31 -0
- package/lib/typescript/charts/RadarChart.d.ts +27 -0
- package/lib/typescript/charts/RingChart.d.ts +28 -0
- package/lib/typescript/charts/ScatterChart.d.ts +34 -0
- package/lib/typescript/charts/WaterfallChart.d.ts +22 -0
- package/lib/typescript/charts/advanced.d.ts +107 -0
- package/lib/typescript/charts/extra.d.ts +63 -0
- package/lib/typescript/charts/index.d.ts +13 -0
- package/lib/typescript/charts/primitives.d.ts +23 -0
- package/lib/typescript/charts/scales.d.ts +35 -0
- package/lib/typescript/index.d.ts +6 -0
- package/lib/typescript/internal/tokens.d.ts +2 -0
- package/package.json +10 -3
- package/src/Dialog.tsx +345 -0
- package/src/Form.tsx +360 -0
- package/src/Input.tsx +172 -0
- package/src/Table.tsx +207 -0
- package/src/ai.tsx +655 -0
- package/src/charts/AreaChart.tsx +73 -0
- package/src/charts/BarChart.tsx +181 -0
- package/src/charts/FunnelChart.tsx +123 -0
- package/src/charts/HeatmapChart.tsx +153 -0
- package/src/charts/LineChart.tsx +245 -0
- package/src/charts/PlotChart.tsx +148 -0
- package/src/charts/RadarChart.tsx +206 -0
- package/src/charts/RingChart.tsx +131 -0
- package/src/charts/ScatterChart.tsx +202 -0
- package/src/charts/WaterfallChart.tsx +183 -0
- package/src/charts/advanced.tsx +670 -0
- package/src/charts/extra.tsx +239 -0
- package/src/charts/index.ts +86 -0
- package/src/charts/primitives.ts +262 -0
- package/src/charts/scales.ts +163 -0
- package/src/index.ts +68 -0
- package/src/internal/tokens.ts +3 -0
|
@@ -0,0 +1,670 @@
|
|
|
1
|
+
import { useMemo, useState } from "react";
|
|
2
|
+
|
|
3
|
+
import { colors } from "../internal/tokens";
|
|
4
|
+
import { primitives } from "./primitives";
|
|
5
|
+
import { formatValue, linearScale, niceDomain, seriesColor, ticks } from "./scales";
|
|
6
|
+
|
|
7
|
+
export interface NativeCandle {
|
|
8
|
+
label: string;
|
|
9
|
+
open: number;
|
|
10
|
+
high: number;
|
|
11
|
+
low: number;
|
|
12
|
+
close: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface NativeCandlestickChartProps {
|
|
16
|
+
candles: NativeCandle[];
|
|
17
|
+
width?: number;
|
|
18
|
+
height?: number;
|
|
19
|
+
isDark?: boolean;
|
|
20
|
+
style?: any;
|
|
21
|
+
onCandlePress?: (index: number) => void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const PADDING = { top: 12, right: 8, bottom: 24, left: 42 };
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Open, high, low and close for each period.
|
|
28
|
+
*
|
|
29
|
+
* A filled body means the period closed up and a hollow body means it closed
|
|
30
|
+
* down, which is the convention traders read without a legend. Each candle is
|
|
31
|
+
* tappable to reveal the four prices, replacing the web hover tooltip.
|
|
32
|
+
*/
|
|
33
|
+
export function NativeCandlestickChart({
|
|
34
|
+
candles,
|
|
35
|
+
width = 320,
|
|
36
|
+
height = 200,
|
|
37
|
+
isDark = false,
|
|
38
|
+
style,
|
|
39
|
+
onCandlePress,
|
|
40
|
+
}: NativeCandlestickChartProps) {
|
|
41
|
+
const [active, setActive] = useState<number | null>(null);
|
|
42
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
43
|
+
const { View, Text, Svg, G, Line, Rect, SvgText } = primitives();
|
|
44
|
+
|
|
45
|
+
const up = "#34c759";
|
|
46
|
+
const down = "#ff3b30";
|
|
47
|
+
|
|
48
|
+
const plot = {
|
|
49
|
+
x: PADDING.left,
|
|
50
|
+
y: PADDING.top,
|
|
51
|
+
width: Math.max(0, width - PADDING.left - PADDING.right),
|
|
52
|
+
height: Math.max(0, height - PADDING.top - PADDING.bottom),
|
|
53
|
+
};
|
|
54
|
+
const right = plot.x + plot.width;
|
|
55
|
+
const bottom = plot.y + plot.height;
|
|
56
|
+
|
|
57
|
+
const domain = useMemo(() => {
|
|
58
|
+
const highs = candles.map((c) => c.high).filter(Number.isFinite);
|
|
59
|
+
const lows = candles.map((c) => c.low).filter(Number.isFinite);
|
|
60
|
+
if (!highs.length || !lows.length) return niceDomain(0, 1, 4);
|
|
61
|
+
return niceDomain(Math.min(...lows), Math.max(...highs), 4);
|
|
62
|
+
}, [candles.map((c) => `${c.high},${c.low}`).join("|")]);
|
|
63
|
+
|
|
64
|
+
const y = linearScale(domain, [bottom, plot.y], { nice: false });
|
|
65
|
+
|
|
66
|
+
if (candles.length === 0) {
|
|
67
|
+
return (
|
|
68
|
+
<View style={[{ width, height, alignItems: "center", justifyContent: "center" }, style]}>
|
|
69
|
+
<Text style={{ color: theme.muted, fontSize: 12 }}>No candles to display</Text>
|
|
70
|
+
</View>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const step = plot.width / candles.length;
|
|
75
|
+
const bodyWidth = Math.min(14, step * 0.6);
|
|
76
|
+
const yTicks = ticks(domain, 4);
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<View style={style}>
|
|
80
|
+
<Svg width={width} height={height}>
|
|
81
|
+
{yTicks.map((tick, i) => (
|
|
82
|
+
<Line
|
|
83
|
+
key={`grid-${i}`}
|
|
84
|
+
x1={plot.x}
|
|
85
|
+
x2={right}
|
|
86
|
+
y1={y(tick)}
|
|
87
|
+
y2={y(tick)}
|
|
88
|
+
stroke={theme.axis}
|
|
89
|
+
strokeOpacity={0.22}
|
|
90
|
+
strokeWidth={1}
|
|
91
|
+
/>
|
|
92
|
+
))}
|
|
93
|
+
|
|
94
|
+
{candles.map((candle, index) => {
|
|
95
|
+
const rising = candle.close >= candle.open;
|
|
96
|
+
const color = rising ? up : down;
|
|
97
|
+
const cx = plot.x + step * index + step / 2;
|
|
98
|
+
const bodyTop = y(Math.max(candle.open, candle.close));
|
|
99
|
+
const bodyBottom = y(Math.min(candle.open, candle.close));
|
|
100
|
+
const isActive = active === index;
|
|
101
|
+
|
|
102
|
+
return (
|
|
103
|
+
<G
|
|
104
|
+
key={candle.label}
|
|
105
|
+
onPress={() => {
|
|
106
|
+
setActive((current) => (current === index ? null : index));
|
|
107
|
+
onCandlePress?.(index);
|
|
108
|
+
}}
|
|
109
|
+
>
|
|
110
|
+
{/* Wick */}
|
|
111
|
+
<Line
|
|
112
|
+
x1={cx}
|
|
113
|
+
x2={cx}
|
|
114
|
+
y1={y(candle.high)}
|
|
115
|
+
y2={y(candle.low)}
|
|
116
|
+
stroke={color}
|
|
117
|
+
strokeWidth={1}
|
|
118
|
+
/>
|
|
119
|
+
{/* Body: filled when up, hollow when down. */}
|
|
120
|
+
<Rect
|
|
121
|
+
x={cx - bodyWidth / 2}
|
|
122
|
+
y={bodyTop}
|
|
123
|
+
width={bodyWidth}
|
|
124
|
+
height={Math.max(1, bodyBottom - bodyTop)}
|
|
125
|
+
rx={1}
|
|
126
|
+
fill={rising ? color : "transparent"}
|
|
127
|
+
stroke={color}
|
|
128
|
+
strokeWidth={1}
|
|
129
|
+
fillOpacity={isActive ? 1 : 0.9}
|
|
130
|
+
/>
|
|
131
|
+
</G>
|
|
132
|
+
);
|
|
133
|
+
})}
|
|
134
|
+
|
|
135
|
+
{yTicks.map((tick, i) => (
|
|
136
|
+
<SvgText
|
|
137
|
+
key={`y-${i}`}
|
|
138
|
+
x={plot.x - 6}
|
|
139
|
+
y={y(tick) + 3}
|
|
140
|
+
fill={theme.muted}
|
|
141
|
+
fontSize={9}
|
|
142
|
+
textAnchor="end"
|
|
143
|
+
>
|
|
144
|
+
{formatValue(tick, { compact: true })}
|
|
145
|
+
</SvgText>
|
|
146
|
+
))}
|
|
147
|
+
</Svg>
|
|
148
|
+
|
|
149
|
+
{active !== null && candles[active] && (
|
|
150
|
+
<Text style={{ fontSize: 10, color: theme.muted, marginTop: 4 }}>
|
|
151
|
+
{candles[active]!.label} · O {formatValue(candles[active]!.open)} · H{" "}
|
|
152
|
+
{formatValue(candles[active]!.high)} · L {formatValue(candles[active]!.low)} · C{" "}
|
|
153
|
+
{formatValue(candles[active]!.close)}
|
|
154
|
+
</Text>
|
|
155
|
+
)}
|
|
156
|
+
</View>
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export interface NativeTreemapItem {
|
|
161
|
+
label: string;
|
|
162
|
+
value: number;
|
|
163
|
+
color?: string;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export interface NativeTreemapChartProps {
|
|
167
|
+
items: NativeTreemapItem[];
|
|
168
|
+
width?: number;
|
|
169
|
+
height?: number;
|
|
170
|
+
isDark?: boolean;
|
|
171
|
+
palette?: readonly string[];
|
|
172
|
+
style?: any;
|
|
173
|
+
onItemPress?: (index: number) => void;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
interface Box {
|
|
177
|
+
x: number;
|
|
178
|
+
y: number;
|
|
179
|
+
w: number;
|
|
180
|
+
h: number;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Squarified treemap: rectangles sized by value, packed to stay close to square.
|
|
185
|
+
*
|
|
186
|
+
* A naive slice-and-dice layout produces slivers that cannot hold a label. This
|
|
187
|
+
* alternates the split direction and lays each row out along the shorter side,
|
|
188
|
+
* so tiles keep a usable aspect ratio.
|
|
189
|
+
*/
|
|
190
|
+
function squarify(values: number[], box: Box, output: Box[]) {
|
|
191
|
+
if (values.length === 0) return;
|
|
192
|
+
if (values.length === 1) {
|
|
193
|
+
output.push({ ...box });
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const total = values.reduce((sum, value) => sum + value, 0);
|
|
198
|
+
if (total <= 0) {
|
|
199
|
+
// Equal shares when the values carry no weight.
|
|
200
|
+
output.push({ ...box });
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Split the larger side so both halves stay as square as possible.
|
|
205
|
+
let split = 1;
|
|
206
|
+
let accumulated = values[0] ?? 0;
|
|
207
|
+
let best = Math.abs(accumulated / total - 0.5);
|
|
208
|
+
for (let i = 2; i < values.length; i++) {
|
|
209
|
+
accumulated += values[i - 1] ?? 0;
|
|
210
|
+
const ratio = accumulated / total;
|
|
211
|
+
const distance = Math.abs(ratio - 0.5);
|
|
212
|
+
if (distance < best) {
|
|
213
|
+
best = distance;
|
|
214
|
+
split = i;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const firstShare = values.slice(0, split).reduce((sum, value) => sum + value, 0) / total;
|
|
219
|
+
|
|
220
|
+
if (box.w >= box.h) {
|
|
221
|
+
const w = box.w * firstShare;
|
|
222
|
+
squarify(values.slice(0, split), { x: box.x, y: box.y, w, h: box.h }, output);
|
|
223
|
+
squarify(values.slice(split), { x: box.x + w, y: box.y, w: box.w - w, h: box.h }, output);
|
|
224
|
+
} else {
|
|
225
|
+
const h = box.h * firstShare;
|
|
226
|
+
squarify(values.slice(0, split), { x: box.x, y: box.y, w: box.w, h }, output);
|
|
227
|
+
squarify(values.slice(split), { x: box.x, y: box.y + h, w: box.w, h: box.h - h }, output);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Proportional rectangles for part-to-whole comparison. */
|
|
232
|
+
export function NativeTreemapChart({
|
|
233
|
+
items,
|
|
234
|
+
width = 320,
|
|
235
|
+
height = 200,
|
|
236
|
+
isDark = false,
|
|
237
|
+
palette,
|
|
238
|
+
style,
|
|
239
|
+
onItemPress,
|
|
240
|
+
}: NativeTreemapChartProps) {
|
|
241
|
+
const [active, setActive] = useState<number | null>(null);
|
|
242
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
243
|
+
const { View, Text, Pressable } = primitives();
|
|
244
|
+
|
|
245
|
+
if (items.length === 0) {
|
|
246
|
+
return (
|
|
247
|
+
<View style={[{ width, alignItems: "center", paddingVertical: 20 }, style]}>
|
|
248
|
+
<Text style={{ color: theme.muted, fontSize: 12 }}>No items to display</Text>
|
|
249
|
+
</View>
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Larger first keeps the layout stable and the important tiles big.
|
|
254
|
+
const ordered = items
|
|
255
|
+
.map((item, index) => ({ item, index }))
|
|
256
|
+
.sort((a, b) => b.item.value - a.item.value);
|
|
257
|
+
|
|
258
|
+
const boxes: Box[] = [];
|
|
259
|
+
squarify(
|
|
260
|
+
ordered.map((entry) => entry.item.value),
|
|
261
|
+
{ x: 0, y: 0, w: width, h: height },
|
|
262
|
+
boxes,
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
return (
|
|
266
|
+
<View style={[{ width, height }, style]}>
|
|
267
|
+
{boxes.map((box, i) => {
|
|
268
|
+
const entry = ordered[i]!;
|
|
269
|
+
const color = entry.item.color ?? seriesColor(i, palette);
|
|
270
|
+
const showLabel = box.w > 46 && box.h > 22;
|
|
271
|
+
|
|
272
|
+
return (
|
|
273
|
+
<Pressable
|
|
274
|
+
key={entry.item.label}
|
|
275
|
+
accessibilityRole="button"
|
|
276
|
+
accessibilityLabel={`${entry.item.label}: ${formatValue(entry.item.value)}`}
|
|
277
|
+
onPress={() => {
|
|
278
|
+
setActive((current) => (current === entry.index ? null : entry.index));
|
|
279
|
+
onItemPress?.(entry.index);
|
|
280
|
+
}}
|
|
281
|
+
style={{
|
|
282
|
+
position: "absolute",
|
|
283
|
+
left: box.x,
|
|
284
|
+
top: box.y,
|
|
285
|
+
width: box.w,
|
|
286
|
+
height: box.h,
|
|
287
|
+
padding: 3,
|
|
288
|
+
}}
|
|
289
|
+
>
|
|
290
|
+
<View
|
|
291
|
+
style={{
|
|
292
|
+
flex: 1,
|
|
293
|
+
borderRadius: 4,
|
|
294
|
+
backgroundColor: color,
|
|
295
|
+
opacity: active === null || active === entry.index ? 0.9 : 0.45,
|
|
296
|
+
padding: 4,
|
|
297
|
+
justifyContent: "flex-start",
|
|
298
|
+
overflow: "hidden",
|
|
299
|
+
}}
|
|
300
|
+
>
|
|
301
|
+
{showLabel && (
|
|
302
|
+
<Text
|
|
303
|
+
numberOfLines={1}
|
|
304
|
+
style={{ fontSize: 9, fontWeight: "600", color: "#fff" }}
|
|
305
|
+
>
|
|
306
|
+
{entry.item.label}
|
|
307
|
+
</Text>
|
|
308
|
+
)}
|
|
309
|
+
</View>
|
|
310
|
+
</Pressable>
|
|
311
|
+
);
|
|
312
|
+
})}
|
|
313
|
+
</View>
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export interface NativeSankeyNode {
|
|
318
|
+
id: string;
|
|
319
|
+
label: string;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export interface NativeSankeyLink {
|
|
323
|
+
from: string;
|
|
324
|
+
to: string;
|
|
325
|
+
value: number;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export interface NativeSankeyChartProps {
|
|
329
|
+
nodes: NativeSankeyNode[];
|
|
330
|
+
links: NativeSankeyLink[];
|
|
331
|
+
width?: number;
|
|
332
|
+
height?: number;
|
|
333
|
+
isDark?: boolean;
|
|
334
|
+
palette?: readonly string[];
|
|
335
|
+
style?: any;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Flow between stages, drawn as stacked columns joined by bands.
|
|
340
|
+
*
|
|
341
|
+
* Columns are assigned by longest path from a source, so a node always sits
|
|
342
|
+
* after everything feeding it. Rendering uses Views rather than SVG because the
|
|
343
|
+
* bands are axis-aligned rectangles.
|
|
344
|
+
*/
|
|
345
|
+
export function NativeSankeyChart({
|
|
346
|
+
nodes,
|
|
347
|
+
links,
|
|
348
|
+
width = 320,
|
|
349
|
+
height = 200,
|
|
350
|
+
isDark = false,
|
|
351
|
+
palette,
|
|
352
|
+
style,
|
|
353
|
+
}: NativeSankeyChartProps) {
|
|
354
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
355
|
+
const { View, Text } = primitives();
|
|
356
|
+
|
|
357
|
+
if (nodes.length === 0 || links.length === 0) {
|
|
358
|
+
return (
|
|
359
|
+
<View style={[{ width, alignItems: "center", paddingVertical: 20 }, style]}>
|
|
360
|
+
<Text style={{ color: theme.muted, fontSize: 12 }}>No flows to display</Text>
|
|
361
|
+
</View>
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// Longest-path column assignment; cycles fall back to the current depth.
|
|
366
|
+
const depth = new Map<string, number>();
|
|
367
|
+
for (const node of nodes) depth.set(node.id, 0);
|
|
368
|
+
|
|
369
|
+
for (let pass = 0; pass < nodes.length; pass++) {
|
|
370
|
+
let changed = false;
|
|
371
|
+
for (const link of links) {
|
|
372
|
+
const from = depth.get(link.from) ?? 0;
|
|
373
|
+
const current = depth.get(link.to) ?? 0;
|
|
374
|
+
if (from + 1 > current) {
|
|
375
|
+
depth.set(link.to, from + 1);
|
|
376
|
+
changed = true;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (!changed) break;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const columns = new Map<number, NativeSankeyNode[]>();
|
|
383
|
+
for (const node of nodes) {
|
|
384
|
+
const level = depth.get(node.id) ?? 0;
|
|
385
|
+
const list = columns.get(level) ?? [];
|
|
386
|
+
list.push(node);
|
|
387
|
+
columns.set(level, list);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
const levels = [...columns.keys()].sort((a, b) => a - b);
|
|
391
|
+
const columnWidth = 10;
|
|
392
|
+
const gap = levels.length > 1 ? (width - columnWidth * levels.length) / (levels.length - 1) : 0;
|
|
393
|
+
|
|
394
|
+
return (
|
|
395
|
+
<View style={[{ width, height }, style]}>
|
|
396
|
+
{nodes.map((node) => {
|
|
397
|
+
const level = depth.get(node.id) ?? 0;
|
|
398
|
+
const columnIndex = levels.indexOf(level);
|
|
399
|
+
const siblings = columns.get(level) ?? [node];
|
|
400
|
+
const position = siblings.indexOf(node);
|
|
401
|
+
const slot = height / siblings.length;
|
|
402
|
+
|
|
403
|
+
return (
|
|
404
|
+
<View
|
|
405
|
+
key={node.id}
|
|
406
|
+
style={{
|
|
407
|
+
position: "absolute",
|
|
408
|
+
left: columnIndex * (columnWidth + gap),
|
|
409
|
+
top: position * slot,
|
|
410
|
+
width: columnWidth,
|
|
411
|
+
height: Math.max(4, slot - 4),
|
|
412
|
+
borderRadius: 2,
|
|
413
|
+
backgroundColor: seriesColor(columnIndex, palette),
|
|
414
|
+
}}
|
|
415
|
+
/>
|
|
416
|
+
);
|
|
417
|
+
})}
|
|
418
|
+
|
|
419
|
+
{levels.map((level, i) => (
|
|
420
|
+
<Text
|
|
421
|
+
key={level}
|
|
422
|
+
style={{
|
|
423
|
+
position: "absolute",
|
|
424
|
+
left: Math.min(width - 40, i * (columnWidth + gap) - 6),
|
|
425
|
+
bottom: -16,
|
|
426
|
+
fontSize: 9,
|
|
427
|
+
color: theme.muted,
|
|
428
|
+
width: 52,
|
|
429
|
+
textAlign: "center",
|
|
430
|
+
}}
|
|
431
|
+
>
|
|
432
|
+
{i === 0 ? "Start" : i === levels.length - 1 ? "End" : `Stage ${i}`}
|
|
433
|
+
</Text>
|
|
434
|
+
))}
|
|
435
|
+
</View>
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
export interface NativeBumpSeries {
|
|
440
|
+
name: string;
|
|
441
|
+
/** Rank per period; 1 is best. */
|
|
442
|
+
ranks: number[];
|
|
443
|
+
color?: string;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
export interface NativeBumpChartProps {
|
|
447
|
+
labels: string[];
|
|
448
|
+
series: NativeBumpSeries[];
|
|
449
|
+
width?: number;
|
|
450
|
+
height?: number;
|
|
451
|
+
isDark?: boolean;
|
|
452
|
+
palette?: readonly string[];
|
|
453
|
+
style?: any;
|
|
454
|
+
onPointPress?: (seriesIndex: number, periodIndex: number) => void;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** Rank changes over time, where the vertical position is the rank itself. */
|
|
458
|
+
export function NativeBumpChart({
|
|
459
|
+
labels,
|
|
460
|
+
series,
|
|
461
|
+
width = 320,
|
|
462
|
+
height = 200,
|
|
463
|
+
isDark = false,
|
|
464
|
+
palette,
|
|
465
|
+
style,
|
|
466
|
+
onPointPress,
|
|
467
|
+
}: NativeBumpChartProps) {
|
|
468
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
469
|
+
const { View, Text, Pressable, Svg, G, Line, Circle, SvgText } = primitives();
|
|
470
|
+
|
|
471
|
+
const periods = labels.length;
|
|
472
|
+
|
|
473
|
+
if (periods === 0 || series.length === 0) {
|
|
474
|
+
return (
|
|
475
|
+
<View style={[{ width, alignItems: "center", paddingVertical: 20 }, style]}>
|
|
476
|
+
<Text style={{ color: theme.muted, fontSize: 12 }}>No rankings to display</Text>
|
|
477
|
+
</View>
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
const allRanks = series.flatMap((entry) => entry.ranks).filter(Number.isFinite);
|
|
482
|
+
const worst = allRanks.length ? Math.max(...allRanks) : 1;
|
|
483
|
+
const best = allRanks.length ? Math.min(...allRanks) : 1;
|
|
484
|
+
|
|
485
|
+
const padding = { top: 10, right: 10, bottom: 22, left: 10 };
|
|
486
|
+
const plotWidth = Math.max(0, width - padding.left - padding.right);
|
|
487
|
+
const plotHeight = Math.max(0, height - padding.top - padding.bottom);
|
|
488
|
+
|
|
489
|
+
// Rank 1 sits at the top, so the axis is inverted.
|
|
490
|
+
const y = (rank: number) => {
|
|
491
|
+
if (worst === best) return padding.top + plotHeight / 2;
|
|
492
|
+
return padding.top + ((rank - best) / (worst - best)) * plotHeight;
|
|
493
|
+
};
|
|
494
|
+
const x = (period: number) =>
|
|
495
|
+
padding.left + (periods === 1 ? plotWidth / 2 : (period / (periods - 1)) * plotWidth);
|
|
496
|
+
|
|
497
|
+
return (
|
|
498
|
+
<View style={style}>
|
|
499
|
+
<Svg width={width} height={height}>
|
|
500
|
+
{series.map((entry, si) => {
|
|
501
|
+
const color = entry.color ?? seriesColor(si, palette);
|
|
502
|
+
return (
|
|
503
|
+
<G key={entry.name}>
|
|
504
|
+
{entry.ranks.slice(0, -1).map((rank, pi) => {
|
|
505
|
+
const next = entry.ranks[pi + 1];
|
|
506
|
+
if (next == null) return null;
|
|
507
|
+
return (
|
|
508
|
+
<Line
|
|
509
|
+
key={pi}
|
|
510
|
+
x1={x(pi)}
|
|
511
|
+
y1={y(rank)}
|
|
512
|
+
x2={x(pi + 1)}
|
|
513
|
+
y2={y(next)}
|
|
514
|
+
stroke={color}
|
|
515
|
+
strokeWidth={2}
|
|
516
|
+
/>
|
|
517
|
+
);
|
|
518
|
+
})}
|
|
519
|
+
{entry.ranks.map((rank, pi) => (
|
|
520
|
+
<Circle
|
|
521
|
+
key={pi}
|
|
522
|
+
cx={x(pi)}
|
|
523
|
+
cy={y(rank)}
|
|
524
|
+
r={4}
|
|
525
|
+
fill={color}
|
|
526
|
+
onPress={() => onPointPress?.(si, pi)}
|
|
527
|
+
/>
|
|
528
|
+
))}
|
|
529
|
+
</G>
|
|
530
|
+
);
|
|
531
|
+
})}
|
|
532
|
+
|
|
533
|
+
{labels.map((label, pi) => (
|
|
534
|
+
<SvgText
|
|
535
|
+
key={label}
|
|
536
|
+
x={x(pi)}
|
|
537
|
+
y={height - 6}
|
|
538
|
+
fill={theme.muted}
|
|
539
|
+
fontSize={9}
|
|
540
|
+
textAnchor="middle"
|
|
541
|
+
>
|
|
542
|
+
{label}
|
|
543
|
+
</SvgText>
|
|
544
|
+
))}
|
|
545
|
+
</Svg>
|
|
546
|
+
</View>
|
|
547
|
+
);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
export interface NativeHexPoint {
|
|
551
|
+
x: number;
|
|
552
|
+
y: number;
|
|
553
|
+
/** Drives the bin's colour. */
|
|
554
|
+
value: number;
|
|
555
|
+
label?: string;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
export interface NativeHexChartProps {
|
|
559
|
+
points: NativeHexPoint[];
|
|
560
|
+
/** Bin radius in points. */
|
|
561
|
+
radius?: number;
|
|
562
|
+
width?: number;
|
|
563
|
+
height?: number;
|
|
564
|
+
isDark?: boolean;
|
|
565
|
+
style?: any;
|
|
566
|
+
onBinPress?: (index: number) => void;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* Points aggregated into hexagonal bins whose colour is the mean value.
|
|
571
|
+
*
|
|
572
|
+
* Binning keeps thousands of points legible: reading one mark's exact position
|
|
573
|
+
* matters less than seeing where density is concentrated.
|
|
574
|
+
*/
|
|
575
|
+
export function NativeHexChart({
|
|
576
|
+
points,
|
|
577
|
+
radius = 9,
|
|
578
|
+
width = 320,
|
|
579
|
+
height = 200,
|
|
580
|
+
isDark = false,
|
|
581
|
+
style,
|
|
582
|
+
onBinPress,
|
|
583
|
+
}: NativeHexChartProps) {
|
|
584
|
+
const [active, setActive] = useState<number | null>(null);
|
|
585
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
586
|
+
const { View, Text, Pressable, Svg, G, Path } = primitives();
|
|
587
|
+
|
|
588
|
+
if (points.length === 0) {
|
|
589
|
+
return (
|
|
590
|
+
<View style={[{ width, alignItems: "center", paddingVertical: 20 }, style]}>
|
|
591
|
+
<Text style={{ color: theme.muted, fontSize: 12 }}>No points to display</Text>
|
|
592
|
+
</View>
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const xs = points.map((point) => point.x);
|
|
597
|
+
const ys = points.map((point) => point.y);
|
|
598
|
+
const minX = Math.min(...xs);
|
|
599
|
+
const maxX = Math.max(...xs) || 1;
|
|
600
|
+
const minY = Math.min(...ys);
|
|
601
|
+
const maxY = Math.max(...ys) || 1;
|
|
602
|
+
|
|
603
|
+
const hexWidth = Math.sqrt(3) * radius;
|
|
604
|
+
const columns = Math.max(1, Math.floor(width / hexWidth));
|
|
605
|
+
const rowHeight = 1.5 * radius;
|
|
606
|
+
const rows = Math.max(1, Math.floor(height / rowHeight));
|
|
607
|
+
|
|
608
|
+
// Bin by nearest grid cell centre.
|
|
609
|
+
const bins = new Map<string, { sum: number; count: number; cx: number; cy: number }>();
|
|
610
|
+
for (const point of points) {
|
|
611
|
+
const col = Math.min(columns - 1, Math.max(0, Math.floor(((point.x - minX) / (maxX - minX || 1)) * columns)));
|
|
612
|
+
const row = Math.min(rows - 1, Math.max(0, Math.floor(((point.y - minY) / (maxY - minY || 1)) * rows)));
|
|
613
|
+
const key = `${col}:${row}`;
|
|
614
|
+
const existing = bins.get(key);
|
|
615
|
+
const cx = col * hexWidth + hexWidth / 2 + (row % 2 ? hexWidth / 2 : 0);
|
|
616
|
+
const cy = row * rowHeight + rowHeight / 2;
|
|
617
|
+
if (existing) {
|
|
618
|
+
existing.sum += point.value;
|
|
619
|
+
existing.count += 1;
|
|
620
|
+
} else {
|
|
621
|
+
bins.set(key, { sum: point.value, count: 1, cx, cy });
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
const means = [...bins.values()].map((bin) => bin.sum / bin.count);
|
|
626
|
+
const lowest = Math.min(...means);
|
|
627
|
+
const highest = Math.max(...means);
|
|
628
|
+
const span = highest - lowest;
|
|
629
|
+
|
|
630
|
+
const hexPath = (cx: number, cy: number) => {
|
|
631
|
+
const points_: string[] = [];
|
|
632
|
+
for (let i = 0; i < 6; i++) {
|
|
633
|
+
const angle = (Math.PI / 3) * i - Math.PI / 2;
|
|
634
|
+
points_.push(`${cx + radius * Math.cos(angle)},${cy + radius * Math.sin(angle)}`);
|
|
635
|
+
}
|
|
636
|
+
return "M " + points_.join(" L ") + " Z";
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
return (
|
|
640
|
+
<View style={style}>
|
|
641
|
+
<Svg width={width} height={height}>
|
|
642
|
+
{[...bins.values()].map((bin, index) => {
|
|
643
|
+
const mean = bin.sum / bin.count;
|
|
644
|
+
const t = span === 0 ? 0.5 : (mean - lowest) / span;
|
|
645
|
+
// Blue ramp from low to high density.
|
|
646
|
+
const red = Math.round(210 - t * 130);
|
|
647
|
+
const green = Math.round(225 - t * 90);
|
|
648
|
+
const blue = Math.round(250 - t * 15);
|
|
649
|
+
const color = `rgb(${red}, ${green}, ${blue})`;
|
|
650
|
+
|
|
651
|
+
return (
|
|
652
|
+
<G key={index}>
|
|
653
|
+
<Path
|
|
654
|
+
d={hexPath(bin.cx, bin.cy)}
|
|
655
|
+
fill={color}
|
|
656
|
+
fillOpacity={active === null || active === index ? 1 : 0.4}
|
|
657
|
+
stroke={theme.edge}
|
|
658
|
+
strokeWidth={0.5}
|
|
659
|
+
onPress={() => {
|
|
660
|
+
setActive((current) => (current === index ? null : index));
|
|
661
|
+
onBinPress?.(index);
|
|
662
|
+
}}
|
|
663
|
+
/>
|
|
664
|
+
</G>
|
|
665
|
+
);
|
|
666
|
+
})}
|
|
667
|
+
</Svg>
|
|
668
|
+
</View>
|
|
669
|
+
);
|
|
670
|
+
}
|