What is the best way to scale and center a graph using d3-graphviz? I was hopeful that I could use scale(0.5) but this leaves the resulting graph uncentered.
I could probably go in with an .attributer() and manually adjust the <svg> and <g> elements to get what I'm looking for, but I figured there was probably a better way?
d3.select("#graph")
.graphviz()
.width(300)
.height(300)
.fit(true)
.scale(.5)
.renderDot('digraph {a -> b}');
<script src="//d3js.org/d3.v5.min.js"></script>
<script src="https://unpkg.com/@hpcc-js/[email protected]/dist/index.min.js"></script>
<script src="https://unpkg.com/[email protected]/build/d3-graphviz.js"></script>
<div id="graph" style="width: 300px; height: 300px; border: 1px solid black"></div>
Based on magjac's comment, I skipped
.fit(),.scale(),.width(), and.height()and did it all in the attributer. This allows the solution to work even for larger graphs.A few things to note:
<svg>to100%allows us to skip.width()and.height()and have the<svg>fill its container div.scalevariable that can be set (0-1) to determine the scale of the graphThank you magjac for this awesome library!