Search code examples
graphviz

how to create a graph with a straight top-level path and grouped sub-paths


I have a general data flow structure that consists essentially of a top-level stack of decisions. Each decision will lead you up and down the stack until a termination event happens in the middle of the stack, or until the top or bottom of the stack is reached. Some of the decisions involve checking boolean conditions to determine which decisions to make. I'm looking for some help on making the output capture the thought process more clearly, and it appears mainly to be a problem in grouping / rank, but maybe something else also.

The graphviz code for my first pass is shown below and rendered with dot gives the following graph:

enter image description here

while this is an accurate graph for the decision stack that I'm making, I'm hoping to make it look something more like this:

enter image description here

here is the graphviz code:

graph decision_path {
    node [shape="rectangle"]
    edge [dir="both"]
    newrank=true
    
    Top
    d1 [label="Decision 1"]
    cd1 [label="Composite Decision 1"]
    cd2 [label="Composite Decision 2"]
    d2 [label="Decision 2"]
    d3 [label="Decision 3"]
    d4 [label="Decision 4"]
    source [label="Decision Source" style="filled" color="#EEFFEE"]
    
    Top -- d1 [dir="back"]
    d1 -- cd1
    cd1 -- cd2
    cd2 -- d2
    d2 -- d3
    d3 -- d4
    d4 -- Bottom [dir="forward"]
    
    cd1 -- source [dir="back"]
    
    subgraph {
        node [style=filled, color="#EEEEFF"]
        cd1_d1 [label="Kill"]
        cd1_d2 [label="Continue"]
        cd1_d1 -- cd1_d2 [dir="forward"]
        subgraph {
            rank=same
            node [style=filled color="#FFEEEE"]
            cd1_d1_c1 [label="Conditional 1"]
            cd1_d1 -- cd1_d1_c1 [dir="forward"]
        }
    }
    cd1 -- cd1_d1 [dir="forward"]
    
    subgraph {
        node [style=filled, color="#EEEEFF"]
        cd2_d1 [label="Continue"]
        cd2_d2 [label="Kill"]
        cd2_d1 -- cd2_d2 [dir="forward"]
        subgraph {
            rank=same
            node [style=filled color="#FFEEEE"]
            cd2_d1_c1 [label="Conditional 1"]
            cd2_d1_c2 [label="Conditional 2"]
            cd2_d1 -- cd2_d1_c1 [dir="forward"]
            cd2_d1_c1 -- cd2_d1_c2 [dir="forward"]
        }
        
    }
    cd2 -- cd2_d1 [dir="forward"]
}

Solution

  • Changed rankdir to LR - maybe not necessary, but seems to work pretty well.

    graph decision_path {
        rankdir=LR
        node [shape="rectangle"]
        edge [dir="both"]
        newrank=true
        nodesep=.6
        
        { rank=same
        Top
        d1 [label="Decision 1"]
        cd1 [label="Composite Decision 1"]
        cd2 [label="Composite Decision 2" group=cd2g]
        d2 [label="Decision 2"]
        d3 [label="Decision 3"]
        d4 [label="Decision 4"]
        Bottom
        }
        source [label="Decision Source" style="filled" color="#EEFFEE"]
    
        Top -- d1 [dir="back"]
        d1 -- cd1
        cd1 -- cd2
        cd2 -- d2
        d2 -- d3
        d3 -- d4
        d4 -- Bottom [dir="forward"]
        
        source -- cd1  
        
        subgraph {
            node [style=filled, color="#EEEEFF"]
            cd1_d1 [label="Kill"]
            cd1_d2 [label="Continue"]
            cd1_d1 -- cd1_d2 [dir="forward"]
                node [style=filled color="#FFEEEE"]
                cd1_d1_c1 [label="Conditional 1" ]
                cd1_d1 -- cd1_d1_c1 [dir="forward"]
        }
        cd1 -- cd1_d1 [dir="forward"]
        
        subgraph {
            node [style=filled, color="#EEEEFF"]
            cd2_d1 [label="Continue" group=cd2g]
            cd2_d2 [label="Kill"]
            cd2_d1 -- cd2_d2 [dir="forward"]
                node [style=filled color="#FFEEEE"]
                cd2_d1_c1 [label="Conditional 1" group=cd2g]
                cd2_d1_c2 [label="Conditional 2" group=cd2g]
                cd2_d1 -- cd2_d1_c1 [dir="forward"]
                cd2_d1_c1 -- cd2_d1_c2 [dir="forward"]
        }
        cd2 -- cd2_d1 [dir="forward"]
    }
    
    

    Giving:
    enter image description here