Daniel
Daniel

Reputation: 325

Graphviz: arranging nodes

Is it possible to tell GraphViz (Dot) to try arranging nodes of the graph without stretching either of the dimensions? For example, if I create a graph with 25 nodes and no edges, GraphViz visualizes it with all nodes in a single row. What I want is to get a 5 x 5 "field" of nodes.

Upvotes: 7

Views: 4700

Answers (2)

vaettchen
vaettchen

Reputation: 7659

rank=same in conjunction with invisible edges are your friend:

digraph Test 
{
    nodesep = 0.5;                 // even node distribution
    node [ shape = circle, width = 0.7 ];
    edge [ style = invis ];

    { rank = same; A; B; C; D; E }
    { rank = same; F; G; H; I; J }
    { rank = same; K; L; M; N; O }
    { rank = same; P; Q; R; S; T }
    { rank = same; U; V; W; X; Y }

    C -> { F G H I J } 
    H -> { K L M N O }
    M -> { P Q R S T }
    R -> { U V W X Y }
}

Instead of the last four lines, you could simply use

A -> F -> K -> P -> U;

This would lead to the same result with the given nodes but may be less stable when node sizes are varying.

enter image description here

Upvotes: 6

igon
igon

Reputation: 3066

You can force the position of nodes in Graphviz although it won't work with the dot engine. That means you will have to come up with an algorithm to specify the position of your node.

Look at this question: How to force node position (x and y) in graphviz

Alternatively you can use invisible edges:

nodeA -> nodeB [style=invis]

And create a mesh between your nodes, that would likely cause the engine to arrange the nodes in a orderly fashion.

In both cases you will have to specify how you want to arrange the nodes by either specifying their position or connecting them together.

Upvotes: 1

Related Questions