I have a D3JS tree and managed to download it as an SVG file, however, I want the viewBox size to vary based on the svg box size to allow for better visualization. there are recommendation to use getElementById in conjunction with getBBox. I am not able to figure out how to pass it the correct svg content to getElementById and I am getting null for any input I provide.
As I was doubting my code, I tried it with a standard D3JS tree example to experiment with the functions usage, but still could not figure out how to use these functions.
My svg select is
var svg = d3.select("div.svg-container-ar").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.call(zoom)
.on("wheel.zoom", null) //disable zooming on mouse wheel scroll
.on("dblclick.zoom", null)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")scale(.7,.7)");
zoom.translate([margin.left, margin.top]).scale(.7);
and my usage of getElementById that is retuning null is
// This event is used to download SVG document
$('#download-SVG').click(function() {
var svgElement = document.getElementById('svg');
// console.log(svgElement);
let {width, height} = svgElement.getBBox();
.
.
}
I would appreciate any help on this, or if there is another way to achieve variable viewBox size requirement.