✍️🌆 Checkpoint

./public/index.js:1954897/86056
This commit is contained in:
Glitch (glitch-hello-node) committed 2021-11-14 07:12:09 +00:00
1 parent a64b13acd0
commit ad6446ed34
2 files changed
+67 -7

No files matched your search

+32 -7
View File
@@ -28,12 +28,14 @@ const buildings = {
}
}
const data = getData()
const map = L.map('map', {
minZoom: 15,
maxZoom: 18
}).setView([32.98649, -96.7485], 18)
L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
@@ -62,11 +64,12 @@ for (const [id, building] of Object.entries(buildings)) {
buildings[id].poly = poly
}
fetch('/db.csv').then(res => res.text()).then(text => {
const data = text.trim().split('\n').map(line => {
const [loc, time, count] = line.split(',')
return { loc, time: new Date(+time), count: parseInt(count) }
})
// Update map colors every minute
drawRegions()
setTimeout(drawRegions, 60 * 1000)
async function drawRegions() {
const data = await getData()
const ecsw = data.filter(d => d.loc === 'ECSW')
@@ -79,9 +82,20 @@ fetch('/db.csv').then(res => res.text()).then(text => {
const color = busy ? '#ff0000' : '#00ff00'
buildings.ECSW.poly.setStyle({ color })
console.log({avg, current, delta, color})
})
}
function getData() {
return fetch('/db.csv').then(res => res.text()).then(text => {
return text.trim().split('\n').map(line => {
const [loc, time, count] = line.split(',')
return { loc, time: new Date(+time), count: parseInt(count) }
})
})
}
async function displayCard(id) {
const data = (await getData()).filter(d => d.loc === id)
const card = document.getElementById("card")
await card.animate([
{ transform: 'translateY(50px)', opacity: "0" },
@@ -90,9 +104,20 @@ async function displayCard(id) {
fill: 'forwards',
easing: 'cubic-bezier(0.4, 0.0, 1, 1)'
}).finished
console.log(data)
const avg = data.reduce((acc, v) => acc + v.count, 0) / data.length
const current = data[data.length - 1]?.count ?? NaN
const delta = current - avg
const busy = delta > 0
const color = busy ? '#ff0000' : '#00ff00'
console.log({ avg, current, delta, busy, color })
card.innerHTML = `
<h1>${buildings[id].name}</h1>
<p>Busier than usual</p>
<p>${busy ? "Busier than usual" : "Not as busy :)"}</p>
<canvas id="myChart" height="80"></canvas>
`
card.style.setProperty("--region", `'${id}'`)