Does Graphviz Have a Theme Package Feature?

A few days ago, I researched mermaid. Functionally, mermaid has more built-in types, but that’s not the main point. What I want to say is that I find the default flowchart theme of mermaid very comfortable to look at, with automatic coloring. Therefore, I am somewhat curious if Graphviz has a similar theme package feature that allows specifying a theme to automatically apply it to the images.

Since it is possible to specify the properties of a node-edge graph in the command line, can we directly store these properties in a file, and then simply read from this same file each time to maintain a consistent configuration?

Short answer: no. There are several current requests, but they are not being worked on (at least not publicly)

Longer mumble: There are several ways to approach this capability. But most are OS dependant and somewhat fiddly

  • command line specification (your idea):
    • fairly easy w/ Linux (& MACOS?), not sure on Windows
    • e.g. dot -Tpng $(cat myconfig1.txt) myfile.gv …
    • only allows one theme for all nodes and one for all edges
  • use cpp (the c preprocessor / macro language), m4 another macro language, python, gawk, or … to include multiple config files throughout your .gv file
    • e.g. #include myconfig.txt
    • must have the desired program installed on your computer
    • maybe more challenging on Windows
    • it works
    • only allows one set of node/edge configs at-a-time
  • same as above, but add macro expansion to define multiple node / edge types
    • all the benefits of the above
    • supports multiple definitions at the same time
    • starts to approach the complexity of programming in this second language
    • like so:

command line: cpp -include "stylesheet0.cpp" stylesheetTest0.gv | dot ...

stylesheetTest0.gv:

graph test{
  them [TeamA]
  others [TeamB]
  us [TeamA]

  us -- them [EdgeYes]
  us -- others [EdgeNo]
}

stylesheet0.cpp:

#define TeamA   shape=circle style=filled fillcolor=lightblue color=red fontname=arial
#define TeamB   shape=square style=filled fillcolor=pink
#define EdgeYes color=green penwidth=2
#define EdgeNo  color=orange style=dashed penwidth=3

Giving:

Surprised, it can actually be used this way to generate a dot file using the cpp command. This should mean that it only utilizes the macro definition feature of cpp.

In that case, would it be more reasonable to use a gvpr file as the theme file and apply the theme by running gvpr once?

the cpp includes, conditionals and line statements could also be used
As to using gvpr, I can’t find an easy way to make a gvpr-based solution as powerful as *cpp.

Also note that there are several stand-alone cpp varients.

I came to Graphviz from Mermaid and similar tools, and was very focused on writing the graph myself, node by node in the text editor. This is still generally useful, but when I want a specific theme, it gets cumbersome.

It took me quite long to make the jump to generating graphs from code. Now it feels like the obvious solution to many problems.

Graphs can be built trivially in any scripting language. You just print the graph code line by line. You can print all the nodes first, then print all the connections, or print them interleaved.

With a handful of helper functions, you can quickly design node types, and maintain your color palette.

Maybe it’s worth mentioning :blush:

1 Like