Graphviz: how to have sub-graph nodes lined-up in a straight line?
Asked Answered
D

2

10

I'm trying to visualize the history of my source-code repository using Graphviz in the following fashion (top-to-bottom or left-to-right doesn't matter):

m1 -> m2 ----> m3 -----> m4 -> m5
    \                    ^
     \-> b1 -> b2 -> b3 -/

Given this dot file:

digraph git {
    subgraph master {
        m1 -> m2 -> m3 -> m4 -> m5
    }
    subgraph branch {
        m2 -> b1 // branch from master
        b1 -> b2 -> b3
        b3 -> m4 // merge into master
    }
}

Graphviz diagram

what attribute should I set to have all the nodes of a sub-graph (that belongs to the same branch) to be placed on a straight-line?

Defy answered 10/3, 2011 at 12:48 Comment(2)
Do you really need to use subgraphs for the layout?Debatable
@Debatable No: it was just an attempt to set the layout of that group of nodes.Defy
D
17

The simplest solution is to set the weight of the branching and merging edges to 0:

digraph git {
    rankdir=LR
    subgraph master {
        m1 -> m2 -> m3 -> m4 -> m5
    }
    subgraph branch {
        m2 -> b1[weight=0] // branch from master
        b1 -> b2 -> b3
        b3 -> m4[weight=0] // merge into master
    }
}

rankdir=LR changes the layout from top-bottom to left-right.

graphviz graph

See also my answers to a similar question: Forcing "main line" nodes into a straight line in Graphviz (or alternatives)

Debatable answered 15/3, 2011 at 9:20 Comment(1)
@Christopher Mahan Check out the linked question for an alternative solution using the group attributeDebatable
D
3

You can use the attribute setting rank=same

I think your particular graph gets arranged in two perfect lines anyway, but if it were more complicated, you can see the effect.

Add two more connections, for example.

// new complexity
m1 -> m5 
b3 -> b1    

Now to make that new resultant graph look better, try the following.

 { rankdir=LR ; rank=same ; m1; m2; m3; m4; m5 }
 { rankdir=LR ; rank=same ; b1; b2; b3 }

subgraph master { 
m1 -> m2 -> m3 -> m4 -> m5
}

subgraph branch { 
m2 -> b1 // branch from master
b1 -> b2 -> b3
b3 -> m4 // merge into master
}


// new complexity
m1 -> m5 
b3 -> b1    

enter image description here

Diseur answered 17/3, 2011 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.