map = {
const width = 975;
const height = 610;
const path = d3.geoPath();
const states = topojson.feature(us, us.objects.states);
const nodes = [];
const links = [];
for (const f of states.features) {
if (f.id === "02" || f.id === "15" || f.id === "72") continue;
const centroid = path.centroid(f);
if (centroid.some(isNaN)) return;
f.cx = f.x = centroid[0];
f.cy = f.y = centroid[1];
nodes.push(f);
}
const delaunay = d3.Delaunay.from(nodes, (f) => f.x, (f) => f.y);
const voronoi = delaunay.voronoi([0, 0, width, height]);
for (let i = 0; i < nodes.length; i++) {
for (const j of voronoi.neighbors(i)) {
if (i < j) {
const source = nodes[i];
const target = nodes[j];
const dx = source.x - target.x;
const dy = source.y - target.y;
links.push({source, target, distance: Math.hypot(dx, dy)});
}
}
}
const simulation = d3.forceSimulation(nodes)
.alphaDecay(0)
.force("link", d3.forceLink(links).id((d) => d.id).distance((d) => d.distance))
.on("tick", ticked);
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-100, -100, width + 200, height + 200])
.attr("style", "width: 100%; height: auto; overflow: visible;");
const link = svg.append("g")
.attr("stroke", "currentColor")
.attr("stroke-width", 1.5)
.selectAll("line")
.data(links)
.join("line");
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("fill", "#777")
.attr("fill-opacity", 0.5)
.selectAll("path")
.data(nodes)
.join("path")
.attr("d", path);
node.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
function ticked() {
link
.attr("x1", (d) => d.source.x)
.attr("y1", (d) => d.source.y)
.attr("x2", (d) => d.target.x)
.attr("y2", (d) => d.target.y);
node
.attr("transform", (d) => `translate(${d.x - d.cx},${d.y - d.cy})`);
}
function dragstarted(event) {
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
}
function dragged(event) {
event.subject.fx = event.x;
event.subject.fy = event.y;
}
function dragended(event) {
event.subject.fx = null;
event.subject.fy = null;
}
invalidation.then(() => simulation.stop());
return svg.node();
}