Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Michał Woźniak
covid
Commits
7c2dd48c
Commit
7c2dd48c
authored
Apr 11, 2020
by
Michał Woźniak
Browse files
bugfixes
parent
f420e92e
Pipeline
#173
passed with stage
in 0 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
covid.js
View file @
7c2dd48c
...
...
@@ -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
=
''
})
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment