Published
Edited
May 25, 2021
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Load the data from NYT
raw_data = FileAttachment('date_state_cases.csv').csv()


//FileAttachment("fixed_fundraising_states_day@1.json").json()
Insert cell
// List of states
states = [...d3.group(raw_data, d => d.state).keys()]
Insert cell
tParser = d3.utcParse("%Y/%m/%d");
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: dd.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))
// s => d3.ascending(d.states)
)*/

cases_per_day =
states.map(state => {
const state_data = raw_data.filter(d => d.state === state);
const data = state_data.map((dd, i) => {
return {
date: tParser(dd.date),
value: parseInt(dd.cases)
};
});
return {
state,
data
};
})

Insert cell
interval = d3.utcDay
Insert cell
start = new Date(d3.min(raw_data, (d) => d.date))
Insert cell
stop = new Date(d3.max(raw_data, (d) => d.date))
Insert cell
Insert cell
// Setting defaults
timeChart = TimeChart.defaults({
interval,
start,
stop,
width:width,
marginTop: 2,
scheme: "greens"
})
Insert cell
import { TimeChart, TimeAxis, d3 } from "@observablehq/timechart"
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