-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.js
107 lines (102 loc) · 3.49 KB
/
analysis.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
// analysis.js
// Step 1: Data Structure for Collecting Sensor Data
const sensorData = {
timestamps: [], // Array to store the timestamps
temperature: [], // Array to store temperature readings
humidity: [], // Array to store humidity readings
dustLevel: [], // Array to store dust level readings
smokeDensity: [], // Array to store smoke density readings
heartRate: [], // Array to store heart rate readings
oxygenLevel: [] // Array to store oxygen level readings
};
// Function to add new data
function addSensorData(timestamp, temperature, humidity, dustLevel, smokeDensity, heartRate, oxygenLevel) {
sensorData.timestamps.push(timestamp);
sensorData.temperature.push(temperature);
sensorData.humidity.push(humidity);
sensorData.dustLevel.push(dustLevel);
sensorData.smokeDensity.push(smokeDensity);
sensorData.heartRate.push(heartRate);
sensorData.oxygenLevel.push(oxygenLevel);
}
// Fetching Data from Firebase
const dbRef = firebase.database().ref('/sensors'); // Modify the path as per your database structure
dbRef.on('value', (snapshot) => {
snapshot.forEach((childSnapshot) => {
const data = childSnapshot.val();
addSensorData(data.timestamp, data.temperature, data.humidity, data.dustLevel, data.smokeDensity, data.heartRate, data.oxygenLevel);
});
// After collecting data, render the graph
renderSensorDataGraph();
});
// Step 3: Render the Graph
function renderSensorDataGraph() {
const ctx = document.getElementById('sensorDataChart').getContext('2d');
new Chart(ctx, {
type: 'line',
data: {
labels: sensorData.timestamps, // X-axis (time)
datasets: [
{
label: 'Temperature',
data: sensorData.temperature,
borderColor: 'cyan',
fill: false
},
{
label: 'Humidity',
data: sensorData.humidity,
borderColor: 'blue',
fill: false
},
{
label: 'Dust Level',
data: sensorData.dustLevel,
borderColor: 'purple',
fill: false
},
{
label: 'Smoke Density',
data: sensorData.smokeDensity,
borderColor: 'green',
fill: false
},
{
label: 'Heart Rate',
data: sensorData.heartRate,
borderColor: 'orange',
fill: false
},
{
label: 'Oxygen Level',
data: sensorData.oxygenLevel,
borderColor: 'red',
fill: false
}
]
},
options: {
responsive: true,
title: {
display: true,
text: 'Sensor Data Over Time'
},
scales: {
x: {
display: true,
title: {
display: true,
text: 'Time'
}
},
y: {
display: true,
title: {
display: true,
text: 'Sensor Value'
}
}
}
}
});
}