Having obtained a D3JS tree and successfully saved it as an SVG file, my goal is to adjust the viewBox size based on the svg box size for better visualization. Suggestions include using getElementById along with getBBox.
I am struggling to correctly pass the svg content to getElementById, resulting in receiving null for any input I provide.
To test my code, I attempted it with a standard D3JS tree example to try out the functions but still couldn't grasp their usage.
The select statement for my svg 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)
.on("dblclick.zoom", null)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")scale(.7,.7)");
zoom.translate([margin.left, margin.top]).scale(.7);
My attempt at using getElementById that results in null:
// 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 greatly appreciate any guidance on this issue, or if there exists another method to achieve the variable viewBox size requirement.