render = (data) => {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("border", "1px solid #ccc");
const { nodes, links } = data;
const simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(d => d.id))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
simulation.nodes(nodes);
simulation.force("link").links(links);
simulation.alpha(1).restart();
const link = svg.append("g")
.attr("stroke", "#ebd3a2")
.selectAll("line")
.data(links)
.join("line");
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", () => _.random(4, 8))
.attr("fill", "#4e1d4c");
simulation.on("tick", () => {
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("cx", d => d.x)
.attr("cy", d => d.y);
});
return svg.node();
}