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);
svg.call(zoom)
.on("wheel.zoom", null)
const g = svg.append("g");
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);
g.append("g").attr("id", "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())
)
}
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();
}