function cp_chart(
data,
{
children,
space = 300,
width = 500 + space,
height = 500 + space / 2,
fill = "white",
fillOpacity = 0.9,
stroke = "white",
padding = 3,
vb_pad = 30,
strokeWidth,
strokeOpacity,
pad = { top: 20, bottom: 50, left: 15, right: 20 }
} = {}
) {
if (!(data instanceof d3.hierarchy)) throw "Data is not a d3.hierarchy";
const W = width - width / 10;
const H = height - height / 10;
const color = d3
.scaleOrdinal()
.domain(Array.from(groupes))
.range(d3.schemeTableau10);
function pack_data2() {
return d3.pack().size([W, H]).padding(padding)(
data
);
}
const packed = pack_data2();
const descendants = packed.descendants();
const feuilles = packed.leaves();
feuilles.forEach((d, i) => (d.index = i));
let title = (d) =>
d.height == 0
? `${d.parent.data[0]}\n${[...d.data[0].split(",")]}\n`
: `${d.data[0]}\n`;
const svg = d3
.create("svg")
.attr("viewBox", [-5, -5, width, height])
.attr("font-size", 13)
.attr("font-family", "sans-serif")
.style("fill", "none")
.style("stroke-width", ".03px");
const leaf = svg
.selectAll("g")
.data(descendants)
.enter()
.append("g")
.attr("transform", (d) => `translate(${d.x},${d.y})`)
.attr("class", (d) => d.data[0]);
const circle = leaf
.append("circle")
.attr("r", (d) => d.r)
.attr("fill", (d) => (d.children ? (d.height == 1 ? null : "black") : fill))
.attr("fill-opacity", (d) => (d.children ? null : fillOpacity))
.attr("stroke", (d) => (d.height == 1 ? color(d.data[0]) : null))
.attr("stroke-width", (d) =>
d.height == 1 ? `${d3.max([4, padding - 3])}px` : null
)
.on("click", handleClick)
.on("mouseover", (e, d) => {
handleMouseOverCircle(e, d, svg, width, height, vb_pad, color);
})
.on("mouseout", (e, d) => {
if (d.depth <= 1) handleMouseOutCircle(svg);
})
.append("title")
.text((d) => (d.height < 2 ? title(d) : null));
initClef(svg, vb_pad, color);
return svg.node();
}