chart = {
console.log('root', root)
const nodeSize = 16
const nodes = root.descendants()
console.log('nodes', nodes)
const links = root.links()
console.log('links', links)
const svg = d3.create('svg')
.attr('viewBox', [
-nodeSize / 2,
-nodeSize * 3 / 2,
width,
(nodes.length + 1) * nodeSize
])
.attr('font-family', 'sans-serif')
.attr('font-size', 10)
.style('overflow', 'visible');
const link = svg.append('g')
.attr('fill', 'none')
.attr('stroke', '#999')
.selectAll('path')
.data(links)
.join('path')
.attr('d', d => `
M${d.source.depth * nodeSize},${d.source.index * nodeSize}
V${d.target.index * nodeSize}
h${nodeSize}
`);
const gNode = svg.append('g')
.selectAll('g')
.data(nodes)
.join('g')
.attr('transform', d => `translate(0,${d.index * nodeSize})`);
gNode.append('circle')
.attr('cx', d => d.depth * nodeSize)
.attr('r', 2.5)
.attr('fill', d => d.children ? null : '#999');
gNode.append('text')
.attr('dy', '0.32em')
.attr('x', d => d.depth * nodeSize + 6)
.text(d => d.data.data ? d.data.data.Name : d.data.Name);
gNode.append('title')
.text(d => d.ancestors()
.reverse()
.map(d => d.data.data ? d.data.data.Name : d.data.Name)
.join('/')
);
console.debug('root.copy().descendants()', root.copy().descendants())
for (const {label, value, x} of columns) {
let svgColumnLabel = svg.append('text')
.attr('dy', '0.32em')
.attr('y', -nodeSize)
.attr('x', x)
.attr('font-weight', 'bold')
.text(label);
let svgColumnData = gNode.append('text')
.attr('dy', '0.32em')
.attr('x', x)
.data(root.copy().descendants())
.text(d => value(d) );
if (label == '# Children') {
svgColumnLabel.attr('text-anchor','end')
svgColumnData.attr('text-anchor','end')
}
}
return svg.node();
}