Published
Edited
May 1, 2021
2 forks
Importers
19 stars
Insert cell
Insert cell
Insert cell
stackChart
Insert cell
Insert cell
Insert cell
Insert cell
death = d3.csv(
"https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv"
)
Insert cell
confirmed = d3.csv(
"https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv"
)
Insert cell
recovered = d3.csv(
"https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv"
)
Insert cell
Insert cell
tidy = (data, type) => {
const t = data
.map(d => {
let prev = 0; // previous total, to compute diffs
return (
Object.keys(d)
.filter(parseDateMDY)
// .filter(d => d !== "2/12/20") // bad data day
.map(k => {
const total = +d[k],
cases = total - prev;
prev = total;
return {
type,
country: d["Country/Region"],
province_name: d["Province/State"],
province: `${d["Country/Region"]}:${d["Province/State"]}`,
lat: +d["Lat"],
long: +d["Long"],
date: parseDateMDY(k),
ymd: d3.timeFormat("%Y-%m-%d")(parseDateMDY(k)),
cases,
total
};
})
);
})
.flat()
.filter(d => d.total > 0);

return t;
}
Insert cell
tdeath = tidy(death, "death")
Insert cell
tconfirmed = tidy(confirmed, "confirmed")
Insert cell
trecovered = tidy(recovered, "recovered")
Insert cell
Insert cell
data = [...tdeath, ...tconfirmed, ...trecovered].sort((a, b) =>
d3.ascending(a.date, b.date)
)
Insert cell
provinces = d3.groups(data, d => d.province).map(d => d[0])
Insert cell
Insert cell
import { chart as stackChart } with {
databyday as data,
color,
margin
} from "@d3/stacked-area-chart"
Insert cell
margin = ({ top: 20, right: 30, bottom: 30, left: 90 })
Insert cell
databyday = {
const rollups = d3
.rollups(
data.filter(d => d.type === type),
v => {
const o = {};
provinces.forEach(p => (o[p] = 0));
v.forEach(d => (o[d.province] = d.total));
return o;
},
d => d.ymd
)
.map(d => ({ date: parseDateYMD(d[0]), ...d[1] }));

const last = rollups[rollups.length - 1];

return Object.assign(rollups, {
columns: [...Object.keys(last)].sort((a, b) =>
d3.descending(last[a], last[b])
),
y: `Cumulative ${type} cases of covid-19 infection, by country & province`
});
}
Insert cell
color = d3
.scaleOrdinal()
.domain(provinces)
.range(d3.schemeOranges[9])
Insert cell
Insert cell
d3 = require("d3@5", "d3-array@2")
Insert cell
parseDateYMD = d3.timeParse("%Y-%m-%d")
Insert cell
parseDateMDY = d3.timeParse("%m/%d/%y")
Insert cell
parseDateMDYHMP = d3.timeParse("%m/%d/%y %H:%M")
Insert cell
import { select } from "@jashkenas/inputs"
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more