在 Graphviz 中使用子图时对齐节点

Align the nodes when using subgraphs in Graphviz

我想在 Graphviz 中使用子图时对齐节点。

它在没有子图的情况下工作得很好。但是当我引入子图时,节点会发生(意外的?)偏移。

这是一个简单的例子。

digraph My_test_without_subgraphs {

  graph [overlap = true, compound = true, rankdir = LR]

  node [shape = box, color = lightgrey, style = filled, fontcolor = black]
  T1 [label = 'my task 1']
  T2 [label = 'my task 2']
  T3 [label = 'my task 3']
  T4 [label = 'my task 4']

  T1 -> T3
  T2 -> T3
  T3 -> T4

}

digraph My_test_with_subgraphs {

  graph [overlap = true, compound = true, rankdir = LR]

  node [shape = box, color = lightgrey, style = filled, fontcolor = black]
  T1 [label = 'my task 1']
  T2 [label = 'my task 2']
  T3 [label = 'my task 3']
  T4 [label = 'my task 4']

  subgraph cluster1 {
  label = 'cluster 1'
  color = cornsilk
  style = filled
  T1 -> T3
  T2 -> T3
  }

  subgraph cluster2 {
    label = 'cluster 2'
    color = cornsilk
    style = filled
    T3 -> T4
  }
}

像这样向第二个簇添加稍微小一点的边距:

margin = 7.99

成功了。

不要问我为什么...

是的,保证金可以解决问题。数值本身并没有那么重要,只要是小于8的数字即可。

顺便说一下,请使用 " 而不是 ',一些解释器,尤其是在线编辑器,会引发错误。

digraph My_test_without_subgraphs {

  graph [overlap = true, compound = true, rankdir = LR]

  node [shape = box, color = lightgrey, style = filled, fontcolor = black]
  T1 [label = "my task 1"]
  T2 [label = "my task 2"]
  T3 [label = "my task 3"]
  T4 [label = "my task 4"]

  T1 -> T3
  T2 -> T3
  T3 -> T4

}

结果:

digraph My_test_with_subgraphs {

  graph [overlap = true, compound = true, rankdir = LR]

  node [shape = box, color = lightgrey, style = filled, fontcolor = black]
  T1 [label = "my task 1"]
  T2 [label = "my task 2"]
  T3 [label = "my task 3"]
  T4 [label = "my task 4"]

  subgraph cluster1 {
  label = "cluster 1"
  color = cornsilk
  style = filled
  T1 -> T3
  T2 -> T3
  }

  subgraph cluster2 {
    margin = 6
    label = "cluster 2"
    color = cornsilk
    style = filled
    T3 -> T4
  }
}

结果