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