Published
Edited
Jul 18, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Load the data from NYT
raw_data = {
mutable isLoading = true;
return d3
.csv(
"https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv",
d3.autoType
)
.then(d => {
mutable isLoading = false;
return d;
});
}
Insert cell
// List of states
states = [...d3.group(raw_data, d => d.state).keys()]
Insert cell
cases_per_day = d3.sort(
states.map(state => {
const state_data = raw_data.filter(d => d.state === state);
const data = state_data.map((dd, i) => {
// Calculate the *new* cases and deaths today by substracting the cases yesterday
let new_cases = 0;
if (i === 0) new_cases = +dd.cases;
else new_cases = +dd.cases - +state_data[i - 1].cases;

return {
date: dd.date,
value: new_cases
};
});
return {
state,
data
};
}),
// Sort by the date where the moving average is the highest
s => -d3.greatestIndex(movingAverage(s.data.map(d => d.value), 7))
)
Insert cell
interval = d3.utcDay
Insert cell
start = d3.min(raw_data, (d) => d.date)
Insert cell
stop = d3.max(raw_data, (d) => d.date)
Insert cell
Insert cell
// Setting defaults
timeChart = TimeChart.defaults({
interval,
start,
stop,
dateFormat: "%m/%d/%Y",
width,
marginTop: 5,
scheme: "reds"
})
Insert cell
import {movingAverage} from "@d3/moving-average"
Insert cell
import { TimeChart, TimeAxis, d3 } from "@observablehq/timechart"
Insert cell
import { Select } from "@observablehq/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