垂直对齐Graphviz中连接子图的节点

时间:2013-05-18 20:12:35

标签: alignment graphviz dot

我将以下输入提供给Dot:

digraph G {
  subgraph cluster1 {
    fontsize = 20;
    label = "Group 1";
    A -> B -> C -> D;
    style = "dashed";
  }

  subgraph {
    O [shape=box];
  }

  subgraph cluster2 {
    fontsize = 20;
    label = "Group 2";
    Z -> Y -> X -> W [dir=back];
    style = "dashed";
  }

  D -> O [constraint=false];
  W -> O [constraint=false, dir=back];
}

它产生:

picture with node O aligned with A and Z

如何对齐节点O,使其与DW具有相同的排名?也就是说,图表看起来像:

A   Z
|   |
B   Y
|   |
C   X
|   |
D-O-W

添加

 { rank=same; D; O; W; }

产生错误

Warning: D was already in a rankset, ignored in cluster G
Warning: W was already in a rankset, ignored in cluster G

我想我可以通过在O的子图中添加不可见的节点和边来破解它,但我想知道我是否遗漏了一些Dot魔法。

1 个答案:

答案 0 :(得分:13)

您可以使用rankdir=LR的方法,并使用constraint=false作为群集内的边缘:

digraph G {
  rankdir=LR;

  subgraph cluster1 {
    fontsize = 20;
    label = "Group 1";
    rank=same;
    A -> B -> C -> D [constraint=false];
    style = "dashed";
  }

  subgraph cluster2 {
    fontsize = 20;
    label = "Group 2";
    rank=same;
    Z -> Y -> X -> W [dir=back, constraint=false];
    style = "dashed";
  }

  O [shape=box];
  D -> O -> W;
}

这不是点魔术:-),但它达到了这个目的:

graphviz output with rankdir LR

使用不可见节点进行黑客攻击也有效:

digraph G {
  subgraph cluster1 {
    fontsize = 20;
    label = "Group 1";
    A -> B -> C -> D;
    style = "dashed";
  }

  subgraph {
    O1[style=invis];
    O2[style=invis];
    O3[style=invis];
    O [shape=box];

    O1 -> O2 -> O3 -> O [style=invis];
  }

  subgraph cluster2 {
    fontsize = 20;
    label = "Group 2";
    Z -> Y -> X -> W [dir=back];
    style = "dashed";
  }

  edge[constraint=false];
  D -> O -> W;
}

结果几乎相同:

graphviz output with invisible nodes