@react-native-ohos/victory-native-xl 41.1.1

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 ADDED
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright © 2024 <copyright holders>
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # @react-native-ohos/victory-native-xl
2
+
3
+ 本项目基于 [victory-native-xl](https://github.com/FormidableLabs/victory-native-xl)
4
+
5
+ ## 文档地址 / Documentation URL
6
+
7
+ [中文 / Chinese](https://gitcode.com/CPF-RN/usage-docs/blob/master/zh-cn/victory-native-xl.md)
8
+
9
+ [英文 / English](https://gitcode.com/CPF-RN/usage-docs/blob/master/zh-en/victory-native-xl.md)
10
+
11
+ ## 请悉知 / Acknowledgements
12
+
13
+ 本项目基于 [The MIT License (MIT)](https://www.mit-license.org/) ,请自由地享受和参与开源。
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@react-native-ohos/victory-native-xl",
3
+ "version": "41.1.1",
4
+ "private": false,
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/react-native-oh-library/victory-native-xl"
9
+ },
10
+ "harmony": {
11
+ "alias": "victory-native"
12
+ },
13
+ "main": "src/index.ts",
14
+ "files": [
15
+ "src"
16
+ ],
17
+ "scripts": {
18
+ "build": "rm -rf dist && tsc -p ./tsconfig.build.json",
19
+ "dev": "tsc --watch",
20
+ "typecheck": "tsc --noEmit",
21
+ "test": "vitest run",
22
+ "test:watch": "vitest"
23
+ },
24
+ "dependencies": {
25
+ "d3-scale": "^4.0.2",
26
+ "d3-shape": "^3.2.0",
27
+ "react-fast-compare": "^3.2.2",
28
+ "victory-native": "41.1.0",
29
+ "its-fine": "^1.2.5"
30
+ },
31
+ "peerDependencies": {
32
+ "react": "*",
33
+ "react-native": "*",
34
+ "@shopify/react-native-skia": ">=1.2.3",
35
+ "react-native-reanimated": ">=3.0.0",
36
+ "react-native-gesture-handler": ">=2.0.0"
37
+ },
38
+ "devDependencies": {
39
+ "@types/d3-scale": "^4.0.3",
40
+ "@types/d3-shape": "^3.1.1",
41
+ "@types/react": "~18.2.14",
42
+ "typescript": "^5.1.6",
43
+ "vitest": "^0.34.2"
44
+ },
45
+ "publishConfig": {
46
+ "provenance": true
47
+ }
48
+ }
@@ -0,0 +1,405 @@
1
+ import * as React from "react";
2
+ import { type LayoutChangeEvent, Button, View } from "react-native";
3
+ import { Canvas, Group, rect, Path } from "@shopify/react-native-skia";
4
+ import { useSharedValue } from "react-native-reanimated";
5
+ import {
6
+ Gesture,
7
+ GestureDetector,
8
+ GestureHandlerRootView,
9
+ type TouchData,
10
+ } from "react-native-gesture-handler";
11
+ import type {
12
+ AxisProps,
13
+ CartesianChartRenderArg,
14
+ InputFields,
15
+ NumericalFields,
16
+ SidedNumber,
17
+ TransformedData,
18
+ ChartBounds,
19
+ } from "victory-native/src/types";
20
+ import { transformInputData } from "victory-native/src/cartesian/utils/transformInputData";
21
+ import { findClosestPoint } from "victory-native/src/utils/findClosestPoint";
22
+ import { valueFromSidedNumber } from "victory-native/src/utils/valueFromSidedNumber";
23
+ import {
24
+ CartesianAxis,
25
+ CartesianAxisDefaultProps,
26
+ } from "victory-native/src/cartesian/components/CartesianAxis";
27
+ import { asNumber } from "victory-native/src/utils/asNumber";
28
+ import type { ChartPressState } from "victory-native/src/cartesian/hooks/useChartPressState";
29
+ import { useFunctionRef } from "victory-native/src/hooks/useFunctionRef";
30
+ import { CartesianChartProvider } from "victory-native/src/cartesian/contexts/CartesianChartContext";
31
+
32
+ type CartesianChartProps<
33
+ RawData extends Record<string, unknown>,
34
+ XK extends keyof InputFields<RawData>,
35
+ YK extends keyof NumericalFields<RawData>,
36
+ > = {
37
+ data: RawData[];
38
+ xKey: XK;
39
+ yKeys: YK[];
40
+ padding?: SidedNumber;
41
+ domainPadding?: SidedNumber;
42
+ domain?: { x?: [number] | [number, number]; y?: [number] | [number, number] };
43
+ chartPressState?:
44
+ | ChartPressState<{ x: InputFields<RawData>[XK]; y: Record<YK, number> }>
45
+ | ChartPressState<{ x: InputFields<RawData>[XK]; y: Record<YK, number> }>[];
46
+ children: (args: CartesianChartRenderArg<RawData, YK>) => React.ReactNode;
47
+ renderOutside?: (
48
+ args: CartesianChartRenderArg<RawData, YK>,
49
+ ) => React.ReactNode;
50
+ axisOptions?: Partial<Omit<AxisProps<RawData, XK, YK>, "xScale" | "yScale">>;
51
+ onChartBoundsChange?: (bounds: ChartBounds) => void;
52
+ gestureLongPressDelay?: number;
53
+ };
54
+
55
+ export function CartesianChart<
56
+ RawData extends Record<string, unknown>,
57
+ XK extends keyof InputFields<RawData>,
58
+ YK extends keyof NumericalFields<RawData>,
59
+ >({
60
+ data,
61
+ xKey,
62
+ yKeys,
63
+ padding,
64
+ domainPadding,
65
+ children,
66
+ renderOutside = () => null,
67
+ axisOptions,
68
+ domain,
69
+ chartPressState,
70
+ onChartBoundsChange,
71
+ gestureLongPressDelay = 100,
72
+ }: CartesianChartProps<RawData, XK, YK>) {
73
+ const [size, setSize] = React.useState({ width: 0, height: 0 });
74
+ const [hasMeasuredLayoutSize, setHasMeasuredLayoutSize] =
75
+ React.useState(false);
76
+ const onLayout = React.useCallback(
77
+ ({ nativeEvent: { layout } }: LayoutChangeEvent) => {
78
+ setHasMeasuredLayoutSize(true);
79
+ setSize(layout);
80
+ },
81
+ [],
82
+ );
83
+ const tData = useSharedValue<TransformedData<RawData, XK, YK>>({
84
+ ix: [],
85
+ ox: [],
86
+ y: yKeys.reduce(
87
+ (acc, key) => {
88
+ acc[key] = { i: [], o: [] };
89
+ return acc;
90
+ },
91
+ {} as TransformedData<RawData, XK, YK>["y"],
92
+ ),
93
+ });
94
+
95
+ const {
96
+ xTicksNormalized,
97
+ yTicksNormalized,
98
+ xScale,
99
+ yScale,
100
+ chartBounds,
101
+ isNumericalData,
102
+ _tData,
103
+ } = React.useMemo(() => {
104
+ const {
105
+ xScale,
106
+ yScale,
107
+ isNumericalData,
108
+ xTicksNormalized,
109
+ yTicksNormalized,
110
+ ..._tData
111
+ } = transformInputData({
112
+ data,
113
+ xKey,
114
+ yKeys,
115
+ axisOptions: axisOptions
116
+ ? Object.assign({}, CartesianAxisDefaultProps, axisOptions)
117
+ : undefined,
118
+ outputWindow: {
119
+ xMin: valueFromSidedNumber(padding, "left"),
120
+ xMax: size.width - valueFromSidedNumber(padding, "right"),
121
+ yMin: valueFromSidedNumber(padding, "top"),
122
+ yMax: size.height - valueFromSidedNumber(padding, "bottom"),
123
+ },
124
+ domain,
125
+ domainPadding,
126
+ });
127
+ tData.value = _tData;
128
+
129
+ const chartBounds = {
130
+ left: xScale(xScale.domain().at(0) || 0),
131
+ right: xScale(xScale.domain().at(-1) || 0),
132
+ top: yScale(yScale.domain().at(0) || 0),
133
+ bottom: yScale(yScale.domain().at(-1) || 0),
134
+ };
135
+
136
+ return {
137
+ xTicksNormalized,
138
+ yTicksNormalized,
139
+ tData,
140
+ xScale,
141
+ yScale,
142
+ chartBounds,
143
+ isNumericalData,
144
+ _tData,
145
+ };
146
+ }, [
147
+ data,
148
+ xKey,
149
+ yKeys,
150
+ axisOptions,
151
+ padding,
152
+ size.width,
153
+ size.height,
154
+ domain,
155
+ domainPadding,
156
+ tData,
157
+ ]);
158
+
159
+ /**
160
+ * Pan gesture handling
161
+ */
162
+ const lastIdx = useSharedValue(null as null | number);
163
+ /**
164
+ * Take a "press value" and an x-value and update the shared values accordingly.
165
+ */
166
+ const handleTouch = (
167
+ v: ChartPressState<{ x: InputFields<RawData>[XK]; y: Record<YK, number> }>,
168
+ x: number,
169
+ ) => {
170
+ "worklet";
171
+ const idx = findClosestPoint(tData.value.ox, x);
172
+ if (typeof idx !== "number") return;
173
+
174
+ const isInYs = (yk: string): yk is YK & string => yKeys.includes(yk as YK);
175
+ // Shared value
176
+ if (v) {
177
+ try {
178
+ v.x.value.value = tData.value.ix[idx]!;
179
+ v.x.position.value = asNumber(tData.value.ox[idx]);
180
+ for (const yk in v.y) {
181
+ if (isInYs(yk)) {
182
+ v.y[yk].value.value = asNumber(tData.value.y[yk].i[idx]);
183
+ v.y[yk].position.value = asNumber(tData.value.y[yk].o[idx]);
184
+ }
185
+ }
186
+ } catch (err) {
187
+ // no-op
188
+ }
189
+ }
190
+
191
+ lastIdx.value = idx;
192
+ };
193
+
194
+ /**
195
+ * Touch gesture is a modified Pan gesture handler that allows for multiple presses:
196
+ * - Using Pan Gesture handler effectively _just_ for the .activateAfterLongPress functionality.
197
+ * - Tracking the finger is handled with .onTouchesMove instead of .onUpdate, because
198
+ * .onTouchesMove gives us access to each individual finger.
199
+ * - The activation gets a bit complicated because we want to wait til "start" state before updating Press Value
200
+ * which gives time for the gesture to get cancelled before we start updating the shared values.
201
+ * Therefore we use gestureState.bootstrap to store some "bootstrap" information if gesture isn't active when finger goes down.
202
+ */
203
+ // touch ID -> value index mapping to keep track of which finger updates which value
204
+ const touchMap = useSharedValue({} as Record<number, number | undefined>);
205
+ const activePressSharedValues = Array.isArray(chartPressState)
206
+ ? chartPressState
207
+ : [chartPressState];
208
+ const gestureState = useSharedValue({
209
+ isGestureActive: false,
210
+ bootstrap: [] as [
211
+ ChartPressState<{ x: InputFields<RawData>[XK]; y: Record<YK, number> }>,
212
+ TouchData,
213
+ ][],
214
+ });
215
+
216
+ const panGesture = Gesture.Pan()
217
+ /**
218
+ * When a finger goes down, either update the state or store in the bootstrap array.
219
+ */
220
+ .onTouchesDown((e) => {
221
+ const vals = activePressSharedValues || [];
222
+ if (!vals.length || e.numberOfTouches === 0) return;
223
+
224
+ for (let i = 0; i < Math.min(e.allTouches.length, vals.length); i++) {
225
+ const touch = e.allTouches[i];
226
+ const v = vals[i];
227
+ if (!v || !touch) continue;
228
+
229
+ if (gestureState.value.isGestureActive) {
230
+ // Update the mapping
231
+ if (typeof touchMap.value[touch.id] !== "number")
232
+ touchMap.value[touch.id] = i;
233
+
234
+ v.isActive.value = true;
235
+ handleTouch(v, touch.x);
236
+ } else {
237
+ gestureState.value.bootstrap.push([v, touch]);
238
+ }
239
+ }
240
+ })
241
+ /**
242
+ * On start, check if we have any bootstraped updates we need to apply.
243
+ */
244
+ .onStart(() => {
245
+ gestureState.value.isGestureActive = true;
246
+
247
+ for (let i = 0; i < gestureState.value.bootstrap.length; i++) {
248
+ const [v, touch] = gestureState.value.bootstrap[i]!;
249
+ // Update the mapping
250
+ if (typeof touchMap.value[touch.id] !== "number")
251
+ touchMap.value[touch.id] = i;
252
+
253
+ v.isActive.value = true;
254
+ handleTouch(v, touch.x);
255
+ }
256
+ })
257
+ /**
258
+ * Clear gesture state on gesture end.
259
+ */
260
+ .onFinalize(() => {
261
+ gestureState.value.isGestureActive = false;
262
+ gestureState.value.bootstrap = [];
263
+ })
264
+ /**
265
+ * As fingers move, update the shared values accordingly.
266
+ */
267
+ .onTouchesMove((e) => {
268
+ const vals = activePressSharedValues || [];
269
+ if (!vals.length || e.numberOfTouches === 0) return;
270
+
271
+ for (let i = 0; i < Math.min(e.allTouches.length, vals.length); i++) {
272
+ const touch = e.allTouches[i];
273
+ const touchId = touch?.id;
274
+ const idx = typeof touchId === "number" && touchMap.value[touchId];
275
+ const v = typeof idx === "number" && vals?.[idx];
276
+
277
+ if (!v || !touch) continue;
278
+ if (!v.isActive.value) v.isActive.value = true;
279
+ handleTouch(v, touch.x);
280
+ }
281
+ })
282
+ /**
283
+ * On each finger up, start to update values and "free up" the touch map.
284
+ */
285
+ .onTouchesUp((e) => {
286
+ for (const touch of e.changedTouches) {
287
+ const vals = activePressSharedValues || [];
288
+
289
+ // Set active state to false
290
+ const touchId = touch?.id;
291
+ const idx = typeof touchId === "number" && touchMap.value[touchId];
292
+ const val = typeof idx === "number" && vals[idx];
293
+ if (val) {
294
+ val.isActive.value = false;
295
+ }
296
+
297
+ // Free up touch map for this touch
298
+ touchMap.value[touch.id] = undefined;
299
+ }
300
+ })
301
+ /**
302
+ * Once the gesture ends, ensure all active values are falsified.
303
+ */
304
+ .onEnd(() => {
305
+ const vals = activePressSharedValues || [];
306
+ // Set active state to false for all vals
307
+ for (const val of vals) {
308
+ if (val) {
309
+ val.isActive.value = false;
310
+ }
311
+ }
312
+ })
313
+ /**
314
+ * Activate after a long press, which helps with preventing all touch hijacking.
315
+ * This is important if this chart is inside of some sort of scrollable container.
316
+ */
317
+ .activateAfterLongPress(gestureLongPressDelay);
318
+
319
+ /**
320
+ * Allow end-user to request "raw-ish" data for a given yKey.
321
+ * Generate this on demand using a proxy.
322
+ */
323
+ type PointsArg = CartesianChartRenderArg<RawData, YK>["points"];
324
+ const points = React.useMemo<PointsArg>(() => {
325
+ const cache = {} as Record<YK, PointsArg[keyof PointsArg]>;
326
+ return new Proxy(
327
+ {},
328
+ {
329
+ get(_, property: string): PointsArg[keyof PointsArg] | undefined {
330
+ const key = property as YK;
331
+ if (!yKeys.includes(key)) return undefined;
332
+ if (cache[key]) return cache[key];
333
+
334
+ cache[key] = _tData.ix.map((x, i) => ({
335
+ x: asNumber(_tData.ox[i]),
336
+ xValue: x,
337
+ y: _tData.y[key].o[i],
338
+ yValue: _tData.y[key].i[i],
339
+ }));
340
+
341
+ return cache[key];
342
+ },
343
+ },
344
+ ) as PointsArg;
345
+ }, [_tData, yKeys]);
346
+
347
+ // On bounds change, emit
348
+ const onChartBoundsRef = useFunctionRef(onChartBoundsChange);
349
+ React.useEffect(() => {
350
+ onChartBoundsRef.current?.(chartBounds);
351
+ }, [chartBounds, onChartBoundsRef]);
352
+
353
+ const renderArg: CartesianChartRenderArg<RawData, YK> = {
354
+ xScale,
355
+ yScale,
356
+ chartBounds,
357
+ canvasSize: size,
358
+ points,
359
+ };
360
+ const clipRect = rect(
361
+ chartBounds.left,
362
+ chartBounds.top,
363
+ chartBounds.right - chartBounds.left,
364
+ chartBounds.bottom - chartBounds.top,
365
+ );
366
+ // Body of the chart.
367
+ const body = (
368
+ <View style={{ flex: 1 }}>
369
+ <Canvas style={{ flex: 1 }} onLayout={onLayout}>
370
+ {hasMeasuredLayoutSize && (
371
+ <>
372
+ {axisOptions ? (
373
+ <CartesianAxis
374
+ {...{
375
+ ...axisOptions,
376
+ xScale,
377
+ yScale,
378
+ isNumericalData,
379
+ xTicksNormalized,
380
+ yTicksNormalized,
381
+ ix: _tData.ix,
382
+ }}
383
+ />
384
+ ) : null}
385
+ </>
386
+ )}
387
+ <CartesianChartProvider yScale={yScale} xScale={xScale}>
388
+ <Group clip={clipRect}>
389
+ {hasMeasuredLayoutSize && children(renderArg)}
390
+ </Group>
391
+ </CartesianChartProvider>
392
+ {hasMeasuredLayoutSize && renderOutside?.(renderArg)}
393
+ </Canvas>
394
+ </View>
395
+ );
396
+
397
+ // Conditionally wrap the body in gesture handler based on activePressSharedValue
398
+ return chartPressState ? (
399
+ <GestureHandlerRootView style={{ flex: 1 }}>
400
+ <GestureDetector gesture={panGesture}>{body}</GestureDetector>
401
+ </GestureHandlerRootView>
402
+ ) : (
403
+ body
404
+ );
405
+ }
package/src/index.ts ADDED
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Cartesian chart exports (including useful types)
3
+ */
4
+ export { CartesianChart } from "./cartesian/CartesianChart";
5
+ export { PolarChart } from "./polar/PolarChart";
6
+
7
+
8
+ export { type CurveType } from "victory-native";
9
+ export { type RoundedCorners } from "victory-native";
10
+
11
+ export { useAnimatedPath } from "victory-native";
12
+ export { AnimatedPath } from "victory-native";
13
+ export { usePrevious } from "victory-native";
14
+ export {
15
+ useChartPressState,
16
+ type ChartPressState,
17
+ } from "victory-native";
18
+
19
+ // Line
20
+ export { useLinePath } from "victory-native";
21
+ export { Line } from "victory-native";
22
+
23
+ // Bar
24
+ export { useBarPath } from "victory-native";
25
+ export { Bar } from "victory-native";
26
+
27
+ // Bar group
28
+ export { useBarGroupPaths } from "victory-native";
29
+ export { BarGroup } from "victory-native";
30
+
31
+ // Area
32
+ export { useAreaPath } from "victory-native";
33
+ export { Area } from "victory-native";
34
+
35
+ // Scatter
36
+ export { Scatter, type ScatterShape } from "victory-native";
37
+
38
+ // Grid and Axis
39
+ export { CartesianAxis } from "victory-native";
40
+
41
+ /**
42
+ * Polar chart exports
43
+ */
44
+
45
+ /**
46
+ * Pie chart exports (including useful types)
47
+ */
48
+ export { Pie, type PieSliceData } from "victory-native";
49
+
50
+ export { useSlicePath } from "victory-native";
51
+ export { useSliceAngularInsetPath } from "victory-native";
@@ -0,0 +1,144 @@
1
+ import * as React from "react";
2
+
3
+ import { Canvas } from "@shopify/react-native-skia";
4
+ import { useContextBridge, FiberProvider } from "its-fine";
5
+ import {
6
+ StyleSheet,
7
+ View,
8
+ type ViewStyle,
9
+ type LayoutChangeEvent,
10
+ type StyleProp,
11
+ } from "react-native";
12
+ import {
13
+ PolarChartProvider,
14
+ usePolarChartContext,
15
+ } from "victory-native/src/polar/contexts/PolarChartContext";
16
+ import type {
17
+ ColorFields,
18
+ InputFields,
19
+ NumericalFields,
20
+ StringKeyOf,
21
+ } from "victory-native/src/types";
22
+
23
+ type PolarChartBaseProps = {
24
+ onLayout: ({ nativeEvent: { layout } }: LayoutChangeEvent) => void;
25
+ hasMeasuredLayoutSize: boolean;
26
+ canvasSize: { width: number; height: number };
27
+ containerStyle?: StyleProp<ViewStyle>;
28
+ canvasStyle?: StyleProp<ViewStyle>;
29
+ };
30
+
31
+ interface PolarChartContext {
32
+ data: Record<string, unknown>[];
33
+ canvasSize: { width: number; height: number };
34
+ labelKey: string;
35
+ valueKey: string;
36
+ colorKey: string;
37
+ }
38
+
39
+ const PolarChartBase = (
40
+ props: React.PropsWithChildren<PolarChartBaseProps>,
41
+ ) => {
42
+ const {
43
+ containerStyle,
44
+ canvasStyle,
45
+ children,
46
+ onLayout,
47
+ hasMeasuredLayoutSize,
48
+ canvasSize,
49
+ } = props;
50
+ const { width, height } = canvasSize;
51
+
52
+ const ctx = usePolarChartContext();
53
+ const ContextBridge = useContextBridge();
54
+ return (
55
+ <View style={[styles.baseContainer, containerStyle]}>
56
+ <Canvas
57
+ onLayout={onLayout}
58
+ style={StyleSheet.flatten([
59
+ styles.canvasContainer,
60
+ hasMeasuredLayoutSize ? { width, height } : null,
61
+ canvasStyle,
62
+ ])}
63
+ >
64
+ {/* https://shopify.github.io/react-native-skia/docs/canvas/contexts/
65
+ we have to re-inject our context to make it available in the skia renderer
66
+ */}
67
+ <ContextBridge {...ctx}>
68
+ {children}
69
+ </ContextBridge>
70
+ {/* <PolarChartProvider {...ctx} canvasSize={canvasSize}>
71
+ {children}
72
+ </PolarChartProvider> */}
73
+ </Canvas>
74
+ </View>
75
+ );
76
+ };
77
+
78
+ type PolarChartProps<
79
+ RawData extends Record<string, unknown>,
80
+ LabelKey extends StringKeyOf<InputFields<RawData>>,
81
+ ValueKey extends StringKeyOf<NumericalFields<RawData>>,
82
+ ColorKey extends StringKeyOf<ColorFields<RawData>>,
83
+ > = {
84
+ data: RawData[];
85
+ colorKey: ColorKey;
86
+ labelKey: LabelKey;
87
+ valueKey: ValueKey;
88
+ } & Omit<
89
+ PolarChartBaseProps,
90
+ "canvasSize" | "onLayout" | "hasMeasuredLayoutSize" // omit exposing internal props for calculating canvas layout/size
91
+ >;
92
+ export const PolarChart = <
93
+ RawData extends Record<string, unknown>,
94
+ LabelKey extends StringKeyOf<InputFields<RawData>>,
95
+ ValueKey extends StringKeyOf<NumericalFields<RawData>>,
96
+ ColorKey extends StringKeyOf<ColorFields<RawData>>,
97
+ >(
98
+ props: React.PropsWithChildren<
99
+ PolarChartProps<RawData, LabelKey, ValueKey, ColorKey>
100
+ >,
101
+ ) => {
102
+ const { data, labelKey, colorKey, valueKey } = props;
103
+
104
+ const [canvasSize, setCanvasSize] = React.useState({ width: 0, height: 0 });
105
+
106
+ const [hasMeasuredLayoutSize, setHasMeasuredLayoutSize] =
107
+ React.useState(false);
108
+
109
+ const onLayout = React.useCallback(
110
+ ({ nativeEvent: { layout } }: LayoutChangeEvent) => {
111
+ setHasMeasuredLayoutSize(true);
112
+ setCanvasSize(layout);
113
+ },
114
+ [],
115
+ );
116
+
117
+ return (
118
+ <FiberProvider>
119
+ <PolarChartProvider
120
+ data={data}
121
+ labelKey={labelKey.toString()}
122
+ colorKey={colorKey.toString()}
123
+ valueKey={valueKey.toString()}
124
+ canvasSize={canvasSize}
125
+ >
126
+ <PolarChartBase
127
+ {...props}
128
+ onLayout={onLayout}
129
+ hasMeasuredLayoutSize={hasMeasuredLayoutSize}
130
+ canvasSize={canvasSize}
131
+ />
132
+ </PolarChartProvider>
133
+ </FiberProvider>
134
+ );
135
+ };
136
+
137
+ const styles = StyleSheet.create({
138
+ baseContainer: {
139
+ flex: 1,
140
+ },
141
+ canvasContainer: {
142
+ flex: 1,
143
+ },
144
+ });