chart = {
const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height]);
applySimulation(nodes, links);
let link = svg.append("g")
.selectAll("line")
.data(links)
.join("line")
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y)
.attr("stroke-dasharray", "0 5")
.attr("stroke-linecap", "round")
.attr("stroke-width", 4)
.attr("stroke", d => d.source.color)
let node = svg.append("g")
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("r", 20)
.attr("stroke-width", "2px")
.attr("stroke", "#000")
.attr('cursor', 'pointer')
.style("fill", d => d.color)
.on("click", (e, d) => addNodes(d))
function addNodes(parent) {
const { color, r, id } = parent;
Array.from({length: nodesToAdd}, () => {
const newId = uuid();
nodes.push({ id: newId, color, r: parent.r + 300 });
links.push({ source: id, target: newId });
})
applySimulation(nodes, links)
node = node
.data(nodes)
.join("circle")
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("r", 20)
.attr("stroke-width", "2px")
.attr("stroke", "#000")
.attr('cursor', 'pointer')
.style("fill", d => d.color)
.on("click", (e, d) => addNodes(d))
link = link
.data(links)
.join("line")
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y)
.attr("stroke-dasharray", "0 5")
.attr("stroke-linecap", "round")
.attr("stroke-width", 4)
.attr("stroke", d => d.source.color)
}
return svg.node();
}