Published
Edited
May 6, 2021
Fork of Choropleth
1 fork
2 stars
Insert cell
Insert cell
Insert cell
mutable z = 42
Insert cell
chart = {
const width = 975;
const height = 610;
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const zoom = d3.zoom()
.scaleExtent([1, 8])
.on("zoom", zoomed);

// Apply zoom listeners to the SVG
svg.call(zoom)
.on("wheel.zoom", null) // ignore wheel events
// .on("click dblclick", dblclicked);
const g = svg.append("g");

// States
g.append("g").attr("id", "states").selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.join("path")
.attr("stroke", "steelblue")
.attr("stroke-linejoin", "round")
.attr("fill", "white")
.attr("d", path)
.on("mouseover", function(_, d_i) {
mutable z = d_i;
g.selectAll("#states path").sort((_, d_j) => d_j == d_i ? -1 : 0);
d3.select(this).attr("stroke-width", 3)
})
.on("mouseout", function() { d3.select(this).attr("stroke-width", null) })
.on("click", clicked)
.on("dblclick", dblclicked);
// Counties
g.append("g").attr("id", "counties");
//all_counties();
svg.append("g")
.attr("transform", "translate(610,20)")
.append(() => legend({color, title: data.title, width: 260}));
function clicked(event, d) {
event.stopPropagation();
const geojson = topojson.feature(us, states.get(d.id.slice(0, 2)));
const [[x0, y0], [x1, y1]] = path.bounds(geojson);
const state = d.id;
g.select("#counties").selectAll("path")
.data(topojson.feature(us, us.objects.counties).features.filter(d => d.id.slice(0,2) == state))
.join("path")
.attr("fill", d => color(data.get(d.id)))
.attr("d", path)

g.transition().duration(750).call(
zoom.transform,
d3.zoomIdentity
.translate(width / 2, height / 2)
.scale(Math.min(8, 0.9 / Math.max((x1 - x0) / width, (y1 - y0) / height)))
.translate(-(x0 + x1) / 2, -(y0 + y1) / 2),
d3.pointer(event, svg.node())
);
}
function dblclicked(event, d) {
event.stopPropagation();
g.transition().duration(750).call(
zoom.transform,
d3.zoomIdentity,
d3.pointer(event, svg.node())
)//.on("end", () => all_counties());
}
function zoomed(event) {
mutable z = event;
const {transform} = event;
g.attr("transform", transform);
g.attr("stroke-width", 1 / transform.k);
}
function all_counties() {
g.select("#counties").selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.join("path")
.attr("d", path)
.attr("fill", d => color(data.get(d.id)))
}

return svg.node();
}
Insert cell
data = Object.assign(new Map(d3.csvParse(await FileAttachment("unemployment-x.csv").text(), ({id, rate}) => [id, +rate])), {title: "Unemployment rate (%)"})
Insert cell
color = d3.scaleQuantize([1, 10], d3.schemeBlues[9])
Insert cell
path = d3.geoPath()
Insert cell
format = d => `${d}%`
Insert cell
states = new Map(us.objects.states.geometries.map(d => [d.id, d])) // we'll use "d.geometry" for zooming
Insert cell
us = FileAttachment("counties-albers-10m.json").json()
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@6")
Insert cell
import {legend} from "@d3/color-legend"
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