在Graphviz中保持节点的水平排序

时间:2018-04-12 09:37:46

标签: graphviz dot directed-acyclic-graphs

我有以下点文件内容:

digraph G {
    start -> {a0, b0} -> end;
    start -> c0 -> c1 -> c2 -> end;
    start -> d0 -> d1 -> d2 -> end;
    start -> {e0, f0} -> end;

    subgraph cluster_test {
        {
            rank = same;
            a0; b0; c0; d0; e0; f0;
        }

        {
            rank = same;
            c1; d1;
        }

        {
            rank = same;
            c2; d2;
        }
    }
}

结果图如下: enter image description here

我想要的是维持0级节点的排序,即我希望a0,b0在c0之前,d0在水平方向上。

我如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

空节点,具有权重的边和集群中顶行的显式排序有助于。请参阅以下带注释的代码:

digraph so 
{
    // we define all nodes in the beginning, before edges and clusters
    // may not be essential but I think it's good practice
    start
    a0 b0 c0 d0 e0 f0
    c1 d1 
    c2 d2
    end
    // we define "empty" nodes that can be used to route the edges
    node[ shape = point, height = 0 ];
    ax bx ex fx

    subgraph cluster_test 
    {
        // we need to keep explicit order of the top nodes in the cluster
        { rank = same; a0 -> b0 -> c0 -> d0 -> e0 ->  f0[ style = invis ] }
        // the original layout in the cluster, empty nodes added at the bottom
        { rank = same; c1 d1 }
        { rank = same; ax bx c2 d2 ex fx }
        c0 -> c1 -> c2;
        d0 -> d1 -> d2;
        // routing through invisible nodes keeps the position of all other nodes
        // edges with no arrowheads, strong weight to keep it vertical
        edge[ dir = none, weight = 10 ]
        a0 -> ax;
        b0 -> bx;
        e0 -> ex;
        f0 -> fx;
    }

    // connecting to the start and end node, normal edges again
    edge[ weight = 1, dir = forw ];
    start -> { a0 b0 c0 d0 e0 f0 }
    { ax bx  c2 d2 ex fx } -> end;
}

给你

enter image description here