11import React , { useMemo , useState } from 'react'
22import { View , StyleSheet , TouchableOpacity , Text , Switch } from 'react-native'
33
4- import Chart , { ChartProps , Dataset , Point } from 'react-native-d3-chart'
4+ import Chart , { ChartProps , Dataset } from 'react-native-d3-chart'
55
6- import { generateRandomPulses } from './helpers/generateRandomPulses'
6+ import { buildSlices } from './helpers/buildSlices'
7+ import { generateTimeSeriesData } from './helpers/generateTimeSeriesData'
8+ import { temperatureData , visits } from './mockData'
79
810type TimeDomainType = 'hour' | 'day' | 'week' | 'month'
911
@@ -18,62 +20,20 @@ const chartColors: ChartProps['colors'] = {
1820 highlightTime : '#444' ,
1921}
2022
21- // Generate data points every minute from a month ago to now
22- const generateDataPoints = ( {
23- startingValue = 400 ,
24- minimum = 0 ,
25- maximum = 3000 ,
26- radomFactor = 20 ,
27- } = { } ) => {
28- const points = [ ]
29- const now = Date . now ( )
30- const monthAgo = now - 30 * 24 * 60 * 60 * 1000 // 30 days ago
31- let value = startingValue
32-
33- for ( let timestamp = monthAgo ; timestamp <= now ; timestamp += 60 * 1000 ) {
34- const randomVariation = ( Math . random ( ) - 0.5 ) * radomFactor
35- value += randomVariation
36-
37- // either randomVariation was negative and value went below minimum
38- // or randomVariation was positive and value went above maximum
39- if ( value < minimum || value > maximum ) {
40- // invert direction to keep within bounds
41- value -= 2 * randomVariation
42- }
43-
44- points . push ( { timestamp, value } )
45- }
46-
47- return points
48- }
49-
5023enum Measurement {
5124 Temperature = 'Temperature' ,
5225 Blue = 'Blue' ,
5326 Green = 'Green' ,
5427 Pink = 'Pink' ,
55- Pulses = 'Pulses ' ,
56- PulseRate = 'Pulse Rate' ,
28+ Visits = 'Visits ' ,
29+ VisitRate = 'Visit Rate' ,
5730}
5831
59- const averagePulseRatePerHour = 120
60- const pulsePoints = generateRandomPulses ( {
61- maxAgeDays : 60 ,
62- burstFactor : 10 ,
63- burstProbability : 0.05 ,
64- averageRatePerHour : averagePulseRatePerHour ,
65- } )
66-
6732const measurementKeys = Object . values ( Measurement )
6833const measurementsRecords : Record < Measurement , Dataset > = {
6934 [ Measurement . Temperature ] : {
7035 unit : '°C' ,
71- points : generateDataPoints ( {
72- maximum : 40 ,
73- minimum : - 10 ,
74- radomFactor : 1 ,
75- startingValue : - 8 ,
76- } ) ,
36+ points : temperatureData ,
7737 decimals : 0 ,
7838 areaColor : '#c4deff' ,
7939 color : {
@@ -92,7 +52,7 @@ const measurementsRecords: Record<Measurement, Dataset> = {
9252 } ,
9353 [ Measurement . Blue ] : {
9454 unit : 'l' ,
95- points : generateDataPoints ( {
55+ points : generateTimeSeriesData ( {
9656 startingValue : 160 ,
9757 minimum : 50 ,
9858 radomFactor : 6 ,
@@ -103,14 +63,14 @@ const measurementsRecords: Record<Measurement, Dataset> = {
10363 } ,
10464 [ Measurement . Green ] : {
10565 unit : 'kg' ,
106- points : generateDataPoints ( ) ,
66+ points : generateTimeSeriesData ( ) ,
10767 decimals : 0 ,
10868 color : '#6e6' ,
10969 measurementName : Measurement . Green ,
11070 } ,
11171 [ Measurement . Pink ] : {
11272 unit : 'm/s' ,
113- points : generateDataPoints ( {
73+ points : generateTimeSeriesData ( {
11474 startingValue : 20 ,
11575 minimum : 100 ,
11676 } ) ,
@@ -119,108 +79,33 @@ const measurementsRecords: Record<Measurement, Dataset> = {
11979 measurementName : Measurement . Pink ,
12080 } ,
12181
122- [ Measurement . PulseRate ] : {
123- unit : 'pulses/h' ,
124- points : pulsePoints . map ( ( { timestamp } , index , array ) => {
125- const slice = [ ...array . slice ( Math . max ( 0 , index - 60 + 1 ) , index + 1 ) ] // this and previous 59 minutes
126- const hourSum = slice . reduce ( ( sum , point ) => sum + ( point . value || 0 ) , 0 )
127-
128- return {
129- timestamp,
130- value : hourSum ,
131- }
82+ [ Measurement . VisitRate ] : {
83+ unit : 'visits/h' ,
84+ points : visits . movingAveregeData ,
85+ slices : buildSlices ( 'horizontal' , {
86+ end : visits . latestTimestamp ,
87+ start : visits . oldestTimestamp ,
88+ yellowThreshold : visits . averageVisitRatePerHour ,
89+ redThreshold : visits . averageVisitRatePerHour * 1.1 ,
13290 } ) ,
133- slices : {
134- start : pulsePoints [ 0 ] ?. timestamp ?? 0 ,
135- end : pulsePoints [ pulsePoints . length - 1 ] ?. timestamp ?? 0 ,
136- items : [
137- {
138- color : '#08985115' ,
139- start : { bottom : 0 , top : averagePulseRatePerHour } ,
140- end : { bottom : 0 , top : averagePulseRatePerHour } ,
141- } ,
142- {
143- color : '#ffc40015' ,
144- start : {
145- bottom : averagePulseRatePerHour ,
146- top : averagePulseRatePerHour * 1.1 ,
147- } ,
148- end : {
149- bottom : averagePulseRatePerHour ,
150- top : averagePulseRatePerHour * 1.1 ,
151- } ,
152- } ,
153- {
154- color : '#bb222215' ,
155- start : {
156- bottom : averagePulseRatePerHour * 1.1 ,
157- top : averagePulseRatePerHour * 10 ,
158- } ,
159- end : {
160- bottom : averagePulseRatePerHour * 1.1 ,
161- top : averagePulseRatePerHour * 10 ,
162- } ,
163- } ,
164- ] ,
165- } ,
16691 decimals : 0 ,
16792 color : '#000' ,
16893 areaColor : null ,
169- measurementName : Measurement . PulseRate ,
94+ measurementName : Measurement . VisitRate ,
17095 } ,
171- [ Measurement . Pulses ] : {
96+ [ Measurement . Visits ] : {
17297 unit : 'pulses' ,
173- points : pulsePoints . reduce (
174- ( { array, sum } , { timestamp, value } ) => {
175- const newSum = sum + ( value || 0 )
176- array . push ( { timestamp, value : newSum } )
177- return {
178- array,
179- sum : newSum ,
180- }
181- } ,
182- { array : [ ] as Point [ ] , sum : 0 }
183- ) . array ,
98+ points : visits . culmulativeData ,
18499 decimals : 0 ,
185100 color : '#000' ,
186101 areaColor : null ,
187- measurementName : 'Pulses cumulative' ,
188- slices : ( ( ) => {
189- const start = pulsePoints [ 0 ] ?. timestamp ?? 0
190- const end = pulsePoints [ pulsePoints . length - 1 ] ?. timestamp ?? 0
191- const dataDurationMs = end - start
192- const dataDurationHours = dataDurationMs / ( 60 * 60 * 1000 )
193-
194- const warningRate = averagePulseRatePerHour
195- const dangerRate = warningRate * 1.1
196-
197- const warningEnd = dataDurationHours * warningRate
198- const dangerEnd = dataDurationHours * dangerRate
199-
200- const topEdge = dangerRate * 2 * dataDurationHours
201-
202- return {
203- end,
204- start,
205- items : [
206- {
207- color : '#08985115' ,
208- start : { bottom : 0 , top : 0 } ,
209- end : { bottom : 0 , top : warningEnd } ,
210- } ,
211- {
212- color : '#ffc40015' ,
213- start : { bottom : 0 , top : 0 } ,
214- end : { bottom : warningEnd , top : dangerEnd } ,
215- } ,
216- {
217- color : '#bb222215' ,
218- start : { bottom : 0 , top : topEdge } ,
219- end : { bottom : dangerEnd , top : topEdge } ,
220- } ,
221- ] ,
222- }
223- } ) ( ) ,
102+ measurementName : 'Visits cumulative' ,
103+ slices : buildSlices ( 'axial' , {
104+ end : visits . latestTimestamp ,
105+ start : visits . oldestTimestamp ,
106+ yellowThreshold : visits . averageVisitRatePerHour ,
107+ redThreshold : visits . averageVisitRatePerHour * 1.1 ,
108+ } ) ,
224109 } ,
225110}
226111
0 commit comments