From 9d16c923b9cd9200591e5b3931f9b4656739f75e Mon Sep 17 00:00:00 2001 From: Orestis Moresis Date: Fri, 26 May 2023 15:27:17 +0300 Subject: [PATCH] add timeseries in stats --- public/js/loader.js | 1 + public/js/stats.js | 79 +++++++++++++++++++++++++++++ src/Controllers/StatsController.php | 14 +++++ src/Routing/controls.php | 2 + src/Views/main-statistics.php | 19 ++----- 5 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 public/js/stats.js diff --git a/public/js/loader.js b/public/js/loader.js index 46be112..e87326a 100644 --- a/public/js/loader.js +++ b/public/js/loader.js @@ -171,6 +171,7 @@ var loader_script_files=[ "js/layers.js", "js/extras.js", "js/modal.js", + "js/stats.js", "js/maptab/lib/servicesFunctions.js", "js/maptab/lib/layersFunctions.js", "js/maptab/lib/attributesFunctions.js", diff --git a/public/js/stats.js b/public/js/stats.js new file mode 100644 index 0000000..04de35c --- /dev/null +++ b/public/js/stats.js @@ -0,0 +1,79 @@ +function populateStatisticsGrid() { + const statsGrid = new gridjs.Grid({ + columns: ['stations', 'cruises', 'parameters', 'descriptors', 'criteria'], + server: { + url: BASE_URL + '/interface/stats/general', + then: data => data.map(stats => { + return [stats.total_stations, stats.total_cruises, stats.total_parameters, stats.total_criterias, stats.total_descriptors] + }) + } + }).render(grid); +} + + +async function populateTimeseries(category) { + let url = BASE_URL; + let label = category.charAt(0).toUpperCase() + category.slice(1) + ' per Sampling Date'; + switch (category) { + case 'descriptors': + url += '/interface/stats/descriptor-ts'; + break; + case 'stations': + url += '/interface/stats/stations-ts'; + break; + case 'cruises': + url += '/interface/stats/stations-ts'; + break; + } + + + try { + const response = await fetch(url); + response.json().then(response => { + const data = response.map(row => ({ + x: row.sampling_date, + y: parseInt(row.counts) + })); + console.debug(data); + + const chartOptions = { + type: 'bar', + data: { + datasets: [{ + label: label, + data: data, + backgroundColor: 'rgba(54, 162, 235, 0.5)', + borderColor: 'rgba(54, 162, 235, 1)', + borderWidth: 1, + barPercentage: 0.8, // Adjust the bar percentage as desired + categoryPercentage: 0.9 + }] + }, + options: { + scales: { + y: { + beginAtZero: true, + ticks: { + precision: 0 + } + }, + x: { + displayFormats: { + month: 'MMM' + }, + ticks: { + length: 4 + } + } + } + + } }; + + const ctx = document.getElementById(category + '-timeseries').getContext('2d'); + new Chart(ctx, chartOptions); + }); + } catch (error) { + console.error(error); + } +} + diff --git a/src/Controllers/StatsController.php b/src/Controllers/StatsController.php index 4fe4732..02e9cbc 100644 --- a/src/Controllers/StatsController.php +++ b/src/Controllers/StatsController.php @@ -19,6 +19,20 @@ class StatsController { echo $result; } + public function descriptorTimeseries() { + $sql = "SELECT * FROM webapp.stats_timeseries_descriptors"; + $result = PgSql::getJson($sql); + + echo $result; + } + + public function stationsTimeseries() { + $sql = "SELECT * FROM webapp.stats_timeseries_stations"; + $result = PgSql::getJson($sql); + + echo $result; + } + public function countTypes() { $sql = "SELECT * FROM webapp.stats_count_per_type"; $result = PgSql::getJson($sql); diff --git a/src/Routing/controls.php b/src/Routing/controls.php index b6518c1..bc19f61 100644 --- a/src/Routing/controls.php +++ b/src/Routing/controls.php @@ -15,3 +15,5 @@ $router->map('GET', '/actions/requests/[i:id]?', 'MSFD\Controllers\ActionControl $router->map('GET', '/interface/stats/typecount', 'MSFD\Controllers\StatsController@countTypes', 'stat_types'); $router->map('GET', '/interface/stats/totalrows', 'MSFD\Controllers\StatsController@countTotal', 'stat_total'); $router->map('GET', '/interface/stats/general', 'MSFD\Controllers\StatsController@all', 'stat_all'); +$router->map('GET', '/interface/stats/descriptor-ts', 'MSFD\Controllers\StatsController@descriptorTimeseries', 'stat_desc_ts'); +$router->map('GET', '/interface/stats/stations-ts', 'MSFD\Controllers\StatsController@stationsTimeseries', 'stat_station_ts'); diff --git a/src/Views/main-statistics.php b/src/Views/main-statistics.php index b4e8493..2b5d2b6 100644 --- a/src/Views/main-statistics.php +++ b/src/Views/main-statistics.php @@ -1,4 +1,4 @@ -
+

General @@ -13,21 +13,8 @@

-
+ +
-