iwa-panda2

Manage Weather Data by International Weather Agency (Version 2)
Log | Files | Refs | README

commit 9ac3a7588d1e4046a19f6ba321ff95dd08183dee
parent a21abe8c9d7d15d7888bc31984bec6337e6f9f62
Author: Kninteman <[email protected]>
Date:   Tue, 13 Jun 2023 13:37:03 +0200

denny

Diffstat:
MModel/Key.php | 5+++--
Mjs/panda.js | 66+++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
2 files changed, 56 insertions(+), 15 deletions(-)

diff --git a/Model/Key.php b/Model/Key.php @@ -48,7 +48,7 @@ Class Key{ function retrieveRBData($key){ $db = new mysqli("86.92.67.21", "friedel", "hailiwa", "wap2"); $weather_data = []; - $query = "SELECT distinct temperature, dew_point, nl.name as city + $query = "SELECT distinct temperature, dew_point, nl.name as city, date_time FROM weather_data wd JOIN station s ON wd.station_name = s.name JOIN contract_station cs ON cs.station_name = s.name @@ -66,7 +66,8 @@ Class Key{ $humidity = humid($row["temperature"], $row["dew_point"]); $weather_data[] = [ 'humidity' => $humidity, - 'location' => $row['city'] + 'location' => $row['city'], + 'datetime' => $row['date_time'] ]; } diff --git a/js/panda.js b/js/panda.js @@ -47,7 +47,7 @@ var ctx = document.getElementById('myChart').getContext('2d'); var myChart = new Chart(ctx, { type: 'line', data: { - labels: ['Measurement 1', 'Measurement 2', 'Measurement 3', 'Measurement 4', 'Last Measurement'], + labels: ['', '', '', '', ''], datasets: [ { label: 'Chengdu Research Facility 1', @@ -119,19 +119,28 @@ function fetchDataAndUpdateChart() { data.forEach(point => { array.push(point['humidity']); }); + let splitDataResult = splitData(data); let chengduData = splitDataResult.chengduData.splice(-5); let kangdingData = splitDataResult.kangdingData.splice(-5); + let chengduHumidity = getHumidity(chengduData); + let kangdingHumidity = getHumidity(kangdingData); + + let firstDate = getFirstDate(chengduData.slice(0), kangdingData.slice(0)); + let latestDate = getLatestDate(chengduData.slice(-1), kangdingData.slice(-1)); - myChart.data.datasets[0].pointBackgroundColor = chengduData.map(value => value > 80 ? 'rgba(207, 72, 72, 1)' : 'rgba(176, 204, 207, 1)'); - myChart.data.datasets[0].pointBorderColor = chengduData.map(value => value > 80 ? 'rgba(207, 72, 72, 1)' : 'rgba(176, 204, 207, 1)'); - myChart.data.datasets[0].pointBorderWidth = chengduData.map(value => value > 80 ? 10 : 2); - myChart.data.datasets[0].data = chengduData; + myChart.data.labels[0] = firstDate[0]['datetime']; + myChart.data.labels[4] = latestDate[0]['datetime']; - myChart.data.datasets[1].pointBackgroundColor = kangdingData.map(value => value > 80 ? 'rgba(207, 72, 72, 1)' : 'rgba(195, 176, 207, 1)'); - myChart.data.datasets[1].pointBorderColor = kangdingData.map(value => value > 80 ? 'rgba(207, 72, 72, 1)' : 'rgba(195, 176, 207, 1)'); - myChart.data.datasets[1].pointBorderWidth = kangdingData.map(value => value > 80 ? 10 : 2); - myChart.data.datasets[1].data = kangdingData; + myChart.data.datasets[0].pointBackgroundColor = chengduHumidity.map(value => value > 80 ? 'rgba(207, 72, 72, 1)' : 'rgba(176, 204, 207, 1)'); + myChart.data.datasets[0].pointBorderColor = chengduHumidity.map(value => value > 80 ? 'rgba(207, 72, 72, 1)' : 'rgba(176, 204, 207, 1)'); + myChart.data.datasets[0].pointBorderWidth = chengduHumidity.map(value => value > 80 ? 10 : 2); + myChart.data.datasets[0].data = chengduHumidity; + + myChart.data.datasets[1].pointBackgroundColor = kangdingHumidity.map(value => value > 80 ? 'rgba(207, 72, 72, 1)' : 'rgba(195, 176, 207, 1)'); + myChart.data.datasets[1].pointBorderColor = kangdingHumidity.map(value => value > 80 ? 'rgba(207, 72, 72, 1)' : 'rgba(195, 176, 207, 1)'); + myChart.data.datasets[1].pointBorderWidth = kangdingHumidity.map(value => value > 80 ? 10 : 2); + myChart.data.datasets[1].data = kangdingHumidity; myChart.update(); }) @@ -143,13 +152,44 @@ function splitData(data) { for (let item of data) { if (item.location === 'Chengdu') { - chengduData.push(item['humidity']); + chengduData.push(item); } else if (item.location === 'Kangding') { - kangdingData.push(item['humidity']); + kangdingData.push(item); } } return { chengduData, kangdingData }; } +function getHumidity(entries){ + let humidity = [] + entries.forEach(el => { + humidity.push(el['humidity']) + }); + return humidity; +} + +function getFirstDate(entry1, entry2) { + let datetime1 = new Date(entry1['datetime']); + let datetime2 = new Date(entry2['datetime']); + + if (datetime1 < datetime2) { + return entry1; + } else { + return entry2; + } +} + +function getLatestDate(entry1, entry2) { + let datetime1 = new Date(entry1['datetime']); + let datetime2 = new Date(entry2['datetime']); + + if (datetime1 > datetime2) { + return entry1; + } else { + return entry2; + } +} + + fetchDataAndUpdateChart(); -setInterval(fetchDataAndUpdateChart, 300); -\ No newline at end of file +setInterval(fetchDataAndUpdateChart, 3000); +\ No newline at end of file