From ff835a960ebe5a9b0dbf7922240d77ba3140fc11 Mon Sep 17 00:00:00 2001 From: omoya_fbit Date: Fri, 24 Apr 2020 00:12:34 +0200 Subject: [PATCH 1/8] death rate moving average chart labels included --- src/util/i18n.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/util/i18n.js b/src/util/i18n.js index c3aa806..fc939e1 100644 --- a/src/util/i18n.js +++ b/src/util/i18n.js @@ -44,6 +44,8 @@ const resources = { poblacio: "Población", uvac_actius: "Activos UVAC", act_dades: "Último registro", + morts_per_dia_title:"Fallecidos-día por millón de habitantes", + mitjana_mobil_subtitle:"Media móvil de 7 días", }, }, ca: { @@ -82,6 +84,8 @@ const resources = { poblacio: "Población", uvac_actius: "Actius UVAC", act_dades: "Darrer registre", + morts_per_dia_title:"Morts-día per milió d'habitants", + mitjana_mobil_subtitle:"Mitjana mòbil de 7 dies", }, }, }; From c8dcb3ae51535051b254e8755f93b8b53b6c2ff1 Mon Sep 17 00:00:00 2001 From: omoya_fbit Date: Fri, 24 Apr 2020 00:13:10 +0200 Subject: [PATCH 2/8] created function that calculates moving avg series --- src/util/movingAverage.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/util/movingAverage.js diff --git a/src/util/movingAverage.js b/src/util/movingAverage.js new file mode 100644 index 0000000..9347d7c --- /dev/null +++ b/src/util/movingAverage.js @@ -0,0 +1,28 @@ +import last from "lodash/last"; + +/** + * @desc calculates a moving average series for a given time window. + * @param Array $dailyCountsArray - an array of objects {deaths_per_million, date} that represents a daily-based time series. + * @param window $int - the number of days that should be considered to calculate the moving average. + * @return Array - a series of moving averages per day. +*/ + +const calculateMovingAverageSeries = (dailyCountsArray, window) => { + let resultingSeries = []; + for (let i = 0; i < dailyCountsArray.length + 1 - window; i++) { + let dataInWindow = dailyCountsArray.slice(i, i + window); + console.log(dataInWindow) + let lastDateInWindow = last(dataInWindow).data + let deaths_per_million = dataInWindow.map((item) => { + return item.deaths_per_million; }); + let sum = deaths_per_million.reduce( + (previous, current) => (current += previous) + ); + let avg = (sum / deaths_per_million.length).toFixed(2); + resultingSeries.push({moving_avg: avg, data: lastDateInWindow}); + } + console.log(resultingSeries) + return resultingSeries; +}; + +export default calculateMovingAverageSeries \ No newline at end of file From 068c840676bd9bfc01db693526f3e72d3475cb40 Mon Sep 17 00:00:00 2001 From: omoya_fbit Date: Fri, 24 Apr 2020 00:13:51 +0200 Subject: [PATCH 3/8] Death rates moving avg chart implementation --- src/pages/index.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/pages/index.js b/src/pages/index.js index e7bd8c1..903a3d2 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -6,6 +6,7 @@ import "moment/locale/ca"; import "moment/locale/es"; import last from "lodash/last"; import "../util/i18n"; +import calculateMovingAverageSeries from "../util/movingAverage"; import { useTranslation } from "react-i18next"; import TooltipMap from "../charts/map/TooltipMap"; @@ -61,7 +62,18 @@ const Index = () => { let nodes = data.allDataJson.nodes.sort((a, b) => a.timestamp - b.timestamp); const currentNode = last(nodes); + + // 1.188 million inhabitants in Balearic Islands (Source INE:) + // to calculate deaths per million inhabitants we apply a 1/1.188 factor + let deathsPerMillionDailySeries = nodes.map((item) => { + return { deaths_per_million: (item.exitus / 1.188), data: item.data }; + }); + + // moving average series + let movingAverageSeries = calculateMovingAverageSeries(deathsPerMillionDailySeries, 7); + const { t } = useTranslation(); + return (
{ color={Colors.tertiary} /> + + +