chart = {
const root = d3.hierarchy(data);
root.x0 = 0;
root.y0 = dy/2;
root.descendants().forEach((d, i) => {
d.id = i;
d._children = d.children;
if (d.depth) d.children = null;
});
const svg = d3.select(DOM.svg(width, width))
.style("width", "100%")
.style("height", "auto")
.style("padding", "10px")
.style("box-sizing", "border-box")
.style("font", "10px sans-serif")
.style("background","green");
const g = svg.append("g")
const gLink = g.append("g")
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-opacity", 0.4)
.attr("stroke-width", 1.5);
const gNode = g.append("g")
.attr("cursor", "pointer")
.attr("pointer-events", "all")
const zoomBehaviours = d3.zoom()
.scaleExtent([0.05, 3])
.on('zoom', () => g.attr('transform', d3.event.transform));
svg.call(zoomBehaviours);
setTimeout(() => zoomBehaviours.translateTo(svg, 0, 0), 100);
function update(source) {
const duration = d3.event && d3.event.altKey ? 2500 : 250;
const nodes = root.descendants().reverse();
const links = root.links();
tree(root);
const transition = svg.transition()
.duration(duration)
.tween("resize", window.ResizeObserver ? null : () => () => svg.dispatch("toggle"));
const node = gNode.selectAll("g")
.data(nodes, d => d.id);
const nodeEnter = node.enter().append("g")
.attr("transform", d => `translate(${source.y0},${source.x0})`)
.attr("fill-opacity", 0)
.attr("stroke-opacity", 0)
.on("click", d => {
d.children = d.children ? null : d._children;
update(d);
if(d3.event && d3.event.altKey){
setTimeout(() => {zoomToFit();}, duration + 100);
}
});
const nodeShape = nodeEnter.append("circle")
.attr("r", 8)
.attr("fill", d => d._children ? "white" : "green")
.attr("stroke-width", 10);
nodeEnter.append("text")
.attr("dy", "1em")
.attr("x", d => d._children ? -6 : 6)
.attr("text-anchor", d => d._children ? "end" : "start")
.text(d => d.data.name)
.attr("stroke-linejoin", "round")
.attr("stroke-width", 3)
.style('fill', '#F5F5F5');
const nodeUpdate = node.merge(nodeEnter).transition(transition)
.attr("transform", d => `translate(${d.y},${d.x})`)
.attr("fill-opacity", 1)
.attr("stroke-opacity", 1);
const nodeExit = node.exit().transition(transition).remove()
.attr("transform", d => `translate(${source.y},${source.x})`)
.attr("fill-opacity", 0)
.attr("stroke-opacity", 0);
const link = gLink.selectAll("path")
.data(links, d => d.target.id);
const linkEnter = link.enter().append("path")
.attr("d", d => {
const o = {y: source.x0, x: source.y0};
return diagonal({source: o, target: o});
});
link.merge(linkEnter).transition(transition)
.attr("d", diagonal);
link.exit().transition(transition).remove()
.attr("d", d => {
const o = {y: source.x, x: source.y};
return diagonal({source: o, target: o});
});
root.eachBefore(d => {
d.y0 = d.x;
d.x0 = d.y;
});
}
function zoomToFit(paddingPercent) {
const bounds = g.node().getBBox();
const parent = svg.node().parentElement;
const fullWidth = parent.clientWidth;
const fullHeight = parent.clientHeight;
const width = bounds.width;
const height = bounds.height;
const midX = bounds.x + (width / 2);
const midY = bounds.y + (height / 2);
if (width == 0 || height == 0) return;
const scale = (paddingPercent || 0.75) / Math.max(width / fullWidth, height / fullHeight);
const translate = [fullWidth / 2 - scale * midX, fullHeight / 2 - scale * midY];
const transform = d3.zoomIdentity
.translate(translate[0], translate[1])
.scale(scale);
svg
.transition()
.duration(500)
.call(zoomBehaviours.transform, transform);
}
update(root);
return svg.node();
}