Amandasaurus
Amandasaurus

Reputation: 60649

GraphViz - How to have a subgraph be left-to-right when main graph is top-to-bottom?

I have a graph file like this:

digraph {
    "Step1" -> "Step2" -> "Step3";

    subgraph step2detail {
        "Step2" -> "note1";
        "Step2" -> "note2";
        "Step2" -> "note3";
        "Step2" -> "note4";
        rankdir=TB
   }
}

I want the subgraph step2detail to hang off to the right of 'Step2'.

Right now it looks like this:

enter image description here

I want Step1, Step2 and Step3 to all be vertically under each other and in 1 column.

Upvotes: 34

Views: 32757

Answers (5)

irgeek
irgeek

Reputation: 191

The trick to get the graph you described is to use two subgraphs and link from one to the other. The invisible edges in "details" are what keep the notes aligned.

digraph {
    rankdir="LR";

    subgraph steps {
        rank="same";
        "Step1" -> "Step2" -> "Step3";
    }

    subgraph details {
        rank="same";
        edge[style="invisible",dir="none"];
        "note1" -> "note2" -> "note3" -> "note4";
    }

    "Step2" -> "note1";
    "Step2" -> "note2";
    "Step2" -> "note3";
    "Step2" -> "note4";
}

The result is:

enter image description here

Upvotes: 19

Photodeus
Photodeus

Reputation: 667

By grouping the Step nodes into a clustered subgraph, the output is as follows:

digraph {
    subgraph cluster_0 {
        color=invis;
        "Step1" -> "Step2" -> "Step3";
    }

    subgraph cluster_1 {
        color=invis;
        "Step2" -> "note4";
        "Step2" -> "note3";
        "Step2" -> "note2";
        "Step2" -> "note1";
   }
}

color=invis removes the border that would otherwise be drawn around the cluster

Upvotes: 7

Jason Kleban
Jason Kleban

Reputation: 20758

rankdir doesn't work directly in the subgraph, but if you add another set of curly braces - whatever that's called - rankdir works:

digraph {
    "Step1" -> "Step2" -> "Step3";

    subgraph step2detail {
        {
            "Step2" -> "note1";
            "Step2" -> "note2";
            "Step2" -> "note3";
            "Step2" -> "note4";
            rankdir=TB
            rank=same
        }
   }
}

enter image description here

Upvotes: 3

marapet
marapet

Reputation: 56446

Here's as simple as it gets - just use the group attribute to have graphviz prefer straight edges:

digraph {
    node[group=a, fontname="Arial", fontsize=14];
    "Step1" -> "Step2" -> "Step3";

    node[group=""];
    "Step2" -> "note1";
    "Step2" -> "note2";
    "Step2" -> "note3";
    "Step2" -> "note4";
}

graphviz output

Upvotes: 8

Cafeo
Cafeo

Reputation: 3

Use the command: rankdir=LR;

digraph {
rankdir=LR;

    "Step1" -> "Step2" -> "Step3";

    subgraph step2detail {
        "Step2" -> "note1";
        "Step2" -> "note2";
        "Step2" -> "note3";
        "Step2" -> "note4";
        rankdir=TB
   }

}

Upvotes: -4

Related Questions