Search code examples
graphvizdot

Force some connections to be horizontal


I'm using DOT to visualize a lisp AST, and the picture that is generated currently looks like this:

Currently, the vertical lines are specified normally like parent -> child;, and the skewed ones are specified using constraint like so: parent -> child [constraint=false];.

This kind of works, but what I'm really looking for is a way to make the vertical connections stay the same where each connection puts the child one row downwards, but make the horizontal connections be actually horizontal. This would create something that looks more like this:

Is this possible?


Solution

  • You may be making it too complicated - this simple basic code does the job:

    digraph so
    {
      # nodes
      A[ label = "list" ];
      B[ label = "ident: +" ];
      C[ label = "literal: 1" ];
      D[ label = "list" ];
      E[ label = "ident: *" ];
      F[ label = "literal: 3" ];
      G[ label = "literal: 2" ];
    
      # layout
      { rank = same; B C D }
      { rank = same; E F G }
    
      # edges
      A -> B;
      B -> C -> D;
      D -> E;
      E -> F -> G;
    }
    

    compiled with dot -T png -o so.png so.dot yields what you want:

    enter image description here