import { Canvas, Path } from "@shopify/react-native-skia"; import { useCallback, useMemo, useState } from "react"; import { LayoutChangeEvent, StyleSheet, View } from "react-native"; import { AxisLabelContainer } from "./AxisLabel"; import type { LineChartProps } from "./LineChart"; import { computePath, computeGraphData } from "./computations"; import { batchedUpdates } from "../../libs/batchedUpdates"; export const StaticLineChart: React.FC> = ({ points = [], precision = 2, strokeWidth = 2, curveType = "linear", TopAxisLabel = null, BottomAxisLabel = null, PathFill = null, // None of these properties are used in StaticLineChart ...viewProps }) => { const [width, setWidth] = useState(0); const [height, setHeight] = useState(0); // We separate the computation of the data from the rendering. This is so that these values are // not recomputed when the width or height of the chart changes, but only when the points change. const data = useMemo(() => computeGraphData(points), [points]); const path = useMemo(() => { return computePath({ ...data, width, height, cursorRadius: 0, curveType }); }, [data, width, height, curveType]); const onLayout = useCallback((e: LayoutChangeEvent) => { // Batch the updates to avoid unnecessary re-renders batchedUpdates(() => { setWidth(e.nativeEvent.layout.width); setHeight(e.nativeEvent.layout.height); }); }, []); return ( {TopAxisLabel && ( )} {PathFill && PathFill({ width, height, strokeWidth })} {BottomAxisLabel && ( )} ); }; StaticLineChart.displayName = "StaticLineChart"; const styles = StyleSheet.create({ root: { position: "relative", overflow: "hidden" }, container: { flex: 1 }, canvas: { flex: 1 }, });