Published
Edited
May 4, 2021
Importers
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.csv('https://pressfreedomtracker.us/all-incidents/export/?fields=title,slug,date,categories', ({date, ...d}) => ({
date: d3.timeParse('%Y-%m-%d')(date),
...d
}))
Insert cell
dates = d3.timeDay.range(START_DATE, END_DATE).map((date, i) => ({
daysFromStart: i,
date,
incidents: data.filter(d => +d3.timeDay.floor(d.date) === +d3.timeDay.floor(date))
}))
Insert cell
annotations = [
[new Date(2017, 0, 20), 'Inauguration day.'],
[new Date(2017, 8, 15), 'A cop is found not guilty of murdering a black man, Anthony Lamar, whom he shot and killed, sparking a month of protests in St. Louis MO.'],
[new Date(2020, 2, 17), 'Stay-at-home restrictions begin in a few states.'],
[new Date(2020, 4, 25), 'Minneapolis police kill George Floyd, a black man in their custody, sparking a series of nationwide protests.'],
[new Date(2020, 10, 4), 'Election day.'],
[new Date(2021, 0, 6), 'Rioters storm the U.S. Capitol.'],
]
Insert cell
START_DATE = new Date(Date.UTC(2017, 0, 20))
Insert cell
END_DATE = new Date(Date.UTC(2021, 0, 21))
Insert cell
CHART_WIDTH = 500
Insert cell
CHART_HEIGHT = Math.ceil(dates.length / COLUMNS) * xScale.bandwidth() - 2
Insert cell
COLUMNS = 28
Insert cell
GUTTER = 2
Insert cell
xScale = d3.scaleBand().domain(d3.range(0, COLUMNS)).range([0, CHART_WIDTH + GUTTER])
Insert cell
yScale = v => Math.floor(v / xScale.domain().length) * xScale.bandwidth()
Insert cell
colorScale = d3.scaleLinear().domain([0, 10, 20]).range(['#EEE', '#38A', '#000'])
Insert cell
daysSinceStart = date => d3.timeDay.count(START_DATE, date) - 1
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