|
| 1 | +import { type AxisLabelProps, LineChart } from "@codeherence/react-native-graph"; |
| 2 | +import { Paint } from "@shopify/react-native-skia"; |
| 3 | +import { useReducer } from "react"; |
| 4 | +import { Button, ScrollView, StyleSheet, Text } from "react-native"; |
| 5 | +import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 6 | + |
| 7 | +import { priceMap } from "../src/store/prices"; |
| 8 | + |
| 9 | +const formatter = new Intl.NumberFormat("en-US", { |
| 10 | + style: "currency", |
| 11 | + currency: "USD", |
| 12 | +}); |
| 13 | + |
| 14 | +const AxisLabel: React.FC<AxisLabelProps> = ({ value }) => ( |
| 15 | + <Text selectable={false}>{formatter.format(value)}</Text> |
| 16 | +); |
| 17 | + |
| 18 | +const priceMapKeys = Object.keys(priceMap) as (keyof typeof priceMap)[]; |
| 19 | + |
| 20 | +export default () => { |
| 21 | + const { bottom, left, right } = useSafeAreaInsets(); |
| 22 | + const [counter, increment] = useReducer((s: number) => s + 1, 0); |
| 23 | + |
| 24 | + const symbol = priceMapKeys[counter % priceMapKeys.length]!; |
| 25 | + const data = priceMap[symbol]; |
| 26 | + |
| 27 | + return ( |
| 28 | + <ScrollView |
| 29 | + style={styles.container} |
| 30 | + contentContainerStyle={[ |
| 31 | + styles.contentContainer, |
| 32 | + { |
| 33 | + paddingBottom: bottom, |
| 34 | + paddingLeft: left, |
| 35 | + paddingRight: right, |
| 36 | + }, |
| 37 | + ]} |
| 38 | + showsVerticalScrollIndicator={false} |
| 39 | + > |
| 40 | + <Button title={`Showing ${symbol}. Press to switch.`} onPress={increment} /> |
| 41 | + <LineChart |
| 42 | + isStatic |
| 43 | + points={data} |
| 44 | + style={styles.chart} |
| 45 | + TopAxisLabel={AxisLabel} |
| 46 | + BottomAxisLabel={AxisLabel} |
| 47 | + strokeWidth={2} |
| 48 | + PathFill={({ strokeWidth }) => ( |
| 49 | + <Paint style="stroke" strokeWidth={strokeWidth} color="lightblue" /> |
| 50 | + )} |
| 51 | + /> |
| 52 | + </ScrollView> |
| 53 | + ); |
| 54 | +}; |
| 55 | + |
| 56 | +const styles = StyleSheet.create({ |
| 57 | + container: { flex: 1 }, |
| 58 | + contentContainer: { flexGrow: 1 }, |
| 59 | + chart: { flex: 1, maxHeight: 300 }, |
| 60 | + price: { fontSize: 32 }, |
| 61 | +}); |
0 commit comments