Commit f3b89649 authored by Michał Woźniak's avatar Michał Woźniak
Browse files

rolling average now calculated from days preceeding the selected day (fixes #1)

parent 133e28b9
Pipeline #285 passed with stage
in 6 seconds
......@@ -50,3 +50,11 @@ Some improvements have been suggested, no promises if and when they get implemen
- ***Logarithmic scale does not properly show negative numbers!***
That's [true](https://canvasjs.com/forums/topic/negative-values-with-logarithmic-scale/), and [very hard to work around](https://canvasjs.com/forums/topic/negative-values-with-logarithmic-scale/)
## Changelog
### 06.12.2020
- Changed the way rolling average is calculated: a rolling average over `n` days for the current day is now calculated from data from the day and `n-1` proceeding days, instead of centering the rolling average on the selected day.
Fixes #1.
- The above also means we can easily support even numbers for the number of days a rolling average is calculated over. So we do. And as a cherry on top, rolling average over a period of up to 31 days can be selected; there are no and never have been any restrictions over period if entered directly in the url (for example, here's a [65-day rolling average of new cases in USA](https://rys.io/covid/#delta,average:65;united-states))
......@@ -1389,26 +1389,19 @@ let updateChartData = (siteSelect) => {
// are we doing the moving average thing?
var avg_over = document.querySelector('input[type=number][name=chart-average]').value
if ( (document.querySelector('input[type=radio][name=chart-cases]:checked').value === 'new') && (avg_over > 1) ) {
var half_avg_over = Math.floor(avg_over/2)
to_chart.data = to_chart.data.reduce((acc, cur, idx, arr) => {
var mean = 0;
var avg_start = idx - half_avg_over
var avg_end = idx + half_avg_over
var avg_start = idx - avg_over + 1
var avg_end = idx
// if we don't have enough previous data to average over
// use whatever we have
if (idx < half_avg_over) {
if (idx < avg_over) {
avg_start = 0
}
// if we don't have enough followinf data to average over
// use whatever we have
if (idx >= (arr.length - half_avg_over)) {
avg_end = arr.length - 1
}
// calculate the mean
for (var j=avg_start; j<=avg_end; j++) {
mean += arr[j];
......
......@@ -530,7 +530,7 @@
</div>
<div class="chart-config-group chart-average">
<p>Average over:</p>
<input type="number" min="1" max="15" step="2" value="1" name="chart-average" id="chart-average"/>
<input type="number" min="1" max="31" step="1" value="1" name="chart-average" id="chart-average"/>
<p>datapoints</p>
</div>
<div class="chart-config-group">
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment