chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, 975, 610]);
function moveTooltip(event, d) {
let [x, y] = path.centroid(d);
console.log(x, width);
if (x > 700) {
x -= 200;
y -= 15;
} else {
x += 20;
y -= 15;
}
tooltip.attr("transform", `translate(${x}, ${y})`);
}
svg
.selectAll(".county")
.data(counties.features)
.join("path")
.attr("class", "county")
.style("stroke", "#ccc")
.style("fill", (d) => color(lookup[d.id]))
.attr("d", (d) => path(d))
.on("mouseover", (event, d) => {
tooltip.style("visibility", "visible");
moveTooltip(event, d);
const pct = (lookup[d.id] * 100).toFixed(1);
tooltipText.text(`${d.properties.name}: ${pct}%`);
})
.on("mousemove", (event, d) => {
moveTooltip(event, d);
})
.on("mouseout", () => tooltip.style("visibility", "hidden"));
svg
.selectAll(".state")
.data(states.features)
.join("path")
.attr("class", "state")
.style("stroke", "black")
.style("fill", "none")
.attr("d", (d) => path(d));
const tooltip = svg.append("g");
const tooltipRect = tooltip
.append("rect")
.style("stroke", "black")
.style("fill", "rgba(0, 0, 0, 0.5)")
.attr("pointer-events", "none")
.attr("rx", 10)
.attr("width", 180)
.attr("height", 30);
const tooltipText = tooltip
.append("text")
.style("fill", "white")
.style("font-family", "Arial")
.style("font-weight", "bold")
.style("font-size", "12pt")
.attr("x", 10)
.attr("y", 21);
return svg.node();
}