-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.html
308 lines (271 loc) · 11.3 KB
/
dashboard.html
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Driving Dashboard</title>
<style>
body {
font-family: Arial, sans-serif;
}
.topbar {
padding: 5px;
}
.header {
width: 300px;
height: 225px;
float: left;
background: #f3f3f3;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
text-align: center;
font-size: 1.2em;
}
.chart-container {
margin-left: 325px;
height: 225px;
border: 1px solid #ddd;
}
#calendar {
font-family: Arial, sans-serif;
border-collapse: collapse;
width: 100%;
font-size: .8em;
}
#calendar td, #calendar th {
border: 1px solid #ddd;
text-align: center;
padding: 1px;
}
#calendar th {
background-color: #c2c2c2;
}
.current-day {
color: blue;
font-weight: bold;
}
iframe {
width: 100%;
border: 1px solid #ddd;
height: 650px;
}
</style>
</head>
<body>
<div class="topbar" width="100%">
<div class="header">
<div class="location" id="location">Loading location...</div>
<!-- <div class="time" id="time">00:00:00</div> -->
<div class="date" id="date">Day Month Year</div>
<div class="cal" id="caldiv"><table id="calendar"></table></div>
</div>
<div class="chart-container">
<canvas id="weatherChart">Chart</canvas>
</div>
</div>
<!-- Embedding external website with geolocation permission -->
<iframe src="https://teslawaze.azurewebsites.net/" title="External Website" allow="geolocation"></iframe>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script>
function updateTime() {
const now = new Date();
const dateString = now.toLocaleDateString('en-US', {
weekday: 'short', // long, short, narrow
month: 'long',
day: '2-digit',
year: 'numeric'
});
const timeString = now.toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: true // This ensures the time is in 12-hour format with AM/PM
});
document.getElementById('date').textContent = dateString;
// document.getElementById('time').textContent = timeString;
}
updateTime();
setInterval(updateTime, 1000);
function createCalendar(id, year, month, day) {
const elem = document.getElementById(id);
const mon = month - 1; // months in JS are 0..11, not 1..12
const d = new Date(year, mon);
let table = '<table><tr><th>Su</th><th>Mo</th><th>Tu</th><th>We</th><th>Th</th><th>Fr</th><th>Sa</th></tr><tr>';
// spaces for the first row
// from Monday till the first day of the month
for (let i = 0; i < d.getDay(); i++) {
table += '<td></td>';
}
// fill the table with days
while (d.getMonth() == mon) {
table += '<td>';
if (d.getDate() == day){
table += '<strong class="current-day">' + d.getDate() + '</strong>'
} else {
table += d.getDate();
}
table += '</td>';
if (d.getDay() % 7 == 6) { // sunday, last day of the week - newline
table += '</tr><tr>';
}
d.setDate(d.getDate() + 1);
}
// spaces for the last row
if (d.getDay() != 0) {
for (let i = d.getDay(); i < 7; i++) {
table += '<td></td>';
}
}
// close the table
table += '</tr></table>';
// only attach if it's not null
if (elem) {
elem.innerHTML = table;
}
}
// get the current year and month
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1; // getMonth returns 0-11
const day = now.getDate();
console.log("now.day: ", day)
createCalendar('calendar', year, month, day);
// Function to check if two dates are the same day
function isSameDay(d1, d2) {
return d1.getFullYear() === d2.getFullYear() &&
d1.getMonth() === d2.getMonth() &&
d1.getDate() === d2.getDate();
}
async function fetchWeather(lat, lon) {
console.log("Fetch weather @ ", lat, " ", lon);
const gridPointResponse = await fetch(`https://api.weather.gov/points/${lat},${lon}`);
const gridPointData = await gridPointResponse.json();
document.getElementById('location').textContent = `${gridPointData.properties.relativeLocation.properties.city}, ${gridPointData.properties.relativeLocation.properties.state}`;
const forecastResponse = await fetch(gridPointData.properties.forecastHourly);
const forecastData = await forecastResponse.json();
const labels = forecastData.properties.periods.map(period => new Date(period.startTime).toLocaleTimeString([], {hour: 'numeric'}));
const tempData = forecastData.properties.periods.map(period => period.temperature);
const rainData = forecastData.properties.periods.map(period => period.probabilityOfPrecipitation.value);
const maxTemp = Math.max(...tempData);
console.log("maxTemp: ", maxTemp)
const annotations2 = forecastData.properties.periods.map((period, index) => ({
type: 'box',
xMin: index,
xMax: index + 1,
backgroundColor: period.isDaytime ? 'rgba(211, 211, 211, 0.5)' : 'rgba(169, 169, 169, 0.5)',
borderColor: 'rgba(0, 0, 0, 0)',
}));
const annotations = forecastData.properties.periods.map((period, index) => {
// Create box annotations for day and night.
return {
type: 'box',
xMin: index,
xMax: index + 1,
backgroundColor: period.isDaytime ? 'rgba(211, 211, 211, 0.5)' : 'rgba(169, 169, 169, 0.5)',
borderColor: 'rgba(0, 0, 0, 0)',
};
}).concat(
forecastData.properties.periods.reduce((acc, period, index) => {
// Create text annotations for days at noon.
const date = new Date(period.startTime);
if (date.getHours() === 12) { // Check if it's noon.
const dayLabel = date.toLocaleDateString('en-US', { weekday: 'short' });
acc.push({
type: 'label',
xValue: index,
yValue: maxTemp,
content: dayLabel,
backgroundColor: 'rgba(255, 255, 255, 0)', // Transparent background.
font: {
size: 14
}
});
}
return acc;
}, [])
);
// Generate day change annotations
const dayChangeAnnotations = [];
let previousPeriodDate = new Date(forecastData.properties.periods[0].startTime);
forecastData.properties.periods.forEach((period, index) => {
const currentDate = new Date(period.startTime);
if (!isSameDay(previousPeriodDate, currentDate)) {
dayChangeAnnotations.push({
type: 'line',
mode: 'vertical',
scaleID: 'x',
value: index,
borderColor: 'black',
borderWidth: 2,
});
}
previousPeriodDate = currentDate;
});
const ctx = document.getElementById('weatherChart').getContext('2d');
const weatherChart = new Chart(ctx, {
data: {
labels: labels,
datasets: [{
type: 'line',
label: 'Temperature (°F)',
data: tempData,
borderColor: 'rgba(255, 99, 132, 1)',
backgroundColor: 'transparent',
yAxisID: 'y',
}, {
type: 'bar',
label: 'Chance of Rain (%)',
data: rainData,
borderColor: 'rgba(54, 162, 235, 1)',
backgroundColor: 'rgba(54, 162, 235, 1)',
yAxisID: 'y1',
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: false,
type: 'linear',
display: true,
position: 'left',
},
y1: {
beginAtZero: true,
suggestedMax: 100,
type: 'linear',
display: true,
position: 'right',
grid: {
drawOnChartArea: false,
},
},
},
plugins: {
annotation: {
annotations: [...annotations, ...dayChangeAnnotations]
},
legend: {
display: false // This will hide the legend
}
}
}
});
}
function getLocationAndWeather() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
fetchWeather(position.coords.latitude, position.coords.longitude).catch(console.error);
}, () => {
console.error('Geolocation is not supported by this browser.');
});
} else {
console.error('Geolocation is not supported by this browser.');
}
}
getLocationAndWeather();
setInterval(getLocationAndWeather, 900000); // Refresh the weather every 15 minutes
</script>
</body>
</html>