chart = {
const width = 975;
const height = 610;
const zoom = d3.zoom()
.scaleExtent([1, 8])
.on("zoom", zoomed);
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.on("click", reset);
const g = svg.append("g");
const counties = g.append("g").attr("class", "counties");
const states = g.append("g")
.attr("cursor", "pointer")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.join("path")
.attr("fill", d => color(state_data.get(d.id)))
.attr("d", path)
.attr("stroke", "black")
.on("click",clicked);
function draw_counties(d){
let state_id = d.id;
let state_counties = us.objects.counties.filter(d => d.geometries.id.slice(0, 2) == state_id);
mutable z = state_counties;
// counties
// .selectAll("path")
// .data(topojson.feature(us, us.objects.counties).features)
// .join("path")
// .attr("fill", d => color(county_data.get(d.id)))
// .attr("d", path)
// .attr("stroke", "black")
// .append("title")
// .text(d => `${d.properties.name}\n`
// + `${format((county_data.get(d.id))*100)}\n`
// + `${filtered_current_states_covid_data.values.name}`);
}
// svg.call(zoom);
svg.append("g")
.attr("transform", "translate(610,20)")
.append(() => legend({color, title: state_data.title, width: 260}));
function zoomed({transform}) {
g.attr("transform", transform);
}
function reset() {
states.transition().style("fill", null);
svg.transition().call(
zoom.transform,
d3.zoomIdentity,
d3.zoomTransform(svg.node()).invert([width / 2, height / 2])
);
}
function clicked(event, d) {
const [[x0, y0], [x1, y1]] = path.bounds(d);
event.stopPropagation();
states.transition().style("fill", null);
d3.select(this).transition().style("fill", "red");
mutable s = d3.select(this).data()[0];
// counties();
svg.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())
);
draw_counties(d);
}
return Object.assign(svg.node(), {
zoomIn: () => svg.transition().call(zoom.scaleBy, 2),
zoomOut: () => svg.transition().call(zoom.scaleBy, 0.5),
// zoomTo: () => zoomToState(),
zoomReset: () => reset(),
});
}