Graphviz 子图布局

问题描述 投票:0回答:1

我有一个包含 3 个簇的图。主图全部为蓝色边缘。集群 1 具有所有绿色边缘,集群 2 具有所有红色边缘,如下所示。

        graph [bb="0,0,414,258.8",
                layout=dot,
                rankdir=LR,
                splines=ortho
        ];
        node [label="\N",
                shape=ellipse
        ];
        edge [color=black,
                weight=10
        ];
        
        0 [shape=rectangle]
        1 [shape=rectangle]
        0 -> 1 [color=blue, weight=10, label=S0]
        2 [shape=rectangle]
        1 -> 2 [color=blue, weight=10, label=S1]
        5 [shape=rectangle]
        2 -> 5 [color=blue, weight=100, label=S4]
        3 [shape=rectangle]
        2 -> 3 [label=S5]
        4 [shape=rectangle]
        2 -> 4 [label=S4]
        
        subgraph {
            graph [rankdir=RL]
            edge[weight=1]
            11 [shape=rectangle]
            2 -> 11 [color=green, xlabel=M1]
            10 [shape=rectangle]
            11 -> 10 [color=green, xlabel=M2]
            10 -> 11 [color=green, xlabel=M3]
            11 -> 2 [color=green, xlabel=M4]
        }
        
        subgraph {
            graph [rankdir=LR]
            edge[weight=8]
            12 [shape=rectangle]
            2 -> 12 [color=red, weight=10, xlabel=M5]
            12 -> 5 [color=red, xlabel=M6]
            5 -> 12 [color=red, xlabel=M7]
            12 -> 2 [color=red, xlabel=M8]
        }
}

这给了我这个图表!

如何将所有红色边缘对齐到主图的左下角,将绿色边缘对齐到主图的右下角,如下所示。

Graphivz 编辑器链接 -> 此处

visualization graphviz dot subgraph
1个回答
1
投票

变化:

  • constraint=false 固定排名(在本例中为左右定位)
  • group= 对齐 0 1 2 5
  • 重新排序节点和边声明,使 3 和 4 高于 5(Graphviz“神奇”,输入命令排序会产生影响)
  • 注释掉不必要的属性
 digraph {
        graph [// bb="0,0,414,258.8",  // dot ignores bb
                layout=dot,
                rankdir=LR,
                splines=ortho
        ];
        node [label="\N",       // already default
                shape=ellipse   // already default, why not just set to rect?
        ];
        edge [color=black,      // already default
                XYZweight=10    // effectively commented out all weight references
        ];
        // use group to align 0 1 2 5
        0 [shape=rectangle group=123]
        1 [shape=rectangle group=123]
        2 [shape=rectangle group=123]
        3 [shape=rectangle]
        4 [shape=rectangle]
        5 [shape=rectangle group=123]
        0 -> 1 [color=blue, XYZweight=10, label=S0]
        1 -> 2 [color=blue, XYZweight=10, label=S1]
        2 -> 5 [color=blue, XYZweight=100, label=S4]
        2 -> 3 [label=S5]
        2 -> 4 [label=S4]
        
        subgraph {
            //graph [rankdir=RL]    // rankdir only applies to root graph
            edge[XYZweight=1]
            11 [shape=rectangle]
            2 -> 11 [constraint=false color=green, xlabel=M1]
            10 [shape=rectangle]
            11 -> 10 [ constraint=false color=green, xlabel=M2]
            10 -> 11 [color=green, xlabel=M3]
            11 -> 2  [color=green, xlabel=M4]
        }
        
        subgraph {
            //graph [rankdir=LR]    // rankdir only applies to root graph
            edge[XYZweight=8]
            12 [shape=rectangle]
            2 -> 12 [color=red, XYZweight=10, xlabel=M5]
            12 -> 5 [ constraint=false color=red, xlabel=M6]
            5 -> 12 [ constraint=false color=red, xlabel=M7]
            12 -> 2 [color=red, xlabel=M8]
        }
}

给予:

© www.soinside.com 2019 - 2024. All rights reserved.