panelui-native 0.22.5 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -3
- package/lib/module/components/area-chart/index.js +826 -0
- package/lib/module/components/area-chart/index.js.map +1 -0
- package/lib/module/components/avatar/index.js +1 -1
- package/lib/module/components/avatar/index.js.map +1 -1
- package/lib/module/components/bar-chart/index.js +853 -0
- package/lib/module/components/bar-chart/index.js.map +1 -0
- package/lib/module/components/bottom-sheet/index.js +207 -23
- package/lib/module/components/bottom-sheet/index.js.map +1 -1
- package/lib/module/components/breadcrumb/index.js +275 -0
- package/lib/module/components/breadcrumb/index.js.map +1 -0
- package/lib/module/components/chip/index.js +2 -2
- package/lib/module/components/direction/index.js +13 -19
- package/lib/module/components/direction/index.js.map +1 -1
- package/lib/module/components/frame/index.js +17 -0
- package/lib/module/components/frame/index.js.map +1 -1
- package/lib/module/components/heatmap-chart/index.js +28 -8
- package/lib/module/components/heatmap-chart/index.js.map +1 -1
- package/lib/module/components/input-group/index.js +2 -2
- package/lib/module/components/input-group/index.js.map +1 -1
- package/lib/module/components/line-chart/index.js +4 -146
- package/lib/module/components/line-chart/index.js.map +1 -1
- package/lib/module/components/loader/index.js +758 -0
- package/lib/module/components/loader/index.js.map +1 -0
- package/lib/module/components/map/basemap.js +265 -0
- package/lib/module/components/map/basemap.js.map +1 -0
- package/lib/module/components/map/index.js +850 -0
- package/lib/module/components/map/index.js.map +1 -0
- package/lib/module/components/map/maplibre.js +87 -0
- package/lib/module/components/map/maplibre.js.map +1 -0
- package/lib/module/components/message/index.js +8 -7
- package/lib/module/components/message/index.js.map +1 -1
- package/lib/module/components/otp-input/index.js +2 -2
- package/lib/module/components/progress/index.js +8 -2
- package/lib/module/components/progress/index.js.map +1 -1
- package/lib/module/components/ring-chart/index.js +423 -0
- package/lib/module/components/ring-chart/index.js.map +1 -0
- package/lib/module/components/section-rail/index.js +2 -2
- package/lib/module/components/separator/index.js +76 -22
- package/lib/module/components/separator/index.js.map +1 -1
- package/lib/module/components/shimmer/index.js +10 -1
- package/lib/module/components/shimmer/index.js.map +1 -1
- package/lib/module/components/slider/index.js +19 -5
- package/lib/module/components/slider/index.js.map +1 -1
- package/lib/module/components/steps/index.js +1 -1
- package/lib/module/components/switch/index.js +6 -1
- package/lib/module/components/switch/index.js.map +1 -1
- package/lib/module/components/typography/index.js +5 -0
- package/lib/module/components/typography/index.js.map +1 -1
- package/lib/module/hooks/use-direction.js +43 -0
- package/lib/module/hooks/use-direction.js.map +1 -0
- package/lib/module/icons/index.js +117 -2
- package/lib/module/icons/index.js.map +1 -1
- package/lib/module/index.js +8 -2
- package/lib/module/index.js.map +1 -1
- package/lib/module/primitives/text.js +30 -10
- package/lib/module/primitives/text.js.map +1 -1
- package/lib/module/utils/chart.js +279 -0
- package/lib/module/utils/chart.js.map +1 -0
- package/lib/typescript/src/components/area-chart/index.d.ts +197 -0
- package/lib/typescript/src/components/area-chart/index.d.ts.map +1 -0
- package/lib/typescript/src/components/bar-chart/index.d.ts +228 -0
- package/lib/typescript/src/components/bar-chart/index.d.ts.map +1 -0
- package/lib/typescript/src/components/bottom-sheet/index.d.ts +75 -4
- package/lib/typescript/src/components/bottom-sheet/index.d.ts.map +1 -1
- package/lib/typescript/src/components/breadcrumb/index.d.ts +100 -0
- package/lib/typescript/src/components/breadcrumb/index.d.ts.map +1 -0
- package/lib/typescript/src/components/direction/index.d.ts +11 -10
- package/lib/typescript/src/components/direction/index.d.ts.map +1 -1
- package/lib/typescript/src/components/frame/index.d.ts +17 -0
- package/lib/typescript/src/components/frame/index.d.ts.map +1 -1
- package/lib/typescript/src/components/heatmap-chart/index.d.ts +22 -3
- package/lib/typescript/src/components/heatmap-chart/index.d.ts.map +1 -1
- package/lib/typescript/src/components/line-chart/index.d.ts +1 -0
- package/lib/typescript/src/components/line-chart/index.d.ts.map +1 -1
- package/lib/typescript/src/components/loader/index.d.ts +38 -0
- package/lib/typescript/src/components/loader/index.d.ts.map +1 -0
- package/lib/typescript/src/components/map/basemap.d.ts +76 -0
- package/lib/typescript/src/components/map/basemap.d.ts.map +1 -0
- package/lib/typescript/src/components/map/index.d.ts +367 -0
- package/lib/typescript/src/components/map/index.d.ts.map +1 -0
- package/lib/typescript/src/components/map/maplibre.d.ts +193 -0
- package/lib/typescript/src/components/map/maplibre.d.ts.map +1 -0
- package/lib/typescript/src/components/message/index.d.ts.map +1 -1
- package/lib/typescript/src/components/progress/index.d.ts.map +1 -1
- package/lib/typescript/src/components/ring-chart/index.d.ts +156 -0
- package/lib/typescript/src/components/ring-chart/index.d.ts.map +1 -0
- package/lib/typescript/src/components/separator/index.d.ts +36 -1
- package/lib/typescript/src/components/separator/index.d.ts.map +1 -1
- package/lib/typescript/src/components/shimmer/index.d.ts.map +1 -1
- package/lib/typescript/src/components/slider/index.d.ts.map +1 -1
- package/lib/typescript/src/components/switch/index.d.ts.map +1 -1
- package/lib/typescript/src/components/typography/index.d.ts +6 -0
- package/lib/typescript/src/components/typography/index.d.ts.map +1 -1
- package/lib/typescript/src/hooks/use-direction.d.ts +20 -0
- package/lib/typescript/src/hooks/use-direction.d.ts.map +1 -0
- package/lib/typescript/src/icons/index.d.ts +5 -0
- package/lib/typescript/src/icons/index.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +9 -3
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/primitives/text.d.ts.map +1 -1
- package/lib/typescript/src/utils/chart.d.ts +96 -0
- package/lib/typescript/src/utils/chart.d.ts.map +1 -0
- package/package.json +9 -1
- package/src/components/area-chart/index.tsx +994 -0
- package/src/components/avatar/index.tsx +1 -1
- package/src/components/bar-chart/index.tsx +1048 -0
- package/src/components/bottom-sheet/index.tsx +280 -20
- package/src/components/breadcrumb/index.tsx +365 -0
- package/src/components/chip/index.tsx +2 -2
- package/src/components/direction/index.tsx +20 -21
- package/src/components/frame/index.tsx +17 -0
- package/src/components/heatmap-chart/index.tsx +58 -11
- package/src/components/input-group/index.tsx +2 -2
- package/src/components/line-chart/index.tsx +13 -181
- package/src/components/loader/index.tsx +812 -0
- package/src/components/map/basemap.ts +304 -0
- package/src/components/map/index.tsx +1118 -0
- package/src/components/map/maplibre.ts +201 -0
- package/src/components/message/index.tsx +8 -7
- package/src/components/otp-input/index.tsx +2 -2
- package/src/components/progress/index.tsx +10 -2
- package/src/components/ring-chart/index.tsx +543 -0
- package/src/components/section-rail/index.tsx +2 -2
- package/src/components/separator/index.tsx +77 -19
- package/src/components/shimmer/index.tsx +10 -1
- package/src/components/slider/index.tsx +20 -5
- package/src/components/steps/index.tsx +1 -1
- package/src/components/switch/index.tsx +6 -1
- package/src/components/typography/index.tsx +5 -0
- package/src/hooks/use-direction.ts +45 -0
- package/src/icons/index.tsx +66 -0
- package/src/index.ts +84 -0
- package/src/primitives/text.tsx +25 -7
- package/src/utils/chart.ts +355 -0
|
@@ -0,0 +1,994 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AreaChart — filled bands over time, stacked or overlaid.
|
|
3
|
+
*
|
|
4
|
+
* ```tsx
|
|
5
|
+
* <AreaChart data={traffic} xDataKey="date" stacked>
|
|
6
|
+
* <AreaChart.Grid />
|
|
7
|
+
* <AreaChart.Area dataKey="direct" />
|
|
8
|
+
* <AreaChart.Area dataKey="search" colorIndex={2} />
|
|
9
|
+
* <AreaChart.XAxis />
|
|
10
|
+
* <AreaChart.Tooltip />
|
|
11
|
+
* </AreaChart>
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* ## Why this is not `LineChart.Area`
|
|
15
|
+
*
|
|
16
|
+
* `LineChart.Area` shades under a line, which is one series saying "this is
|
|
17
|
+
* the shape of the thing". This chart answers a different question — *what is
|
|
18
|
+
* it made of* — and that needs stacking: each band sits on the running total
|
|
19
|
+
* of the ones below it, so the top edge is the whole and the thickness of each
|
|
20
|
+
* band is its share.
|
|
21
|
+
*
|
|
22
|
+
* Stacking is not a flag that could be bolted on. It changes the y-domain from
|
|
23
|
+
* the largest single series to the largest *sum*, it makes each area's
|
|
24
|
+
* baseline a curve rather than the axis, and it makes the order the series are
|
|
25
|
+
* declared in load-bearing. A chart where all of that is true of some children
|
|
26
|
+
* and not others is a chart nobody can read, so it is a chart of its own.
|
|
27
|
+
*
|
|
28
|
+
* Unstacked, the bands overlay each other and the fills are drawn translucent,
|
|
29
|
+
* which is the right reading when the series are alternatives rather than
|
|
30
|
+
* parts — two plans compared, not two slices of one total.
|
|
31
|
+
*/
|
|
32
|
+
import {
|
|
33
|
+
Children,
|
|
34
|
+
createContext,
|
|
35
|
+
forwardRef,
|
|
36
|
+
isValidElement,
|
|
37
|
+
useContext,
|
|
38
|
+
useEffect,
|
|
39
|
+
useId,
|
|
40
|
+
useImperativeHandle,
|
|
41
|
+
useMemo,
|
|
42
|
+
useRef,
|
|
43
|
+
useState,
|
|
44
|
+
type ReactNode,
|
|
45
|
+
} from 'react';
|
|
46
|
+
import { StyleSheet, View, type LayoutChangeEvent, type ViewProps } from 'react-native';
|
|
47
|
+
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
|
|
48
|
+
import Animated, {
|
|
49
|
+
Easing,
|
|
50
|
+
runOnJS,
|
|
51
|
+
useAnimatedProps,
|
|
52
|
+
useAnimatedStyle,
|
|
53
|
+
useReducedMotion,
|
|
54
|
+
useSharedValue,
|
|
55
|
+
withTiming,
|
|
56
|
+
type SharedValue,
|
|
57
|
+
} from 'react-native-reanimated';
|
|
58
|
+
import Svg, {
|
|
59
|
+
Circle,
|
|
60
|
+
ClipPath,
|
|
61
|
+
Defs,
|
|
62
|
+
G,
|
|
63
|
+
Line as SvgLine,
|
|
64
|
+
LinearGradient,
|
|
65
|
+
Path,
|
|
66
|
+
Rect,
|
|
67
|
+
Stop,
|
|
68
|
+
} from 'react-native-svg';
|
|
69
|
+
import { useCSSVariable } from 'uniwind';
|
|
70
|
+
import { Text } from '../../primitives/text';
|
|
71
|
+
import {
|
|
72
|
+
areaPath,
|
|
73
|
+
columnValues,
|
|
74
|
+
compactNumber,
|
|
75
|
+
linePath,
|
|
76
|
+
useSeriesColor,
|
|
77
|
+
xOf,
|
|
78
|
+
yOf,
|
|
79
|
+
type ChartCurve,
|
|
80
|
+
type Plot,
|
|
81
|
+
type SeriesColorIndex,
|
|
82
|
+
} from '../../utils/chart';
|
|
83
|
+
import { cn } from '../../utils/cn';
|
|
84
|
+
|
|
85
|
+
const AnimatedPath = Animated.createAnimatedComponent(Path);
|
|
86
|
+
const AnimatedRect = Animated.createAnimatedComponent(Rect);
|
|
87
|
+
const AnimatedCircle = Animated.createAnimatedComponent(Circle);
|
|
88
|
+
|
|
89
|
+
const PADDING = { top: 12, right: 10, bottom: 22, left: 10 };
|
|
90
|
+
const LABEL_WIDTH = 132;
|
|
91
|
+
const DOT = 9;
|
|
92
|
+
|
|
93
|
+
/** Left gutter reserved when a `YAxis` is present, for its labels to sit in. */
|
|
94
|
+
const Y_AXIS_WIDTH = 40;
|
|
95
|
+
|
|
96
|
+
type Layer = 'svg' | 'overlay';
|
|
97
|
+
|
|
98
|
+
export type AreaChartStatus = 'loading' | 'ready';
|
|
99
|
+
export type AreaChartDatum = Record<string, string | number | null | undefined>;
|
|
100
|
+
|
|
101
|
+
interface AreaChartContextValue {
|
|
102
|
+
data: AreaChartDatum[];
|
|
103
|
+
xDataKey: string;
|
|
104
|
+
plot: Plot;
|
|
105
|
+
status: AreaChartStatus;
|
|
106
|
+
curve: ChartCurve;
|
|
107
|
+
stacked: boolean;
|
|
108
|
+
series: [string, string][];
|
|
109
|
+
registerSeries: (key: string, color: string) => void;
|
|
110
|
+
unregisterSeries: (key: string) => void;
|
|
111
|
+
domainMin: SharedValue<number>;
|
|
112
|
+
domainMax: SharedValue<number>;
|
|
113
|
+
extent: [number, number];
|
|
114
|
+
activeIndex: SharedValue<number>;
|
|
115
|
+
activeIndexJS: number;
|
|
116
|
+
setActiveIndexJS: (index: number) => void;
|
|
117
|
+
clipId: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const AreaChartContext = createContext<AreaChartContextValue | null>(null);
|
|
121
|
+
|
|
122
|
+
function useChart(component: string): AreaChartContextValue {
|
|
123
|
+
const context = useContext(AreaChartContext);
|
|
124
|
+
if (!context) {
|
|
125
|
+
throw new Error(`${component} must be used within an <AreaChart>`);
|
|
126
|
+
}
|
|
127
|
+
return context;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The point under the crosshair, for something rendered *inside* the chart.
|
|
132
|
+
* A readout in the card's header is outside this provider — use
|
|
133
|
+
* `onActiveIndexChange` for that.
|
|
134
|
+
*/
|
|
135
|
+
export function useAreaChart() {
|
|
136
|
+
const { data, activeIndexJS, xDataKey } = useChart('useAreaChart');
|
|
137
|
+
return {
|
|
138
|
+
activeIndex: activeIndexJS,
|
|
139
|
+
activePoint: activeIndexJS >= 0 ? (data[activeIndexJS] ?? null) : null,
|
|
140
|
+
xDataKey,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export interface AreaChartProps extends ViewProps {
|
|
145
|
+
className?: string;
|
|
146
|
+
/** The rows. Each one is a point along the x-axis. */
|
|
147
|
+
data: AreaChartDatum[];
|
|
148
|
+
/** Key holding the x label. Used by the axis and the crosshair readout. */
|
|
149
|
+
xDataKey?: string;
|
|
150
|
+
/** `loading` draws a flat skeleton and grows into the real bands on `ready`. */
|
|
151
|
+
status?: AreaChartStatus;
|
|
152
|
+
/** Width ÷ height. `2` is the wide card shape. */
|
|
153
|
+
aspectRatio?: number;
|
|
154
|
+
/** Milliseconds for the reveal on mount. */
|
|
155
|
+
animationDuration?: number;
|
|
156
|
+
/** Milliseconds for the y-axis to settle after the data changes. */
|
|
157
|
+
domainDuration?: number;
|
|
158
|
+
/** Fix the y-axis instead of deriving it from the data. */
|
|
159
|
+
yDomain?: [number, number];
|
|
160
|
+
/**
|
|
161
|
+
* Sit each band on the running total of the ones below it, so the top edge
|
|
162
|
+
* is the whole and each thickness is a share. The order the `Area` children
|
|
163
|
+
* are declared in is the stacking order, bottom first.
|
|
164
|
+
*
|
|
165
|
+
* Unstacked, the bands overlay and their fills are translucent — the right
|
|
166
|
+
* reading when the series are alternatives rather than parts of a total.
|
|
167
|
+
*/
|
|
168
|
+
stacked?: boolean;
|
|
169
|
+
/** `monotone` never overshoots between points; `linear` joins them straight. */
|
|
170
|
+
curve?: ChartCurve;
|
|
171
|
+
/**
|
|
172
|
+
* The point under the crosshair as it moves, and `-1`/`null` when it lifts.
|
|
173
|
+
* Fires when the index changes, not per frame.
|
|
174
|
+
*/
|
|
175
|
+
onActiveIndexChange?: (index: number, datum: AreaChartDatum | null) => void;
|
|
176
|
+
/** Drop the axis padding so the bands reach the edges, for a sparkline. */
|
|
177
|
+
compact?: boolean;
|
|
178
|
+
children?: ReactNode;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Imperative handle: re-run the reveal on demand, for a "replay" control. */
|
|
182
|
+
export interface AreaChartHandle {
|
|
183
|
+
replay: () => void;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function ChildSlot({ children }: { children: ReactNode }) {
|
|
187
|
+
return <>{children}</>;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function partition(children: ReactNode) {
|
|
191
|
+
const svg: ReactNode[] = [];
|
|
192
|
+
const overlay: ReactNode[] = [];
|
|
193
|
+
Children.forEach(children, (child, index) => {
|
|
194
|
+
if (!isValidElement(child)) return;
|
|
195
|
+
const layer = (child.type as { layer?: Layer }).layer ?? 'svg';
|
|
196
|
+
const slot = <ChildSlot key={index}>{child}</ChildSlot>;
|
|
197
|
+
(layer === 'overlay' ? overlay : svg).push(slot);
|
|
198
|
+
});
|
|
199
|
+
return { svg, overlay };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const AreaChartRoot = forwardRef<AreaChartHandle, AreaChartProps>(function AreaChartRoot(
|
|
203
|
+
{
|
|
204
|
+
className,
|
|
205
|
+
data,
|
|
206
|
+
xDataKey = 'date',
|
|
207
|
+
status = 'ready',
|
|
208
|
+
aspectRatio = 2,
|
|
209
|
+
animationDuration = 1100,
|
|
210
|
+
domainDuration = 500,
|
|
211
|
+
yDomain,
|
|
212
|
+
stacked = false,
|
|
213
|
+
curve = 'monotone',
|
|
214
|
+
onActiveIndexChange,
|
|
215
|
+
compact = false,
|
|
216
|
+
children,
|
|
217
|
+
...props
|
|
218
|
+
},
|
|
219
|
+
ref
|
|
220
|
+
) {
|
|
221
|
+
const [size, setSize] = useState({ width: 0, height: 0 });
|
|
222
|
+
const [series, setSeries] = useState<[string, string][]>([]);
|
|
223
|
+
const [activeIndexJS, setActiveIndexJS] = useState(-1);
|
|
224
|
+
const clipId = `panelui-area-clip-${useId().replace(/[^a-zA-Z0-9]/g, '')}`;
|
|
225
|
+
|
|
226
|
+
const reveal = useSharedValue(0);
|
|
227
|
+
const domainMin = useSharedValue(0);
|
|
228
|
+
const domainMax = useSharedValue(0);
|
|
229
|
+
const activeIndex = useSharedValue(-1);
|
|
230
|
+
const reducedMotion = useReducedMotion();
|
|
231
|
+
|
|
232
|
+
const registerSeries = useMemo(
|
|
233
|
+
() => (key: string, color: string) =>
|
|
234
|
+
setSeries((current) => {
|
|
235
|
+
const existing = current.find(([k]) => k === key);
|
|
236
|
+
if (existing?.[1] === color) return current;
|
|
237
|
+
return [...current.filter(([k]) => k !== key), [key, color]];
|
|
238
|
+
}),
|
|
239
|
+
[]
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
const unregisterSeries = useMemo(
|
|
243
|
+
() => (key: string) => setSeries((current) => current.filter(([k]) => k !== key)),
|
|
244
|
+
[]
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
/*
|
|
248
|
+
* Whether an axis is asking for room. It has to be known before the plot is
|
|
249
|
+
* laid out, and only the root sees the children early enough to ask — the
|
|
250
|
+
* axis itself renders into a box that has already been decided.
|
|
251
|
+
*/
|
|
252
|
+
const hasYAxis = useMemo(() => {
|
|
253
|
+
let found = false;
|
|
254
|
+
Children.forEach(children, (child) => {
|
|
255
|
+
if (isValidElement(child) && (child.type as { axis?: string }).axis === 'y') {
|
|
256
|
+
found = true;
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
return found;
|
|
260
|
+
}, [children]);
|
|
261
|
+
|
|
262
|
+
const pad = compact
|
|
263
|
+
? { top: 2, right: 1, bottom: 2, left: 1 }
|
|
264
|
+
: { ...PADDING, left: hasYAxis ? Y_AXIS_WIDTH : PADDING.left };
|
|
265
|
+
const plot: Plot = {
|
|
266
|
+
left: pad.left,
|
|
267
|
+
top: pad.top,
|
|
268
|
+
width: Math.max(size.width - pad.left - pad.right, 0),
|
|
269
|
+
height: Math.max(size.height - pad.top - pad.bottom, 0),
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const seriesKeys = series.map(([key]) => key).join('|');
|
|
273
|
+
const extent = useMemo<[number, number]>(() => {
|
|
274
|
+
if (yDomain) return yDomain;
|
|
275
|
+
const keys = seriesKeys ? seriesKeys.split('|') : [];
|
|
276
|
+
let min = Infinity;
|
|
277
|
+
let max = -Infinity;
|
|
278
|
+
|
|
279
|
+
for (const row of data) {
|
|
280
|
+
/*
|
|
281
|
+
* Stacked, the tall thing is the sum of the row; overlaid, it is the
|
|
282
|
+
* largest single value in it. Reading the same number for both crops a
|
|
283
|
+
* stack at its largest band and clips everything above it.
|
|
284
|
+
*/
|
|
285
|
+
let rowTotal = 0;
|
|
286
|
+
for (const key of keys) {
|
|
287
|
+
const value = row[key];
|
|
288
|
+
if (typeof value !== 'number' || Number.isNaN(value)) continue;
|
|
289
|
+
if (stacked) {
|
|
290
|
+
rowTotal += value;
|
|
291
|
+
} else {
|
|
292
|
+
if (value < min) min = value;
|
|
293
|
+
if (value > max) max = value;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
if (stacked) {
|
|
297
|
+
if (rowTotal < min) min = rowTotal;
|
|
298
|
+
if (rowTotal > max) max = rowTotal;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (min === Infinity) return [0, 1];
|
|
303
|
+
if (min === max) return [min - 1, max + 1];
|
|
304
|
+
/*
|
|
305
|
+
* An area is a filled region, and a region floating above the bottom of
|
|
306
|
+
* the plot reads as a shape rather than as a quantity — so a series that
|
|
307
|
+
* never goes below zero is drawn from zero. Padding only the top.
|
|
308
|
+
*/
|
|
309
|
+
const floor = min >= 0 ? 0 : min - (max - min) * 0.1;
|
|
310
|
+
return [floor, max + (max - min) * 0.1];
|
|
311
|
+
}, [data, yDomain, seriesKeys, stacked]);
|
|
312
|
+
|
|
313
|
+
const loading = status === 'loading';
|
|
314
|
+
|
|
315
|
+
useEffect(() => {
|
|
316
|
+
if (loading) return;
|
|
317
|
+
const [min, max] = extent;
|
|
318
|
+
const first = domainMin.value === 0 && domainMax.value === 0;
|
|
319
|
+
if (first || reducedMotion) {
|
|
320
|
+
domainMin.value = min;
|
|
321
|
+
domainMax.value = max;
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
domainMin.value = withTiming(min, { duration: domainDuration });
|
|
325
|
+
domainMax.value = withTiming(max, { duration: domainDuration });
|
|
326
|
+
}, [extent, loading, reducedMotion, domainDuration, domainMin, domainMax]);
|
|
327
|
+
|
|
328
|
+
const revealed = useRef(false);
|
|
329
|
+
const playReveal = useMemo(
|
|
330
|
+
() => () => {
|
|
331
|
+
if (reducedMotion) {
|
|
332
|
+
reveal.value = 1;
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
reveal.value = 0;
|
|
336
|
+
reveal.value = withTiming(1, {
|
|
337
|
+
duration: animationDuration,
|
|
338
|
+
easing: Easing.bezier(0.85, 0, 0.15, 1),
|
|
339
|
+
});
|
|
340
|
+
},
|
|
341
|
+
[reducedMotion, animationDuration, reveal]
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
useEffect(() => {
|
|
345
|
+
if (revealed.current || loading || plot.width <= 0 || !data.length) return;
|
|
346
|
+
revealed.current = true;
|
|
347
|
+
playReveal();
|
|
348
|
+
}, [loading, plot.width, data.length, playReveal]);
|
|
349
|
+
|
|
350
|
+
useImperativeHandle(ref, () => ({ replay: playReveal }), [playReveal]);
|
|
351
|
+
|
|
352
|
+
const clipProps = useAnimatedProps(() => ({ width: plot.width * reveal.value }));
|
|
353
|
+
|
|
354
|
+
const handleActiveIndex = useMemo(
|
|
355
|
+
() => (index: number) => {
|
|
356
|
+
setActiveIndexJS(index);
|
|
357
|
+
onActiveIndexChange?.(index, index >= 0 ? (data[index] ?? null) : null);
|
|
358
|
+
},
|
|
359
|
+
[onActiveIndexChange, data]
|
|
360
|
+
);
|
|
361
|
+
|
|
362
|
+
const onLayout = (event: LayoutChangeEvent) => {
|
|
363
|
+
const { width, height } = event.nativeEvent.layout;
|
|
364
|
+
setSize((current) =>
|
|
365
|
+
Math.abs(current.width - width) < 1 && Math.abs(current.height - height) < 1
|
|
366
|
+
? current
|
|
367
|
+
: { width, height }
|
|
368
|
+
);
|
|
369
|
+
props.onLayout?.(event);
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
const context = useMemo<AreaChartContextValue>(
|
|
373
|
+
() => ({
|
|
374
|
+
data,
|
|
375
|
+
xDataKey,
|
|
376
|
+
plot,
|
|
377
|
+
status,
|
|
378
|
+
curve,
|
|
379
|
+
stacked,
|
|
380
|
+
series,
|
|
381
|
+
registerSeries,
|
|
382
|
+
unregisterSeries,
|
|
383
|
+
domainMin,
|
|
384
|
+
domainMax,
|
|
385
|
+
extent,
|
|
386
|
+
activeIndex,
|
|
387
|
+
activeIndexJS,
|
|
388
|
+
setActiveIndexJS: handleActiveIndex,
|
|
389
|
+
clipId,
|
|
390
|
+
}),
|
|
391
|
+
// `plot` is rebuilt every render from `size`, so it is compared by value.
|
|
392
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
393
|
+
[
|
|
394
|
+
data,
|
|
395
|
+
xDataKey,
|
|
396
|
+
plot.width,
|
|
397
|
+
plot.height,
|
|
398
|
+
plot.left,
|
|
399
|
+
plot.top,
|
|
400
|
+
status,
|
|
401
|
+
curve,
|
|
402
|
+
stacked,
|
|
403
|
+
series,
|
|
404
|
+
registerSeries,
|
|
405
|
+
unregisterSeries,
|
|
406
|
+
domainMin,
|
|
407
|
+
domainMax,
|
|
408
|
+
extent,
|
|
409
|
+
activeIndex,
|
|
410
|
+
activeIndexJS,
|
|
411
|
+
handleActiveIndex,
|
|
412
|
+
clipId,
|
|
413
|
+
]
|
|
414
|
+
);
|
|
415
|
+
|
|
416
|
+
const { svg, overlay } = partition(children);
|
|
417
|
+
|
|
418
|
+
return (
|
|
419
|
+
<AreaChartContext.Provider value={context}>
|
|
420
|
+
<View
|
|
421
|
+
{...props}
|
|
422
|
+
onLayout={onLayout}
|
|
423
|
+
style={[{ aspectRatio }, props.style]}
|
|
424
|
+
className={cn('w-full', className)}
|
|
425
|
+
>
|
|
426
|
+
{plot.width > 0 ? (
|
|
427
|
+
<>
|
|
428
|
+
<Svg width="100%" height="100%" style={StyleSheet.absoluteFill}>
|
|
429
|
+
<Defs>
|
|
430
|
+
{/*
|
|
431
|
+
* One clip for everything in the plot. Sharing it is what makes
|
|
432
|
+
* the reveal read as the chart arriving rather than as several
|
|
433
|
+
* bands animating in at once.
|
|
434
|
+
*/}
|
|
435
|
+
<ClipPath id={clipId}>
|
|
436
|
+
<AnimatedRect
|
|
437
|
+
x={plot.left}
|
|
438
|
+
y={0}
|
|
439
|
+
height={size.height}
|
|
440
|
+
animatedProps={clipProps}
|
|
441
|
+
/>
|
|
442
|
+
</ClipPath>
|
|
443
|
+
</Defs>
|
|
444
|
+
{svg}
|
|
445
|
+
</Svg>
|
|
446
|
+
{overlay}
|
|
447
|
+
</>
|
|
448
|
+
) : null}
|
|
449
|
+
</View>
|
|
450
|
+
</AreaChartContext.Provider>
|
|
451
|
+
);
|
|
452
|
+
});
|
|
453
|
+
AreaChartRoot.displayName = 'AreaChart';
|
|
454
|
+
|
|
455
|
+
/* -------------------------------------------------------------------------- */
|
|
456
|
+
/* SVG layer */
|
|
457
|
+
/* -------------------------------------------------------------------------- */
|
|
458
|
+
|
|
459
|
+
export interface AreaChartGridProps {
|
|
460
|
+
rows?: number;
|
|
461
|
+
color?: string;
|
|
462
|
+
dashArray?: string;
|
|
463
|
+
opacity?: number;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** Horizontal rules, so a band can be read against a number. */
|
|
467
|
+
function AreaChartGrid({ rows = 4, color, dashArray = '4,6', opacity = 1 }: AreaChartGridProps) {
|
|
468
|
+
const { plot } = useChart('AreaChart.Grid');
|
|
469
|
+
const token = useCSSVariable('--color-border');
|
|
470
|
+
const stroke = color ?? (typeof token === 'string' ? token : 'rgba(0,0,0,0.1)');
|
|
471
|
+
|
|
472
|
+
return (
|
|
473
|
+
<G opacity={opacity}>
|
|
474
|
+
{Array.from({ length: rows + 1 }, (_unused, index) => index / rows).map((fraction) => (
|
|
475
|
+
<SvgLine
|
|
476
|
+
key={fraction}
|
|
477
|
+
x1={plot.left}
|
|
478
|
+
x2={plot.left + plot.width}
|
|
479
|
+
y1={plot.top + plot.height * fraction}
|
|
480
|
+
y2={plot.top + plot.height * fraction}
|
|
481
|
+
stroke={stroke}
|
|
482
|
+
strokeDasharray={dashArray}
|
|
483
|
+
strokeWidth={1}
|
|
484
|
+
/>
|
|
485
|
+
))}
|
|
486
|
+
</G>
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
AreaChartGrid.displayName = 'AreaChart.Grid';
|
|
490
|
+
AreaChartGrid.layer = 'svg' as Layer;
|
|
491
|
+
|
|
492
|
+
export interface AreaChartAreaProps {
|
|
493
|
+
/** Column in the data holding this series' values. */
|
|
494
|
+
dataKey: string;
|
|
495
|
+
/** Explicit colour. Defaults to the `--color-chart-*` token for `colorIndex`. */
|
|
496
|
+
color?: string;
|
|
497
|
+
/** Which of the five chart tokens to take. */
|
|
498
|
+
colorIndex?: SeriesColorIndex;
|
|
499
|
+
/** Opacity of the fill at the top of the band. */
|
|
500
|
+
fillOpacity?: number;
|
|
501
|
+
/** Opacity at the bottom. `0` fades the band out; match `fillOpacity` for a flat fill. */
|
|
502
|
+
gradientToOpacity?: number;
|
|
503
|
+
/** Draw the line along the top edge of the band. */
|
|
504
|
+
showLine?: boolean;
|
|
505
|
+
/** Thickness of that line. */
|
|
506
|
+
strokeWidth?: number;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* One filled band.
|
|
511
|
+
*
|
|
512
|
+
* The fill is a gradient by default rather than a flat wash, because a flat
|
|
513
|
+
* fill of any weight competes with the line on top of it — fading it downward
|
|
514
|
+
* keeps the top edge, which is the part carrying the numbers, the darkest
|
|
515
|
+
* thing in the band.
|
|
516
|
+
*/
|
|
517
|
+
function AreaChartArea({
|
|
518
|
+
dataKey,
|
|
519
|
+
color,
|
|
520
|
+
colorIndex = 1,
|
|
521
|
+
fillOpacity,
|
|
522
|
+
gradientToOpacity = 0,
|
|
523
|
+
showLine = true,
|
|
524
|
+
strokeWidth = 2,
|
|
525
|
+
}: AreaChartAreaProps) {
|
|
526
|
+
const {
|
|
527
|
+
data,
|
|
528
|
+
plot,
|
|
529
|
+
status,
|
|
530
|
+
curve,
|
|
531
|
+
stacked,
|
|
532
|
+
series,
|
|
533
|
+
registerSeries,
|
|
534
|
+
unregisterSeries,
|
|
535
|
+
domainMin,
|
|
536
|
+
domainMax,
|
|
537
|
+
clipId,
|
|
538
|
+
} = useChart('AreaChart.Area');
|
|
539
|
+
|
|
540
|
+
const stroke = useSeriesColor(color, colorIndex);
|
|
541
|
+
const gradientId = `panelui-area-fill-${useId().replace(/[^a-zA-Z0-9]/g, '')}`;
|
|
542
|
+
|
|
543
|
+
// Stacked bands touch, so a translucent one shows the band beneath through
|
|
544
|
+
// it and the two read as a third colour. Overlaid they must be translucent,
|
|
545
|
+
// because the one in front would otherwise hide the ones behind entirely.
|
|
546
|
+
const topOpacity = fillOpacity ?? (stacked ? 0.85 : 0.35);
|
|
547
|
+
|
|
548
|
+
useEffect(() => {
|
|
549
|
+
registerSeries(dataKey, stroke);
|
|
550
|
+
return () => unregisterSeries(dataKey);
|
|
551
|
+
}, [dataKey, stroke, registerSeries, unregisterSeries]);
|
|
552
|
+
|
|
553
|
+
const values = useMemo(() => columnValues(data, dataKey), [data, dataKey]);
|
|
554
|
+
|
|
555
|
+
/*
|
|
556
|
+
* The running total of the series declared below this one, which is what
|
|
557
|
+
* this band sits on. Registration order is declaration order, so the first
|
|
558
|
+
* `Area` child is the bottom of the stack.
|
|
559
|
+
*/
|
|
560
|
+
const baselines = useMemo(() => {
|
|
561
|
+
if (!stacked) return undefined;
|
|
562
|
+
const below = series.slice(0, series.findIndex(([key]) => key === dataKey));
|
|
563
|
+
if (!below.length) return undefined;
|
|
564
|
+
return data.map((row) => {
|
|
565
|
+
let total = 0;
|
|
566
|
+
for (const [key] of below) {
|
|
567
|
+
const value = row[key];
|
|
568
|
+
if (typeof value === 'number' && !Number.isNaN(value)) total += value;
|
|
569
|
+
}
|
|
570
|
+
return total;
|
|
571
|
+
});
|
|
572
|
+
}, [stacked, series, dataKey, data]);
|
|
573
|
+
|
|
574
|
+
const loading = status === 'loading';
|
|
575
|
+
|
|
576
|
+
const fillProps = useAnimatedProps(() => ({
|
|
577
|
+
d: areaPath(values, plot, domainMin.value, domainMax.value, curve, loading, baselines),
|
|
578
|
+
}));
|
|
579
|
+
|
|
580
|
+
const lineProps = useAnimatedProps(() => ({
|
|
581
|
+
d: linePath(values, plot, domainMin.value, domainMax.value, curve, loading, baselines),
|
|
582
|
+
}));
|
|
583
|
+
|
|
584
|
+
return (
|
|
585
|
+
<G clipPath={`url(#${clipId})`}>
|
|
586
|
+
<Defs>
|
|
587
|
+
<LinearGradient id={gradientId} x1="0" y1="0" x2="0" y2="1">
|
|
588
|
+
<Stop offset="0" stopColor={stroke} stopOpacity={topOpacity} />
|
|
589
|
+
<Stop offset="1" stopColor={stroke} stopOpacity={gradientToOpacity} />
|
|
590
|
+
</LinearGradient>
|
|
591
|
+
</Defs>
|
|
592
|
+
<AnimatedPath animatedProps={fillProps} fill={`url(#${gradientId})`} />
|
|
593
|
+
{showLine ? (
|
|
594
|
+
<AnimatedPath
|
|
595
|
+
animatedProps={lineProps}
|
|
596
|
+
stroke={stroke}
|
|
597
|
+
strokeWidth={strokeWidth}
|
|
598
|
+
strokeLinecap="round"
|
|
599
|
+
strokeLinejoin="round"
|
|
600
|
+
fill="none"
|
|
601
|
+
/>
|
|
602
|
+
) : null}
|
|
603
|
+
</G>
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
AreaChartArea.displayName = 'AreaChart.Area';
|
|
607
|
+
AreaChartArea.layer = 'svg' as Layer;
|
|
608
|
+
|
|
609
|
+
/* -------------------------------------------------------------------------- */
|
|
610
|
+
/* Overlay layer */
|
|
611
|
+
/* -------------------------------------------------------------------------- */
|
|
612
|
+
|
|
613
|
+
export interface AreaChartXAxisProps {
|
|
614
|
+
ticks?: number;
|
|
615
|
+
format?: (datum: AreaChartDatum, index: number) => string;
|
|
616
|
+
className?: string;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/** The x labels. Real text, so they follow the theme's font and text scaling. */
|
|
620
|
+
function AreaChartXAxis({ ticks = 4, format, className }: AreaChartXAxisProps) {
|
|
621
|
+
const { data, xDataKey, plot } = useChart('AreaChart.XAxis');
|
|
622
|
+
|
|
623
|
+
const labels = useMemo(() => {
|
|
624
|
+
if (!data.length) return [];
|
|
625
|
+
const count = Math.min(ticks, data.length);
|
|
626
|
+
const step = count > 1 ? (data.length - 1) / (count - 1) : 0;
|
|
627
|
+
return Array.from({ length: count }, (_unused, index) => {
|
|
628
|
+
const dataIndex = Math.round(index * step);
|
|
629
|
+
const datum = data[dataIndex];
|
|
630
|
+
if (!datum) return null;
|
|
631
|
+
return {
|
|
632
|
+
key: dataIndex,
|
|
633
|
+
text: format ? format(datum, dataIndex) : String(datum[xDataKey] ?? ''),
|
|
634
|
+
};
|
|
635
|
+
}).filter((label): label is { key: number; text: string } => label !== null);
|
|
636
|
+
}, [data, ticks, format, xDataKey]);
|
|
637
|
+
|
|
638
|
+
return (
|
|
639
|
+
<View
|
|
640
|
+
pointerEvents="none"
|
|
641
|
+
style={{
|
|
642
|
+
position: 'absolute',
|
|
643
|
+
left: plot.left,
|
|
644
|
+
right: PADDING.right,
|
|
645
|
+
bottom: 0,
|
|
646
|
+
height: PADDING.bottom,
|
|
647
|
+
}}
|
|
648
|
+
className={cn('flex-row items-center justify-between', className)}
|
|
649
|
+
>
|
|
650
|
+
{labels.map((label) => (
|
|
651
|
+
<Text key={label.key} size="xs" muted>
|
|
652
|
+
{label.text}
|
|
653
|
+
</Text>
|
|
654
|
+
))}
|
|
655
|
+
</View>
|
|
656
|
+
);
|
|
657
|
+
}
|
|
658
|
+
AreaChartXAxis.displayName = 'AreaChart.XAxis';
|
|
659
|
+
AreaChartXAxis.layer = 'overlay' as Layer;
|
|
660
|
+
|
|
661
|
+
export interface AreaChartYAxisProps {
|
|
662
|
+
ticks?: number;
|
|
663
|
+
format?: (value: number) => string;
|
|
664
|
+
className?: string;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/** Value labels down the side, aligned to the grid lines. */
|
|
668
|
+
function AreaChartYAxis({ ticks = 4, format, className }: AreaChartYAxisProps) {
|
|
669
|
+
const { plot, extent } = useChart('AreaChart.YAxis');
|
|
670
|
+
|
|
671
|
+
const labels = useMemo(() => {
|
|
672
|
+
const [min, max] = extent;
|
|
673
|
+
if (min === 0 && max === 0) return [];
|
|
674
|
+
return Array.from({ length: ticks + 1 }, (_unused, index) => {
|
|
675
|
+
const value = max - ((max - min) * index) / ticks;
|
|
676
|
+
return { key: index, text: format ? format(value) : compactNumber(value) };
|
|
677
|
+
});
|
|
678
|
+
}, [extent, ticks, format]);
|
|
679
|
+
|
|
680
|
+
return (
|
|
681
|
+
<View
|
|
682
|
+
pointerEvents="none"
|
|
683
|
+
style={{ position: 'absolute', left: 0, top: plot.top, height: plot.height }}
|
|
684
|
+
className={cn('justify-between', className)}
|
|
685
|
+
>
|
|
686
|
+
{labels.map((label) => (
|
|
687
|
+
<Text key={label.key} size="xs" muted numberOfLines={1}>
|
|
688
|
+
{label.text}
|
|
689
|
+
</Text>
|
|
690
|
+
))}
|
|
691
|
+
</View>
|
|
692
|
+
);
|
|
693
|
+
}
|
|
694
|
+
AreaChartYAxis.displayName = 'AreaChart.YAxis';
|
|
695
|
+
AreaChartYAxis.layer = 'overlay' as Layer;
|
|
696
|
+
// Read by the root, which has to leave room for the labels before it
|
|
697
|
+
// lays the plot out — an axis drawn over the plot is unreadable, and
|
|
698
|
+
// makes what it is drawn over unreadable too.
|
|
699
|
+
AreaChartYAxis.axis = 'y' as const;
|
|
700
|
+
|
|
701
|
+
/** A dot riding the top edge of one band as the crosshair moves. */
|
|
702
|
+
function CrosshairDot({
|
|
703
|
+
values,
|
|
704
|
+
baselines,
|
|
705
|
+
plot,
|
|
706
|
+
domainMin,
|
|
707
|
+
domainMax,
|
|
708
|
+
activeIndex,
|
|
709
|
+
total,
|
|
710
|
+
color,
|
|
711
|
+
}: {
|
|
712
|
+
values: (number | null)[];
|
|
713
|
+
baselines: number[] | undefined;
|
|
714
|
+
plot: Plot;
|
|
715
|
+
domainMin: SharedValue<number>;
|
|
716
|
+
domainMax: SharedValue<number>;
|
|
717
|
+
activeIndex: SharedValue<number>;
|
|
718
|
+
total: number;
|
|
719
|
+
color: string;
|
|
720
|
+
}) {
|
|
721
|
+
const props = useAnimatedProps(() => {
|
|
722
|
+
const index = activeIndex.value;
|
|
723
|
+
const value = index >= 0 ? values[index] : null;
|
|
724
|
+
if (index < 0 || value === null || value === undefined) {
|
|
725
|
+
return { opacity: 0, cx: 0, cy: 0 };
|
|
726
|
+
}
|
|
727
|
+
const stacked = value + (baselines?.[index] ?? 0);
|
|
728
|
+
return {
|
|
729
|
+
opacity: 1,
|
|
730
|
+
cx: xOf(index, total, plot),
|
|
731
|
+
cy: yOf(stacked, plot, domainMin.value, domainMax.value),
|
|
732
|
+
};
|
|
733
|
+
});
|
|
734
|
+
|
|
735
|
+
return (
|
|
736
|
+
<AnimatedCircle
|
|
737
|
+
animatedProps={props}
|
|
738
|
+
r={DOT / 2}
|
|
739
|
+
fill={color}
|
|
740
|
+
stroke="white"
|
|
741
|
+
strokeWidth={2}
|
|
742
|
+
/>
|
|
743
|
+
);
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
export interface AreaChartTooltipProps {
|
|
747
|
+
color?: string;
|
|
748
|
+
/** Format one series' value. Defaults to a compact number. */
|
|
749
|
+
formatValue?: (value: number, key: string) => string;
|
|
750
|
+
/** Format the readout's heading from the row. Defaults to the value at xDataKey. */
|
|
751
|
+
formatX?: (datum: AreaChartDatum) => string;
|
|
752
|
+
className?: string;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* The crosshair, the gesture that drives it, and the readout that rides it.
|
|
757
|
+
*
|
|
758
|
+
* The hit area is the whole plot. A crosshair you have to land on a band to
|
|
759
|
+
* summon is a crosshair nobody finds.
|
|
760
|
+
*/
|
|
761
|
+
function AreaChartTooltip({ color, formatValue, formatX, className }: AreaChartTooltipProps) {
|
|
762
|
+
const {
|
|
763
|
+
data,
|
|
764
|
+
xDataKey,
|
|
765
|
+
plot,
|
|
766
|
+
stacked,
|
|
767
|
+
series,
|
|
768
|
+
domainMin,
|
|
769
|
+
domainMax,
|
|
770
|
+
activeIndex,
|
|
771
|
+
activeIndexJS,
|
|
772
|
+
setActiveIndexJS,
|
|
773
|
+
status,
|
|
774
|
+
} = useChart('AreaChart.Tooltip');
|
|
775
|
+
|
|
776
|
+
const token = useCSSVariable('--color-foreground');
|
|
777
|
+
const stroke = color ?? (typeof token === 'string' ? token : '#888888');
|
|
778
|
+
|
|
779
|
+
const total = data.length;
|
|
780
|
+
const left = plot.left;
|
|
781
|
+
const width = plot.width;
|
|
782
|
+
|
|
783
|
+
/*
|
|
784
|
+
* Declared inside the memo, next to its callers: a worklet may only call
|
|
785
|
+
* another worklet, and the rule is enforced by crashing rather than warning.
|
|
786
|
+
*/
|
|
787
|
+
const pan = useMemo(() => {
|
|
788
|
+
const resolve = (x: number) => {
|
|
789
|
+
'worklet';
|
|
790
|
+
if (total < 2 || width <= 0) return;
|
|
791
|
+
const ratio = (x - left) / width;
|
|
792
|
+
const next = Math.round(Math.min(1, Math.max(0, ratio)) * (total - 1));
|
|
793
|
+
if (next === activeIndex.value) return;
|
|
794
|
+
activeIndex.value = next;
|
|
795
|
+
runOnJS(setActiveIndexJS)(next);
|
|
796
|
+
};
|
|
797
|
+
|
|
798
|
+
return Gesture.Pan()
|
|
799
|
+
.minDistance(0)
|
|
800
|
+
.onBegin((event) => {
|
|
801
|
+
'worklet';
|
|
802
|
+
resolve(event.x);
|
|
803
|
+
})
|
|
804
|
+
.onUpdate((event) => {
|
|
805
|
+
'worklet';
|
|
806
|
+
resolve(event.x);
|
|
807
|
+
})
|
|
808
|
+
.onFinalize(() => {
|
|
809
|
+
'worklet';
|
|
810
|
+
activeIndex.value = -1;
|
|
811
|
+
runOnJS(setActiveIndexJS)(-1);
|
|
812
|
+
});
|
|
813
|
+
}, [total, left, width, activeIndex, setActiveIndexJS]);
|
|
814
|
+
|
|
815
|
+
const crosshairStyle = useAnimatedStyle(() => {
|
|
816
|
+
const index = activeIndex.value;
|
|
817
|
+
return {
|
|
818
|
+
opacity: index < 0 ? 0 : 0.45,
|
|
819
|
+
transform: [{ translateX: index < 0 ? 0 : xOf(index, total, plot) }],
|
|
820
|
+
};
|
|
821
|
+
});
|
|
822
|
+
|
|
823
|
+
const labelStyle = useAnimatedStyle(() => {
|
|
824
|
+
const index = activeIndex.value;
|
|
825
|
+
if (index < 0) return { opacity: 0 };
|
|
826
|
+
const x = xOf(index, total, plot);
|
|
827
|
+
const half = LABEL_WIDTH / 2;
|
|
828
|
+
const clamped = Math.min(
|
|
829
|
+
plot.left + plot.width - half,
|
|
830
|
+
Math.max(plot.left + half, x)
|
|
831
|
+
);
|
|
832
|
+
return { opacity: 1, transform: [{ translateX: clamped - half }] };
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
/*
|
|
836
|
+
* The dots ride the *top edge* of each band, which for a stack is the
|
|
837
|
+
* running total rather than the value — so the baselines are recomputed here
|
|
838
|
+
* the same way the bands compute them, and for the same reason.
|
|
839
|
+
*/
|
|
840
|
+
const stacks = useMemo(() => {
|
|
841
|
+
return series.map(([key, seriesColor], index) => {
|
|
842
|
+
const below = series.slice(0, index);
|
|
843
|
+
const baselines =
|
|
844
|
+
stacked && below.length
|
|
845
|
+
? data.map((row) => {
|
|
846
|
+
let sum = 0;
|
|
847
|
+
for (const [k] of below) {
|
|
848
|
+
const value = row[k];
|
|
849
|
+
if (typeof value === 'number' && !Number.isNaN(value)) sum += value;
|
|
850
|
+
}
|
|
851
|
+
return sum;
|
|
852
|
+
})
|
|
853
|
+
: undefined;
|
|
854
|
+
return { key, color: seriesColor, values: columnValues(data, key), baselines };
|
|
855
|
+
});
|
|
856
|
+
}, [series, data, stacked]);
|
|
857
|
+
|
|
858
|
+
const active = activeIndexJS >= 0 ? data[activeIndexJS] : null;
|
|
859
|
+
const fmtValue = formatValue ?? ((value: number) => compactNumber(value));
|
|
860
|
+
const fmtX = formatX ?? ((datum: AreaChartDatum) => String(datum[xDataKey] ?? ''));
|
|
861
|
+
|
|
862
|
+
if (status === 'loading') return null;
|
|
863
|
+
|
|
864
|
+
return (
|
|
865
|
+
<GestureDetector gesture={pan}>
|
|
866
|
+
<View style={StyleSheet.absoluteFill}>
|
|
867
|
+
<Animated.View
|
|
868
|
+
pointerEvents="none"
|
|
869
|
+
style={[
|
|
870
|
+
{
|
|
871
|
+
position: 'absolute',
|
|
872
|
+
left: 0,
|
|
873
|
+
top: plot.top,
|
|
874
|
+
width: 1,
|
|
875
|
+
height: plot.height,
|
|
876
|
+
backgroundColor: stroke,
|
|
877
|
+
},
|
|
878
|
+
crosshairStyle,
|
|
879
|
+
]}
|
|
880
|
+
/>
|
|
881
|
+
<Svg
|
|
882
|
+
width="100%"
|
|
883
|
+
height="100%"
|
|
884
|
+
style={StyleSheet.absoluteFill}
|
|
885
|
+
pointerEvents="none"
|
|
886
|
+
>
|
|
887
|
+
{stacks.map((stack) => (
|
|
888
|
+
<CrosshairDot
|
|
889
|
+
key={stack.key}
|
|
890
|
+
values={stack.values}
|
|
891
|
+
baselines={stack.baselines}
|
|
892
|
+
plot={plot}
|
|
893
|
+
domainMin={domainMin}
|
|
894
|
+
domainMax={domainMax}
|
|
895
|
+
activeIndex={activeIndex}
|
|
896
|
+
total={total}
|
|
897
|
+
color={stack.color}
|
|
898
|
+
/>
|
|
899
|
+
))}
|
|
900
|
+
</Svg>
|
|
901
|
+
<Animated.View
|
|
902
|
+
pointerEvents="none"
|
|
903
|
+
style={[
|
|
904
|
+
{ position: 'absolute', left: 0, top: 0, width: LABEL_WIDTH },
|
|
905
|
+
labelStyle,
|
|
906
|
+
]}
|
|
907
|
+
>
|
|
908
|
+
{active ? (
|
|
909
|
+
<View
|
|
910
|
+
className={cn(
|
|
911
|
+
'rounded-xl border border-border bg-popover px-2.5 py-1.5 shadow-lg',
|
|
912
|
+
className
|
|
913
|
+
)}
|
|
914
|
+
>
|
|
915
|
+
<Text size="xs" muted numberOfLines={1}>
|
|
916
|
+
{fmtX(active)}
|
|
917
|
+
</Text>
|
|
918
|
+
{series.map(([key, seriesColor]) => {
|
|
919
|
+
const value = active[key];
|
|
920
|
+
if (typeof value !== 'number') return null;
|
|
921
|
+
return (
|
|
922
|
+
<View key={key} className="flex-row items-center gap-1.5">
|
|
923
|
+
<View
|
|
924
|
+
style={{
|
|
925
|
+
width: 6,
|
|
926
|
+
height: 6,
|
|
927
|
+
borderRadius: 3,
|
|
928
|
+
backgroundColor: seriesColor,
|
|
929
|
+
}}
|
|
930
|
+
/>
|
|
931
|
+
<Text size="xs" weight="medium">
|
|
932
|
+
{fmtValue(value, key)}
|
|
933
|
+
</Text>
|
|
934
|
+
</View>
|
|
935
|
+
);
|
|
936
|
+
})}
|
|
937
|
+
</View>
|
|
938
|
+
) : null}
|
|
939
|
+
</Animated.View>
|
|
940
|
+
</View>
|
|
941
|
+
</GestureDetector>
|
|
942
|
+
);
|
|
943
|
+
}
|
|
944
|
+
AreaChartTooltip.displayName = 'AreaChart.Tooltip';
|
|
945
|
+
AreaChartTooltip.layer = 'overlay' as Layer;
|
|
946
|
+
|
|
947
|
+
export interface AreaChartLegendProps extends ViewProps {
|
|
948
|
+
className?: string;
|
|
949
|
+
/** Prettier names for the series keys. */
|
|
950
|
+
labels?: Record<string, string>;
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
/**
|
|
954
|
+
* A swatch and a name per series.
|
|
955
|
+
*
|
|
956
|
+
* Reversed for a stack, so the key reads in the order the bands appear on the
|
|
957
|
+
* chart: the last one declared is the top band, and a legend listing it last
|
|
958
|
+
* points at the bottom one.
|
|
959
|
+
*/
|
|
960
|
+
function AreaChartLegend({ className, labels, ...props }: AreaChartLegendProps) {
|
|
961
|
+
const { series, stacked } = useChart('AreaChart.Legend');
|
|
962
|
+
if (!series.length) return null;
|
|
963
|
+
const ordered = stacked ? [...series].reverse() : series;
|
|
964
|
+
|
|
965
|
+
return (
|
|
966
|
+
<View
|
|
967
|
+
{...props}
|
|
968
|
+
pointerEvents="none"
|
|
969
|
+
className={cn('absolute right-2 top-1 flex-row gap-3', className)}
|
|
970
|
+
>
|
|
971
|
+
{ordered.map(([key, color]) => (
|
|
972
|
+
<View key={key} className="flex-row items-center gap-1.5">
|
|
973
|
+
<View
|
|
974
|
+
style={{ width: 8, height: 8, borderRadius: 2, backgroundColor: color }}
|
|
975
|
+
/>
|
|
976
|
+
<Text size="xs" muted>
|
|
977
|
+
{labels?.[key] ?? key}
|
|
978
|
+
</Text>
|
|
979
|
+
</View>
|
|
980
|
+
))}
|
|
981
|
+
</View>
|
|
982
|
+
);
|
|
983
|
+
}
|
|
984
|
+
AreaChartLegend.displayName = 'AreaChart.Legend';
|
|
985
|
+
AreaChartLegend.layer = 'overlay' as Layer;
|
|
986
|
+
|
|
987
|
+
export const AreaChart = Object.assign(AreaChartRoot, {
|
|
988
|
+
Grid: AreaChartGrid,
|
|
989
|
+
Area: AreaChartArea,
|
|
990
|
+
XAxis: AreaChartXAxis,
|
|
991
|
+
YAxis: AreaChartYAxis,
|
|
992
|
+
Tooltip: AreaChartTooltip,
|
|
993
|
+
Legend: AreaChartLegend,
|
|
994
|
+
});
|