dominikbraun / graph

A library for creating generic graph data structures and modifying, analyzing, and visualizing them.
https://graph.dominikbraun.io
Apache License 2.0
1.82k stars 96 forks source link

Global attributes for DOT #118

Open jimidle opened 1 year ago

jimidle commented 1 year ago

It is often convenient to give some default options for graph, nodes and edges, as in:

strict digraph {
    graph [bgcolor = gray10, fontcolor = white, fontname = "Arial"]
    node [shape = circle, color = green4, style = filled, fillcolor = cadetblue1, fontname = "Arial", fontsize=12, fixedsize=false, penwidth=3]
    edge [color = white, fontcolor = white, fontname = "Arial", fontsize = 12, penwidth=3]

      6 [rank="max" label="JIM" ]
      ...

But as things stand, this cannot be achieved. If you try and "fool" it, as so:

...
draw.GraphAttribute("graph", "[bgcolor = gray10, fontcolor = white, fontname = Arial]")
...

Then you get:

...
graph="[bgcolor = gray10, fontcolor = white, fontname = Arial]"
...

Which is obviously incorrect syntax, and it is easy to see why from the template of course. This would require an extra type or two, which I would have called GraphAttribute, NodeAttribute, etc. But of course these functional options names have been taken. Perhaps just:

 WithDefaults("graph", "[x=y, b=c, ...]"

This seems like a fairly easy thing to implement and is preferable to having to add attributes to every individual node and edge.

dominikbraun commented 1 year ago

Thanks a lot, great suggestion! I'll add this in one of the next releases.