Published
Edited
Jul 30, 2019
Insert cell
Insert cell
chart = {
const width = 960;
const height = 600;
const path = d3.geoPath();
const formatNumber = d3.format(",.0f");
const radius = d3.scaleSqrt().domain([0, 1e6]).range([0, 15]);

const svg = d3.select(DOM.svg(width, height))
.style("width", "100%")
.style("height", "auto")
.style("background", "#4cdaed");

svg.append("path")
.datum(topojson.feature(us, us.objects.nation))
.attr("fill", "white")
.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", "black")
.attr("transform", `translate(${width - 50},${height - 2})`)
.attr("text-anchor", "middle")
.style("font", "10px sans-serif")
.selectAll("g")
.data([1e6, 5e6, 1e7])
.join("g");

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

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

svg.append("g")
.attr("fill", "#4cdaed")
.attr("fill-opacity", 0.5)
.attr("stroke", "white")
.attr("stroke-width", 0.5)
.selectAll("circle")
.data(topojson.feature(us, us.objects.counties).features
.map(d => (d.population = population.get(d.id), d))
.sort((a, b) => b.population - a.population))
.join("circle")
.attr("transform", d => `translate(${path.centroid(d)})`)
.attr("r", d => radius(d.population))
.append("title")
.text(d => formatNumber(d.population));

return svg.node();
}
Insert cell
population = new Map((await d3.json("https://api.census.gov/data/2016/acs/acs5/cprofile?get=CP05_2012_2016_001E&for=county:*")).slice(1).map(([population, state, county]) => [state + county, +population]))
Insert cell
us = d3.json("https://unpkg.com/us-atlas@1/us/10m.json")
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@5")
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