function cp_chart (data, {
children,
space = 300,
width = 500+space,
height = 500+(space/2),
fill = "white",
fillOpacity = .9,
stroke = "white",
padding = 3,
vb_pad = 10,
strokeWidth,
strokeOpacity,
} = {}) {
if(!(data instanceof d3.hierarchy))
throw 'Pump the jam'
const W = width - width/10
const H = height- height/10
const groupes = d3.map(data.children, d => d.data[0])
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", [-1, -vb_pad*2, width, height])
.attr('width', width)
.attr('height', height)
.attr('font-size', 10)
.attr('font-family', 'sans-serif')
.attr('text-anchor', 'middle')
.style('border', '1px solid black')
.style('stroke', 'black')
.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})`);
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)
.append("title")
.text(d => d.height < 2 ? title(d) : null)
const clef = svg.append("clef").attr("transform", `translate(${0},10)`)
svg.selectAll("text.clefs")
.data(groupes).enter()
.append("text")
.attr("class", "clefs")
.attr("font-size", "10px")
.attr("fill", "black")
.attr("x", width - 85)
.attr("y", (d, i) => i * 25 + 20)
.attr("text-align", 'left')
.text(d => d)
svg.selectAll("rect").data(groupes)
.enter()
.append("rect")
.attr("x", width - 105)
.attr("y", (d, i) => i * 25 + 25)
.attr("width", 40)
.attr("height", 10)
.attr("fill", d => color(d))
.attr("stroke", "black")
.attr("stroke-width", '.15px')
.attr("transform", `translate(0,-25)`)
return svg.node()
}