Published
Edited
Dec 3, 2021
1 fork
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
statesSummary = d3.csv(`https://api.covidactnow.org/v2/states.csv?apiKey=${API_KEY}`)
Insert cell
data = d3.csv(`https://api.covidactnow.org/v2/states.timeseries.csv?apiKey=${API_KEY}`, d3.autoType)
Insert cell
selectedStateData = data.filter(d => d.state === state).filter(d => d.date > d3.timeParse('%Y-%m-%d')(startDate))
Insert cell
blur = arr => ArrayBlur.blur().radius(blurRadius)(arr)
Insert cell
blurredCases = blur(selectedStateData.map(d => d['actuals.newCases']))
Insert cell
blurredDeaths = blur(selectedStateData.map(d => d['actuals.newDeaths']))
Insert cell
annotatedStateData = selectedStateData
.map((d, i, arr) => ({
date: d.date,
newCases: d['actuals.newCases'],
newDeaths: d['actuals.newDeaths'],
newCasesSmooth: blurredCases[i],
newDeathsSmooth: blurredDeaths[i],
casePeak: blurredCases[i - 1] < blurredCases[i] && blurredCases[i + 1] < blurredCases[i],
deathPeak: blurredDeaths[i - 1] < blurredDeaths[i] && blurredDeaths[i + 1] < blurredDeaths[i],
caseValley: blurredCases[i - 1] > blurredCases[i] && blurredCases[i + 1] > blurredCases[i],
deathValley: blurredDeaths[i - 1] > blurredDeaths[i] && blurredDeaths[i + 1] > blurredDeaths[i],
vaccinesInitiatedRatio: d['metrics.vaccinationsInitiatedRatio'],
vaccinesCompletedRatio: d['metrics.vaccinationsCompletedRatio'],
}))
.map((d, i, arr) => ({
...d,
// Find the next death peak data point and record it's index in the dataset
nextDeathPeakIdx: (
arr.slice(i).findIndex(d => d.deathPeak) !== -1 ?
i + arr.slice(i).findIndex(d => d.deathPeak) :
null
),
nextDeathValleyIdx: (
arr.slice(i).findIndex(d => d.deathValley) !== -1 ?
i + arr.slice(i).findIndex(d => d.deathValley) :
null
)
}))
.map((d, i, arr) => ({
...d,
// Fetch the next death peak's date
nextDeathPeak: (
d.nextDeathPeakIdx ?
arr[d.nextDeathPeakIdx].date :
null
),
nextDeathValley: (
d.nextDeathValleyIdx ?
arr[d.nextDeathValleyIdx].date :
null
)
}))
.map((d, i, arr) => ({
...d,
// Calculate the time in days until the next death peak
daysTillNextDeathPeak: (
d.nextDeathPeak ?
d3.timeDay.range(d.date, d.nextDeathPeak).length :
null
),
// Calculate the time in days until the next death valley
daysTillNextDeathValley: (
d.nextDeathValley ?
d3.timeDay.range(d.date, d.nextDeathValley).length :
null
)
}))
Insert cell
validPeakPair = d => (
(
// Make sure this point is a case peak and there's a death peak after it
d.casePeak &&
d.nextDeathPeak &&
// Exclude peaks that seem either too soon or too late to be related
d.daysTillNextDeathPeak > minPeakDelay &&
d.daysTillNextDeathPeak < maxPeakDelay
) || (
// Make sure this point is a case valley and there's a death valley after it
d.caseValley &&
d.nextDeathValley &&
// Exclude valleys that seem either too soon or too late to be related
d.daysTillNextDeathValley > minPeakDelay &&
d.daysTillNextDeathValley < maxPeakDelay
)
)
Insert cell
Insert cell
Insert cell
Insert cell
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