Search code examples
doxygengraphvizdot

How to add top and bottom labels for dot graph?


I am trying to add two labels to a graph with dot, one at the top and one at the bottom of the graph. Really, I'm trying to modify the dot files generated by Doxygen to contain these labels. I thought this would be an easy thing but it has turned into more of a pain than I had thought. I have tried the following:

1) Having two labels within a digraph but only the last one shows up

2) Stacking subgraphs vertically and trying to place the nodes appropriately but this looked bad and turned into a headache (example shown below)

digraph D {

  subgraph cluster_p {
    label = "Top Label";
    fontname=calibri;
    fontsize=9;

    rankdir=TB
    subgraph test {
      pencolor=black
      e
    }
    subgraph cluster_c3 {
      label = "Bottom Label";
      labelloc=b;
      fontname=calibri;
      fontsize=9;
      pencolor=white
      f;
    }
    e->f;
  }
}

Update: About two hours after posting this I finally found something that supports my needs.

digraph G {
    subgraph cluster_0 {
        label="Top";
        subgraph cluster_1 {
            label="Bottom";
            labelloc=b;
            pencolor=white;

            // diagram to enclose
            e->f;
        }
    }
}

This is simple enough to automatically insert with a python script into each dot file and supports my particular needs. Thank you.


Solution

  • You can leave one label at the top, and simulate the other label by adding a node at the end inside a subgraph with rank=sink attribute. This attribute forces the nodes, which are defined inside it, to appear not only at the lowest rank, but at the rank after that. Which ensures them to be at the bottomest bottom, just what you need:

    digraph D {
        pencolor=black
        labelloc=t
        fontname=calibri
        fontsize=9
        label = "Top Label"
    
        e
        e->f;
    
        {
            rank=sink
            bottomlabel [
              shape=plain
              label = "Bottom Label"
              fontname=calibri
              fontsize=9
            ]
        }
    }