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,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NativeRadarChart = NativeRadarChart;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const tokens_1 = require("../internal/tokens");
|
|
7
|
+
const primitives_1 = require("./primitives");
|
|
8
|
+
const scales_1 = require("./scales");
|
|
9
|
+
const TAU = Math.PI * 2;
|
|
10
|
+
/** Point on a circle at `index` of `count`, starting at 12 o'clock. */
|
|
11
|
+
function vertex(index, count, radius, cx, cy) {
|
|
12
|
+
const angle = (index / count) * TAU - Math.PI / 2;
|
|
13
|
+
return { x: cx + Math.cos(angle) * radius, y: cy + Math.sin(angle) * radius };
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Several axes around a circle, one closed shape per series.
|
|
17
|
+
*
|
|
18
|
+
* Reading many dimensions off a phone is hard, so every axis label is also a
|
|
19
|
+
* touch target: tapping one lists that axis's values for all series instead of
|
|
20
|
+
* relying on a hover tooltip.
|
|
21
|
+
*/
|
|
22
|
+
function NativeRadarChart({ axes, series, size = 260, rings = 4, max, isDark = false, palette, style, onAxisPress, }) {
|
|
23
|
+
const [active, setActive] = (0, react_1.useState)(null);
|
|
24
|
+
const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
|
|
25
|
+
const { View, Text, Pressable, Svg, G, Line, Polygon, Circle, SvgText } = (0, primitives_1.primitives)();
|
|
26
|
+
if (axes.length < 3 || series.length === 0) {
|
|
27
|
+
return ((0, jsx_runtime_1.jsx)(View, { style: [{ width: size, alignItems: "center", paddingVertical: 20 }, style], children: (0, jsx_runtime_1.jsx)(Text, { style: { color: theme.muted, fontSize: 12 }, children: axes.length < 3 ? "A radar chart needs at least 3 axes" : "No series to display" }) }));
|
|
28
|
+
}
|
|
29
|
+
const cx = size / 2;
|
|
30
|
+
const cy = size / 2;
|
|
31
|
+
// Leave room for the axis labels, which sit outside the outer ring.
|
|
32
|
+
const radius = size / 2 - 26;
|
|
33
|
+
const allValues = series.flatMap((s) => s.values).filter(Number.isFinite);
|
|
34
|
+
const highest = max ?? (allValues.length ? Math.max(...allValues) : 1);
|
|
35
|
+
const ceiling = highest > 0 ? highest : 1;
|
|
36
|
+
const ringPolygons = Array.from({ length: rings }, (_, i) => {
|
|
37
|
+
const r = (radius * (i + 1)) / rings;
|
|
38
|
+
return axes
|
|
39
|
+
.map((_, a) => {
|
|
40
|
+
const p = vertex(a, axes.length, r, cx, cy);
|
|
41
|
+
return `${p.x},${p.y}`;
|
|
42
|
+
})
|
|
43
|
+
.join(" ");
|
|
44
|
+
});
|
|
45
|
+
return ((0, jsx_runtime_1.jsxs)(View, { style: [{ width: size }, style], children: [(0, jsx_runtime_1.jsxs)(Svg, { width: size, height: size, children: [ringPolygons.map((points, i) => ((0, jsx_runtime_1.jsx)(Polygon, { points: points, fill: "none", stroke: theme.axis, strokeOpacity: 0.22, strokeWidth: 1 }, `ring-${i}`))), axes.map((_, a) => {
|
|
46
|
+
const p = vertex(a, axes.length, radius, cx, cy);
|
|
47
|
+
return ((0, jsx_runtime_1.jsx)(Line, { x1: cx, y1: cy, x2: p.x, y2: p.y, stroke: theme.axis, strokeOpacity: 0.22, strokeWidth: 1 }, `spoke-${a}`));
|
|
48
|
+
}), series.map((s, si) => {
|
|
49
|
+
const color = s.color ?? (0, scales_1.seriesColor)(si, palette);
|
|
50
|
+
const points = s.values
|
|
51
|
+
.map((value, a) => {
|
|
52
|
+
const ratio = Math.max(0, Math.min(1, value / ceiling));
|
|
53
|
+
const p = vertex(a, axes.length, radius * ratio, cx, cy);
|
|
54
|
+
return `${p.x},${p.y}`;
|
|
55
|
+
})
|
|
56
|
+
.join(" ");
|
|
57
|
+
return ((0, jsx_runtime_1.jsxs)(G, { children: [(0, jsx_runtime_1.jsx)(Polygon, { points: points, fill: color, fillOpacity: active === null ? 0.18 : 0.1, stroke: color, strokeWidth: 2 }), s.values.map((value, a) => {
|
|
58
|
+
const ratio = Math.max(0, Math.min(1, value / ceiling));
|
|
59
|
+
const p = vertex(a, axes.length, radius * ratio, cx, cy);
|
|
60
|
+
return ((0, jsx_runtime_1.jsx)(Circle, { cx: p.x, cy: p.y, r: active === a ? 4 : 2.5, fill: color }, a));
|
|
61
|
+
})] }, s.name));
|
|
62
|
+
}), axes.map((axis, a) => {
|
|
63
|
+
const p = vertex(a, axes.length, radius + 13, cx, cy);
|
|
64
|
+
// Labels on the left half should end at the axis, not start from it.
|
|
65
|
+
const anchor = p.x < cx - 1 ? "end" : p.x > cx + 1 ? "start" : "middle";
|
|
66
|
+
return ((0, jsx_runtime_1.jsx)(SvgText, { x: p.x, y: p.y + 3, fill: active === a ? theme.foreground : theme.muted, fontSize: 9, textAnchor: anchor, children: axis }, `label-${a}`));
|
|
67
|
+
})] }), (0, jsx_runtime_1.jsx)(View, { style: { flexDirection: "row", flexWrap: "wrap", gap: 6, marginTop: 8 }, children: axes.map((axis, a) => ((0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "button", accessibilityLabel: `${axis} axis`, onPress: () => {
|
|
68
|
+
setActive((current) => (current === a ? null : a));
|
|
69
|
+
onAxisPress?.(a);
|
|
70
|
+
}, style: {
|
|
71
|
+
paddingVertical: 3,
|
|
72
|
+
paddingHorizontal: 8,
|
|
73
|
+
borderRadius: 7,
|
|
74
|
+
backgroundColor: active === a ? theme.glassActive : theme.glass,
|
|
75
|
+
}, children: (0, jsx_runtime_1.jsx)(Text, { style: { color: theme.foreground, fontSize: 10 }, children: axis }) }, axis))) }), active !== null && ((0, jsx_runtime_1.jsx)(View, { style: { marginTop: 8 }, children: series.map((s, si) => ((0, jsx_runtime_1.jsxs)(Text, { style: { color: theme.muted, fontSize: 10, marginTop: 2 }, children: [s.name, ": ", (0, scales_1.formatValue)(s.values[active] ?? 0)] }, s.name))) }))] }));
|
|
76
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NativeRingChart = NativeRingChart;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const tokens_1 = require("../internal/tokens");
|
|
7
|
+
const primitives_1 = require("./primitives");
|
|
8
|
+
const scales_1 = require("./scales");
|
|
9
|
+
const TAU = Math.PI * 2;
|
|
10
|
+
/**
|
|
11
|
+
* Proportional slices as a ring, drawn with react-native-svg.
|
|
12
|
+
*
|
|
13
|
+
* Shares the arc maths with the web RingChart, so slices land in the same
|
|
14
|
+
* places on both platforms. A legend is on by default: on a phone there is no
|
|
15
|
+
* hover, so colour alone cannot identify a slice.
|
|
16
|
+
*/
|
|
17
|
+
function NativeRingChart({ data, size = 180, thickness = 22, isDark = false, palette, showLegend = true, style, onSlicePress, }) {
|
|
18
|
+
const [active, setActive] = (0, react_1.useState)(null);
|
|
19
|
+
const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
|
|
20
|
+
const { View, Text, Pressable, Svg, Path } = (0, primitives_1.primitives)();
|
|
21
|
+
const total = data.reduce((sum, slice) => sum + Math.max(0, slice.value), 0);
|
|
22
|
+
if (data.length === 0 || total <= 0) {
|
|
23
|
+
return ((0, jsx_runtime_1.jsx)(View, { style: [{ width: size, alignItems: "center" }, style], children: (0, jsx_runtime_1.jsx)(Text, { style: { color: theme.muted, fontSize: 12 }, children: "No slices to display" }) }));
|
|
24
|
+
}
|
|
25
|
+
const outer = size / 2;
|
|
26
|
+
const inner = Math.max(0, outer - thickness);
|
|
27
|
+
let cursor = -Math.PI / 2;
|
|
28
|
+
return ((0, jsx_runtime_1.jsxs)(View, { style: [{ alignItems: "center" }, style], children: [(0, jsx_runtime_1.jsx)(Svg, { width: size, height: size, children: data.map((slice, i) => {
|
|
29
|
+
const share = Math.max(0, slice.value) / total;
|
|
30
|
+
const start = cursor;
|
|
31
|
+
const end = cursor + share * TAU;
|
|
32
|
+
cursor = end;
|
|
33
|
+
return ((0, jsx_runtime_1.jsx)(Path, { d: (0, scales_1.arcPath)(outer, outer, outer, inner, start, end), fill: slice.color ?? (0, scales_1.seriesColor)(i, palette), opacity: active === null || active === i ? 1 : 0.4, onPress: () => {
|
|
34
|
+
setActive((current) => (current === i ? null : i));
|
|
35
|
+
onSlicePress?.(i);
|
|
36
|
+
} }, slice.label));
|
|
37
|
+
}) }), showLegend && ((0, jsx_runtime_1.jsx)(View, { style: { marginTop: 12, width: "100%", maxWidth: 260, gap: 6 }, children: data.map((slice, i) => ((0, jsx_runtime_1.jsxs)(Pressable, { accessibilityRole: "button", accessibilityLabel: `${slice.label}: ${(0, scales_1.formatValue)(slice.value)}, ${Math.round((slice.value / total) * 100)} percent`, onPress: () => {
|
|
38
|
+
setActive((current) => (current === i ? null : i));
|
|
39
|
+
onSlicePress?.(i);
|
|
40
|
+
}, style: { flexDirection: "row", alignItems: "center", gap: 8 }, children: [(0, jsx_runtime_1.jsx)(View, { style: {
|
|
41
|
+
width: 9,
|
|
42
|
+
height: 9,
|
|
43
|
+
borderRadius: 3,
|
|
44
|
+
backgroundColor: slice.color ?? (0, scales_1.seriesColor)(i, palette),
|
|
45
|
+
} }), (0, jsx_runtime_1.jsx)(Text, { style: { color: theme.muted, fontSize: 12, flex: 1 }, numberOfLines: 1, children: slice.label }), (0, jsx_runtime_1.jsx)(Text, { style: {
|
|
46
|
+
color: theme.foreground,
|
|
47
|
+
fontSize: 12,
|
|
48
|
+
fontWeight: active === i ? "700" : "600",
|
|
49
|
+
}, children: (0, scales_1.formatValue)(slice.value) })] }, slice.label))) }))] }));
|
|
50
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NativeScatterChart = NativeScatterChart;
|
|
4
|
+
exports.NativeBubbleChart = NativeBubbleChart;
|
|
5
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
const react_1 = require("react");
|
|
7
|
+
const tokens_1 = require("../internal/tokens");
|
|
8
|
+
const primitives_1 = require("./primitives");
|
|
9
|
+
const scales_1 = require("./scales");
|
|
10
|
+
const PADDING = { top: 12, right: 12, bottom: 24, left: 40 };
|
|
11
|
+
/**
|
|
12
|
+
* Two numeric axes, drawn with react-native-svg.
|
|
13
|
+
*
|
|
14
|
+
* BubbleChart is this component with `bubble` set, so the two share their
|
|
15
|
+
* axes, padding and touch behaviour instead of drifting apart.
|
|
16
|
+
*/
|
|
17
|
+
function NativeScatterChart({ series, width = 320, height = 200, radius = 4, bubble = false, isDark = false, palette, style, onPointPress, }) {
|
|
18
|
+
const [active, setActive] = (0, react_1.useState)(null);
|
|
19
|
+
const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
|
|
20
|
+
const { View, Text, Svg, G, Line, Circle, SvgText } = (0, primitives_1.primitives)();
|
|
21
|
+
const plot = {
|
|
22
|
+
x: PADDING.left,
|
|
23
|
+
y: PADDING.top,
|
|
24
|
+
width: Math.max(0, width - PADDING.left - PADDING.right),
|
|
25
|
+
height: Math.max(0, height - PADDING.top - PADDING.bottom),
|
|
26
|
+
};
|
|
27
|
+
const right = plot.x + plot.width;
|
|
28
|
+
const bottom = plot.y + plot.height;
|
|
29
|
+
const all = series.flatMap((s) => s.points);
|
|
30
|
+
const xDomain = (0, react_1.useMemo)(() => {
|
|
31
|
+
const xs = all.map((p) => p.x).filter(Number.isFinite);
|
|
32
|
+
return (0, scales_1.niceDomain)(xs.length ? Math.min(...xs) : 0, xs.length ? Math.max(...xs) : 1, 4);
|
|
33
|
+
}, [all.map((p) => p.x).join(",")]);
|
|
34
|
+
const yDomain = (0, react_1.useMemo)(() => {
|
|
35
|
+
const ys = all.map((p) => p.y).filter(Number.isFinite);
|
|
36
|
+
return (0, scales_1.niceDomain)(ys.length ? Math.min(...ys) : 0, ys.length ? Math.max(...ys) : 1, 4);
|
|
37
|
+
}, [all.map((p) => p.y).join(",")]);
|
|
38
|
+
const x = (0, scales_1.linearScale)(xDomain, [plot.x, right], { nice: false });
|
|
39
|
+
const y = (0, scales_1.linearScale)(yDomain, [bottom, plot.y], { nice: false });
|
|
40
|
+
const sizes = all.map((p) => p.size ?? 1);
|
|
41
|
+
const maxSize = sizes.length ? Math.max(...sizes) : 1;
|
|
42
|
+
const minSize = sizes.length ? Math.min(...sizes) : 0;
|
|
43
|
+
const maxRadius = Math.min(plot.width, plot.height) / 7;
|
|
44
|
+
const radiusFor = (point) => {
|
|
45
|
+
if (!bubble)
|
|
46
|
+
return radius;
|
|
47
|
+
const size = point.size ?? minSize;
|
|
48
|
+
const t = maxSize === minSize ? 1 : (size - minSize) / (maxSize - minSize);
|
|
49
|
+
return 3 + t * (maxRadius - 3);
|
|
50
|
+
};
|
|
51
|
+
if (all.length === 0) {
|
|
52
|
+
return ((0, jsx_runtime_1.jsx)(View, { style: [{ width, height, alignItems: "center", justifyContent: "center" }, style], children: (0, jsx_runtime_1.jsx)(Text, { style: { color: theme.muted, fontSize: 12 }, children: "No points to display" }) }));
|
|
53
|
+
}
|
|
54
|
+
const yTicks = (0, scales_1.ticks)(yDomain, 4);
|
|
55
|
+
const xTicks = (0, scales_1.ticks)(xDomain, 4);
|
|
56
|
+
return ((0, jsx_runtime_1.jsxs)(View, { style: style, children: [(0, jsx_runtime_1.jsxs)(Svg, { width: width, height: height, children: [yTicks.map((tick, i) => ((0, jsx_runtime_1.jsx)(Line, { x1: plot.x, x2: right, y1: y(tick), y2: y(tick), stroke: theme.axis, strokeOpacity: 0.24, strokeWidth: 1 }, `grid-${i}`))), series.map((s, si) => {
|
|
57
|
+
const color = s.color ?? (0, scales_1.seriesColor)(si, palette);
|
|
58
|
+
return ((0, jsx_runtime_1.jsx)(G, { children: s.points.map((point, pi) => {
|
|
59
|
+
const isActive = active?.s === si && active?.p === pi;
|
|
60
|
+
return ((0, jsx_runtime_1.jsx)(Circle, { cx: x(point.x), cy: y(point.y), r: radiusFor(point) + (isActive ? 2 : 0), fill: color, fillOpacity: isActive ? 0.95 : bubble ? 0.55 : 0.8, onPress: () => {
|
|
61
|
+
setActive(isActive ? null : { s: si, p: pi });
|
|
62
|
+
onPointPress?.(si, pi);
|
|
63
|
+
} }, pi));
|
|
64
|
+
}) }, s.name));
|
|
65
|
+
}), yTicks.map((tick, i) => ((0, jsx_runtime_1.jsx)(SvgText, { x: plot.x - 6, y: y(tick) + 3, fill: theme.muted, fontSize: 9, textAnchor: "end", children: (0, scales_1.formatValue)(tick, { compact: true }) }, `y-${i}`))), xTicks.map((tick, i) => ((0, jsx_runtime_1.jsx)(SvgText, { x: x(tick), y: height - 8, fill: theme.muted, fontSize: 9, textAnchor: "middle", children: (0, scales_1.formatValue)(tick, { compact: true }) }, `x-${i}`)))] }), active !== null && series[active.s]?.points[active.p] && ((0, jsx_runtime_1.jsxs)(View, { style: {
|
|
66
|
+
position: "absolute",
|
|
67
|
+
top: 0,
|
|
68
|
+
left: PADDING.left,
|
|
69
|
+
paddingVertical: 3,
|
|
70
|
+
paddingHorizontal: 7,
|
|
71
|
+
borderRadius: 8,
|
|
72
|
+
backgroundColor: theme.glass,
|
|
73
|
+
}, pointerEvents: "none", children: [(0, jsx_runtime_1.jsx)(Text, { style: { color: theme.foreground, fontSize: 10, fontWeight: "600" }, children: series[active.s].points[active.p].label ?? series[active.s].name }), (0, jsx_runtime_1.jsxs)(Text, { style: { color: theme.muted, fontSize: 10 }, children: ["x ", (0, scales_1.formatValue)(series[active.s].points[active.p].x), " \u00B7 y", " ", (0, scales_1.formatValue)(series[active.s].points[active.p].y)] })] }))] }));
|
|
74
|
+
}
|
|
75
|
+
/** Scatter marks sized by a third value. */
|
|
76
|
+
function NativeBubbleChart(props) {
|
|
77
|
+
return (0, jsx_runtime_1.jsx)(NativeScatterChart, { ...props, bubble: true });
|
|
78
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NativeWaterfallChart = NativeWaterfallChart;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const tokens_1 = require("../internal/tokens");
|
|
7
|
+
const primitives_1 = require("./primitives");
|
|
8
|
+
const scales_1 = require("./scales");
|
|
9
|
+
const PADDING = { top: 12, right: 8, bottom: 26, left: 42 };
|
|
10
|
+
/**
|
|
11
|
+
* How a running total moves step by step.
|
|
12
|
+
*
|
|
13
|
+
* Each bar starts where the previous one ended, so the reader sees the bridge
|
|
14
|
+
* from opening to closing balance rather than a set of unrelated changes.
|
|
15
|
+
* Increases and decreases are coloured differently, and totals span the axis.
|
|
16
|
+
*/
|
|
17
|
+
function NativeWaterfallChart({ steps, width = 320, height = 200, isDark = false, style, onStepPress, }) {
|
|
18
|
+
const [active, setActive] = (0, react_1.useState)(null);
|
|
19
|
+
const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
|
|
20
|
+
const { View, Text, Pressable, Svg, G, Line, Rect, SvgText } = (0, primitives_1.primitives)();
|
|
21
|
+
if (steps.length === 0) {
|
|
22
|
+
return ((0, jsx_runtime_1.jsx)(View, { style: [{ width, alignItems: "center", paddingVertical: 20 }, style], children: (0, jsx_runtime_1.jsx)(Text, { style: { color: theme.muted, fontSize: 12 }, children: "No steps to display" }) }));
|
|
23
|
+
}
|
|
24
|
+
const up = "#34c759";
|
|
25
|
+
const down = "#ff375f";
|
|
26
|
+
const plot = {
|
|
27
|
+
x: PADDING.left,
|
|
28
|
+
y: PADDING.top,
|
|
29
|
+
width: Math.max(0, width - PADDING.left - PADDING.right),
|
|
30
|
+
height: Math.max(0, height - PADDING.top - PADDING.bottom),
|
|
31
|
+
};
|
|
32
|
+
const right = plot.x + plot.width;
|
|
33
|
+
const bottom = plot.y + plot.height;
|
|
34
|
+
// Bounds come from the running totals, not the raw deltas: a −50 midway
|
|
35
|
+
// through a climb needs the axis to reach below zero only if the running
|
|
36
|
+
// total actually does.
|
|
37
|
+
let running = 0;
|
|
38
|
+
const geometry = steps.map((step) => {
|
|
39
|
+
const start = step.isTotal ? 0 : running;
|
|
40
|
+
const end = step.isTotal ? step.value : running + step.value;
|
|
41
|
+
if (!step.isTotal)
|
|
42
|
+
running = end;
|
|
43
|
+
return {
|
|
44
|
+
start,
|
|
45
|
+
end,
|
|
46
|
+
low: Math.min(start, end),
|
|
47
|
+
high: Math.max(start, end),
|
|
48
|
+
};
|
|
49
|
+
});
|
|
50
|
+
const lows = geometry.map((g) => g.low);
|
|
51
|
+
const highs = geometry.map((g) => g.high);
|
|
52
|
+
const min = Math.min(0, ...lows);
|
|
53
|
+
const max = Math.max(0, ...highs);
|
|
54
|
+
const span = max - min || 1;
|
|
55
|
+
const y = (value) => bottom - ((value - min) / span) * plot.height;
|
|
56
|
+
const stepWidth = plot.width / steps.length;
|
|
57
|
+
const barWidth = Math.min(34, stepWidth * 0.6);
|
|
58
|
+
const zeroY = y(0);
|
|
59
|
+
return ((0, jsx_runtime_1.jsxs)(View, { style: style, children: [(0, jsx_runtime_1.jsxs)(Svg, { width: width, height: height, children: [(0, jsx_runtime_1.jsx)(Line, { x1: plot.x, x2: right, y1: zeroY, y2: zeroY, stroke: theme.axis, strokeOpacity: 0.4, strokeWidth: 1 }), geometry.map((g, i) => {
|
|
60
|
+
const step = steps[i];
|
|
61
|
+
const top = y(g.high);
|
|
62
|
+
const barHeight = Math.max(1, y(g.low) - top);
|
|
63
|
+
const color = step.isTotal ? seriesTotalColor(isDark) : step.value >= 0 ? up : down;
|
|
64
|
+
const x = plot.x + stepWidth * i + (stepWidth - barWidth) / 2;
|
|
65
|
+
return ((0, jsx_runtime_1.jsxs)(G, { children: [(0, jsx_runtime_1.jsx)(Rect, { x: x, y: top, width: barWidth, height: barHeight, rx: 3, fill: color, fillOpacity: active === null || active === i ? 1 : 0.45, onPress: () => {
|
|
66
|
+
setActive((current) => (current === i ? null : i));
|
|
67
|
+
onStepPress?.(i);
|
|
68
|
+
} }), i < steps.length - 1 && !steps[i + 1].isTotal && ((0, jsx_runtime_1.jsx)(Line, { x1: x + barWidth, x2: plot.x + stepWidth * (i + 1) + (stepWidth - barWidth) / 2, y1: y(g.end), y2: y(g.end), stroke: theme.axis, strokeOpacity: 0.4, strokeWidth: 1, strokeDasharray: "2,2" })), (0, jsx_runtime_1.jsx)(SvgText, { x: x + barWidth / 2, y: height - 8, fill: theme.muted, fontSize: 8, textAnchor: "middle", children: step.label })] }, step.label));
|
|
69
|
+
}), [max, min].map((value, i) => ((0, jsx_runtime_1.jsx)(SvgText, { x: plot.x - 6, y: y(value) + 3, fill: theme.muted, fontSize: 9, textAnchor: "end", children: (0, scales_1.formatValue)(value, { compact: true }) }, `y-${i}`)))] }), active !== null && ((0, jsx_runtime_1.jsxs)(Text, { style: { color: theme.foreground, fontSize: 11, marginTop: 6 }, children: [steps[active].label, ": ", steps[active].value >= 0 ? "+" : "", (0, scales_1.formatValue)(steps[active].value), steps[active].isTotal
|
|
70
|
+
? " (total)"
|
|
71
|
+
: ` → ${(0, scales_1.formatValue)(geometry[active].end)} running`] }))] }));
|
|
72
|
+
}
|
|
73
|
+
/** Totals are neutral so they do not read as a gain or loss. */
|
|
74
|
+
function seriesTotalColor(isDark) {
|
|
75
|
+
return isDark ? "#8e8e93" : "#3c3c43";
|
|
76
|
+
}
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NativeCandlestickChart = NativeCandlestickChart;
|
|
4
|
+
exports.NativeTreemapChart = NativeTreemapChart;
|
|
5
|
+
exports.NativeSankeyChart = NativeSankeyChart;
|
|
6
|
+
exports.NativeBumpChart = NativeBumpChart;
|
|
7
|
+
exports.NativeHexChart = NativeHexChart;
|
|
8
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
9
|
+
const react_1 = require("react");
|
|
10
|
+
const tokens_1 = require("../internal/tokens");
|
|
11
|
+
const primitives_1 = require("./primitives");
|
|
12
|
+
const scales_1 = require("./scales");
|
|
13
|
+
const PADDING = { top: 12, right: 8, bottom: 24, left: 42 };
|
|
14
|
+
/**
|
|
15
|
+
* Open, high, low and close for each period.
|
|
16
|
+
*
|
|
17
|
+
* A filled body means the period closed up and a hollow body means it closed
|
|
18
|
+
* down, which is the convention traders read without a legend. Each candle is
|
|
19
|
+
* tappable to reveal the four prices, replacing the web hover tooltip.
|
|
20
|
+
*/
|
|
21
|
+
function NativeCandlestickChart({ candles, width = 320, height = 200, isDark = false, style, onCandlePress, }) {
|
|
22
|
+
const [active, setActive] = (0, react_1.useState)(null);
|
|
23
|
+
const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
|
|
24
|
+
const { View, Text, Svg, G, Line, Rect, SvgText } = (0, primitives_1.primitives)();
|
|
25
|
+
const up = "#34c759";
|
|
26
|
+
const down = "#ff3b30";
|
|
27
|
+
const plot = {
|
|
28
|
+
x: PADDING.left,
|
|
29
|
+
y: PADDING.top,
|
|
30
|
+
width: Math.max(0, width - PADDING.left - PADDING.right),
|
|
31
|
+
height: Math.max(0, height - PADDING.top - PADDING.bottom),
|
|
32
|
+
};
|
|
33
|
+
const right = plot.x + plot.width;
|
|
34
|
+
const bottom = plot.y + plot.height;
|
|
35
|
+
const domain = (0, react_1.useMemo)(() => {
|
|
36
|
+
const highs = candles.map((c) => c.high).filter(Number.isFinite);
|
|
37
|
+
const lows = candles.map((c) => c.low).filter(Number.isFinite);
|
|
38
|
+
if (!highs.length || !lows.length)
|
|
39
|
+
return (0, scales_1.niceDomain)(0, 1, 4);
|
|
40
|
+
return (0, scales_1.niceDomain)(Math.min(...lows), Math.max(...highs), 4);
|
|
41
|
+
}, [candles.map((c) => `${c.high},${c.low}`).join("|")]);
|
|
42
|
+
const y = (0, scales_1.linearScale)(domain, [bottom, plot.y], { nice: false });
|
|
43
|
+
if (candles.length === 0) {
|
|
44
|
+
return ((0, jsx_runtime_1.jsx)(View, { style: [{ width, height, alignItems: "center", justifyContent: "center" }, style], children: (0, jsx_runtime_1.jsx)(Text, { style: { color: theme.muted, fontSize: 12 }, children: "No candles to display" }) }));
|
|
45
|
+
}
|
|
46
|
+
const step = plot.width / candles.length;
|
|
47
|
+
const bodyWidth = Math.min(14, step * 0.6);
|
|
48
|
+
const yTicks = (0, scales_1.ticks)(domain, 4);
|
|
49
|
+
return ((0, jsx_runtime_1.jsxs)(View, { style: style, children: [(0, jsx_runtime_1.jsxs)(Svg, { width: width, height: height, children: [yTicks.map((tick, i) => ((0, jsx_runtime_1.jsx)(Line, { x1: plot.x, x2: right, y1: y(tick), y2: y(tick), stroke: theme.axis, strokeOpacity: 0.22, strokeWidth: 1 }, `grid-${i}`))), candles.map((candle, index) => {
|
|
50
|
+
const rising = candle.close >= candle.open;
|
|
51
|
+
const color = rising ? up : down;
|
|
52
|
+
const cx = plot.x + step * index + step / 2;
|
|
53
|
+
const bodyTop = y(Math.max(candle.open, candle.close));
|
|
54
|
+
const bodyBottom = y(Math.min(candle.open, candle.close));
|
|
55
|
+
const isActive = active === index;
|
|
56
|
+
return ((0, jsx_runtime_1.jsxs)(G, { onPress: () => {
|
|
57
|
+
setActive((current) => (current === index ? null : index));
|
|
58
|
+
onCandlePress?.(index);
|
|
59
|
+
}, children: [(0, jsx_runtime_1.jsx)(Line, { x1: cx, x2: cx, y1: y(candle.high), y2: y(candle.low), stroke: color, strokeWidth: 1 }), (0, jsx_runtime_1.jsx)(Rect, { x: cx - bodyWidth / 2, y: bodyTop, width: bodyWidth, height: Math.max(1, bodyBottom - bodyTop), rx: 1, fill: rising ? color : "transparent", stroke: color, strokeWidth: 1, fillOpacity: isActive ? 1 : 0.9 })] }, candle.label));
|
|
60
|
+
}), yTicks.map((tick, i) => ((0, jsx_runtime_1.jsx)(SvgText, { x: plot.x - 6, y: y(tick) + 3, fill: theme.muted, fontSize: 9, textAnchor: "end", children: (0, scales_1.formatValue)(tick, { compact: true }) }, `y-${i}`)))] }), active !== null && candles[active] && ((0, jsx_runtime_1.jsxs)(Text, { style: { fontSize: 10, color: theme.muted, marginTop: 4 }, children: [candles[active].label, " \u00B7 O ", (0, scales_1.formatValue)(candles[active].open), " \u00B7 H", " ", (0, scales_1.formatValue)(candles[active].high), " \u00B7 L ", (0, scales_1.formatValue)(candles[active].low), " \u00B7 C", " ", (0, scales_1.formatValue)(candles[active].close)] }))] }));
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Squarified treemap: rectangles sized by value, packed to stay close to square.
|
|
64
|
+
*
|
|
65
|
+
* A naive slice-and-dice layout produces slivers that cannot hold a label. This
|
|
66
|
+
* alternates the split direction and lays each row out along the shorter side,
|
|
67
|
+
* so tiles keep a usable aspect ratio.
|
|
68
|
+
*/
|
|
69
|
+
function squarify(values, box, output) {
|
|
70
|
+
if (values.length === 0)
|
|
71
|
+
return;
|
|
72
|
+
if (values.length === 1) {
|
|
73
|
+
output.push({ ...box });
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const total = values.reduce((sum, value) => sum + value, 0);
|
|
77
|
+
if (total <= 0) {
|
|
78
|
+
// Equal shares when the values carry no weight.
|
|
79
|
+
output.push({ ...box });
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
// Split the larger side so both halves stay as square as possible.
|
|
83
|
+
let split = 1;
|
|
84
|
+
let accumulated = values[0] ?? 0;
|
|
85
|
+
let best = Math.abs(accumulated / total - 0.5);
|
|
86
|
+
for (let i = 2; i < values.length; i++) {
|
|
87
|
+
accumulated += values[i - 1] ?? 0;
|
|
88
|
+
const ratio = accumulated / total;
|
|
89
|
+
const distance = Math.abs(ratio - 0.5);
|
|
90
|
+
if (distance < best) {
|
|
91
|
+
best = distance;
|
|
92
|
+
split = i;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const firstShare = values.slice(0, split).reduce((sum, value) => sum + value, 0) / total;
|
|
96
|
+
if (box.w >= box.h) {
|
|
97
|
+
const w = box.w * firstShare;
|
|
98
|
+
squarify(values.slice(0, split), { x: box.x, y: box.y, w, h: box.h }, output);
|
|
99
|
+
squarify(values.slice(split), { x: box.x + w, y: box.y, w: box.w - w, h: box.h }, output);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
const h = box.h * firstShare;
|
|
103
|
+
squarify(values.slice(0, split), { x: box.x, y: box.y, w: box.w, h }, output);
|
|
104
|
+
squarify(values.slice(split), { x: box.x, y: box.y + h, w: box.w, h: box.h - h }, output);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** Proportional rectangles for part-to-whole comparison. */
|
|
108
|
+
function NativeTreemapChart({ items, width = 320, height = 200, isDark = false, palette, style, onItemPress, }) {
|
|
109
|
+
const [active, setActive] = (0, react_1.useState)(null);
|
|
110
|
+
const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
|
|
111
|
+
const { View, Text, Pressable } = (0, primitives_1.primitives)();
|
|
112
|
+
if (items.length === 0) {
|
|
113
|
+
return ((0, jsx_runtime_1.jsx)(View, { style: [{ width, alignItems: "center", paddingVertical: 20 }, style], children: (0, jsx_runtime_1.jsx)(Text, { style: { color: theme.muted, fontSize: 12 }, children: "No items to display" }) }));
|
|
114
|
+
}
|
|
115
|
+
// Larger first keeps the layout stable and the important tiles big.
|
|
116
|
+
const ordered = items
|
|
117
|
+
.map((item, index) => ({ item, index }))
|
|
118
|
+
.sort((a, b) => b.item.value - a.item.value);
|
|
119
|
+
const boxes = [];
|
|
120
|
+
squarify(ordered.map((entry) => entry.item.value), { x: 0, y: 0, w: width, h: height }, boxes);
|
|
121
|
+
return ((0, jsx_runtime_1.jsx)(View, { style: [{ width, height }, style], children: boxes.map((box, i) => {
|
|
122
|
+
const entry = ordered[i];
|
|
123
|
+
const color = entry.item.color ?? (0, scales_1.seriesColor)(i, palette);
|
|
124
|
+
const showLabel = box.w > 46 && box.h > 22;
|
|
125
|
+
return ((0, jsx_runtime_1.jsx)(Pressable, { accessibilityRole: "button", accessibilityLabel: `${entry.item.label}: ${(0, scales_1.formatValue)(entry.item.value)}`, onPress: () => {
|
|
126
|
+
setActive((current) => (current === entry.index ? null : entry.index));
|
|
127
|
+
onItemPress?.(entry.index);
|
|
128
|
+
}, style: {
|
|
129
|
+
position: "absolute",
|
|
130
|
+
left: box.x,
|
|
131
|
+
top: box.y,
|
|
132
|
+
width: box.w,
|
|
133
|
+
height: box.h,
|
|
134
|
+
padding: 3,
|
|
135
|
+
}, children: (0, jsx_runtime_1.jsx)(View, { style: {
|
|
136
|
+
flex: 1,
|
|
137
|
+
borderRadius: 4,
|
|
138
|
+
backgroundColor: color,
|
|
139
|
+
opacity: active === null || active === entry.index ? 0.9 : 0.45,
|
|
140
|
+
padding: 4,
|
|
141
|
+
justifyContent: "flex-start",
|
|
142
|
+
overflow: "hidden",
|
|
143
|
+
}, children: showLabel && ((0, jsx_runtime_1.jsx)(Text, { numberOfLines: 1, style: { fontSize: 9, fontWeight: "600", color: "#fff" }, children: entry.item.label })) }) }, entry.item.label));
|
|
144
|
+
}) }));
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Flow between stages, drawn as stacked columns joined by bands.
|
|
148
|
+
*
|
|
149
|
+
* Columns are assigned by longest path from a source, so a node always sits
|
|
150
|
+
* after everything feeding it. Rendering uses Views rather than SVG because the
|
|
151
|
+
* bands are axis-aligned rectangles.
|
|
152
|
+
*/
|
|
153
|
+
function NativeSankeyChart({ nodes, links, width = 320, height = 200, isDark = false, palette, style, }) {
|
|
154
|
+
const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
|
|
155
|
+
const { View, Text } = (0, primitives_1.primitives)();
|
|
156
|
+
if (nodes.length === 0 || links.length === 0) {
|
|
157
|
+
return ((0, jsx_runtime_1.jsx)(View, { style: [{ width, alignItems: "center", paddingVertical: 20 }, style], children: (0, jsx_runtime_1.jsx)(Text, { style: { color: theme.muted, fontSize: 12 }, children: "No flows to display" }) }));
|
|
158
|
+
}
|
|
159
|
+
// Longest-path column assignment; cycles fall back to the current depth.
|
|
160
|
+
const depth = new Map();
|
|
161
|
+
for (const node of nodes)
|
|
162
|
+
depth.set(node.id, 0);
|
|
163
|
+
for (let pass = 0; pass < nodes.length; pass++) {
|
|
164
|
+
let changed = false;
|
|
165
|
+
for (const link of links) {
|
|
166
|
+
const from = depth.get(link.from) ?? 0;
|
|
167
|
+
const current = depth.get(link.to) ?? 0;
|
|
168
|
+
if (from + 1 > current) {
|
|
169
|
+
depth.set(link.to, from + 1);
|
|
170
|
+
changed = true;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (!changed)
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
const columns = new Map();
|
|
177
|
+
for (const node of nodes) {
|
|
178
|
+
const level = depth.get(node.id) ?? 0;
|
|
179
|
+
const list = columns.get(level) ?? [];
|
|
180
|
+
list.push(node);
|
|
181
|
+
columns.set(level, list);
|
|
182
|
+
}
|
|
183
|
+
const levels = [...columns.keys()].sort((a, b) => a - b);
|
|
184
|
+
const columnWidth = 10;
|
|
185
|
+
const gap = levels.length > 1 ? (width - columnWidth * levels.length) / (levels.length - 1) : 0;
|
|
186
|
+
return ((0, jsx_runtime_1.jsxs)(View, { style: [{ width, height }, style], children: [nodes.map((node) => {
|
|
187
|
+
const level = depth.get(node.id) ?? 0;
|
|
188
|
+
const columnIndex = levels.indexOf(level);
|
|
189
|
+
const siblings = columns.get(level) ?? [node];
|
|
190
|
+
const position = siblings.indexOf(node);
|
|
191
|
+
const slot = height / siblings.length;
|
|
192
|
+
return ((0, jsx_runtime_1.jsx)(View, { style: {
|
|
193
|
+
position: "absolute",
|
|
194
|
+
left: columnIndex * (columnWidth + gap),
|
|
195
|
+
top: position * slot,
|
|
196
|
+
width: columnWidth,
|
|
197
|
+
height: Math.max(4, slot - 4),
|
|
198
|
+
borderRadius: 2,
|
|
199
|
+
backgroundColor: (0, scales_1.seriesColor)(columnIndex, palette),
|
|
200
|
+
} }, node.id));
|
|
201
|
+
}), levels.map((level, i) => ((0, jsx_runtime_1.jsx)(Text, { style: {
|
|
202
|
+
position: "absolute",
|
|
203
|
+
left: Math.min(width - 40, i * (columnWidth + gap) - 6),
|
|
204
|
+
bottom: -16,
|
|
205
|
+
fontSize: 9,
|
|
206
|
+
color: theme.muted,
|
|
207
|
+
width: 52,
|
|
208
|
+
textAlign: "center",
|
|
209
|
+
}, children: i === 0 ? "Start" : i === levels.length - 1 ? "End" : `Stage ${i}` }, level)))] }));
|
|
210
|
+
}
|
|
211
|
+
/** Rank changes over time, where the vertical position is the rank itself. */
|
|
212
|
+
function NativeBumpChart({ labels, series, width = 320, height = 200, isDark = false, palette, style, onPointPress, }) {
|
|
213
|
+
const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
|
|
214
|
+
const { View, Text, Pressable, Svg, G, Line, Circle, SvgText } = (0, primitives_1.primitives)();
|
|
215
|
+
const periods = labels.length;
|
|
216
|
+
if (periods === 0 || series.length === 0) {
|
|
217
|
+
return ((0, jsx_runtime_1.jsx)(View, { style: [{ width, alignItems: "center", paddingVertical: 20 }, style], children: (0, jsx_runtime_1.jsx)(Text, { style: { color: theme.muted, fontSize: 12 }, children: "No rankings to display" }) }));
|
|
218
|
+
}
|
|
219
|
+
const allRanks = series.flatMap((entry) => entry.ranks).filter(Number.isFinite);
|
|
220
|
+
const worst = allRanks.length ? Math.max(...allRanks) : 1;
|
|
221
|
+
const best = allRanks.length ? Math.min(...allRanks) : 1;
|
|
222
|
+
const padding = { top: 10, right: 10, bottom: 22, left: 10 };
|
|
223
|
+
const plotWidth = Math.max(0, width - padding.left - padding.right);
|
|
224
|
+
const plotHeight = Math.max(0, height - padding.top - padding.bottom);
|
|
225
|
+
// Rank 1 sits at the top, so the axis is inverted.
|
|
226
|
+
const y = (rank) => {
|
|
227
|
+
if (worst === best)
|
|
228
|
+
return padding.top + plotHeight / 2;
|
|
229
|
+
return padding.top + ((rank - best) / (worst - best)) * plotHeight;
|
|
230
|
+
};
|
|
231
|
+
const x = (period) => padding.left + (periods === 1 ? plotWidth / 2 : (period / (periods - 1)) * plotWidth);
|
|
232
|
+
return ((0, jsx_runtime_1.jsx)(View, { style: style, children: (0, jsx_runtime_1.jsxs)(Svg, { width: width, height: height, children: [series.map((entry, si) => {
|
|
233
|
+
const color = entry.color ?? (0, scales_1.seriesColor)(si, palette);
|
|
234
|
+
return ((0, jsx_runtime_1.jsxs)(G, { children: [entry.ranks.slice(0, -1).map((rank, pi) => {
|
|
235
|
+
const next = entry.ranks[pi + 1];
|
|
236
|
+
if (next == null)
|
|
237
|
+
return null;
|
|
238
|
+
return ((0, jsx_runtime_1.jsx)(Line, { x1: x(pi), y1: y(rank), x2: x(pi + 1), y2: y(next), stroke: color, strokeWidth: 2 }, pi));
|
|
239
|
+
}), entry.ranks.map((rank, pi) => ((0, jsx_runtime_1.jsx)(Circle, { cx: x(pi), cy: y(rank), r: 4, fill: color, onPress: () => onPointPress?.(si, pi) }, pi)))] }, entry.name));
|
|
240
|
+
}), labels.map((label, pi) => ((0, jsx_runtime_1.jsx)(SvgText, { x: x(pi), y: height - 6, fill: theme.muted, fontSize: 9, textAnchor: "middle", children: label }, label)))] }) }));
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Points aggregated into hexagonal bins whose colour is the mean value.
|
|
244
|
+
*
|
|
245
|
+
* Binning keeps thousands of points legible: reading one mark's exact position
|
|
246
|
+
* matters less than seeing where density is concentrated.
|
|
247
|
+
*/
|
|
248
|
+
function NativeHexChart({ points, radius = 9, width = 320, height = 200, isDark = false, style, onBinPress, }) {
|
|
249
|
+
const [active, setActive] = (0, react_1.useState)(null);
|
|
250
|
+
const theme = isDark ? tokens_1.colors.dark : tokens_1.colors.light;
|
|
251
|
+
const { View, Text, Pressable, Svg, G, Path } = (0, primitives_1.primitives)();
|
|
252
|
+
if (points.length === 0) {
|
|
253
|
+
return ((0, jsx_runtime_1.jsx)(View, { style: [{ width, alignItems: "center", paddingVertical: 20 }, style], children: (0, jsx_runtime_1.jsx)(Text, { style: { color: theme.muted, fontSize: 12 }, children: "No points to display" }) }));
|
|
254
|
+
}
|
|
255
|
+
const xs = points.map((point) => point.x);
|
|
256
|
+
const ys = points.map((point) => point.y);
|
|
257
|
+
const minX = Math.min(...xs);
|
|
258
|
+
const maxX = Math.max(...xs) || 1;
|
|
259
|
+
const minY = Math.min(...ys);
|
|
260
|
+
const maxY = Math.max(...ys) || 1;
|
|
261
|
+
const hexWidth = Math.sqrt(3) * radius;
|
|
262
|
+
const columns = Math.max(1, Math.floor(width / hexWidth));
|
|
263
|
+
const rowHeight = 1.5 * radius;
|
|
264
|
+
const rows = Math.max(1, Math.floor(height / rowHeight));
|
|
265
|
+
// Bin by nearest grid cell centre.
|
|
266
|
+
const bins = new Map();
|
|
267
|
+
for (const point of points) {
|
|
268
|
+
const col = Math.min(columns - 1, Math.max(0, Math.floor(((point.x - minX) / (maxX - minX || 1)) * columns)));
|
|
269
|
+
const row = Math.min(rows - 1, Math.max(0, Math.floor(((point.y - minY) / (maxY - minY || 1)) * rows)));
|
|
270
|
+
const key = `${col}:${row}`;
|
|
271
|
+
const existing = bins.get(key);
|
|
272
|
+
const cx = col * hexWidth + hexWidth / 2 + (row % 2 ? hexWidth / 2 : 0);
|
|
273
|
+
const cy = row * rowHeight + rowHeight / 2;
|
|
274
|
+
if (existing) {
|
|
275
|
+
existing.sum += point.value;
|
|
276
|
+
existing.count += 1;
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
bins.set(key, { sum: point.value, count: 1, cx, cy });
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const means = [...bins.values()].map((bin) => bin.sum / bin.count);
|
|
283
|
+
const lowest = Math.min(...means);
|
|
284
|
+
const highest = Math.max(...means);
|
|
285
|
+
const span = highest - lowest;
|
|
286
|
+
const hexPath = (cx, cy) => {
|
|
287
|
+
const points_ = [];
|
|
288
|
+
for (let i = 0; i < 6; i++) {
|
|
289
|
+
const angle = (Math.PI / 3) * i - Math.PI / 2;
|
|
290
|
+
points_.push(`${cx + radius * Math.cos(angle)},${cy + radius * Math.sin(angle)}`);
|
|
291
|
+
}
|
|
292
|
+
return "M " + points_.join(" L ") + " Z";
|
|
293
|
+
};
|
|
294
|
+
return ((0, jsx_runtime_1.jsx)(View, { style: style, children: (0, jsx_runtime_1.jsx)(Svg, { width: width, height: height, children: [...bins.values()].map((bin, index) => {
|
|
295
|
+
const mean = bin.sum / bin.count;
|
|
296
|
+
const t = span === 0 ? 0.5 : (mean - lowest) / span;
|
|
297
|
+
// Blue ramp from low to high density.
|
|
298
|
+
const red = Math.round(210 - t * 130);
|
|
299
|
+
const green = Math.round(225 - t * 90);
|
|
300
|
+
const blue = Math.round(250 - t * 15);
|
|
301
|
+
const color = `rgb(${red}, ${green}, ${blue})`;
|
|
302
|
+
return ((0, jsx_runtime_1.jsx)(G, { children: (0, jsx_runtime_1.jsx)(Path, { d: hexPath(bin.cx, bin.cy), fill: color, fillOpacity: active === null || active === index ? 1 : 0.4, stroke: theme.edge, strokeWidth: 0.5, onPress: () => {
|
|
303
|
+
setActive((current) => (current === index ? null : index));
|
|
304
|
+
onBinPress?.(index);
|
|
305
|
+
} }) }, index));
|
|
306
|
+
}) }) }));
|
|
307
|
+
}
|