I'm recreating a D3 Tidy Tree with this example. I generate my own JSON and feed it to the tree. This works fine, except I can't figure out a way to adjust the width of the tree to the depth of the data.
Even in the examples and in the documentation, the width of the SVG graphic is just hardcoded. From the Observable example:
let width = 1954
// create the D3 hierarchy using width
tree = data => {
const root = d3.hierarchy(data);
root.dx = 10;
root.dy = width / (root.height + 1);
return d3.tree().nodeSize([root.dx, root.dy])(root);
}
// create the SVG graphic using width
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, x1 - x0 + root.dx * 2]);
But this looks very weird if the data is much smaller or larger than expected. Is this a glitch in D3? How can I adjust the size of a D3 SVG to its own contents? Am I overlooking something in the documentation?