0

I want to align the nodes when using subgraphs in Graphviz.

It works perfectly well in a plot without subgraphs. But when I introduce subgraphs there is a (unexpected ?) shift of the nodes.

Here is a simple example.

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
  }
}
Kumpelka
  • 869
  • 3
  • 11
  • 33

2 Answers2

1

Yes, margin does the trick. The value itself is not so important, as long it is a number smaller 8.

By the way please use " instead of ', some interpreters, esp. the online editors, would raises an error.

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

}

Results in: graph1

and

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
  }
}

results in graph2

CodeFreezr
  • 362
  • 2
  • 18
0

Adding a slightly smaller margin to the second cluster like this:

margin = 7.99

does the trick.

Don't ask me why...

marapet
  • 54,856
  • 12
  • 170
  • 184
  • Thanks. It works perfectly well on this simple example but on a more complexe plot (three subgraphs, more nodes), the trick doesn't work... Do you know how we can « generalize » the trick ? – Kumpelka Feb 20 '18 at 11:01
  • Sorry, it was a lucky shot, not sure how to do this properly. – marapet Feb 20 '18 at 16:12