chart = {
const root = pack(data);
console.log(root);
const simulation = d3
.forceSimulation(root.leaves())
.force("x", d3.forceX(width / 2).strength(0.5))
.force("y", d3.forceY(height / 2).strength(0.5))
.force('charge', d3.forceManyBody().strength(500))
.force(
"collision",
d3
.forceCollide()
.radius(function(d) {
return d.r + 2;
})
.strength(0.2)
);
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.attr("font-size", 20)
.attr("font-family", "OpenSans")
.attr("font-weight", "bold")
.attr("text-anchor", "middle");
const leaf = svg
.selectAll("g")
.data(root.leaves())
.join("g")
.attr("transform", d => `translate(${d.x + 1},${d.y + 1})`);
leaf
.append("circle")
.attr("r", d => d.r)
.attr("fill-opacity", 0.25)
.attr("fill", d => d.data.color);
leaf
.append("text")
.attr("fill", d => d.data.color)
.selectAll("tspan")
.data(d => d.data.title.split(/(?=[A-Z][^A-Z])/g))
.join("tspan")
.attr("x", 0)
.attr("y", (d, i, nodes) => `${i - nodes.length / 2 + 0.8}em`)
.text(d => d);
simulation.on("tick", () => {
console.log(root.leaves());
leaf.attr("transform", d => `translate(${d.x + 1},${d.y + 1})`);
});
invalidation.then(() => simulation.stop());
return svg.node();
}