-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistoricDataFigure.js
174 lines (158 loc) · 6.2 KB
/
HistoricDataFigure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import React, { Component } from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
StatusBar,
TouchableHighlight,
NativeEventEmitter,
NativeModules,
Platform,
PermissionsAndroid,
AppState,
Dimensions,
FlatList,
Image,
ActivityIndicator,
Pressable
} from 'react-native';
import { Button, Icon, Text, Tooltip } from "react-native-elements";
import { LineChart } from "react-native-chart-kit";
var sess = require("./sess.js");
class HistoricDataFigure extends Component {
constructor(props){
super(props)
this.state = {
// data: [1,2,3] // [1,2,3] for dev
data: null,
displayedParam: 'pm25',
timewindow: 30, // in seconds
titleParam: 'PM2.5 [μg/m³]',
titleTimewindow: 'last 30 seconds'
}
}
fetchData() {
fetch(sess.baseurl + '/api/getData?minutes=' + 200, {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}}).then((data) => data. json()).then((jsonobj) => {
this.setState({data:jsonobj})
})
}
componentDidMount() {
this.fetchData()
}
render() {
let dataset = []
let firstidx = 0
if(this.state.data && this.state.data.length > 1) {
// this.state.data.forEach(element => {
// dataset.push(element[this.state.displayedParam])
// });
for(let i = 0; i < this.state.timewindow && i < this.state.data.length; i++) {
let val = this.state.data[i];
// dataset.push(val[this.state.displayedParam]);
dataset.unshift(val[this.state.displayedParam]);
firstidx = i
}
}
let refreshIcon = <Icon type="material-community" name='refresh' color='rgb(32,137,220)'/>
return (this.state.data && this.state.data.length > 1) ?
<View style={{}}>
<View style={{flexDirection:'row'}}>
<View>
<Text style={{ padding: 15, fontWeight: "bold", fontSize: 25 }}>
Recorded sensor data
</Text>
</View>
<View>
<Pressable onPress={this.fetchData.bind(this)} style={{flex:1,justifyContent:'center',flexDirection:'column',alignItems:'center'}}>{refreshIcon}</Pressable>
</View>
</View>
<Text
style={{ padding: 10, fontSize: 15, textDecorationLine: "underline" }}
>
Displayed quantity
</Text>
<View style={{ flexDirection: "row" }}>
<View style={{ margin: 5 }}>
<Button title="PM2.5" onPress={() => this.setState({displayedParam:'pm25',titleParam:'PM2.5 [μg/m³]'})}/>
</View>
<View style={{ margin: 5 }}>
<Button title="PM10" onPress={() => this.setState({displayedParam:'pm10',titleParam:'PM10 [μg/m³]'})}/>
</View>
<View style={{ margin: 5 }}>
<Button title="Humidity" onPress={() => this.setState({displayedParam:'humidity',titleParam:'Relative humidity'})}/>
</View>
</View>
<Text
style={{ padding: 10, fontSize: 15, textDecorationLine: "underline" }}
>
Time window
</Text>
<View style={{ flexDirection: "row" }}>
<View style={{ margin: 5 }}>
<Button title="Seconds" onPress={() => this.setState({timewindow: 30, titleTimewindow: 'last 30 seconds'}) }/>
</View>
<View style={{ margin: 5 }}>
<Button title="Minutes" onPress={() => this.setState({timewindow: 30*60, titleTimewindow: 'last 30 minutes'}) }/>
</View>
<View style={{ margin: 5 }}>
<Button title="Hours" onPress={() => this.setState({timewindow: 10*3600, titleTimewindow: 'last 10 hours'}) }/>
</View>
<View style={{ margin: 5 }}>
<Button title="Days" onPress={() => this.setState({timewindow: 7*24*3600, titleTimewindow: 'last 7 days'}) }/>
</View>
</View>
<View
style={{
borderBottomColor: "black",
borderBottomWidth: 1,
marginTop: 40,
marginBottom: 40
}}
/>
<Text style={{ padding: 10, fontWeight: "bold", fontSize: 17 }}>
{this.state.titleParam} — {this.state.titleTimewindow}
</Text>
<LineChart
data={{
labels: [this.state.data[firstidx].t, '+' + (0.25*this.state.timewindow).toString(), '+' + (0.5*this.state.timewindow).toString() ],
datasets: [{ data: dataset }]
}}
width={Dimensions.get("window").width-20} // from react-native
height={220}
// yAxisLabel="$"
// yAxisSuffix="k"
segments={4}
yAxisInterval={25} // optional, defaults to 1
chartConfig={{
backgroundColor: "#022173",
backgroundGradientFrom: "#022173",
backgroundGradientTo: "#1b3fa0",
decimalPlaces: 2, // optional, defaults to 2dp
color: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
labelColor: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
style: {
borderRadius: 16
},
propsForDots: {
r: "0",
strokeWidth: "2",
stroke: "#000000"
}
}}
bezier
style={{
marginVertical: 8,
borderRadius: 16,
alignSelf:'center'
}}
/>
</View> : <ActivityIndicator size="large" />
}
}
export default HistoricDataFigure;