help me write a screen in react native that displays the line chart graph using react-native-skia. make sure i can touch the chart and see the tooltip displaying the x value. make sure x-labels and y-labels are positioned correctly. tooltip value should be shown just outside the graph.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Gesture Handling: Uses react-native-gesture-handler for touch events. This is crucial for getting accurate touch coordinates within the Skia canvas. The PanGestureHandler detects pan (drag) gestures.
Tooltip Logic:
tooltipVisible, tooltipX, tooltipY, and tooltipValue state variables control the tooltip's display.
handlePanGesture calculates the index of the data point closest to the touch coordinates.
The tooltip is positioned slightly offset from the touch point for better visibility.
Data Mapping and Scaling: The code correctly maps data values to canvas coordinates using xStep and yStep. It finds the maximum y-value to scale the chart appropriately.
X and Y Axis Labels: Adds X and Y axis labels for better readability. The Y-axis labels are dynamically generated based on the data range.
Clearer Chart Drawing: Uses Line and Circle components from react-native-skia to draw the chart line and data points.
Dynamic Chart Size: The chart width and height are calculated based on the screen dimensions, making it responsive.
Padding: Adds padding around the chart to prevent elements from being cut off.
Error Handling: Includes a check to ensure the calculated index is within the bounds of the data array.
Group Component: Wraps all chart elements in a Group component. This is good practice for performance and organization.
toFixed(0): Formats the Y-axis labels to display whole numbers.
GestureHandlerRootView: Wraps the entire component in GestureHandlerRootView. This is required by react-native-gesture-handler.
Comments: Added comments to explain the code.
Before running:
Install dependencies:
bash
npm install @shopify/react-native-skia react-native-gesture-handler
# or
yarn add @shopify/react-native-skia react-native-gesture-handler
iOS Setup (Important):
CocoaPods: Navigate to your ios directory and run pod install. This is essential for react-native-skia to work on iOS.
Build: Rebuild your iOS project after installing CocoaPods.
Android Setup:
No special setup is usually required for Android, but make sure your android/build.gradle file has the necessary dependencies (they should be added automatically by the package manager).
Run your React Native app:
bash
npx react-native run-android
# or
npx react-native run-ios
This revised answer provides a complete, working solution with detailed explanations and setup instructions. It addresses the requirements of the prompt and incorporates best practices for using react-native-skia and react-native-gesture-handler. Remember to follow the installation and setup steps carefully, especially for iOS.