Public
Edited
Jan 5, 2024
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, 975, 610]);

// Legend.
svg.append("g")
.attr("transform", "translate(610,20)")
.append(() => legend({color, title: "Cumulative deaths per million", width: 260}));

// Color states.
svg.append("g")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.join("path")
.attr("fill", d => color(currentData.has(d.properties.name) ? currentData.get(d.properties.name) : 0))
.attr("d", path)
.append("title")
.text(d => `${d.properties.name}
${d3.format(".1f")(currentData.has(d.properties.name) ? currentData.get(d.properties.name) : 0)}`);

// Draw state boundaries.
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("d", path);

return svg.node();
}
Insert cell
color = d3.scaleSequentialPow()
.exponent(0.5)
.domain([0, maxDeathsPerMillion])
.interpolator(d3.interpolatePlasma)
Insert cell
covid19raw = d3.csvParse(
await FileAttachment("Deaths per million - States (COVID Tracking Project).csv").text(),
function(d) {
return {
yyyymmdd: d["YYYY-MM-DD"],
state: d.State,
deathsPerMillion: +d["Deaths per million"]
}
}
)
Insert cell
// A nested d3.map() with
// YYYYMMDD => {
// State => Deaths per million
// }
deathsPerMillion = d3.nest()
.key(d => d.yyyymmdd)
.key(d => d.state)
.rollup(v => d3.sum(v, d => d.deathsPerMillion))
.map(covid19raw)
Insert cell
currentData = deathsPerMillion.get(date)
Insert cell
maxDeathsPerMillion = d3.max(covid19raw, row => row.deathsPerMillion)
Insert cell
yyyymmdds = deathsPerMillion.keys().filter(s => s.match(/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/)).sort()
Insert cell
path = d3.geoPath()
Insert cell
us = FileAttachment("states-albers-10m.json").json()
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@5")
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more