【问题标题】:How can I control within level node order in graphviz's dot?如何在graphviz的点中控制级别节点顺序?
【发布时间】:2017-10-31 15:40:50
【问题描述】:

我有一个以树为骨干的图。例如,我有一个节点 A,其子节点为 B、C 和 D。假设图形是自上而下绘制的,A 将在一个级别上,然后是 B、C 和 D。我想强制 graphviz在他们的等级内按 B、C、D 顺序排列它们。这可能吗?如果有,怎么做?

如果只有 A、B、C 和 D,我只需将 B、C 和 D 按顺序放入输入点文件中即可获得此效果。但是,如果在 B、C 和/或 D 之外还有其他边,则有时订单会被打乱。这是我想避免的。

【问题讨论】:

    标签: graphviz dot graph-layout


    【解决方案1】:

    为了帮助填写@TomServo 的答案(对于那些为“排名”而苦苦挣扎的人),我将不可见的边缘设为可见:

    【讨论】:

    • 谢谢,这有助于更好地了解排名。
    【解决方案2】:

    这可以通过如图所示的“不可见”边缘来实现。请注意描述其工作原理的 cmets。

    digraph test{
    
    // make invisible ranks
    rank1 [style=invisible];
    rank2 [style=invisible];
    
    // make "invisible" (white) link between them
    rank1 -> rank2 [color=white];
    
    // declare nodes all out of desired order
    A -> D;
    A -> B;
    A -> C;
    A -> E;
    
    // even these new connection don't mess up the order
    B -> F -> G;
    C -> F -> G;
    
    {
    rank = same;
    // Here you enforce the desired order with "invisible" edges and arrowheads
    rank2 -> B -> C -> D -> E [ style=invis ];
    rankdir = LR;
    }
    }
    

    【讨论】:

    • 不需要rank1rank2,见下文。
    【解决方案3】:

    你不需要那些神奇的rank1rank2

    只是:

    1. 照常制作图表。
    2. 在子图中再次添加节点。
    digraph test{
    
    // declare nodes all out of desired order
    A -> D;
    A -> B;
    A -> C;
    A -> E;
    
    B;C;D;E;
    
    // even these new connection don't mess up the order
    B -> F -> G;
    C -> F -> G;
    
    {
    rank = same;
    // Here you enforce the desired order with "invisible" edges and arrowheads
    edge[ style=invis];
    B -> C -> D -> E ;
    rankdir = LR;
    }
    }
    

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,发现魔法咒语是ordering=out

      我的完整示例如下所示:

      digraph game_tree {
      node [shape = circle, ordering=out];
      f, h [shape=doublecircle, color=red];
      k, n [shape=doublecircle, color=blue];
      l, m [shape=doublecircle];
      a -> b [label=1];
      a -> c [label=2];
      a -> d [label=3];
      b -> e [label=4];
      b -> f [label=5];
      c -> g [label=4];
      c -> h [label=5];
      d -> i [label=4];
      d -> j [label=5];
      e -> k [label=6];
      g -> l [label=6];
      i -> m [label=7];
      j -> n [label=8];
      }
      

      【讨论】:

      • 这比上面提出的其他答案要干净得多。使用fontcolor="transparent" 会使标签消失以获得更清晰的结果。
      • 如果您不想要标签,只需使用a -> b; 等而不是a -> b [label=1];
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 2016-11-24
      • 2014-01-16
      • 2015-07-04
      • 2019-01-27
      相关资源
      最近更新 更多