Adding key or legend?

I’m a new GraphViz user, so maybe I’m missing something obvious. I’m working on a graph that (currently) has about 100 nodes. Is there a way to add a key or legend on the bottom of the graph, to show what the different shapes and colors mean? I have a label, but I’d like more than that.

My current solution is a second graph that I import as a node image. I’m working on a timeline, so I have a list of years down the left side of the graph, and I put an extra node at the bottom and rank my legend with that node. Unfortunately, it shows up on the left side of the graph rather then centered with the graph label. I tried using impagepos=bc, but that seems to center the image in the node, not the graph. Is there a way to force the image to be centered, or a way to force the node to be the entire width of the graph (then maybe imagepos will work)?

Another issue is that my graph is top-to-bottom, but I’d like the legend to be left-to-right. Am I correct in thinking GraphViz doesn’t support that?

Is there a more elegant solution than using a second graph?

2 Likes

Is this what you are after?

digraph l {
  subgraph clusterMain {
	graph [labelloc="b" labeljust="r" label=<
	<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
	<TR><TD>left 1</TD><TD>right 1</TD></TR>
	<TR><TD>left 2</TD><TD>right 2</TD></TR>	</TABLE>>];
  a -> b -> c
  "more nodes and edges"
}
}

legend2

1 Like

Thank you for the suggestion. Using a subgraph allowed me make a nice legend. The end result wasn’t quite what I had envisioned, but it’s a more elegant solution that what I had been trying.

1 Like

Unless I am missing something, you don’t need to use a subgraph. You can do the same thing in the root graph. So if you can encode your key using an Html-like string, you should be set.

1 Like

I failed to show why I used the cluster - by doing so I can have a key and a (root) graph title.

1 Like

Gotcha. By the way, I have often used the approach mentioned by Gibson, especially when the additional material can’t be represented in an HTML-like string, such as using node shapes as keys. A simple gvpr script can be used to put the pieces together. As a colleague once said, “People don’t want to draw graphs; they want to draw diagrams that might include graphs.” Since Graphviz just does graphs, there will always be a need for other tools to provide the missing pieces.

My key contains nodes, edges, an HTML table and a few lines of text. I wasn’t able to get all of the elements to line up properly in a box until Steve recommended using a subgraph. That made everything come together very nicely. I’d hoped to have it at the bottom right of the graph, which it isn’t, but it looks better than I had anticipated.