-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatus_chart.php
More file actions
294 lines (262 loc) · 11.3 KB
/
status_chart.php
File metadata and controls
294 lines (262 loc) · 11.3 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<!DOCTYPE html>
<html>
<head>
<div class="status-chart">
<h2>Meter Status</h2>
<script>
const today = new Date();
const todayDateString = today.toLocaleDateString('en-US', { year: 'numeric', month: '2-digit', day: '2-digit' });
let onlineDataArray = [];
let offlineDataArray = [];
async function getCount() {
try {
let onlineCount = 0;
let offlineCount = 0;
const response = await fetch('fetch_table_data.php');
const fetchedData = await response.json(); // Store fetched data in the global variable
onlineDataArray = [];
offlineDataArray = [];
fetchedData.forEach(row => {
const date = row.LastUpdated;
if (!date) {
// Skip rows without a LastUpdated date
return;
}
// Split the date string into parts
const parts = date.split(' ');
// Extract date parts
const datePart = parts[0]; // "2022/21/04"
const timePart = parts[1] + ' ' + parts[2]; // "04:00:00 AM"
// Split the date part into year, day, and month
const dateParts = datePart.split('/');
const year = dateParts[0];
const day = dateParts[1];
const month = dateParts[2];
// Construct a valid date string in "yyyy-mm-dd" format
const validDateString = `${year}-${month}-${day} ${timePart}`;
// Create a Date object from the valid date string
const dateObj = new Date(validDateString);
// Format dateObj into a comparable string format
const formattedDate = dateObj.toLocaleDateString('en-US', { year: 'numeric', day: '2-digit', month: '2-digit'});
// Compare formatted date with today's date
if (formattedDate === todayDateString) {
onlineDataArray.push(row); // Add to online data array if it matches today
onlineCount++; // Increment online count
} else {
offlineDataArray.push(row); // Add to offline data array if it does not match today
offlineCount++; // Increment offline count
}
});
console.log(offlineDataArray);
statusChart.data.datasets[0].data = [onlineCount, offlineCount];
statusChart.update(); // Update the chart
} catch (error) {
console.error('Error fetching data:', error);
}
}
document.addEventListener('DOMContentLoaded', () => {
getCount();
setInterval(getCount, 10000); // Refresh data every 10 seconds
});
</script>
<canvas id="statusChart"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<!-- Modal HTML -->
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<h2 id="modalTitle"></h2>
<table id="modalTable">
<thead>
<tr>
<th>Site Number</th>
<th>Metering 1 (m<sup>3</sup>)</th>
<th>Metering 2 (m<sup>3</sup>)</th>
<th>Metering 3 (m<sup>3</sup>)</th>
<th>Flowrate (m<sup>3</sup>/h)</th>
<th>Meter Remaining Battery</th>
<th>Logger Remaining Battery (Days)</th>
<th>Last Updated</th>
</tr>
</thead>
<tbody id="modalTableBody">
<!-- Dynamic content will be inserted here -->
</tbody>
</table>
</div>
</div>
<script>
var ctx = document.getElementById('statusChart').getContext('2d');
var statusChart = new Chart(ctx, {
type: 'pie',
data: {
labels: ['Online', 'Offline'],
datasets: [{
label: 'Meter Status',
data: [0,0], // placeholder
backgroundColor: ['#36a2eb', '#ff6384'],
hoverOffset: 4
}]
},
options: {
responsive: false,
plugins: {
legend: {
position: 'top',
},
tooltip: {
callbacks: {
label: function(context) {
let label = context.label || '';
if (label) {
label += ': ';
}
if (context.parsed !== null) {
label += context.parsed;
}
return label;
}
}
}
},
onClick: (event, elements) => {
if (elements.length > 0) {
// Get the clicked element
const element = elements[0];
// Get the index of the clicked segment
const dataIndex = element.index;
// Get the label of the clicked segment
const label = statusChart.data.labels[dataIndex];
console.log(label);
console.log(offlineDataArray);
// Populate the modal with table data
const tableBody = document.getElementById('modalTableBody');
tableBody.innerHTML = ''; // Clear previous data
// // Select the appropriate data array based on the clicked label
// const pieLabel = (label === 'Online') ? onlineDataArray : offlineDataArray;
// // Find the data corresponding to the clicked segment
// const filteredData = pieLabel.find(item => item.label === label);
if (label === 'Online'){
modalTitle.textContent = 'Online Details';
if (Array.isArray(onlineDataArray) && onlineDataArray.length > 0) {
// Clear existing table rows
tableBody.innerHTML = '';
// Populate the table with the fetched data
onlineDataArray.forEach(item => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${item.STA_Label || 'N/A'}</td>
<td>${item.Metering1 || 'N/A'}</td>
<td>${item.Metering2 || 'N/A'}</td>
<td>${item.Metering3 || 'N/A'}</td>
<td>${item.Flowrate || 'N/A'}</td>
<td>${item.MeterBattery || 'N/A'}</td>
<td>${item.LoggerBattery || 'N/A'}</td>
<td>${item.LastUpdated || 'N/A'}</td>
`;
tableBody.appendChild(row);
});
} else {
// Handle case where no matching data is found
tableBody.innerHTML = '<tr><td colspan="8">No data available</td></tr>';
}
} else if (label === 'Offline'){
modalTitle.textContent = 'Offline Details';
if (Array.isArray(offlineDataArray) && offlineDataArray.length > 0) {
// Clear existing table rows
tableBody.innerHTML = '';
// Populate the table with the fetched data
offlineDataArray.forEach(item => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${item.STA_Label || 'N/A'}</td>
<td>${item.Metering1 || 'N/A'}</td>
<td>${item.Metering2 || 'N/A'}</td>
<td>${item.Metering3 || 'N/A'}</td>
<td>${item.Flowrate || 'N/A'}</td>
<td>${item.MeterBattery || 'N/A'}</td>
<td>${item.LoggerBattery || 'N/A'}</td>
<td>${item.LastUpdated || 'N/A'}</td>
`;
tableBody.appendChild(row);
});
} else {
// Handle case where no matching data is found
tableBody.innerHTML = '<tr><td colspan="8">No data available</td></tr>';
}
}
// Show the modal
var modal = document.getElementById('myModal');
modal.style.display = 'block'; // Show the modal
}
}
}
});
// Get the modal
var modal = document.getElementById('myModal');
// Get the <span> element that closes the modal
var span = document.getElementsByClassName('close')[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = 'none';
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target === modal) {
modal.style.display = 'none';
}
}
</script>
</div>
<style>
/* Modal Styles */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
padding-top: 60px;
}
.modal-content {
background-color: #fefefe;
margin: 5% auto; /* 15% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 80%; /* Could be more or less, depending on screen size */
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
/* Table Styles */
table {
width: 100%;
border-collapse: collapse;
}
table, th, td {
border: 1px solid #ddd;
}
th, td {
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
</html>