Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

graphviz - Distribute nodes on the same rank of a wide graph to different lines

I have a graph (organigram) how this:

digraph G {
nodesep=0.3;
ranksep=0.2;
margin=0.1;
node [shape=rectangle];
edge [arrowsize=0.8];
1 -> 2;
1 -> 3;
1 -> 4;
1 -> 5;
1 -> 6;
1 -> 7;
1 -> 8;
1 -> 9;
1 -> 10;
}

enter image description here

I have organigrams with 70 people and it's impossible to print in A4. How would I put nodes in 2 or 3 lines?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here are two possibilities (see also this question):

1. Use the unflatten utility

Graphviz provides a tool called unflatten. If you pre-process your graph using this command line:

unflatten -l 3 wide.gv | dot -Tpng -o wide.png

the output image will be similar to the below picture. This is slightly less wide, and you may play with the -l option.

graphviz unflattened graph example

2. Use `rank=same' and invisible edges

You may use the standard techniques to make a automatically layed out graphs look more like you want it to:

  • rank=same to group nodes in subgraphs and to define which nodes should be on the same line
  • invisible edges to ensure that the different subgraphs appear on different ranks
  • maybe some constraint=false for some edges to influence the layout
  • group attributes of nodes to encourage straight edges.

The output graph will not necessarily be prettier...

Here's an example, you can probably do better. Also, this may not be very practical if the graph is generated dynamically.

digraph G {
nodesep=0.3;
ranksep=0.2;
margin=0.1;
node [shape=rectangle];
edge [arrowsize=0.8];


edge[style=invis];
node[group=a];
2->5->8;
node[group=b];
1->3->6->9;
node[group=c];
4->7->10;


edge[style=solid];
1 -> 2;
1 -> 3;
1 -> 4;
edge[constraint=false];
1 -> 5;
1 -> 6;
1 -> 7;
1 -> 8;
1 -> 9;
1 -> 10;
}

graphviz output


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...