|
| 1 | +import { Line, MultiLineChart } from "@codeherence/react-native-graph"; |
| 2 | +import { Circle, Group } from "@shopify/react-native-skia"; |
| 3 | +import * as Haptics from "expo-haptics"; |
| 4 | +import { ScrollView, StyleSheet } from "react-native"; |
| 5 | +import { runOnJS, useDerivedValue, useSharedValue, withTiming } from "react-native-reanimated"; |
| 6 | +import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 7 | + |
| 8 | +const gestureStartImpact = () => { |
| 9 | + Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium); |
| 10 | +}; |
| 11 | + |
| 12 | +export default () => { |
| 13 | + const { bottom, left, right } = useSafeAreaInsets(); |
| 14 | + |
| 15 | + const cursorShown = useSharedValue(false); |
| 16 | + const opacity = useDerivedValue(() => { |
| 17 | + return withTiming(cursorShown.value ? 1 : 0, { duration: 200 }); |
| 18 | + }); |
| 19 | + |
| 20 | + const msftX = useSharedValue(0); |
| 21 | + const msftY = useSharedValue(0); |
| 22 | + |
| 23 | + return ( |
| 24 | + <ScrollView |
| 25 | + style={styles.container} |
| 26 | + contentContainerStyle={[ |
| 27 | + styles.contentContainer, |
| 28 | + { |
| 29 | + paddingBottom: bottom, |
| 30 | + paddingLeft: left, |
| 31 | + paddingRight: right, |
| 32 | + }, |
| 33 | + ]} |
| 34 | + showsVerticalScrollIndicator={false} |
| 35 | + > |
| 36 | + <MultiLineChart |
| 37 | + isStatic={false} |
| 38 | + points={{ msft: [[1000, 0]] }} |
| 39 | + style={styles.chart} |
| 40 | + ExtraCanvasElements={ |
| 41 | + <> |
| 42 | + <Group opacity={opacity}> |
| 43 | + <Circle cx={msftX} cy={msftY} r={4} color="purple" /> |
| 44 | + </Group> |
| 45 | + </> |
| 46 | + } |
| 47 | + onPanGestureBegin={(payload) => { |
| 48 | + "worklet"; |
| 49 | + cursorShown.value = true; |
| 50 | + msftX.value = payload.points.msft.x; |
| 51 | + msftY.value = payload.points.msft.y; |
| 52 | + runOnJS(gestureStartImpact)(); |
| 53 | + }} |
| 54 | + onPanGestureEnd={() => { |
| 55 | + "worklet"; |
| 56 | + cursorShown.value = false; |
| 57 | + }} |
| 58 | + onPanGestureChange={(payload) => { |
| 59 | + "worklet"; |
| 60 | + msftX.value = payload.points.msft.x; |
| 61 | + msftY.value = payload.points.msft.y; |
| 62 | + console.log({ payload: payload.points.msft }); |
| 63 | + }} |
| 64 | + > |
| 65 | + {(args) => ( |
| 66 | + <> |
| 67 | + <Line points={args.points.msft} strokeWidth={1} color="purple" /> |
| 68 | + </> |
| 69 | + )} |
| 70 | + </MultiLineChart> |
| 71 | + </ScrollView> |
| 72 | + ); |
| 73 | +}; |
| 74 | + |
| 75 | +const styles = StyleSheet.create({ |
| 76 | + container: { flex: 1 }, |
| 77 | + contentContainer: { flexGrow: 1 }, |
| 78 | + chart: { flex: 1, maxHeight: 200 }, |
| 79 | + price: { fontSize: 32 }, |
| 80 | + buttonContainer: { |
| 81 | + flexDirection: "row", |
| 82 | + justifyContent: "center", |
| 83 | + paddingVertical: 12, |
| 84 | + }, |
| 85 | + toggleBtn: { |
| 86 | + padding: 12, |
| 87 | + backgroundColor: "blue", |
| 88 | + borderRadius: 12, |
| 89 | + }, |
| 90 | + buttonText: { |
| 91 | + color: "white", |
| 92 | + fontSize: 16, |
| 93 | + }, |
| 94 | +}); |
0 commit comments