mirror of
https://github.com/Sosokker/openweathermap-dashboard.git
synced 2025-12-18 13:44:04 +01:00
192 lines
7.6 KiB
HTML
192 lines
7.6 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>Graph</title>
|
|
<script src="https://cdn.plot.ly/plotly-3.0.0.min.js" charset="utf-8"></script>
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link
|
|
href="https://fonts.googleapis.com/css2?family=Outfit:wght@100..900&family=Roboto:ital,wght@0,100..900;1,100..900&display=swap"
|
|
rel="stylesheet">
|
|
<link rel="stylesheet" href="./index.css">
|
|
|
|
<style>
|
|
body {
|
|
font-family: "Roboto", sans-serif;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<div id="main-section">
|
|
<script>
|
|
async function fetchData(scale) {
|
|
let url = "http://localhost:8080/api/data";
|
|
if (scale === true) {
|
|
url += "?scale=true"
|
|
}
|
|
|
|
try {
|
|
const res = await fetch(url)
|
|
const weatherData = await res.json()
|
|
return weatherData
|
|
} catch {
|
|
console.error(error)
|
|
return null
|
|
}
|
|
}
|
|
</script>
|
|
<div id="statistic">
|
|
<h2>Rainfall Statistics | Rain map</h2>
|
|
<p>Today rain/hr.</p>
|
|
<div id="table-section">
|
|
<script>
|
|
async function renderTable() {
|
|
const res = fetchData(false);
|
|
res.then((weatherData) => {
|
|
|
|
let tableHTML = `
|
|
<table border="1">
|
|
<thead>
|
|
<tr>
|
|
<th rowspan="2"><strong>Location</strong></th>
|
|
<th colspan="2"><strong>Coordinate</strong></th>
|
|
<th rowspan="2"><strong>Rain Per Hour</strong></th>
|
|
</tr>
|
|
<tr>
|
|
<th>Latitude</th>
|
|
<th>Longitude</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>`;
|
|
|
|
weatherData.forEach((data) => {
|
|
console.log(data)
|
|
tableHTML += `
|
|
<tr>
|
|
<td>${data.name || "N/A"}</td>
|
|
<td>${data.coord.lat || 0}</td>
|
|
<td>${data.coord.lon || 0}</td>
|
|
<td>${data.rain["1h"] || "N/A"}</td>
|
|
</tr>`;
|
|
});
|
|
|
|
tableHTML += `
|
|
</tbody>
|
|
</table>`;
|
|
|
|
|
|
const d = document.getElementById("table-section")
|
|
d.innerHTML = tableHTML
|
|
}).catch((error) => {
|
|
const tableHTML = "<p>table not availaible</p>"
|
|
const d = document.getElementById("table-section")
|
|
d.innerHTML = tableHTML
|
|
})
|
|
}
|
|
renderTable();
|
|
</script>
|
|
</div>
|
|
</div>
|
|
<div id="map">
|
|
<div id="rain-marker">
|
|
<script>
|
|
async function updateRainMarker() {
|
|
const weatherData = await fetchData(false);
|
|
if (!weatherData) {
|
|
return;
|
|
}
|
|
|
|
const lon = weatherData.map((location) => location.coord.lon);
|
|
const lat = weatherData.map((location) => location.coord.lat);
|
|
const text = weatherData.map(
|
|
(location) =>
|
|
`${location.name}: ${location.rain["1h"]} mm/hr)`
|
|
);
|
|
|
|
const data = [
|
|
{
|
|
type: "scattergeo",
|
|
mode: "markers+text",
|
|
lon: lon,
|
|
lat: lat,
|
|
marker: {
|
|
color: "rgb(17, 157, 255)",
|
|
size: 10,
|
|
},
|
|
text: text,
|
|
textposition: "bottom right",
|
|
},
|
|
];
|
|
|
|
const layout = {
|
|
map: { center: { lon: -110, lat: 50 }, zoom: 3.3 },
|
|
geo: {
|
|
center: { lon: -100, lat: 40 },
|
|
zoom: 3,
|
|
},
|
|
showlegend: false,
|
|
height: 500,
|
|
width: 750,
|
|
};
|
|
|
|
Plotly.newPlot("rain-marker", data, layout);
|
|
}
|
|
|
|
updateRainMarker();
|
|
</script>
|
|
</div>
|
|
<div id="rain-density">
|
|
<script>
|
|
async function updateRainDensity() {
|
|
const weatherData = await fetchData(true);
|
|
if (!weatherData) {
|
|
console.error("Failed to fetch weather data for rain-density.");
|
|
return;
|
|
}
|
|
|
|
const locations = weatherData.map((location) => location.coord.place);
|
|
console.log(weatherData)
|
|
console.log(locations)
|
|
const z = weatherData.map((location) => location.rain["1h"]);
|
|
var data = [
|
|
{
|
|
type: "choroplethmap",
|
|
name: "Rainfall Per Hour",
|
|
geojson:
|
|
"https://raw.githubusercontent.com/python-visualization/folium/master/examples/data/us-states.json",
|
|
// state-id, rainfall (scale 100)
|
|
locations: locations,
|
|
z: z,
|
|
zmin: 0,
|
|
zmax: 100,
|
|
colorscale: "Blues",
|
|
reversescale: true,
|
|
colorbar: {
|
|
y: 0,
|
|
yanchor: "bottom",
|
|
title: { text: "Rainfall Per Hour", side: "right" },
|
|
marker: { line: { width: 1, color: "white" } },
|
|
},
|
|
},
|
|
];
|
|
var layout = {
|
|
map: { style: "dark", center: { lon: -110, lat: 50 }, zoom: 3.3 },
|
|
width: 750,
|
|
height: 500,
|
|
margin: { t: 0, b: 0 },
|
|
};
|
|
Plotly.newPlot("rain-density", data, layout);
|
|
}
|
|
|
|
updateRainDensity();
|
|
</script>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
|
|
</html> |