function dendrogram (data,options = {}) {
const {
width: width = width,
height: height = height,
innerHeight = height,
linkColor: linkColor = linkColor,
} = options;
const svg = d3
.create("svg")
.attr("width", width )
.attr("height", height )
const root = d3.hierarchy(data)
const maxHeight = root.data.height;
const clusterLayout = d3.cluster().nodeSize([1, width / root.height])
clusterLayout(root)
const allNodes = root.descendants().reverse()
const leafs = allNodes.filter(d => !d.children)
leafs.sort((a,b) => a.x - b.x)
const leafHeight = height / leafs.length
leafs.forEach((d,i) => d.x = i*leafHeight + leafHeight/2)
allNodes.forEach(node => {
if (node.children) {
node.x = d3.mean(node.children, d => d.x)
}})
function transformX(data) {
const height = width ;
return height - (data.data.height / maxHeight) * height;
}
root.links().forEach((link,i) => {
svg
.append("path")
.attr("class", "link")
.attr("stroke", link.source.color || linkColor)
.attr("fill", "none")
.attr("d", elbow(link))
})
function elbow(d) {
return ("M" + transformX(d.source) + "," + d.source.x + "V" + d.target.x + "H" +transformX(d.target))
}
return svg.node()
}