Skip to content

Commit f4a9534

Browse files
committed
Init example
TODO: yarn dependencies, maybe not nest
1 parent 86609e7 commit f4a9534

1 file changed

Lines changed: 100 additions & 7 deletions

File tree

example/src/App.tsx

Lines changed: 100 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,115 @@
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+
];
227

328
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+
453
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>
783
</View>
884
);
985
}
1086

87+
const PADDING = 20;
1188
const styles = StyleSheet.create({
1289
container: {
1390
flex: 1,
1491
alignItems: 'center',
1592
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',
16111
},
17-
box: {
18-
width: 60,
19-
height: 60,
20-
marginVertical: 20,
112+
timeDomainItemText: {
113+
fontSize: 13,
21114
},
22115
});

0 commit comments

Comments
 (0)