11import React , { useMemo , useState } from 'react'
22import { View , StyleSheet , TouchableOpacity , Text , Switch } from 'react-native'
33
4- import Chart , { ChartProps , Dataset } from 'react-native-d3-chart'
4+ import Chart , { ChartProps , Dataset , Point } from 'react-native-d3-chart'
5+
6+ import { generateRandomPulses } from './helpers/generateRandomPulses'
57
68type TimeDomainType = 'hour' | 'day' | 'week' | 'month'
79
@@ -50,7 +52,18 @@ enum Measurement {
5052 Blue = 'Blue' ,
5153 Green = 'Green' ,
5254 Pink = 'Pink' ,
55+ Pulses = 'Pulses' ,
56+ PulseRate = 'Pulse Rate' ,
5357}
58+
59+ const averagePulseRatePerHour = 120
60+ const pulsePoints = generateRandomPulses ( {
61+ maxAgeDays : 60 ,
62+ burstFactor : 10 ,
63+ burstProbability : 0.05 ,
64+ averageRatePerHour : averagePulseRatePerHour ,
65+ } )
66+
5467const measurementKeys = Object . values ( Measurement )
5568const measurementsRecords : Record < Measurement , Dataset > = {
5669 [ Measurement . Temperature ] : {
@@ -62,7 +75,7 @@ const measurementsRecords: Record<Measurement, Dataset> = {
6275 startingValue : - 8 ,
6376 } ) ,
6477 decimals : 0 ,
65- areaColor : '#83cba8 ' ,
78+ areaColor : '#c4deff ' ,
6679 color : {
6780 type : 'thresholds' ,
6881 baseColor : '#3d91ff' ,
@@ -105,6 +118,110 @@ const measurementsRecords: Record<Measurement, Dataset> = {
105118 color : '#e0e' ,
106119 measurementName : Measurement . Pink ,
107120 } ,
121+
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+ }
132+ } ) ,
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+ } ,
166+ decimals : 0 ,
167+ color : '#000' ,
168+ areaColor : null ,
169+ measurementName : Measurement . PulseRate ,
170+ } ,
171+ [ Measurement . Pulses ] : {
172+ 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 ,
184+ decimals : 0 ,
185+ color : '#000' ,
186+ 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+ } ) ( ) ,
224+ } ,
108225}
109226
110227export default function App ( ) {
0 commit comments