|
| 1 | +import { |
| 2 | + type AxisLabelComponentProps, |
| 3 | + LineChart, |
| 4 | + type PanGestureHandlerOnChangeEventPayload, |
| 5 | + HoverGestureHandlerOnChangeEventPayload, |
| 6 | +} from "@codeherence/react-native-graph"; |
| 7 | +import { LinearGradient, vec } from "@shopify/react-native-skia"; |
| 8 | +import { useCallback, useMemo, useReducer } from "react"; |
| 9 | +import { Button, ScrollView, StyleSheet, Text } from "react-native"; |
| 10 | +import { useAnimatedProps, useSharedValue } from "react-native-reanimated"; |
| 11 | +import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 12 | + |
| 13 | +import AnimatedText from "../components/AnimatedText"; |
| 14 | +import aapl_prices from "../data/aapl_prices.json"; |
| 15 | +import msft_prices from "../data/msft_prices.json"; |
| 16 | +import nvda_prices from "../data/nvda_prices.json"; |
| 17 | +import unity_prices from "../data/unity_prices.json"; |
| 18 | + |
| 19 | +const formatter = new Intl.NumberFormat("en-US", { |
| 20 | + style: "currency", |
| 21 | + currency: "USD", |
| 22 | +}); |
| 23 | + |
| 24 | +const AxisLabel: React.FC<AxisLabelComponentProps> = ({ value }) => ( |
| 25 | + <Text selectable={false}>{formatter.format(value)}</Text> |
| 26 | +); |
| 27 | + |
| 28 | +const uiFormatter = (price: number) => { |
| 29 | + "worklet"; |
| 30 | + |
| 31 | + return new Intl.NumberFormat("en-US", { |
| 32 | + style: "currency", |
| 33 | + currency: "USD", |
| 34 | + }).format(price); |
| 35 | +}; |
| 36 | + |
| 37 | +const priceMap = { |
| 38 | + msft: msft_prices.results |
| 39 | + .reverse() |
| 40 | + .map<[number, number]>((r) => [new Date(r.date).getTime(), r.close]), |
| 41 | + aapl: aapl_prices.results |
| 42 | + .reverse() |
| 43 | + .map<[number, number]>((r) => [new Date(r.date).getTime(), r.close]), |
| 44 | + nvda: nvda_prices.results |
| 45 | + .reverse() |
| 46 | + .map<[number, number]>((r) => [new Date(r.date).getTime(), r.close]), |
| 47 | + unity: unity_prices.results |
| 48 | + .reverse() |
| 49 | + .map<[number, number]>((r) => [new Date(r.date).getTime(), r.close]), |
| 50 | +}; |
| 51 | + |
| 52 | +const priceMapKeys = Object.keys(priceMap) as (keyof typeof priceMap)[]; |
| 53 | + |
| 54 | +export default () => { |
| 55 | + const [counter, increment] = useReducer((s: number) => s + 1, 0); |
| 56 | + |
| 57 | + const { bottom, left, right } = useSafeAreaInsets(); |
| 58 | + |
| 59 | + const latestPrice = useSharedValue("0"); |
| 60 | + const symbol = priceMapKeys[counter % priceMapKeys.length]!; |
| 61 | + const data: [number, number][] = useMemo(() => { |
| 62 | + latestPrice.value = uiFormatter(priceMap[symbol][priceMap[symbol].length - 1]![1]); |
| 63 | + return priceMap[symbol]; |
| 64 | + }, [symbol]); |
| 65 | + |
| 66 | + const onHoverChangeWorklet = useCallback((evt: HoverGestureHandlerOnChangeEventPayload) => { |
| 67 | + "worklet"; |
| 68 | + latestPrice.value = uiFormatter(evt.point); |
| 69 | + }, []); |
| 70 | + |
| 71 | + const onGestureChangeWorklet = useCallback((evt: PanGestureHandlerOnChangeEventPayload) => { |
| 72 | + "worklet"; |
| 73 | + latestPrice.value = uiFormatter(evt.point); |
| 74 | + }, []); |
| 75 | + |
| 76 | + const onEndWorklet = useCallback(() => { |
| 77 | + "worklet"; |
| 78 | + latestPrice.value = uiFormatter(data[data.length - 1]![1]); |
| 79 | + }, [data]); |
| 80 | + |
| 81 | + const animatedProps = useAnimatedProps(() => { |
| 82 | + return { text: latestPrice.value }; |
| 83 | + }, []); |
| 84 | + |
| 85 | + return ( |
| 86 | + <ScrollView |
| 87 | + style={styles.container} |
| 88 | + contentContainerStyle={{ |
| 89 | + paddingBottom: bottom, |
| 90 | + paddingLeft: left, |
| 91 | + paddingRight: right, |
| 92 | + }} |
| 93 | + showsVerticalScrollIndicator={false} |
| 94 | + > |
| 95 | + <Button title={`Showing ${symbol}. Click to switch.`} onPress={increment} /> |
| 96 | + <AnimatedText style={styles.price} animatedProps={animatedProps} /> |
| 97 | + <LineChart |
| 98 | + points={data} |
| 99 | + style={styles.chart} |
| 100 | + TopAxisLabel={AxisLabel} |
| 101 | + BottomAxisLabel={AxisLabel} |
| 102 | + onPanGestureChange={onGestureChangeWorklet} |
| 103 | + onPanGestureEnd={onEndWorklet} |
| 104 | + onHoverGestureEnd={onEndWorklet} |
| 105 | + onHoverGestureChange={onHoverChangeWorklet} |
| 106 | + strokeWidth={2} |
| 107 | + PathFill={({ width }) => ( |
| 108 | + <LinearGradient |
| 109 | + start={vec(0, 0)} |
| 110 | + end={vec(width, 0)} |
| 111 | + colors={["blue", "red", "purple"]} |
| 112 | + /> |
| 113 | + )} |
| 114 | + /> |
| 115 | + </ScrollView> |
| 116 | + ); |
| 117 | +}; |
| 118 | + |
| 119 | +const styles = StyleSheet.create({ |
| 120 | + container: { flex: 1 }, |
| 121 | + chart: { flex: 1, minHeight: 400 }, |
| 122 | + price: { fontSize: 32 }, |
| 123 | +}); |
0 commit comments