function createChart(parent) {
const root = pack(data);
const leaves = root.leaves().filter(d => d.depth && d.value);
const svg = d3
.select(parent)
.append("svg")
.style("background", "#fff")
.style("font", "10px sans-serif")
.attr("text-anchor", "middle")
.attr("viewBox", [0, 0, width, height])
.attr("preserveAspectRatio", "xMinYMin meet");
svg.append("g")
.selectAll("circle")
.data(leaves)
.enter().append("circle")
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("r", d => d.r)
.attr("fill", d => color(d.data.name))
.style("cursor", "pointer")
.on("click", (d) => {
d3.select("#my-modal").node().classList.toggle("closed");
d3.select("#my-modal-overlay").node().classList.toggle("closed");
d3.select("#my-modal").select("h1").text(d.data.name);
d3.select("#my-modal").select("p").text(format(d.data.count));
d3.event.stopPropagation();
});
svg.append("g")
.attr("pointer-events", "none")
.selectAll("text")
.data(leaves)
.enter().append("text")
.attr("transform", d => {
const {lines, radius} = fit(d.data.name, isNaN(d.data.count) ? undefined : format(d.data.count));
d.lines = lines;
if (!isNaN(d.data.count)) d.lines[d.lines.length - 1].count = true;
return `translate(${d.x},${d.y})`;
})
.selectAll("tspan")
.data(d => d.lines)
.enter().append("tspan")
.attr("x", 0)
.attr("y", (d, i, data) => (i - data.length / 2 + 0.8) * 11)
.text(d => d.text)
.filter(d => d.value)
.attr("font-weight", 300)
.attr("fill-opacity", 0.5);
return svg.node();
}