Published
Edited
Jun 27, 2020
1 fork
19 stars
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, 975, 610]);

svg.append("path")
.datum(topojson.feature(us, us.objects.nation))
.attr("fill", "#ddd")
.attr("d", path);

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);

const legend = svg.append("g")
.attr("fill", "#777")
.attr("transform", "translate(915,608)")
.attr("text-anchor", "middle")
.style("font", "10px sans-serif")
.selectAll("g")
.data(radius.ticks(4).slice(1))
.join("g");

legend.append("circle")
.attr("fill", "none")
.attr("stroke", "#ccc")
.attr("cy", d => -radius(d))
.attr("r", radius);

legend.append("text")
.attr("y", d => -2 * radius(d))
.attr("dy", "1.3em")
.text(radius.tickFormat(4, "s"));

let circle = svg.append("g")
.attr("fill", "brown")
.attr("fill-opacity", 0.5)
.attr("stroke", "black")
.attr("stroke-width", 0.5)
.selectAll("circle");

return Object.assign(svg.node(), {
update(data) {
circle = circle
.data(
data
.filter(([id, value]) => positions.has(id) && value > 0)
.sort(([, a], [, b]) => d3.descending(a, b)),
([id]) => id
)
.join(
enter => enter.append("circle")
.attr("transform", ([id]) => `translate(${positions.get(id)})`)
.call(circle => circle.append("title"))
)
.attr("r", ([, value]) => radius(value))
.call(circle => circle.select("title").text(([id, value]) => `${names.get(id)}\n${format(value)}`));
}
});
}
Insert cell
update = chart.update(frames.get(date))
Insert cell
rawdata = d3.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv")
Insert cell
index = d3.rollup(rawdata, ([d]) => +d.cases, d => d.fips || `${d.county}, ${d.state}`, d => d.date)
Insert cell
positions = new Map(d3.groups(rawdata, id).map(([id, [d]]) => [id, position(d)]).filter(([, position]) => position))
Insert cell
names = new Map(d3.groups(rawdata, id).map(([id, [d]]) => [id, `${d.county}, ${d.state}`]))
Insert cell
id = d => d.fips || `${d.county}, ${d.state}`
Insert cell
dates = Array.from(d3.group(rawdata, d => d.date).keys())
Insert cell
frames = new Map(d3.pairs(dates, (a, b) => [b, Array.from(index, ([id, data]) => [id, Math.max(0, (data.get(b) || 0) - (data.get(a) || 0), 0)])]))
Insert cell
function position({fips, state, county}) {
if (!fips) switch (`${county}, ${state}`) {
case "New York City, New York": return projection([-74.0060, 40.7128]);
case "Kansas City, Missouri": return projection([-94.5786, 39.0997]);
}
const feature = features.get(fips);
return feature && path.centroid(feature);
}
Insert cell
parseDate = d3.utcParse("%Y-%m-%d")
Insert cell
radius = d3.scaleSqrt([0, 8000], [0, 60])
Insert cell
features = new Map(topojson.feature(us, us.objects.counties).features.map(d => [d.id, d]))
Insert cell
path = d3.geoPath()
Insert cell
projection = d3.geoAlbersUsa().scale(1300).translate([487.5, 305])
Insert cell
format = d3.format(",.0f")
Insert cell
us = FileAttachment("counties-albers-10m.json").json()
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@5", "d3-array@2")
Insert cell
import {Scrubber} from "@mbostock/scrubber"
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