2011-10-15 48 views
18

我想有我圖形左右看起來是這樣的:自上而下的子圖,內子圖

但我只能得到這樣的:

問題是,rankdirdoes not work in subgraph。 那麼,如何模擬它呢?

代碼:

digraph G { 
    node [shape = circle] 
    0 [style = invis] 

    0 -> "0A" 

    subgraph clusterA { 
     label=A 
     "0A" 
     "1A" 
     "2A" -> "0A" [label=•] 
    } 

    subgraph clusterB { 
     label=B 
     "0B" 
     "1B" 
     "2B" -> "0B" [label=•] 
    } 

     subgraph clusterC { 
     label=C 
     "0C" 
     "1C" 
     "2C" -> "0C" [label=•] 
    } 

    subgraph clusterD { 
     label=D 
     "0D" 
     "1D" 
     "2D" -> "0D" [label=•] 
    } 

    subgraph clusterE { 
     label=E 
     "0E" 
     "1E" 
     "2E" -> "0E" [label=•] 
    } 

    subgraph clusterF { 
     label=F 
      {node [shape = doublecircle] "0F" "1F"} 
     "2F" -> "0F" [label=•] 
    } 

    "0A" -> "1B" [label=a] 
    "1A" -> "2B" [label=a] 
    "0B" -> "1C" [label=b] 
    "1B" -> "2C" [label=b] 
    "0C" -> "1D" [label=c] 
    "1C" -> "2D" [label=c] 
    "0D" -> "1E" [label=d] 
    "1D" -> "2E" [label=d] 
    "0E" -> "1F" [label=e] 
    "1E" -> "2F" [label=e] 
} 

回答

31

再現特定的圖形佈局通常可以實現:

  • 隱形節點和邊
  • 等級限制

以下是我複製你的圖形 - 或者至少是部分其中:

digraph g { 
    rankdir="LR"; 
    node[shape = circle, fontsize=14]; 
    fontsize=18; 
    labeljust="l"; 

    edge[style=invis, fontsize=12]; 

    { rank=same; 
     0 [style = invis]; 
     01 [style = invis]; 
     02 [style=invis]; 
     0 -> 01 -> 02; 
    } 

    subgraph clusterA { 
     "0A" -> "1A" -> "2A"; 
     "2A" -> "0A" [label=".", constraint=false, style=solid]; 
     label="A"; 
    } 

    subgraph clusterB { 
     "0B" -> "1B" -> "2B"; 
     "2B" -> "0B" [label=".", constraint=false, style=solid]; 
     label="B"; 
    } 

    subgraph clusterC { 
     "0C" -> "1C" -> "2C"; 
     "2C" -> "0C" [label=".", constraint=false, style=solid]; 
     label="C"; 
    } 

    0 -> "0A"[style=solid]; 
    01 -> "0B"[style=invis]; 
    02 -> "0C"[style=invis]; 

    // edges between clusters 
    edge[constraint=false, style=solid]; 
    "0A" -> "1B" [label=a] 
    "1A" -> "2B" [label=a] 
    "0B" -> "1C" [label=b] 
    "1B" -> "2C" [label=b] 
} 

該解決方案不太直觀。幾個百分點來實現這一目標:

  • 我選擇rankdir="LR"這就造成了更好的邊緣比TB,雖然它並沒有真正用圖形的方向對應
  • 隱形節點和邊緣是使用了排名靠前的節點(0,01,02),以便讓這些集羣左對齊。
  • (看不見的)頂層節點被強制爲相同的等級並且被不可見的邊連接 - 這將確保鏈接到每個節點的簇以正確的順序出現。

結果是:

graphviz output

+0

@傑森-S感謝您的獎金!提交這個答案後的幾年意想不到! – marapet

1

使用約束=假應該得到的節點在子圖轉出你想要 http://www.graphviz.org/doc/info/attrs.html#d:constraint

subgraph clusterB { 
    label=B 
    "0B" 
    "1B" 
    "2B" -> "0B" [constraint=false label=•] 
} 

的方式後,你會發現,你的子圖不要按照你想要的方式排隊。像這樣的東西可以解決這個問題。

"0A" -> "0B" -> "0C" -> "0D" -> "0E" [weight=999 style=invis];