|
1 | | -import { View, StyleSheet } from 'react-native'; |
| 1 | +import React, { useMemo, useState } from 'react'; |
| 2 | +import { View, StyleSheet, TouchableOpacity, Text } from 'react-native'; |
| 3 | +import Chart, { ChartProps, Dataset } from '../../src/index'; |
| 4 | + |
| 5 | +type TimeDomainType = 'hour' | 'day' | 'week' | 'month'; |
| 6 | + |
| 7 | +const TIME_DOMAIN_TYPES: TimeDomainType[] = ['hour', 'day', 'week', 'month']; |
| 8 | + |
| 9 | +const chartColors: ChartProps['colors'] = { |
| 10 | + background: '#fff', |
| 11 | + highlightLine: '#000', |
| 12 | + border: '#555', |
| 13 | + cursorStroke: '#0ff', |
| 14 | + highlightLabel: '#000', |
| 15 | + highlightTime: '#ff0', |
| 16 | +}; |
| 17 | + |
| 18 | +const datasets: Dataset[] = [ |
| 19 | + { |
| 20 | + unit: 'units', |
| 21 | + points: [], |
| 22 | + decimals: 0, |
| 23 | + color: '#e66', |
| 24 | + measurementName: 'Red', |
| 25 | + }, |
| 26 | +]; |
2 | 27 |
|
3 | 28 | export default function App() { |
| 29 | + const [width, setWidth] = useState<number>(0); |
| 30 | + const height = width * 0.8; |
| 31 | + const [timeDomainType, setTimeDomainType] = useState<TimeDomainType>('hour'); |
| 32 | + const timeDomain = useMemo(() => { |
| 33 | + const now = new Date().valueOf(); |
| 34 | + var hours = 1; |
| 35 | + if (timeDomainType !== 'hour') { |
| 36 | + hours *= 24; |
| 37 | + |
| 38 | + if (timeDomainType === 'week') { |
| 39 | + hours *= 7; |
| 40 | + } |
| 41 | + |
| 42 | + if (timeDomainType === 'month') { |
| 43 | + hours *= 30; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + const start = now - hours * 60 * 60 * 1000; |
| 48 | + const end = now; |
| 49 | + |
| 50 | + return { start, end, type: timeDomainType }; |
| 51 | + }, [timeDomainType]); |
| 52 | + |
4 | 53 | return ( |
5 | | - <View style={styles.container}> |
6 | | - {/* <D3ChartView color="#32a852" style={styles.box} /> */} |
| 54 | + <View |
| 55 | + style={styles.holder} |
| 56 | + onLayout={(e) => setWidth(e.nativeEvent.layout.width)} |
| 57 | + > |
| 58 | + <Chart |
| 59 | + width={width} |
| 60 | + height={height} |
| 61 | + datasets={datasets} |
| 62 | + colors={chartColors} |
| 63 | + timeDomain={timeDomain} |
| 64 | + marginHorizontal={PADDING} |
| 65 | + /> |
| 66 | + <View style={{ height: 10 }} /> |
| 67 | + <View style={styles.timeDomainRow}> |
| 68 | + {TIME_DOMAIN_TYPES.map((type) => ( |
| 69 | + <TouchableOpacity |
| 70 | + key={type} |
| 71 | + style={[ |
| 72 | + styles.timeDomainItem, |
| 73 | + type === timeDomainType && { |
| 74 | + backgroundColor: '#f0f', |
| 75 | + }, |
| 76 | + ]} |
| 77 | + onPress={() => setTimeDomainType(type)} |
| 78 | + > |
| 79 | + <Text style={styles.timeDomainItemText}>{type}</Text> |
| 80 | + </TouchableOpacity> |
| 81 | + ))} |
| 82 | + </View> |
7 | 83 | </View> |
8 | 84 | ); |
9 | 85 | } |
10 | 86 |
|
| 87 | +const PADDING = 20; |
11 | 88 | const styles = StyleSheet.create({ |
12 | 89 | container: { |
13 | 90 | flex: 1, |
14 | 91 | alignItems: 'center', |
15 | 92 | justifyContent: 'center', |
| 93 | + backgroundColor: '#eee', |
| 94 | + }, |
| 95 | + holder: { |
| 96 | + width: '100%', |
| 97 | + borderRadius: 10, |
| 98 | + paddingVertical: PADDING, |
| 99 | + backgroundColor: '#fff', |
| 100 | + }, |
| 101 | + timeDomainRow: { |
| 102 | + flexDirection: 'row', |
| 103 | + paddingHorizontal: PADDING, |
| 104 | + }, |
| 105 | + timeDomainItem: { |
| 106 | + flex: 1, |
| 107 | + borderRadius: 11, |
| 108 | + paddingVertical: 2, |
| 109 | + justifyContent: 'center', |
| 110 | + alignItems: 'center', |
16 | 111 | }, |
17 | | - box: { |
18 | | - width: 60, |
19 | | - height: 60, |
20 | | - marginVertical: 20, |
| 112 | + timeDomainItemText: { |
| 113 | + fontSize: 13, |
21 | 114 | }, |
22 | 115 | }); |
0 commit comments