How do I place nodes on the same level in DOT?

久未见 提交于 2019-11-30 11:21:22

问题


I want to render several trees simultaneously and place all root nodes and all leaf nodes on the same level.

Here's an example of what I'm trying to do. Root nodes A and X are on the same level, and so are leaf nodes B, D, and Z.

I unsuccessfully tried putting roots in one rank and leaves in another as follows:

digraph G { 
rankdir = TB;
subgraph {
A -> B
A -> C
C -> D
X -> Y
rank = same; A; X;
rank = same; B; D; Y;
} /* closing subgraph */
}

And got this outcome where everything is on the same rank.

Any suggestions about what I should be trying? I've already got roots and leaves identified.


回答1:


Putting the rank = same; ... statements in braces, e.g.:

digraph G { 
  rankdir = TB;
  subgraph {
    A -> B
    A -> C
    C -> D
    X -> Y
    // note that rank is used in the subgraph
    {rank = same; A; X;}
    {rank = same; B; D; Y;}
  } /* closing subgraph */
}

... gives the desired result:




回答2:


Here's a simple example inspired by @William John Holden's comment -

graph {
  rankdir=LR;
  a -- b -- c;
  d -- e -- f;
  b -- d; {rank = same; b; d;};
}



来源:https://stackoverflow.com/questions/25734244/how-do-i-place-nodes-on-the-same-level-in-dot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!