chart = {
const root = tree(data);
let x0 = Infinity;
let x1 = -x0;
root.each(d => {
if (d.x > x1) x1 = d.x;
if (d.x < x0) x0 = d.x;
});
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, x1 - x0 + root.dx * 2]);
const g = svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("transform", `translate(${root.dy / 3},${root.dx - x0})`);
const link = g.append("g")
.attr("fill", "none")
.attr("stroke", "purple")
.attr("stroke-opacity", 0.2)
.attr("stroke-width", 1.5)
.selectAll("path")
.data(root.links())
.join("path")
.attr('d', d3.linkHorizontal()
.x(d => d.y)
.y(d => d.x));
const node = g.append("g")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 3)
.selectAll("g")
.data(root.descendants())
.join("g")
.attr("transform", d => `translate(${d.y},${d.x})`);
node.append("rect")
.attr("fill", d => d.children ? "blue" : "orange")
.attr("width", d => d.children ? 0 : d.data.value*60)
.attr("opacity", 0.6)
.attr("height", d => d.children ? 5 : 8);
node.append("text")
.attr("dy", ".5em")
.attr("x", d => d.children ? -6 : -6)
.attr("text-anchor", d => d.children ? "end" : "end")
.text(d => d.data.name)
.clone(true).lower()
.attr("stroke", "white");
return svg.node();
}