chart = {
console.clear();
const width = 975;
const height = 610;
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
const usStatesGroup = svg
.append("g")
.attr("fill", "#444")
.attr("cursor", "pointer")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.join("path")
.on("click", clicked)
.attr("d", path);
const zoom = d3.zoom().scaleExtent([1, 8]).on("zoom", zoomed);
zoom(svg);
function clicked(event, d) {
const [[x0, y0], [x1, y1]] = path.bounds(d);
event.stopPropagation();
usStatesGroup.transition().style("fill", null);
d3.select(this).transition().style("fill", "red");
const newTranstition = svg.transition().duration(750);
zoom.transform(
newTranstition,
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)
);
}
function zoomed(event) {
const { transform } = event;
usStatesGroup.attr("transform", transform);
}
return svg.node();
}