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?
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.
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.
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.
To answer a question you didn’t ask and to embellish Steve’s reply, in addition to text, a label can also be an HTML-like label. This can allow you to add a fairly rich table or list to serve as a key to the graph image. HTML-like labels can also contain images, so you can create an image using some other tool and then import that into the label.
You can draw additional invisible edges and node(s) at the bottom of the diagram and set them the same rank as the legend (also need to turn on the newrank attribute for graph). By adjusting the height and width of the invisible node(s), you can move the legend to the desired distance.
I’m new to the forum but I’m a long time Graphviz user. I’m late to this thread but I see that it is still getting sporadic replies so I just wanted to post what I did a while back to implement a legend / key for my graphs / diagrams. I used subgraph / cluster with multiple nodes that consisted of HTML labels. In my case, I have three separate nodes and then linked them together. It isn’t the best solution but I think it ended up working well. For the most part, it stays out of the way of my main diagram content.
Note that the code won’t render the images since they are local for me.