glacia-native 0.3.2 → 0.3.4
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/LICENSE +1 -7
- package/README.md +178 -0
- 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,131 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
|
|
3
|
+
import { colors } from "../internal/tokens";
|
|
4
|
+
import { primitives } from "./primitives";
|
|
5
|
+
import { arcPath, formatValue, seriesColor } from "./scales";
|
|
6
|
+
|
|
7
|
+
export interface NativeRingSlice {
|
|
8
|
+
label: string;
|
|
9
|
+
value: number;
|
|
10
|
+
color?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface NativeRingChartProps {
|
|
14
|
+
data: NativeRingSlice[];
|
|
15
|
+
size?: number;
|
|
16
|
+
/**
|
|
17
|
+
* Ring thickness. Larger leaves a bigger hole.
|
|
18
|
+
* A pie is the same shape with no hole: pass `size` to fill it.
|
|
19
|
+
*/
|
|
20
|
+
thickness?: number;
|
|
21
|
+
isDark?: boolean;
|
|
22
|
+
palette?: readonly string[];
|
|
23
|
+
/** Show the slice list beneath the ring. */
|
|
24
|
+
showLegend?: boolean;
|
|
25
|
+
style?: any;
|
|
26
|
+
onSlicePress?: (index: number) => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const TAU = Math.PI * 2;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Proportional slices as a ring, drawn with react-native-svg.
|
|
33
|
+
*
|
|
34
|
+
* Shares the arc maths with the web RingChart, so slices land in the same
|
|
35
|
+
* places on both platforms. A legend is on by default: on a phone there is no
|
|
36
|
+
* hover, so colour alone cannot identify a slice.
|
|
37
|
+
*/
|
|
38
|
+
export function NativeRingChart({
|
|
39
|
+
data,
|
|
40
|
+
size = 180,
|
|
41
|
+
thickness = 22,
|
|
42
|
+
isDark = false,
|
|
43
|
+
palette,
|
|
44
|
+
showLegend = true,
|
|
45
|
+
style,
|
|
46
|
+
onSlicePress,
|
|
47
|
+
}: NativeRingChartProps) {
|
|
48
|
+
const [active, setActive] = useState<number | null>(null);
|
|
49
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
50
|
+
const { View, Text, Pressable, Svg, Path } = primitives();
|
|
51
|
+
|
|
52
|
+
const total = data.reduce((sum, slice) => sum + Math.max(0, slice.value), 0);
|
|
53
|
+
|
|
54
|
+
if (data.length === 0 || total <= 0) {
|
|
55
|
+
return (
|
|
56
|
+
<View style={[{ width: size, alignItems: "center" }, style]}>
|
|
57
|
+
<Text style={{ color: theme.muted, fontSize: 12 }}>No slices to display</Text>
|
|
58
|
+
</View>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const outer = size / 2;
|
|
63
|
+
const inner = Math.max(0, outer - thickness);
|
|
64
|
+
let cursor = -Math.PI / 2;
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<View style={[{ alignItems: "center" }, style]}>
|
|
68
|
+
<Svg width={size} height={size}>
|
|
69
|
+
{data.map((slice, i) => {
|
|
70
|
+
const share = Math.max(0, slice.value) / total;
|
|
71
|
+
const start = cursor;
|
|
72
|
+
const end = cursor + share * TAU;
|
|
73
|
+
cursor = end;
|
|
74
|
+
|
|
75
|
+
return (
|
|
76
|
+
<Path
|
|
77
|
+
key={slice.label}
|
|
78
|
+
d={arcPath(outer, outer, outer, inner, start, end)}
|
|
79
|
+
fill={slice.color ?? seriesColor(i, palette)}
|
|
80
|
+
opacity={active === null || active === i ? 1 : 0.4}
|
|
81
|
+
onPress={() => {
|
|
82
|
+
setActive((current) => (current === i ? null : i));
|
|
83
|
+
onSlicePress?.(i);
|
|
84
|
+
}}
|
|
85
|
+
/>
|
|
86
|
+
);
|
|
87
|
+
})}
|
|
88
|
+
</Svg>
|
|
89
|
+
|
|
90
|
+
{showLegend && (
|
|
91
|
+
<View style={{ marginTop: 12, width: "100%", maxWidth: 260, gap: 6 }}>
|
|
92
|
+
{data.map((slice, i) => (
|
|
93
|
+
<Pressable
|
|
94
|
+
key={slice.label}
|
|
95
|
+
accessibilityRole="button"
|
|
96
|
+
accessibilityLabel={`${slice.label}: ${formatValue(slice.value)}, ${Math.round(
|
|
97
|
+
(slice.value / total) * 100,
|
|
98
|
+
)} percent`}
|
|
99
|
+
onPress={() => {
|
|
100
|
+
setActive((current) => (current === i ? null : i));
|
|
101
|
+
onSlicePress?.(i);
|
|
102
|
+
}}
|
|
103
|
+
style={{ flexDirection: "row", alignItems: "center", gap: 8 }}
|
|
104
|
+
>
|
|
105
|
+
<View
|
|
106
|
+
style={{
|
|
107
|
+
width: 9,
|
|
108
|
+
height: 9,
|
|
109
|
+
borderRadius: 3,
|
|
110
|
+
backgroundColor: slice.color ?? seriesColor(i, palette),
|
|
111
|
+
}}
|
|
112
|
+
/>
|
|
113
|
+
<Text style={{ color: theme.muted, fontSize: 12, flex: 1 }} numberOfLines={1}>
|
|
114
|
+
{slice.label}
|
|
115
|
+
</Text>
|
|
116
|
+
<Text
|
|
117
|
+
style={{
|
|
118
|
+
color: theme.foreground,
|
|
119
|
+
fontSize: 12,
|
|
120
|
+
fontWeight: active === i ? "700" : "600",
|
|
121
|
+
}}
|
|
122
|
+
>
|
|
123
|
+
{formatValue(slice.value)}
|
|
124
|
+
</Text>
|
|
125
|
+
</Pressable>
|
|
126
|
+
))}
|
|
127
|
+
</View>
|
|
128
|
+
)}
|
|
129
|
+
</View>
|
|
130
|
+
);
|
|
131
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
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 NativeScatterPoint {
|
|
8
|
+
x: number;
|
|
9
|
+
y: number;
|
|
10
|
+
label?: string;
|
|
11
|
+
/** Bubble radius driver; ignored when `bubble` is not set. */
|
|
12
|
+
size?: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface NativeScatterSeries {
|
|
16
|
+
name: string;
|
|
17
|
+
points: NativeScatterPoint[];
|
|
18
|
+
color?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface NativeScatterChartProps {
|
|
22
|
+
series: NativeScatterSeries[];
|
|
23
|
+
width?: number;
|
|
24
|
+
height?: number;
|
|
25
|
+
/** Fixed dot radius. */
|
|
26
|
+
radius?: number;
|
|
27
|
+
/** Size each mark by its `size` value instead of a fixed radius. */
|
|
28
|
+
bubble?: boolean;
|
|
29
|
+
isDark?: boolean;
|
|
30
|
+
palette?: readonly string[];
|
|
31
|
+
style?: any;
|
|
32
|
+
onPointPress?: (seriesIndex: number, pointIndex: number) => void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const PADDING = { top: 12, right: 12, bottom: 24, left: 40 };
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Two numeric axes, drawn with react-native-svg.
|
|
39
|
+
*
|
|
40
|
+
* BubbleChart is this component with `bubble` set, so the two share their
|
|
41
|
+
* axes, padding and touch behaviour instead of drifting apart.
|
|
42
|
+
*/
|
|
43
|
+
export function NativeScatterChart({
|
|
44
|
+
series,
|
|
45
|
+
width = 320,
|
|
46
|
+
height = 200,
|
|
47
|
+
radius = 4,
|
|
48
|
+
bubble = false,
|
|
49
|
+
isDark = false,
|
|
50
|
+
palette,
|
|
51
|
+
style,
|
|
52
|
+
onPointPress,
|
|
53
|
+
}: NativeScatterChartProps) {
|
|
54
|
+
const [active, setActive] = useState<{ s: number; p: number } | null>(null);
|
|
55
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
56
|
+
const { View, Text, Svg, G, Line, Circle, SvgText } = primitives();
|
|
57
|
+
|
|
58
|
+
const plot = {
|
|
59
|
+
x: PADDING.left,
|
|
60
|
+
y: PADDING.top,
|
|
61
|
+
width: Math.max(0, width - PADDING.left - PADDING.right),
|
|
62
|
+
height: Math.max(0, height - PADDING.top - PADDING.bottom),
|
|
63
|
+
};
|
|
64
|
+
const right = plot.x + plot.width;
|
|
65
|
+
const bottom = plot.y + plot.height;
|
|
66
|
+
|
|
67
|
+
const all = series.flatMap((s) => s.points);
|
|
68
|
+
|
|
69
|
+
const xDomain = useMemo(() => {
|
|
70
|
+
const xs = all.map((p) => p.x).filter(Number.isFinite);
|
|
71
|
+
return niceDomain(xs.length ? Math.min(...xs) : 0, xs.length ? Math.max(...xs) : 1, 4);
|
|
72
|
+
}, [all.map((p) => p.x).join(",")]);
|
|
73
|
+
|
|
74
|
+
const yDomain = useMemo(() => {
|
|
75
|
+
const ys = all.map((p) => p.y).filter(Number.isFinite);
|
|
76
|
+
return niceDomain(ys.length ? Math.min(...ys) : 0, ys.length ? Math.max(...ys) : 1, 4);
|
|
77
|
+
}, [all.map((p) => p.y).join(",")]);
|
|
78
|
+
|
|
79
|
+
const x = linearScale(xDomain, [plot.x, right], { nice: false });
|
|
80
|
+
const y = linearScale(yDomain, [bottom, plot.y], { nice: false });
|
|
81
|
+
|
|
82
|
+
const sizes = all.map((p) => p.size ?? 1);
|
|
83
|
+
const maxSize = sizes.length ? Math.max(...sizes) : 1;
|
|
84
|
+
const minSize = sizes.length ? Math.min(...sizes) : 0;
|
|
85
|
+
const maxRadius = Math.min(plot.width, plot.height) / 7;
|
|
86
|
+
|
|
87
|
+
const radiusFor = (point: NativeScatterPoint) => {
|
|
88
|
+
if (!bubble) return radius;
|
|
89
|
+
const size = point.size ?? minSize;
|
|
90
|
+
const t = maxSize === minSize ? 1 : (size - minSize) / (maxSize - minSize);
|
|
91
|
+
return 3 + t * (maxRadius - 3);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
if (all.length === 0) {
|
|
95
|
+
return (
|
|
96
|
+
<View style={[{ width, height, alignItems: "center", justifyContent: "center" }, style]}>
|
|
97
|
+
<Text style={{ color: theme.muted, fontSize: 12 }}>No points to display</Text>
|
|
98
|
+
</View>
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const yTicks = ticks(yDomain, 4);
|
|
103
|
+
const xTicks = ticks(xDomain, 4);
|
|
104
|
+
|
|
105
|
+
return (
|
|
106
|
+
<View style={style}>
|
|
107
|
+
<Svg width={width} height={height}>
|
|
108
|
+
{yTicks.map((tick, i) => (
|
|
109
|
+
<Line
|
|
110
|
+
key={`grid-${i}`}
|
|
111
|
+
x1={plot.x}
|
|
112
|
+
x2={right}
|
|
113
|
+
y1={y(tick)}
|
|
114
|
+
y2={y(tick)}
|
|
115
|
+
stroke={theme.axis}
|
|
116
|
+
strokeOpacity={0.24}
|
|
117
|
+
strokeWidth={1}
|
|
118
|
+
/>
|
|
119
|
+
))}
|
|
120
|
+
|
|
121
|
+
{series.map((s, si) => {
|
|
122
|
+
const color = s.color ?? seriesColor(si, palette);
|
|
123
|
+
return (
|
|
124
|
+
<G key={s.name}>
|
|
125
|
+
{s.points.map((point, pi) => {
|
|
126
|
+
const isActive = active?.s === si && active?.p === pi;
|
|
127
|
+
return (
|
|
128
|
+
<Circle
|
|
129
|
+
key={pi}
|
|
130
|
+
cx={x(point.x)}
|
|
131
|
+
cy={y(point.y)}
|
|
132
|
+
r={radiusFor(point) + (isActive ? 2 : 0)}
|
|
133
|
+
fill={color}
|
|
134
|
+
fillOpacity={isActive ? 0.95 : bubble ? 0.55 : 0.8}
|
|
135
|
+
onPress={() => {
|
|
136
|
+
setActive(isActive ? null : { s: si, p: pi });
|
|
137
|
+
onPointPress?.(si, pi);
|
|
138
|
+
}}
|
|
139
|
+
/>
|
|
140
|
+
);
|
|
141
|
+
})}
|
|
142
|
+
</G>
|
|
143
|
+
);
|
|
144
|
+
})}
|
|
145
|
+
|
|
146
|
+
{yTicks.map((tick, i) => (
|
|
147
|
+
<SvgText
|
|
148
|
+
key={`y-${i}`}
|
|
149
|
+
x={plot.x - 6}
|
|
150
|
+
y={y(tick) + 3}
|
|
151
|
+
fill={theme.muted}
|
|
152
|
+
fontSize={9}
|
|
153
|
+
textAnchor="end"
|
|
154
|
+
>
|
|
155
|
+
{formatValue(tick, { compact: true })}
|
|
156
|
+
</SvgText>
|
|
157
|
+
))}
|
|
158
|
+
|
|
159
|
+
{xTicks.map((tick, i) => (
|
|
160
|
+
<SvgText
|
|
161
|
+
key={`x-${i}`}
|
|
162
|
+
x={x(tick)}
|
|
163
|
+
y={height - 8}
|
|
164
|
+
fill={theme.muted}
|
|
165
|
+
fontSize={9}
|
|
166
|
+
textAnchor="middle"
|
|
167
|
+
>
|
|
168
|
+
{formatValue(tick, { compact: true })}
|
|
169
|
+
</SvgText>
|
|
170
|
+
))}
|
|
171
|
+
</Svg>
|
|
172
|
+
|
|
173
|
+
{active !== null && series[active.s]?.points[active.p] && (
|
|
174
|
+
<View
|
|
175
|
+
style={{
|
|
176
|
+
position: "absolute",
|
|
177
|
+
top: 0,
|
|
178
|
+
left: PADDING.left,
|
|
179
|
+
paddingVertical: 3,
|
|
180
|
+
paddingHorizontal: 7,
|
|
181
|
+
borderRadius: 8,
|
|
182
|
+
backgroundColor: theme.glass,
|
|
183
|
+
}}
|
|
184
|
+
pointerEvents="none"
|
|
185
|
+
>
|
|
186
|
+
<Text style={{ color: theme.foreground, fontSize: 10, fontWeight: "600" }}>
|
|
187
|
+
{series[active.s]!.points[active.p]!.label ?? series[active.s]!.name}
|
|
188
|
+
</Text>
|
|
189
|
+
<Text style={{ color: theme.muted, fontSize: 10 }}>
|
|
190
|
+
x {formatValue(series[active.s]!.points[active.p]!.x)} · y{" "}
|
|
191
|
+
{formatValue(series[active.s]!.points[active.p]!.y)}
|
|
192
|
+
</Text>
|
|
193
|
+
</View>
|
|
194
|
+
)}
|
|
195
|
+
</View>
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Scatter marks sized by a third value. */
|
|
200
|
+
export function NativeBubbleChart(props: Omit<NativeScatterChartProps, "bubble" | "radius">) {
|
|
201
|
+
return <NativeScatterChart {...props} bubble />;
|
|
202
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
|
|
3
|
+
import { colors } from "../internal/tokens";
|
|
4
|
+
import { primitives } from "./primitives";
|
|
5
|
+
import { formatValue } from "./scales";
|
|
6
|
+
|
|
7
|
+
export interface NativeWaterfallStep {
|
|
8
|
+
label: string;
|
|
9
|
+
/** Signed change. A step with `isTotal` set is drawn as an absolute bar. */
|
|
10
|
+
value: number;
|
|
11
|
+
isTotal?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface NativeWaterfallChartProps {
|
|
15
|
+
steps: NativeWaterfallStep[];
|
|
16
|
+
width?: number;
|
|
17
|
+
height?: number;
|
|
18
|
+
isDark?: boolean;
|
|
19
|
+
style?: any;
|
|
20
|
+
onStepPress?: (index: number) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const PADDING = { top: 12, right: 8, bottom: 26, left: 42 };
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* How a running total moves step by step.
|
|
27
|
+
*
|
|
28
|
+
* Each bar starts where the previous one ended, so the reader sees the bridge
|
|
29
|
+
* from opening to closing balance rather than a set of unrelated changes.
|
|
30
|
+
* Increases and decreases are coloured differently, and totals span the axis.
|
|
31
|
+
*/
|
|
32
|
+
export function NativeWaterfallChart({
|
|
33
|
+
steps,
|
|
34
|
+
width = 320,
|
|
35
|
+
height = 200,
|
|
36
|
+
isDark = false,
|
|
37
|
+
style,
|
|
38
|
+
onStepPress,
|
|
39
|
+
}: NativeWaterfallChartProps) {
|
|
40
|
+
const [active, setActive] = useState<number | null>(null);
|
|
41
|
+
const theme = isDark ? colors.dark : colors.light;
|
|
42
|
+
const { View, Text, Pressable, Svg, G, Line, Rect, SvgText } = primitives();
|
|
43
|
+
|
|
44
|
+
if (steps.length === 0) {
|
|
45
|
+
return (
|
|
46
|
+
<View style={[{ width, alignItems: "center", paddingVertical: 20 }, style]}>
|
|
47
|
+
<Text style={{ color: theme.muted, fontSize: 12 }}>No steps to display</Text>
|
|
48
|
+
</View>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const up = "#34c759";
|
|
53
|
+
const down = "#ff375f";
|
|
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
|
+
// Bounds come from the running totals, not the raw deltas: a −50 midway
|
|
65
|
+
// through a climb needs the axis to reach below zero only if the running
|
|
66
|
+
// total actually does.
|
|
67
|
+
let running = 0;
|
|
68
|
+
const geometry = steps.map((step) => {
|
|
69
|
+
const start = step.isTotal ? 0 : running;
|
|
70
|
+
const end = step.isTotal ? step.value : running + step.value;
|
|
71
|
+
if (!step.isTotal) running = end;
|
|
72
|
+
return {
|
|
73
|
+
start,
|
|
74
|
+
end,
|
|
75
|
+
low: Math.min(start, end),
|
|
76
|
+
high: Math.max(start, end),
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const lows = geometry.map((g) => g.low);
|
|
81
|
+
const highs = geometry.map((g) => g.high);
|
|
82
|
+
const min = Math.min(0, ...lows);
|
|
83
|
+
const max = Math.max(0, ...highs);
|
|
84
|
+
const span = max - min || 1;
|
|
85
|
+
|
|
86
|
+
const y = (value: number) => bottom - ((value - min) / span) * plot.height;
|
|
87
|
+
const stepWidth = plot.width / steps.length;
|
|
88
|
+
const barWidth = Math.min(34, stepWidth * 0.6);
|
|
89
|
+
|
|
90
|
+
const zeroY = y(0);
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<View style={style}>
|
|
94
|
+
<Svg width={width} height={height}>
|
|
95
|
+
<Line
|
|
96
|
+
x1={plot.x}
|
|
97
|
+
x2={right}
|
|
98
|
+
y1={zeroY}
|
|
99
|
+
y2={zeroY}
|
|
100
|
+
stroke={theme.axis}
|
|
101
|
+
strokeOpacity={0.4}
|
|
102
|
+
strokeWidth={1}
|
|
103
|
+
/>
|
|
104
|
+
|
|
105
|
+
{geometry.map((g, i) => {
|
|
106
|
+
const step = steps[i]!;
|
|
107
|
+
const top = y(g.high);
|
|
108
|
+
const barHeight = Math.max(1, y(g.low) - top);
|
|
109
|
+
const color = step.isTotal ? seriesTotalColor(isDark) : step.value >= 0 ? up : down;
|
|
110
|
+
const x = plot.x + stepWidth * i + (stepWidth - barWidth) / 2;
|
|
111
|
+
|
|
112
|
+
return (
|
|
113
|
+
<G key={step.label}>
|
|
114
|
+
<Rect
|
|
115
|
+
x={x}
|
|
116
|
+
y={top}
|
|
117
|
+
width={barWidth}
|
|
118
|
+
height={barHeight}
|
|
119
|
+
rx={3}
|
|
120
|
+
fill={color}
|
|
121
|
+
fillOpacity={active === null || active === i ? 1 : 0.45}
|
|
122
|
+
onPress={() => {
|
|
123
|
+
setActive((current) => (current === i ? null : i));
|
|
124
|
+
onStepPress?.(i);
|
|
125
|
+
}}
|
|
126
|
+
/>
|
|
127
|
+
{/* Connector to the next bar's baseline, so the accumulation reads. */}
|
|
128
|
+
{i < steps.length - 1 && !steps[i + 1]!.isTotal && (
|
|
129
|
+
<Line
|
|
130
|
+
x1={x + barWidth}
|
|
131
|
+
x2={plot.x + stepWidth * (i + 1) + (stepWidth - barWidth) / 2}
|
|
132
|
+
y1={y(g.end)}
|
|
133
|
+
y2={y(g.end)}
|
|
134
|
+
stroke={theme.axis}
|
|
135
|
+
strokeOpacity={0.4}
|
|
136
|
+
strokeWidth={1}
|
|
137
|
+
strokeDasharray="2,2"
|
|
138
|
+
/>
|
|
139
|
+
)}
|
|
140
|
+
<SvgText
|
|
141
|
+
x={x + barWidth / 2}
|
|
142
|
+
y={height - 8}
|
|
143
|
+
fill={theme.muted}
|
|
144
|
+
fontSize={8}
|
|
145
|
+
textAnchor="middle"
|
|
146
|
+
>
|
|
147
|
+
{step.label}
|
|
148
|
+
</SvgText>
|
|
149
|
+
</G>
|
|
150
|
+
);
|
|
151
|
+
})}
|
|
152
|
+
|
|
153
|
+
{[max, min].map((value, i) => (
|
|
154
|
+
<SvgText
|
|
155
|
+
key={`y-${i}`}
|
|
156
|
+
x={plot.x - 6}
|
|
157
|
+
y={y(value) + 3}
|
|
158
|
+
fill={theme.muted}
|
|
159
|
+
fontSize={9}
|
|
160
|
+
textAnchor="end"
|
|
161
|
+
>
|
|
162
|
+
{formatValue(value, { compact: true })}
|
|
163
|
+
</SvgText>
|
|
164
|
+
))}
|
|
165
|
+
</Svg>
|
|
166
|
+
|
|
167
|
+
{active !== null && (
|
|
168
|
+
<Text style={{ color: theme.foreground, fontSize: 11, marginTop: 6 }}>
|
|
169
|
+
{steps[active]!.label}: {steps[active]!.value >= 0 ? "+" : ""}
|
|
170
|
+
{formatValue(steps[active]!.value)}
|
|
171
|
+
{steps[active]!.isTotal
|
|
172
|
+
? " (total)"
|
|
173
|
+
: ` → ${formatValue(geometry[active]!.end)} running`}
|
|
174
|
+
</Text>
|
|
175
|
+
)}
|
|
176
|
+
</View>
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Totals are neutral so they do not read as a gain or loss. */
|
|
181
|
+
function seriesTotalColor(isDark: boolean) {
|
|
182
|
+
return isDark ? "#8e8e93" : "#3c3c43";
|
|
183
|
+
}
|