Creating a Legend / Key in GraphViz

I like to include a legend or key in a GraphViz chart. However, it’s hard for me to figure out which code to use. I also want to put it in a corner, but the only coordination that I know for sure is at the bottom left: pos="10,10!" .

Does anyone know how I can make this work?

+58
graphviz legend
Aug 17 '10 at 3:11
source share
6 answers
 digraph { rankdir=LR node [shape=plaintext] subgraph cluster_01 { label = "Legend"; key [label=<<table border="0" cellpadding="2" cellspacing="0" cellborder="0"> <tr><td align="right" port="i1">item 1</td></tr> <tr><td align="right" port="i2">item 2</td></tr> <tr><td align="right" port="i3">item 3</td></tr> <tr><td align="right" port="i4">item 4</td></tr> </table>>] key2 [label=<<table border="0" cellpadding="2" cellspacing="0" cellborder="0"> <tr><td port="i1">&nbsp;</td></tr> <tr><td port="i2">&nbsp;</td></tr> <tr><td port="i3">&nbsp;</td></tr> <tr><td port="i4">&nbsp;</td></tr> </table>>] key:i1:e -> key2:i1:w [style=dashed] key:i2:e -> key2:i2:w [color=gray] key:i3:e -> key2:i3:w [color=peachpuff3] key:i4:e -> key2:i4:w [color=turquoise4, style=dotted] } ... 

enter image description here

I used dot .

+48
Mar 29 '13 at 16:54
source share

I am deeply convinced that Graphviz should not be used this way, but you can use HTML tags . achieve what you want:

 digraph { Foo -> Bar -> Test; Foo -> Baz -> Test; { rank = sink; Legend [shape=none, margin=0, label=< <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4"> <TR> <TD COLSPAN="2"><B>Legend</B></TD> </TR> <TR> <TD>Foo</TD> <TD><FONT COLOR="red">Foo</FONT></TD> </TR> <TR> <TD>Bar</TD> <TD BGCOLOR="RED"></TD> </TR> <TR> <TD>Baz</TD> <TD BGCOLOR="BLUE"></TD> </TR> <TR> <TD>Test</TD> <TD><IMG src="so.png" SCALE="False" /></TD> </TR> <TR> <TD>Test</TD> <TD CELLPADDING="4"> <TABLE BORDER="1" CELLBORDER="0" CELLSPACING="0" CELLPADDING="0"> <TR> <TD BGCOLOR="Yellow"></TD> </TR> </TABLE> </TD> </TR> </TABLE> >]; } } 

What it looks like:

graphviz output

Legends should be positioned like any other node (I used a satchel = shell to finish it) - you can play with its margin attribute for a fine - setting the position.

Edit

Without the use of labels for which it may be directed - I am not sure whether to completely exclude ranksep .

 digraph { mindist=0; ranksep=0; nodesep=0; node[shape=box,margin="0,0",width=1, height=0.5]; edge [style=invis]; Legend[width=2]; Legend -> Foo; Legend -> FooValue; Foo -> Bar; FooValue -> BarValue Bar -> Baz; BarValue -> BazValue; edge [constraint=false]; Foo -> FooValue; Bar -> BarValue Baz -> BazValue; } 

Result:

enter image description here

+26
Jan 20 2018-11-21T00:
source share

There are some problems if you use the [splines=ortho] : the lines are in reverse order.

Point Source:

 digraph { rankdir=LR node [shape=plaintext] graph [splines=ortho] subgraph cluster_01 { label = "Legend"; key [label=<<table border="0" cellpadding="2" cellspacing="0" cellborder="0"> <tr><td align="right" port="i1">item 1</td></tr> <tr><td align="right" port="i2">item 2</td></tr> <tr><td align="right" port="i3">item 3</td></tr> <tr><td align="right" port="i4">item 4</td></tr> <tr><td align="right" port="i5">item 5</td></tr> </table>>] key2 [label=<<table border="0" cellpadding="2" cellspacing="0" cellborder="0"> <tr><td port="i1" bgcolor='greenyellow'>&nbsp;</td></tr> <tr><td port="i2">&nbsp;</td></tr> <tr><td port="i3">&nbsp;</td></tr> <tr><td port="i4">&nbsp;</td></tr> <tr><td port="i5">&nbsp;</td></tr> </table>>] key:i1:e -> key2:i1:w [color=red] key:i2:e -> key2:i2:w [color=gray] key:i3:e -> key2:i3:w [color=peachpuff3] key:i4:e -> key2:i4:w [color=turquoise4, style=dotted] key:i5:e -> key2:i5:w [color=red, style=dotted] } } 
+2
Apr 09 '13 at 15:36
source share

I was lucky with the following. I did not like how wide it was, but otherwise it worked.

  subgraph cluster1 { label = "Legend" ; shape=rectangle ; color = black ; a [style=invis]; b [style=invis] ; c [style=invis] ; d [style=invis] ; c -> d [label="only ts", style=dashed, fontsize=20] ; a -> b [label="ts and js", fontsize=20] ; gui -> controller [style=invis] ; view -> model [style=invis] ; builtins -> utilities [style=invis] ; gui [style=filled, fillcolor="#ffcccc"] ; controller [style=filled, fillcolor="#ccccff"] ; view [style=filled, fillcolor="#ccffcc"] ; model [style=filled, fillcolor="#ffccff"] ; builtins [style=filled, fillcolor="#ffffcc"] ; utilities ; "external libraries" [shape=rectangle] ; } 

The result was

Result

+1
Sep 12 '18 at 17:25
source share

I am trying to do the same. I used a subgraph to make a key of node types:

 digraph G { rankdir=RL; graph [fontsize=10 fontname="Verdana"]; node [style=filled height=0.55 fontname="Verdana" fontsize=10]; subgraph cluster_key { label="Key"; progress [fillcolor="wheat" label="In progress"]; todo [label="To do"]; done [fillcolor=palegreen3 label="Done"]; not_our [fillcolor=none label="Not our\nteam"]; numbers [color=none label="Numbers\nrepresent\nperson\ndays"]; progress -> done [style=invis]; todo -> progress [style=invis]; not_our -> todo [style=invis]; numbers -> not_our [style=invis]; } mappings [fillcolor=palegreen3]; identifiers [fillcolor=palegreen3]; hyperwarp [fillcolor=wheat]; ghost [fillcolor=none] UI [fillcolor=none] events [fillcolor=wheat]; flag [fillcolor=palegreen3]; groups [fillcolor=wheat]; types [fillcolor=wheat]; instances []; resources []; optimize []; remove_flag []; persist []; approval []; edge [style="" dir=forward fontname="Verdana" fontsize=10]; types -> flag; groups -> events; events -> {flag mappings identifiers}; ghost -> hyperwarp; UI -> ghost; resources -> identifiers; optimize -> groups; hyperwarp -> flag; instances -> {ghost UI types events hyperwarp flag}; resources -> {groups flag}; remove_flag -> approval; persist -> approval; approval -> {types resources instances}; } 

that leads to

Graph with legend for node types

But on reflection, seeing the difficulty with which I have to place the legend next to the main graph, how the ranking position of the nodes on the main graph affects the legend’s position, and the complexity of the source that represents it, I am tempted to try a different approach (see my other answer , use a separate schedule for the key)

+1
Feb 07 '19 at 20:21
source share

Having tried several ways to embed the key in the main graphviz image, I decided that it makes sense for me to simply put the legend in a separate separate point file, display it as my own, separate image and then display the images side by side in my documents / pages.

This has several advantages:

a) The source code .dot much simpler. b) It is very easy to change the rankdir of the legend graph to display nodes one above the other or next to get a key that is either located to the right of the main image or under it. c) There is no leakage of such things as ranking positions from the main schedule to the legend.

For example:

Graph of nodes Horizontal legend

+1
Feb 07 '19 at 20:33
source share



All Articles