Search code examples
graphgraphvizdotneato

Border around graph


I would like to create an border around the graph which is getting generated with nop2 engine.

digraph {
  a[pos="0,0!"]
  b[pos="100,0!"]
  a -> b
}

This gets translated into this :

enter image description here

which does not have an border around it. How can we generate an thick border around the whole graph?

I tried using clusters but they are not supported in neato. So, no help.


Solution

  • Here is a gvpr (https://www.graphviz.org/pdf/gvpr.1.pdf) program that will add a periphery to a (root) graph. It does so by adding a rectangular node that is the size of the bounding box (the bb attribute), and then expanding the bb value.
    Her is a command line:
    dot -Knop2 myfile.gv |gvpr -cf addP.gvpr | dot -Knop2 -Tpng >myfile.png

    Save this as addP.gvpr

    /*
      add a periphery around a graph, by way of a large node (bb-sized)
    */
    BEG_G {
      double   llx, lly, urx, ury, dx, dy, Margin;
      string   BB;
      graph_t  aG;
      node_t   aN;
      Margin=8;
      BB=$G.bb;
      sscanf ($G.bb, "%lf,%lf,%lf,%lf", &llx, &lly, &urx, &ury);
      llx-=Margin;
      lly-=Margin;
      urx+=Margin;
      ury+=Margin;
      $G.bb=(string)llx + "," + (string)lly + " " + (string)urx + "," + (string)ury;
    
      aN=node($G,"_____surround");
      aN.pos=(string)(llx+((urx-llx)/2)) + "," + (string)(lly+((ury-lly)/2));
      aN.width=(urx-llx)/72;
      aN.height=(ury-lly)/72;
      aN.shape="rect";
      aN.label="";
      aN.penwidth=4;
      aN.style="solid"  // not filled
    }