Search code examples
graphvizpygraphviz

Converting between pixels and graphviz sizes/coordinates


How can I translate the coordinates of a PNG render of a graph, to the coordinates I get from -Tjson or -Tjson0? For example, I have a PNG that's 167 x 251, but the .json file says the bounding box (bb) is 117 x 180. I need to be able to display a graph, click somewhere on the graph, and have Python respond with the node clicked, but I can't figure out this conversion.


Solution

  • Graphviz uses "points" (pts) - 72/inch as the unit for pos and bb values (somewhere on this page -https://graphviz.org/doc/info/attrs.html- you will find this: At present, most device-independent units are either inches or points, which we take as 72 points per inch.)
    Furthermore the default for output is set to 96 dpi (https://graphviz.org/docs/attrs/dpi/)

    So bb dimensions /72 should equal png dimensions /96.
    Almost, but not quite!

    As a guess, the last bit needed to match the two sets of dimensions, is to factor in pad (https://graphviz.org/docs/attrs/pad/). Add 4 points (4/72 inches) to every Graphviz pos value and 8 points (8/72 to the bb total width & height)