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,245 @@
|
|
|
1
|
+
import { useMemo, useState } from "react";
|
|
2
|
+
|
|
3
|
+
import { colors, radii } from "../internal/tokens";
|
|
4
|
+
import { primitives } from "./primitives";
|
|
5
|
+
import {
|
|
6
|
+
bandScale,
|
|
7
|
+
formatValue,
|
|
8
|
+
linearScale,
|
|
9
|
+
niceDomain,
|
|
10
|
+
seriesColor,
|
|
11
|
+
smoothPath,
|
|
12
|
+
ticks,
|
|
13
|
+
} from "./scales";
|
|
14
|
+
|
|
15
|
+
export interface NativeLineSeries {
|
|
16
|
+
name: string;
|
|
17
|
+
values: number[];
|
|
18
|
+
color?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface NativeLineChartProps {
|
|
22
|
+
labels: string[];
|
|
23
|
+
series: NativeLineSeries[];
|
|
24
|
+
width?: number;
|
|
25
|
+
height?: number;
|
|
26
|
+
/** Fill beneath each line. */
|
|
27
|
+
area?: boolean;
|
|
28
|
+
smooth?: boolean;
|
|
29
|
+
showDots?: boolean;
|
|
30
|
+
isDark?: boolean;
|
|
31
|
+
palette?: readonly string[];
|
|
32
|
+
style?: StyleProps;
|
|
33
|
+
/** Called with the index of the touched point. */
|
|
34
|
+
onPointPress?: (index: number) => void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const PADDING = { top: 12, right: 10, bottom: 24, left: 38 };
|
|
38
|
+
|
|
39
|
+
type StyleProps = any;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A line chart drawn with react-native-svg.
|
|
43
|
+
*
|
|
44
|
+
* Uses the same scale maths as the web LineChart, so the same series produces
|
|
45
|
+
* the same axis values on both platforms. Touching a column selects that
|
|
46
|
+
* point, which is the touch equivalent of hovering on the web.
|
|
47
|
+
*/
|
|
48
|
+
export function NativeLineChart({
|
|
49
|
+
labels,
|
|
50
|
+
series,
|
|
51
|
+
width = 320,
|
|
52
|
+
height = 200,
|
|
53
|
+
area = false,
|
|
54
|
+
smooth = true,
|
|
55
|
+
showDots = false,
|
|
56
|
+
isDark = false,
|
|
57
|
+
palette,
|
|
58
|
+
style,
|
|
59
|
+
onPointPress,
|
|
60
|
+
}: NativeLineChartProps) {
|
|
61
|
+
const [active, setActive] = useState<number | null>(null);
|
|
62
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
63
|
+
const { View, Text, Pressable, Svg, G, Line, Path, Circle, SvgText } = primitives();
|
|
64
|
+
|
|
65
|
+
const plot = {
|
|
66
|
+
x: PADDING.left,
|
|
67
|
+
y: PADDING.top,
|
|
68
|
+
width: Math.max(0, width - PADDING.left - PADDING.right),
|
|
69
|
+
height: Math.max(0, height - PADDING.top - PADDING.bottom),
|
|
70
|
+
};
|
|
71
|
+
const bottom = plot.y + plot.height;
|
|
72
|
+
const right = plot.x + plot.width;
|
|
73
|
+
|
|
74
|
+
const all = series.flatMap((s) => s.values).filter(Number.isFinite);
|
|
75
|
+
|
|
76
|
+
const domain = useMemo(() => {
|
|
77
|
+
if (all.length === 0) return [0, 1] as [number, number];
|
|
78
|
+
const min = Math.min(...all);
|
|
79
|
+
const max = Math.max(...all);
|
|
80
|
+
return niceDomain(area ? Math.min(0, min) : min, max, 4);
|
|
81
|
+
}, [all.join(","), area]);
|
|
82
|
+
|
|
83
|
+
const y = linearScale(domain, [bottom, plot.y], { nice: false });
|
|
84
|
+
const x = bandScale(labels.length, [plot.x, right], 0);
|
|
85
|
+
const yTicks = ticks(domain, 4);
|
|
86
|
+
|
|
87
|
+
if (labels.length === 0 || series.length === 0) {
|
|
88
|
+
return (
|
|
89
|
+
<View style={[{ width, height, alignItems: "center", justifyContent: "center" }, style]}>
|
|
90
|
+
<Text style={{ color: theme.muted, fontSize: 12 }}>No series to display</Text>
|
|
91
|
+
</View>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
<View style={style}>
|
|
97
|
+
<Svg width={width} height={height}>
|
|
98
|
+
{/* Grid */}
|
|
99
|
+
{yTicks.map((tick, i) => (
|
|
100
|
+
<Line
|
|
101
|
+
key={`grid-${i}`}
|
|
102
|
+
x1={plot.x}
|
|
103
|
+
x2={right}
|
|
104
|
+
y1={y(tick)}
|
|
105
|
+
y2={y(tick)}
|
|
106
|
+
stroke={theme.axis}
|
|
107
|
+
strokeOpacity={0.24}
|
|
108
|
+
strokeWidth={1}
|
|
109
|
+
/>
|
|
110
|
+
))}
|
|
111
|
+
|
|
112
|
+
{series.map((s, si) => {
|
|
113
|
+
const color = s.color ?? seriesColor(si, palette);
|
|
114
|
+
const points = s.values.map((value, i) => ({
|
|
115
|
+
x: x.center(i),
|
|
116
|
+
y: Number.isFinite(value) ? y(value) : bottom,
|
|
117
|
+
}));
|
|
118
|
+
const line = smooth
|
|
119
|
+
? smoothPath(points)
|
|
120
|
+
: `M ${points.map((p) => `${p.x} ${p.y}`).join(" L ")}`;
|
|
121
|
+
|
|
122
|
+
return (
|
|
123
|
+
<G key={s.name}>
|
|
124
|
+
{area && (
|
|
125
|
+
<Path
|
|
126
|
+
d={`${line} L ${points[points.length - 1]!.x} ${bottom} L ${points[0]!.x} ${bottom} Z`}
|
|
127
|
+
fill={color}
|
|
128
|
+
fillOpacity={0.16}
|
|
129
|
+
/>
|
|
130
|
+
)}
|
|
131
|
+
<Path d={line} fill="none" stroke={color} strokeWidth={2} strokeLinecap="round" />
|
|
132
|
+
{(showDots || active !== null) &&
|
|
133
|
+
points.map((p, i) => (
|
|
134
|
+
<Circle
|
|
135
|
+
key={i}
|
|
136
|
+
cx={p.x}
|
|
137
|
+
cy={p.y}
|
|
138
|
+
r={active === i ? 4 : 2.5}
|
|
139
|
+
fill={color}
|
|
140
|
+
opacity={active === null || active === i ? 1 : 0.4}
|
|
141
|
+
/>
|
|
142
|
+
))}
|
|
143
|
+
</G>
|
|
144
|
+
);
|
|
145
|
+
})}
|
|
146
|
+
|
|
147
|
+
{/* Selected guide line */}
|
|
148
|
+
{active !== null && (
|
|
149
|
+
<Line
|
|
150
|
+
x1={x.center(active)}
|
|
151
|
+
x2={x.center(active)}
|
|
152
|
+
y1={plot.y}
|
|
153
|
+
y2={bottom}
|
|
154
|
+
stroke={theme.foreground}
|
|
155
|
+
strokeOpacity={0.24}
|
|
156
|
+
strokeWidth={1}
|
|
157
|
+
strokeDasharray="3 3"
|
|
158
|
+
/>
|
|
159
|
+
)}
|
|
160
|
+
|
|
161
|
+
{/* Axis labels */}
|
|
162
|
+
{yTicks.map((tick, i) => (
|
|
163
|
+
<SvgText
|
|
164
|
+
key={`y-${i}`}
|
|
165
|
+
x={plot.x - 6}
|
|
166
|
+
y={y(tick) + 3}
|
|
167
|
+
fill={theme.muted}
|
|
168
|
+
fontSize={9}
|
|
169
|
+
textAnchor="end"
|
|
170
|
+
>
|
|
171
|
+
{formatValue(tick, { compact: true })}
|
|
172
|
+
</SvgText>
|
|
173
|
+
))}
|
|
174
|
+
|
|
175
|
+
{labels.map((label, i) => (
|
|
176
|
+
<SvgText
|
|
177
|
+
key={`x-${i}`}
|
|
178
|
+
x={x.center(i)}
|
|
179
|
+
y={height - 8}
|
|
180
|
+
fill={theme.muted}
|
|
181
|
+
fontSize={9}
|
|
182
|
+
textAnchor="middle"
|
|
183
|
+
>
|
|
184
|
+
{label}
|
|
185
|
+
</SvgText>
|
|
186
|
+
))}
|
|
187
|
+
</Svg>
|
|
188
|
+
|
|
189
|
+
{/* Touch targets: one column per point, as wide as the band. */}
|
|
190
|
+
<View
|
|
191
|
+
style={{
|
|
192
|
+
position: "absolute",
|
|
193
|
+
left: plot.x,
|
|
194
|
+
top: plot.y,
|
|
195
|
+
width: plot.width,
|
|
196
|
+
height: plot.height,
|
|
197
|
+
flexDirection: "row",
|
|
198
|
+
}}
|
|
199
|
+
>
|
|
200
|
+
{labels.map((label, i) => (
|
|
201
|
+
<Pressable
|
|
202
|
+
key={label + i}
|
|
203
|
+
accessibilityRole="button"
|
|
204
|
+
accessibilityLabel={`${label}: ${series
|
|
205
|
+
.map((s) => `${s.name} ${formatValue(s.values[i] ?? 0)}`)
|
|
206
|
+
.join(", ")}`}
|
|
207
|
+
style={{ flex: 1 }}
|
|
208
|
+
onPress={() => {
|
|
209
|
+
setActive((current) => (current === i ? null : i));
|
|
210
|
+
onPointPress?.(i);
|
|
211
|
+
}}
|
|
212
|
+
/>
|
|
213
|
+
))}
|
|
214
|
+
</View>
|
|
215
|
+
|
|
216
|
+
{active !== null && (
|
|
217
|
+
<View
|
|
218
|
+
style={{
|
|
219
|
+
position: "absolute",
|
|
220
|
+
top: 0,
|
|
221
|
+
left: PADDING.left,
|
|
222
|
+
right: PADDING.right,
|
|
223
|
+
paddingVertical: 4,
|
|
224
|
+
paddingHorizontal: 8,
|
|
225
|
+
borderRadius: radii.sm,
|
|
226
|
+
backgroundColor: theme.glass,
|
|
227
|
+
flexDirection: "row",
|
|
228
|
+
flexWrap: "wrap",
|
|
229
|
+
gap: 8,
|
|
230
|
+
}}
|
|
231
|
+
pointerEvents="none"
|
|
232
|
+
>
|
|
233
|
+
<Text style={{ color: theme.foreground, fontSize: 10, fontWeight: "600" }}>
|
|
234
|
+
{labels[active]}
|
|
235
|
+
</Text>
|
|
236
|
+
{series.map((s, si) => (
|
|
237
|
+
<Text key={s.name} style={{ color: s.color ?? seriesColor(si, palette), fontSize: 10 }}>
|
|
238
|
+
{s.name} {formatValue(s.values[active] ?? 0)}
|
|
239
|
+
</Text>
|
|
240
|
+
))}
|
|
241
|
+
</View>
|
|
242
|
+
)}
|
|
243
|
+
</View>
|
|
244
|
+
);
|
|
245
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { useMemo, useState } from "react";
|
|
2
|
+
|
|
3
|
+
import { colors } from "../internal/tokens";
|
|
4
|
+
import { primitives } from "./primitives";
|
|
5
|
+
import { formatValue, linearScale, niceDomain, ticks } from "./scales";
|
|
6
|
+
|
|
7
|
+
export interface NativePlotPoint {
|
|
8
|
+
x: number;
|
|
9
|
+
y: number;
|
|
10
|
+
label?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface NativePlotRenderArgs {
|
|
14
|
+
x: (value: number) => number;
|
|
15
|
+
y: (value: number) => number;
|
|
16
|
+
points: NativePlotPoint[];
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface NativePlotChartProps {
|
|
22
|
+
points: NativePlotPoint[];
|
|
23
|
+
/** Draws the marks. Receives real pixel scales, not a fabricated viewBox. */
|
|
24
|
+
renderMark?: (args: NativePlotRenderArgs) => any;
|
|
25
|
+
width?: number;
|
|
26
|
+
height?: number;
|
|
27
|
+
xLabel?: string;
|
|
28
|
+
yLabel?: string;
|
|
29
|
+
isDark?: boolean;
|
|
30
|
+
style?: any;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const PADDING = { top: 12, right: 12, bottom: 26, left: 44 };
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A numeric plot you supply the marks for.
|
|
37
|
+
*
|
|
38
|
+
* The axes, ticks and padding are handled here and the caller receives real
|
|
39
|
+
* pixel scales. That keeps stroke widths and text sizes honest regardless of
|
|
40
|
+
* the chart's width, instead of a scaled viewBox that would distort them.
|
|
41
|
+
*/
|
|
42
|
+
export function NativePlotChart({
|
|
43
|
+
points,
|
|
44
|
+
renderMark,
|
|
45
|
+
width = 320,
|
|
46
|
+
height = 200,
|
|
47
|
+
xLabel,
|
|
48
|
+
yLabel,
|
|
49
|
+
isDark = false,
|
|
50
|
+
style,
|
|
51
|
+
}: NativePlotChartProps) {
|
|
52
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
53
|
+
const { View, Text, Svg, G, Line, Circle, SvgText } = primitives();
|
|
54
|
+
|
|
55
|
+
const plot = {
|
|
56
|
+
x: PADDING.left,
|
|
57
|
+
y: PADDING.top,
|
|
58
|
+
width: Math.max(0, width - PADDING.left - PADDING.right),
|
|
59
|
+
height: Math.max(0, height - PADDING.top - PADDING.bottom),
|
|
60
|
+
};
|
|
61
|
+
const right = plot.x + plot.width;
|
|
62
|
+
const bottom = plot.y + plot.height;
|
|
63
|
+
|
|
64
|
+
const xDomain = useMemo(() => {
|
|
65
|
+
const values = points.map((point) => point.x).filter(Number.isFinite);
|
|
66
|
+
return niceDomain(values.length ? Math.min(...values) : 0, values.length ? Math.max(...values) : 1, 4);
|
|
67
|
+
}, [points.map((point) => point.x).join(",")]);
|
|
68
|
+
|
|
69
|
+
const yDomain = useMemo(() => {
|
|
70
|
+
const values = points.map((point) => point.y).filter(Number.isFinite);
|
|
71
|
+
return niceDomain(values.length ? Math.min(...values) : 0, values.length ? Math.max(...values) : 1, 4);
|
|
72
|
+
}, [points.map((point) => point.y).join(",")]);
|
|
73
|
+
|
|
74
|
+
const x = linearScale(xDomain, [plot.x, right], { nice: false });
|
|
75
|
+
const y = linearScale(yDomain, [bottom, plot.y], { nice: false });
|
|
76
|
+
|
|
77
|
+
if (points.length === 0) {
|
|
78
|
+
return (
|
|
79
|
+
<View style={[{ width, height, alignItems: "center", justifyContent: "center" }, style]}>
|
|
80
|
+
<Text style={{ color: theme.muted, fontSize: 12 }}>No points to display</Text>
|
|
81
|
+
</View>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const yTicks = ticks(yDomain, 4);
|
|
86
|
+
const xTicks = ticks(xDomain, 4);
|
|
87
|
+
|
|
88
|
+
return (
|
|
89
|
+
<View style={style}>
|
|
90
|
+
<Svg width={width} height={height}>
|
|
91
|
+
{yTicks.map((tick, i) => (
|
|
92
|
+
<Line
|
|
93
|
+
key={`grid-${i}`}
|
|
94
|
+
x1={plot.x}
|
|
95
|
+
x2={right}
|
|
96
|
+
y1={y(tick)}
|
|
97
|
+
y2={y(tick)}
|
|
98
|
+
stroke={theme.axis}
|
|
99
|
+
strokeOpacity={0.22}
|
|
100
|
+
strokeWidth={1}
|
|
101
|
+
/>
|
|
102
|
+
))}
|
|
103
|
+
|
|
104
|
+
{renderMark ? (
|
|
105
|
+
renderMark({ x, y, points, width: plot.width, height: plot.height })
|
|
106
|
+
) : (
|
|
107
|
+
<G>
|
|
108
|
+
{points.map((point, index) => (
|
|
109
|
+
<Circle key={index} cx={x(point.x)} cy={y(point.y)} r={3.5} fill={theme.fill} />
|
|
110
|
+
))}
|
|
111
|
+
</G>
|
|
112
|
+
)}
|
|
113
|
+
|
|
114
|
+
{yTicks.map((tick, i) => (
|
|
115
|
+
<SvgText
|
|
116
|
+
key={`y-${i}`}
|
|
117
|
+
x={plot.x - 6}
|
|
118
|
+
y={y(tick) + 3}
|
|
119
|
+
fill={theme.muted}
|
|
120
|
+
fontSize={9}
|
|
121
|
+
textAnchor="end"
|
|
122
|
+
>
|
|
123
|
+
{formatValue(tick, { compact: true })}
|
|
124
|
+
</SvgText>
|
|
125
|
+
))}
|
|
126
|
+
|
|
127
|
+
{xTicks.map((tick, i) => (
|
|
128
|
+
<SvgText
|
|
129
|
+
key={`x-${i}`}
|
|
130
|
+
x={x(tick)}
|
|
131
|
+
y={height - 8}
|
|
132
|
+
fill={theme.muted}
|
|
133
|
+
fontSize={9}
|
|
134
|
+
textAnchor="middle"
|
|
135
|
+
>
|
|
136
|
+
{formatValue(tick, { compact: true })}
|
|
137
|
+
</SvgText>
|
|
138
|
+
))}
|
|
139
|
+
</Svg>
|
|
140
|
+
|
|
141
|
+
{(xLabel || yLabel) && (
|
|
142
|
+
<Text style={{ fontSize: 9, color: theme.muted, marginTop: 2 }}>
|
|
143
|
+
{[xLabel, yLabel].filter(Boolean).join(" · ")}
|
|
144
|
+
</Text>
|
|
145
|
+
)}
|
|
146
|
+
</View>
|
|
147
|
+
);
|
|
148
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
|
|
3
|
+
import { colors } from "../internal/tokens";
|
|
4
|
+
import { primitives } from "./primitives";
|
|
5
|
+
import { formatValue, seriesColor } from "./scales";
|
|
6
|
+
|
|
7
|
+
export interface NativeRadarSeries {
|
|
8
|
+
name: string;
|
|
9
|
+
/** One value per axis, in the same order as `axes`. */
|
|
10
|
+
values: number[];
|
|
11
|
+
color?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface NativeRadarChartProps {
|
|
15
|
+
axes: string[];
|
|
16
|
+
series: NativeRadarSeries[];
|
|
17
|
+
size?: number;
|
|
18
|
+
/** Number of concentric rings behind the data. */
|
|
19
|
+
rings?: number;
|
|
20
|
+
/** Fixed maximum; defaults to the largest value across series. */
|
|
21
|
+
max?: number;
|
|
22
|
+
isDark?: boolean;
|
|
23
|
+
palette?: readonly string[];
|
|
24
|
+
style?: any;
|
|
25
|
+
onAxisPress?: (index: number) => void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const TAU = Math.PI * 2;
|
|
29
|
+
|
|
30
|
+
/** Point on a circle at `index` of `count`, starting at 12 o'clock. */
|
|
31
|
+
function vertex(index: number, count: number, radius: number, cx: number, cy: number) {
|
|
32
|
+
const angle = (index / count) * TAU - Math.PI / 2;
|
|
33
|
+
return { x: cx + Math.cos(angle) * radius, y: cy + Math.sin(angle) * radius };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Several axes around a circle, one closed shape per series.
|
|
38
|
+
*
|
|
39
|
+
* Reading many dimensions off a phone is hard, so every axis label is also a
|
|
40
|
+
* touch target: tapping one lists that axis's values for all series instead of
|
|
41
|
+
* relying on a hover tooltip.
|
|
42
|
+
*/
|
|
43
|
+
export function NativeRadarChart({
|
|
44
|
+
axes,
|
|
45
|
+
series,
|
|
46
|
+
size = 260,
|
|
47
|
+
rings = 4,
|
|
48
|
+
max,
|
|
49
|
+
isDark = false,
|
|
50
|
+
palette,
|
|
51
|
+
style,
|
|
52
|
+
onAxisPress,
|
|
53
|
+
}: NativeRadarChartProps) {
|
|
54
|
+
const [active, setActive] = useState<number | null>(null);
|
|
55
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
56
|
+
const { View, Text, Pressable, Svg, G, Line, Polygon, Circle, SvgText } = primitives();
|
|
57
|
+
|
|
58
|
+
if (axes.length < 3 || series.length === 0) {
|
|
59
|
+
return (
|
|
60
|
+
<View style={[{ width: size, alignItems: "center", paddingVertical: 20 }, style]}>
|
|
61
|
+
<Text style={{ color: theme.muted, fontSize: 12 }}>
|
|
62
|
+
{axes.length < 3 ? "A radar chart needs at least 3 axes" : "No series to display"}
|
|
63
|
+
</Text>
|
|
64
|
+
</View>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const cx = size / 2;
|
|
69
|
+
const cy = size / 2;
|
|
70
|
+
// Leave room for the axis labels, which sit outside the outer ring.
|
|
71
|
+
const radius = size / 2 - 26;
|
|
72
|
+
|
|
73
|
+
const allValues = series.flatMap((s) => s.values).filter(Number.isFinite);
|
|
74
|
+
const highest = max ?? (allValues.length ? Math.max(...allValues) : 1);
|
|
75
|
+
const ceiling = highest > 0 ? highest : 1;
|
|
76
|
+
|
|
77
|
+
const ringPolygons = Array.from({ length: rings }, (_, i) => {
|
|
78
|
+
const r = (radius * (i + 1)) / rings;
|
|
79
|
+
return axes
|
|
80
|
+
.map((_, a) => {
|
|
81
|
+
const p = vertex(a, axes.length, r, cx, cy);
|
|
82
|
+
return `${p.x},${p.y}`;
|
|
83
|
+
})
|
|
84
|
+
.join(" ");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<View style={[{ width: size }, style]}>
|
|
89
|
+
<Svg width={size} height={size}>
|
|
90
|
+
{ringPolygons.map((points, i) => (
|
|
91
|
+
<Polygon
|
|
92
|
+
key={`ring-${i}`}
|
|
93
|
+
points={points}
|
|
94
|
+
fill="none"
|
|
95
|
+
stroke={theme.axis}
|
|
96
|
+
strokeOpacity={0.22}
|
|
97
|
+
strokeWidth={1}
|
|
98
|
+
/>
|
|
99
|
+
))}
|
|
100
|
+
|
|
101
|
+
{axes.map((_, a) => {
|
|
102
|
+
const p = vertex(a, axes.length, radius, cx, cy);
|
|
103
|
+
return (
|
|
104
|
+
<Line
|
|
105
|
+
key={`spoke-${a}`}
|
|
106
|
+
x1={cx}
|
|
107
|
+
y1={cy}
|
|
108
|
+
x2={p.x}
|
|
109
|
+
y2={p.y}
|
|
110
|
+
stroke={theme.axis}
|
|
111
|
+
strokeOpacity={0.22}
|
|
112
|
+
strokeWidth={1}
|
|
113
|
+
/>
|
|
114
|
+
);
|
|
115
|
+
})}
|
|
116
|
+
|
|
117
|
+
{series.map((s, si) => {
|
|
118
|
+
const color = s.color ?? seriesColor(si, palette);
|
|
119
|
+
const points = s.values
|
|
120
|
+
.map((value, a) => {
|
|
121
|
+
const ratio = Math.max(0, Math.min(1, value / ceiling));
|
|
122
|
+
const p = vertex(a, axes.length, radius * ratio, cx, cy);
|
|
123
|
+
return `${p.x},${p.y}`;
|
|
124
|
+
})
|
|
125
|
+
.join(" ");
|
|
126
|
+
|
|
127
|
+
return (
|
|
128
|
+
<G key={s.name}>
|
|
129
|
+
<Polygon
|
|
130
|
+
points={points}
|
|
131
|
+
fill={color}
|
|
132
|
+
fillOpacity={active === null ? 0.18 : 0.1}
|
|
133
|
+
stroke={color}
|
|
134
|
+
strokeWidth={2}
|
|
135
|
+
/>
|
|
136
|
+
{s.values.map((value, a) => {
|
|
137
|
+
const ratio = Math.max(0, Math.min(1, value / ceiling));
|
|
138
|
+
const p = vertex(a, axes.length, radius * ratio, cx, cy);
|
|
139
|
+
return (
|
|
140
|
+
<Circle
|
|
141
|
+
key={a}
|
|
142
|
+
cx={p.x}
|
|
143
|
+
cy={p.y}
|
|
144
|
+
r={active === a ? 4 : 2.5}
|
|
145
|
+
fill={color}
|
|
146
|
+
/>
|
|
147
|
+
);
|
|
148
|
+
})}
|
|
149
|
+
</G>
|
|
150
|
+
);
|
|
151
|
+
})}
|
|
152
|
+
|
|
153
|
+
{axes.map((axis, a) => {
|
|
154
|
+
const p = vertex(a, axes.length, radius + 13, cx, cy);
|
|
155
|
+
// Labels on the left half should end at the axis, not start from it.
|
|
156
|
+
const anchor = p.x < cx - 1 ? "end" : p.x > cx + 1 ? "start" : "middle";
|
|
157
|
+
return (
|
|
158
|
+
<SvgText
|
|
159
|
+
key={`label-${a}`}
|
|
160
|
+
x={p.x}
|
|
161
|
+
y={p.y + 3}
|
|
162
|
+
fill={active === a ? theme.foreground : theme.muted}
|
|
163
|
+
fontSize={9}
|
|
164
|
+
textAnchor={anchor}
|
|
165
|
+
>
|
|
166
|
+
{axis}
|
|
167
|
+
</SvgText>
|
|
168
|
+
);
|
|
169
|
+
})}
|
|
170
|
+
</Svg>
|
|
171
|
+
|
|
172
|
+
{/* Axis pickers: a phone has no hover, so each axis is separately tappable. */}
|
|
173
|
+
<View style={{ flexDirection: "row", flexWrap: "wrap", gap: 6, marginTop: 8 }}>
|
|
174
|
+
{axes.map((axis, a) => (
|
|
175
|
+
<Pressable
|
|
176
|
+
key={axis}
|
|
177
|
+
accessibilityRole="button"
|
|
178
|
+
accessibilityLabel={`${axis} axis`}
|
|
179
|
+
onPress={() => {
|
|
180
|
+
setActive((current) => (current === a ? null : a));
|
|
181
|
+
onAxisPress?.(a);
|
|
182
|
+
}}
|
|
183
|
+
style={{
|
|
184
|
+
paddingVertical: 3,
|
|
185
|
+
paddingHorizontal: 8,
|
|
186
|
+
borderRadius: 7,
|
|
187
|
+
backgroundColor: active === a ? theme.glassActive : theme.glass,
|
|
188
|
+
}}
|
|
189
|
+
>
|
|
190
|
+
<Text style={{ color: theme.foreground, fontSize: 10 }}>{axis}</Text>
|
|
191
|
+
</Pressable>
|
|
192
|
+
))}
|
|
193
|
+
</View>
|
|
194
|
+
|
|
195
|
+
{active !== null && (
|
|
196
|
+
<View style={{ marginTop: 8 }}>
|
|
197
|
+
{series.map((s, si) => (
|
|
198
|
+
<Text key={s.name} style={{ color: theme.muted, fontSize: 10, marginTop: 2 }}>
|
|
199
|
+
{s.name}: {formatValue(s.values[active] ?? 0)}
|
|
200
|
+
</Text>
|
|
201
|
+
))}
|
|
202
|
+
</View>
|
|
203
|
+
)}
|
|
204
|
+
</View>
|
|
205
|
+
);
|
|
206
|
+
}
|