chart = {
const width = scale;
const height = width;
const cx = width * 0.5;
const cy = height * 0.5;
const radius = Math.min(width, height) / 2 + spread;
const tree = d3.tree()
.size([2 * Math.PI, radius])
.separation((a) =>1 / a.depth);
let hier = d3.hierarchy(data)
.sort((a, b) => d3.ascending(a.data.name, b.data.name));
const addNode = (targetName) => {
const targetNode = hier.descendants().find(d=>d.data.name==targetName).copy();
const emptyNode = d3.hierarchy({name:'', id:targetName});
hier.each(d=>{
if(d.children && d.children.find(e=>e.data.name == targetName)){
d.children = d.children.map(child => child.data.name == targetName ? emptyNode : child);
}
if(d.data.id==targetName){
d.children = [targetNode]
}
});
hier = d3.hierarchy(hier);
hier.each(d=>d.data = d.data.data)
}
console.log('---')
console.log(hier)
const leaves = hier.leaves();
const depths = leaves.map(leaf => leaf.depth);
const maxDepth = depths.sort().pop();
const lnames = leaves.map(leaf => leaf.data.name)
console.log(maxDepth);
const nodesToPush = [];
leaves.forEach(leaf =>{
if(leaf.depth < maxDepth && leaf.depth > limit) {
console.log('---')
// console.log(leaf)
// console.log(leaf.parent.parent.children)
// if parent have siblings that don't have grandchild, then push down grandparent.
// i.e. if you can find a grandchild in the parent's siblings, i.e. a cousin's children,
// then don't push grandparent
//if (leaf.parent.parent.children
// && ! leaf.parent.parent.children
// .find(parentSibling => parentSibling.children &&
// parentSibling.children.find(cousin => cousin.children)
// )
//) console.log(leaf); // nodesToPush.push(leaf.parent.parent);
//if (!leaf.parent.parent.descendants().find(node=>node.depth-leaf.parent.parent.depth>2)) nodesToPush.push(leaf.parent.parent)
// if siblings (parent.children) have a child, i.e. there are not leaves, then push down leaf
if (leaf.parent.children.find(child => child.children )) nodesToPush.push(leaf);
// if siblings have no child, then push parent with all siblings leaf.
else if (!nodesToPush.includes(leaf.parent)) nodesToPush.push(leaf.parent)
}
})
nodesToPush.forEach(node => {
addNode(node.data.name);
})
// Buiding the coordinates
const root = tree(hier);
console.log(root)
// Creates the SVG container.
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-cx, -cy, width, height])
.attr("style", "width: 100%; height: auto; font: 10px sans-serif;");
// Append links.
svg.append("g")
.attr("fill", "none")
.attr("stroke", "#555")
.attr("stroke-opacity", 0.4)
.attr("stroke-width", 1.5)
.selectAll()
.data(root.links())
.join("path")
.attr("d", d3.linkRadial()
.angle(d => d.x)
.radius(d => d.y));
// Append nodes.
svg.append("g")
.selectAll()
.data(root.descendants())
.join("circle")
.attr("transform", d => `rotate(${d.x * 180 / Math.PI - 90}) translate(${d.y},0)`)
.attr("fill", d => d.children ? "#555" : "#999")
.attr("r", d => d.data.name ? 2.5 : 0);
// Append labels.
svg.append("g")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 3)
.selectAll()
.data(root.descendants())
.join("text")
.attr("transform", d => `rotate(${d.x * 180 / Math.PI - 90}) translate(${d.y},0) rotate(${d.x >= Math.PI ? 180 : 0})`)
.attr("dy", "0.31em")
.attr("x", d => d.x < Math.PI === !d.children ? 6 : -6)
.attr("text-anchor", d => d.x < Math.PI === !d.children ? "start" : "end")
.attr("paint-order", "stroke")
.attr("stroke", "white")
.attr("fill", "currentColor")
.text(d => d.data.name);
return svg.node();
}