chart = {
const width = 975;
const height = 610;
const zoom = d3
.zoom()
.scaleExtent([1, 10])
.on("zoom", zoomed);
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
const g = svg.append("g");
g.append("g")
.attr("fill", "lightgray")
.attr("stroke", "black")
.selectAll("path")
.data(nationGeoJSON.features)
.join("path")
.attr("d", path);
const nation = g
.append("g")
.attr("fill", "transparent")
.attr("stroke", "black")
.attr('stroke-width', 1.5)
.attr("cursor", "pointer")
.selectAll("path")
.data(nationGeoJSON.features)
.join("path")
.attr("d", path);
const nationOriginalPath = nation.attr('d');
const states = g
.append("g")
.attr("fill", "transparent")
.attr("cursor", "pointer")
.selectAll("path")
.data(statesGeoJSON.features)
.join("path")
.on("mouseenter", clicked)
.attr("d", path);
svg.call(zoom);
function reset() {
console.log('reset');
const stateToNationInterpolator = flubber.interpolate(
nation.attr('d'),
nationOriginalPath
);
nation
.transition()
.duration(1250)
.attrTween("d", function() {
return stateToNationInterpolator;
});
}
function clicked(event, d) {
const [[x0, y0], [x1, y1]] = path.bounds(d);
event.stopPropagation();
const nationToStateInterpolator = flubber.interpolate(
nation.attr('d'),
path(d)
);
nation
.transition()
.duration(150)
.attrTween("d", function() {
return nationToStateInterpolator;
});
}
function zoomed(event) {
const { transform } = event;
g.attr("transform", transform);
g.attr("stroke-width", 1 / transform.k);
}
return svg.node();
}