Published
Edited
Aug 17, 2022
Insert cell
# Mass Shootings in the United States
Insert cell
viewof date = Scrubber(d3.utcWeek.every(2).range(...d3.extent(data, d => d.date)), {format: d3.utcFormat("%Y %b %-d"), loop: false})
Insert cell
chart.update(date)
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, 960, 600]);

svg.append("path")
.datum(topojson.merge(us, us.objects.lower48.geometries))
.attr("fill", "#ddd")
.attr("d", d3.geoPath());

svg.append("path")
.datum(topojson.mesh(us, us.objects.lower48, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("d", d3.geoPath());

const g = svg.append("g")
.attr("fill","steelblue")
.attr("stroke", "none")

const dots = g.selectAll("circle")
.data(data)
.join("circle")
.attr("transform", d => `translate(${d})`)
.attr("opacity", .5)

let prevDate = -Infinity
let yearText = 1982

const textBlock = svg.append("g")
.attr("font-family", "Poppins")
.attr("font-weight", "bold")

const year = textBlock.append("text")
.attr("x", 800)
.attr("y", 50)
.attr("font-size", "72px")

const counter = svg.append("text")
.attr("x", 800)
.attr("y", 90)
.attr("fill", "red")
.attr("font-size", "30px")

return Object.assign(svg.node(), {
update(date) {
dots
.filter(d => d.date > prevDate && d.date <= date)
.transition().attr("r", 10)
.transition().duration(500).ease(d3.easeCubic)
.attr("r", 5)
.transition().attr("fill", "steelblue")


dots
.filter(d => d.date <= prevDate && d.date > date)
.transition().attr("fill", "red")
.transition().duration(500)
.attr("r", 0);

prevDate = date
year.text(date.getFullYear())
counter.text(`${(yearCounts[date.getFullYear()] || "")} shootings`)
}
})
}
Insert cell
parseDate = d3.utcParse("%m/%d/%y")
Insert cell
parseDate2 = d3.utcParse("%Y-%m-%d")
Insert cell
parseDate3 = d3.utcParse("%m/%d/%Y")
Insert cell
projection = d3.geoAlbersUsa().scale(1280).translate([480, 300])
Insert cell
topojson = require("topojson-client@3")
Insert cell
us = {
const us = await d3.json("https://cdn.jsdelivr.net/npm/us-atlas@1/us/10m.json");
us.objects.lower48 = {
type: "GeometryCollection",
geometries: us.objects.states.geometries.filter(d => d.id !== "02" && d.id !== "15")
};
return us;
}
Insert cell
data = (await FileAttachment("Mother Jones - Mass Shootings Database, 1982 - 2022 - Sheet1.json").json())
.map(d => {
const p = projection({0: d.lng, 1: d.lat})
if (d.date.length === 10) {
p.date = parseDate2(d.date)
} else if (d.date.length > 8) {
p.date = parseDate3(d.date)
} else {
p.date = parseDate(d.date)
}
return p
})

Insert cell
raw = (await FileAttachment("Mother Jones - Mass Shootings Database, 1982 - 2022 - Sheet1.json").json())
Insert cell
yearCounts = raw
.map(d => d.year).reduce((a,b) => {
a[b] = (a[b] || 0) + 1
return a
}, {})
Insert cell
import {Scrubber} from "@mbostock/scrubber"
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