1- import React , { useMemo , useState } from 'react' ;
2- import { View , StyleSheet , TouchableOpacity , Text , Switch } from 'react-native' ;
3- import Chart , { ChartProps , Dataset } from 'react-native-d3-chart' ;
1+ import React , { useMemo , useState } from 'react'
2+ import { View , StyleSheet , TouchableOpacity , Text , Switch } from 'react-native'
43
5- type TimeDomainType = 'hour' | 'day' | 'week' | 'month' ;
4+ import Chart , { ChartProps , Dataset } from 'react-native-d3-chart'
65
7- const TIME_DOMAIN_TYPES : TimeDomainType [ ] = [ 'hour' , 'day' , 'week' , 'month' ] ;
6+ type TimeDomainType = 'hour' | 'day' | 'week' | 'month'
7+
8+ const TIME_DOMAIN_TYPES : TimeDomainType [ ] = [ 'hour' , 'day' , 'week' , 'month' ]
89
910const chartColors : ChartProps [ 'colors' ] = {
1011 background : '#fff' ,
@@ -13,42 +14,42 @@ const chartColors: ChartProps['colors'] = {
1314 cursorStroke : '#0ff' ,
1415 highlightLabel : '#000' ,
1516 highlightTime : '#444' ,
16- } ;
17+ }
1718
1819// Generate data points every minute from a month ago to now
1920const generateDataPoints = ( {
2021 startingValue = 400 ,
2122 minimum = 0 ,
2223 radomFactor = 20 ,
2324} = { } ) => {
24- const points = [ ] ;
25- const now = Date . now ( ) ;
26- const monthAgo = now - 30 * 24 * 60 * 60 * 1000 ; // 30 days ago
27- let value = startingValue ;
25+ const points = [ ]
26+ const now = Date . now ( )
27+ const monthAgo = now - 30 * 24 * 60 * 60 * 1000 // 30 days ago
28+ let value = startingValue
2829
2930 for ( let timestamp = monthAgo ; timestamp <= now ; timestamp += 60 * 1000 ) {
30- const randomVariation = ( Math . random ( ) - 0.5 ) * radomFactor ;
31- value += randomVariation ;
31+ const randomVariation = ( Math . random ( ) - 0.5 ) * radomFactor
32+ value += randomVariation
3233
3334 // randomVariation was negative and value went below minimum
3435 if ( value < minimum ) {
3536 // invert direction to keep above minimum
36- value -= 2 * randomVariation ;
37+ value -= 2 * randomVariation
3738 }
3839
39- points . push ( { timestamp, value } ) ;
40+ points . push ( { timestamp, value } )
4041 }
4142
42- return points ;
43- } ;
43+ return points
44+ }
4445
4546enum Measurement {
4647 Red = 'Red' ,
4748 Blue = 'Blue' ,
4849 Green = 'Green' ,
4950 Pink = 'Pink' ,
5051}
51- const measurementKeys = Object . values ( Measurement ) ;
52+ const measurementKeys = Object . values ( Measurement )
5253const measurementsRecords : Record < Measurement , Dataset > = {
5354 [ Measurement . Red ] : {
5455 unit : '°C' ,
@@ -85,41 +86,41 @@ const measurementsRecords: Record<Measurement, Dataset> = {
8586 color : '#e0e' ,
8687 measurementName : Measurement . Pink ,
8788 } ,
88- } ;
89+ }
8990
9091export default function App ( ) {
91- const [ width , setWidth ] = useState < number > ( 0 ) ;
92- const height = width * 1.1 ;
93- const [ timeDomainType , setTimeDomainType ] = useState < TimeDomainType > ( 'hour' ) ;
92+ const [ width , setWidth ] = useState < number > ( 0 )
93+ const height = width * 1.1
94+ const [ timeDomainType , setTimeDomainType ] = useState < TimeDomainType > ( 'hour' )
9495 const timeDomain = useMemo ( ( ) => {
95- const now = new Date ( ) . valueOf ( ) ;
96- var hours = 1 ;
96+ const now = new Date ( ) . valueOf ( )
97+ let hours = 1
9798 if ( timeDomainType !== 'hour' ) {
98- hours *= 24 ;
99+ hours *= 24
99100
100101 if ( timeDomainType === 'week' ) {
101- hours *= 7 ;
102+ hours *= 7
102103 }
103104
104105 if ( timeDomainType === 'month' ) {
105- hours *= 30 ;
106+ hours *= 30
106107 }
107108 }
108109
109- const start = now - hours * 60 * 60 * 1000 ;
110- const end = now ;
110+ const start = now - hours * 60 * 60 * 1000
111+ const end = now
111112
112- return { start, end, type : timeDomainType } ;
113- } , [ timeDomainType ] ) ;
113+ return { start, end, type : timeDomainType }
114+ } , [ timeDomainType ] )
114115
115116 const [ enabledMeasurements , setEnabledMeasurements ] = useState < Measurement [ ] > (
116117 [ Measurement . Red ]
117- ) ;
118+ )
118119
119120 const datasets = useMemo < Dataset [ ] > (
120121 ( ) => enabledMeasurements . map ( ( m ) => measurementsRecords [ m ] ) ,
121122 [ enabledMeasurements ]
122- ) ;
123+ )
123124
124125 return (
125126 < View
@@ -153,52 +154,39 @@ export default function App() {
153154 </ View >
154155 { /* Measurement toggles */ }
155156 { measurementKeys . map ( ( measurement ) => (
156- < View
157- key = { measurement }
158- style = { {
159- flexDirection : 'row' ,
160- alignItems : 'center' ,
161- marginVertical : 10 ,
162- } }
163- >
157+ < View key = { measurement } style = { styles . switchContainer } >
164158 < Switch
165159 value = { enabledMeasurements . includes ( measurement ) }
166160 onValueChange = { ( ) =>
167161 setEnabledMeasurements ( ( prev ) => {
168162 if ( ! prev . includes ( measurement ) )
169163 // enable
170- return prev . concat ( measurement ) ;
164+ return prev . concat ( measurement )
171165
172166 if ( prev . length !== 1 )
173167 // disable
174- return prev . filter ( ( m ) => m !== measurement ) ;
168+ return prev . filter ( ( m ) => m !== measurement )
175169
176170 // only one measurement was enabled, switch to next one
177171 const currentIndex = measurementKeys . findIndex (
178172 ( m ) => m === measurement
179- ) ;
180- const nextIndex = ( currentIndex + 1 ) % measurementKeys . length ;
181- const nextMeasurement = measurementKeys [ nextIndex ] ! ;
173+ )
174+ const nextIndex = ( currentIndex + 1 ) % measurementKeys . length
175+ const nextMeasurement = measurementKeys [ nextIndex ] !
182176
183- return [ nextMeasurement ] ;
177+ return [ nextMeasurement ]
184178 } )
185179 }
186180 />
187- < Text style = { { marginLeft : 10 } } > { measurement } </ Text >
181+ < Text style = { styles . switchLabel } > { measurement } </ Text >
188182 </ View >
189183 ) ) }
190184 </ View >
191- ) ;
185+ )
192186}
193187
194- const PADDING = 20 ;
188+ const PADDING = 20
195189const styles = StyleSheet . create ( {
196- container : {
197- flex : 1 ,
198- alignItems : 'center' ,
199- justifyContent : 'center' ,
200- backgroundColor : '#eee' ,
201- } ,
202190 holder : {
203191 width : '100%' ,
204192 flex : 1 ,
@@ -226,4 +214,10 @@ const styles = StyleSheet.create({
226214 timeDomainItemText : {
227215 fontSize : 13 ,
228216 } ,
229- } ) ;
217+ switchContainer : {
218+ marginVertical : 10 ,
219+ flexDirection : 'row' ,
220+ alignItems : 'center' ,
221+ } ,
222+ switchLabel : { marginLeft : 10 } ,
223+ } )
0 commit comments