{
const width = 975;
const height = 610;
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto;");
const states = topojson.feature(us, us.objects.states);
const statemap = new Map(states.features.map(d => [d.id, d]));
const path = d3.geoPath();
const color = d3.scaleQuantize([1, 10], d3.schemeBlues[9]);
svg.append("g")
.selectAll("path")
.data(states.features)
.join("path")
.attr("fill", d => color(valuemap.get(d.id)))
.attr("d", path)
.on("pointerenter pointermove", pointermoved)
.on("pointerleave", pointerleft)
.on("touchstart", event => event.preventDefault());
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "white")
.attr("d", path);
const legendWidth = 260;
svg.append("g")
.attr("transform", `translate(${width-legendWidth - 100}, 20)`)
.append(() => Legend(color, {title: "Unemployment rate(%)", width: legendWidth}));
const tooltip = svg.append("g");
function pointermoved(event) {
if (event == null) return;
const pointer = d3.pointer(event);
const pointerY = pointer[1]
tooltip.style("display", null);
tooltip.attr("transform", `translate(${pointer[0]}, ${pointerY})`);
const data = d3.select(this).data()[0];
const stateName = data['properties']['name'];
const stateUnemploymentRate = valuemap.get(data['id']);
const text = tooltip.selectAll("text")
.attr("text-anchor", 'middle')
.data([,])
.join("text")
.call(text => text.selectAll("tspan")
.data([stateName, stateUnemploymentRate])
.join("tspan")
.attr("x", 0)
.attr("y", (d, i) => (pointerY + 50 > height) ? `-${1.1 * i + 1.6}em` : `${1.1 * i + 1.6}em`)
.attr("font-weight", (_, i) => i ? null : "bold")
.style("opacity", (_, i) => i ? 0.5 : 0.6)
.text(d => d));
}
function pointerleft() {
tooltip.style("display", "none");
}
return svg.node();
}