Published
Edited
Mar 23, 2020
Importers
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.json(
'https://raw.githubusercontent.com/opencovid19-fr/data/master/dist/chiffres-cles.json'
)
Insert cell
groups = d3.rollup(data, v => v, d => d.sourceType, d => d.code.slice(0, 3))
Insert cell
datasets = {
const groups = d3.rollup(
data,
v => v,
d => d.sourceType,
d => d.code.slice(0, 3)
);

for (const [dataset, values] of groups) {
if (values.has('FRA')) values.set('FRA', uncumulate(values.get('FRA')));
if (values.has('WOR')) values.set('WOR', uncumulate(values.get('WOR')));
uncumulateAndFlatten(values, ['DEP', 'REG', 'COM']);
}

return groups;
}
Insert cell
uncumulateAndFlatten = (values, keys = [], orderBy = 'date') => {
_.forEach(keys, key => {
if (values.has(key)) {
const grouped = d3.group(values.get(key), d => d.code);
const uncumulatedMap = uncumulateMap(grouped, orderBy);
values.set(key, _.fromPairs([...uncumulatedMap]));
values.set(
'FLAT_' + key,
_([...uncumulatedMap.values()])
.flatMap()
.orderBy(orderBy)
.value()
);
}
});
}
Insert cell
uncumulateMap = function(
map,
sortBy = 'date',
attributes = uncumulateAttributes
) {
const newMap = new Map();
for (const [key, series] of map) {
newMap.set(key, uncumulate(series, sortBy, attributes));
}
return newMap;
}
Insert cell
uncumulate = function(series, sortBy, attributes = uncumulateAttributes) {
return _(series)
.sortBy(sortBy)
.map((s, index, ss) => {
if (index === 0) return s;

const prev = ss[index - 1];
const curr = { ...s };

_.forEach(attributes, attr => {
const pAttr = prev[attr],
cAttr = curr[attr];
if (pAttr !== undefined && cAttr !== undefined) {
curr[attr] = cAttr - pAttr;
}
});

return curr;
})
.value();
}
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require('d3', 'd3-array')
Insert cell
_ = require('lodash')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more