click_and_zoom = {
const svgHeight = 0.3 * width;
const top = htl.html`
<div class="wrapper">
<div class="info">
<div class="info-label">
</div>
<div class="info-diagram">
</div>
</div>
<svg viewBox="0 0 ${width} ${svgHeight}" class="map">
</svg>
</div>
`;
const projection = d3.geoAlbersUsa();
const path = d3.geoPath().projection(projection);
projection.scale(1).translate([0, 0]);
const b = path.bounds(districts);
const s =
0.95 /
Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / svgHeight);
const t = [
(width - s * (b[1][0] + b[0][0])) / 2,
(svgHeight - s * (b[1][1] + b[0][1])) / 2
];
projection.scale(s).translate(t);
let svgMap = d3
.select(top)
.select("svg.map")
.on("click", function () {
d3.select(top)
.select("div.info-label")
.text("Click or touch a district for info");
svgMap.selectAll("path").attr("class", "district");
d3.select(top)
.select("div.info-diagram")
.html("");
});
let map = svgMap.append("g");
map
.selectAll("path")
.data(districts.features)
.enter()
.append("path")
.attr("d", path)
.attr("id", (d) => d.properties.district)
.attr("class", "district")
.on("click", function (evt, d) {
d3.selectAll("path").attr("class", "district");
d3.select(this).attr("class", "selected");
d3.select(top).select("div.info-label").text(d.properties.district);
d3.select(top).select('div.info-diagram').html('');
d3.select(top).select("div.info-diagram").append(() => spatialDetails(can_by_district.get(d.properties.district)));
evt.stopPropagation();
});
d3.select(top).select("div.info-label").text("Click or touch a district for info");
svgMap.call(
d3
.zoom()
.extent([
[0, 0],
[width, svgHeight]
])
.scaleExtent([1, 8])
.on("zoom", zoomed)
);
function zoomed({ transform }) {
map.attr("transform", transform);
}
return top;
}