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

bugfixes

parent f420e92e
Pipeline #173 passed with stage
in 0 seconds
......@@ -1239,101 +1239,105 @@ let updateChartData = (siteSelect) => {
clearTimeout(theChart.updateTimeout)
theChart.updateTimeout = false
// add the data to the chart
var to_chart = {
label: siteSelect.value,
fill: false,
borderColor: niceChartColors[siteSelect.siteNo],
borderWidth: 1,
lineTension: 0.1
}
// do we have any data to work with?
if (siteSelect.value in siteData) {
// add the data to the chart
var to_chart = {
label: siteSelect.value,
fill: false,
borderColor: niceChartColors[siteSelect.siteNo],
borderWidth: 1,
lineTension: 0.1
}
// population_ratio is also used as an indicator that we're doing a per-million chart
var population_ratio = false
if (document.querySelector('input[type=radio][name=chart-values]:checked').value === 'per-million') {
population_ratio = (siteData[siteSelect.value].population / 1000000)
}
// population_ratio is also used as an indicator that we're doing a per-million chart
var population_ratio = false
if (document.querySelector('input[type=radio][name=chart-values]:checked').value === 'per-million') {
population_ratio = (siteData[siteSelect.value].population / 1000000)
}
// where do we start with the data?
var start_at = document.querySelector('input[type=radio][name=chart-start]:checked').value
var start_at_date = false
// where do we start with the data?
var start_at = document.querySelector('input[type=radio][name=chart-start]:checked').value
var start_at_date = false
if (start_at === 'date') {
start_at_date = document.querySelector('input[type=date][name=chart-start-date]').value.replace(/-0/g, '-')
} else if (population_ratio) {
start_at = start_at * population_ratio
}
if (start_at === 'date') {
start_at_date = document.querySelector('input[type=date][name=chart-start-date]').value.replace(/-0/g, '-')
} else if (population_ratio) {
start_at = start_at * population_ratio
}
// which data are we looking at
var dataset = document.querySelector('input[type=radio][name=chart-data]:checked').value
// filter the data
var found_start = false
filtered_data = siteData[siteSelect.value]
.data
.filter((row, idx) => {
if (found_start) {
return true
} else if ( ( (start_at_date) && ( row.date === start_at_date) ) || (row[dataset] >= start_at) ) {
found_start = true
return true
}
return false
})
// which data are we looking at
var dataset = document.querySelector('input[type=radio][name=chart-data]:checked').value
// use cumulative case numbers
if (document.querySelector('input[type=radio][name=chart-cases]:checked').value === 'cumulative') {
to_chart.data = filtered_data.map(row => row[dataset])
// use new cases count
} else {
to_chart.data = filtered_data.map(row => row[`new_${dataset}`])
}
// filter the data
var found_start = false
filtered_data = siteData[siteSelect.value]
.data
.filter((row, idx) => {
if (found_start) {
return true
} else if ( ( (start_at_date) && ( row.date === start_at_date) ) || (row[dataset] >= start_at) ) {
found_start = true
return true
}
return false
})
// are we in for per-million values?
if (population_ratio) {
// we've set population ratio before already
to_chart.data = to_chart.data.map(row => Math.round(row / population_ratio))
}
// use cumulative case numbers
if (document.querySelector('input[type=radio][name=chart-cases]:checked').value === 'cumulative') {
to_chart.data = filtered_data.map(row => row[dataset])
// use new cases count
} else {
to_chart.data = filtered_data.map(row => row[`new_${dataset}`])
}
// 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)
// are we in for per-million values?
if (population_ratio) {
// we've set population ratio before already
to_chart.data = to_chart.data.map(row => Math.round(row / population_ratio))
}
to_chart.data = to_chart.data.reduce((acc, cur, idx, arr) => {
// 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)
var mean = 0;
var avg_start = idx - half_avg_over
var avg_end = idx + half_avg_over
to_chart.data = to_chart.data.reduce((acc, cur, idx, arr) => {
// if we don't have enough previous data to average over
// use whatever we have
if (idx < half_avg_over) {
avg_start = 0
}
var mean = 0;
var avg_start = idx - half_avg_over
var avg_end = idx + half_avg_over
// 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
}
// if we don't have enough previous data to average over
// use whatever we have
if (idx < half_avg_over) {
avg_start = 0
}
// calculate the mean
for (var j=avg_start; j<=avg_end; j++) {
mean += arr[j];
}
acc.push(Math.round(mean / (avg_end - avg_start + 1)))
return acc
}, [])
}
// 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];
}
acc.push(Math.round(mean / (avg_end - avg_start + 1)))
return acc
}, [])
}
// assign the data to the chart
theChart.data.datasets[siteSelect.siteNo + 3] = to_chart
// assign the data to the chart
theChart.data.datasets[siteSelect.siteNo + 3] = to_chart
// if we have some undefined elements in the chart's data array
// that means that not all data is in yet -- bail!
if (theChart.data.datasets.includes(undefined)) {
return;
// if we have some undefined elements in the chart's data array
// that means that not all data is in yet -- bail!
if (theChart.data.datasets.includes(undefined)) {
return;
}
}
// update the chart settings
......@@ -1680,6 +1684,7 @@ document.addEventListener('DOMContentLoaded', (e)=>{
})
clearAllSites.addEventListener('click', (e)=>{
e.preventDefault()
window.location.hash = ''
})
......
Supports Markdown
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