代码之家  ›  专栏  ›  技术社区  ›  stklik

带子图的graphviz三秩布局

  •  2
  • stklik  · 技术社区  · 7 年前

    我试图以自动生成的流程图的形式定位元素。 一般来说 inputs (绿色)应该在最左边, outputs (红色)在最右边,其余部分应根据布局放在中间。 我正在使用 rank=source rank=sink 为了这个。 在标准图形中,它运行良好。

    然而,当我开始嵌套图时 秩=源 似乎不起作用。我希望<&燃气轮机;(电、开关、室温)放在最左边(正如在子图中发生的那样),子图+状态(棕色圆圈)和输入和输出之间的蓝色方框。

    有没有办法指定“秩=中心”(或类似的东西?)

    我已经经历了 documentation 但是没有找到正确的属性(以及在哪里指定它们)。

    MWE

    digraph MyGraph {
        node [fontsize=8  margin=".1,.01" width=.5 height=.5 shape=box]
        edge [fontsize=8]
        rankdir=LR;
        ranksep = .25;
        nodesep= .5;
    
    subgraph cluster_4386357488 {
        label = " <<GrowLamp>>"
        style=solid
        {rank=source;
            4386357544 [label="electricity" style=filled fillcolor="#b5fed9"]
            4386357712 [label="room_temperature" style=filled fillcolor="#b5fed9"]
            4386357768 [label="switch" style=filled fillcolor="#b5fed9"]
        }
        {
            4386357880 [label="off" style=filled fillcolor="#e2cbc1" shape=doublecircle]
            4386357936 [label="on" style=filled fillcolor="#e2cbc1" shape=circle]
            4386357656 [label="on_time" style=filled fillcolor="#d2ceef"]
        }
        {rank=sink;
            4386357600 [label="light" style=filled fillcolor="#fcc5b3"]
            4386357824 [label="temperature" style=filled fillcolor="#fcc5b3"]
        }
        4386357880 -> 4386357936
        4386357936 -> 4386357880
        {
            subgraph cluster_4386357992 {
                label = "<<Adder>>"
                style=dashed
                {rank=source;
                    4386358048 [label="heat_in" style=filled fillcolor="#b5fed9"]
                    4386358104 [label="room_temp_in" style=filled fillcolor="#b5fed9"]
                }
                {
                    4386358216 [label="state" style=filled fillcolor="#e2cbc1" shape=doublecircle]
                }
                {rank=sink;
                    4386358160 [label="temperature" style=filled fillcolor="#fcc5b3"]
                }
                4386358216 -> 4386358160 [style="dashed"]
            }
    
    
            subgraph cluster_4386358328 {
                label = "<<HeatElement>>"
                style=solid
                {rank=source;
                    4386358384 [label="electricity" style=filled fillcolor="#b5fed9"]
                }
                {
                    4386358496 [label="on" style=filled fillcolor="#e2cbc1" shape=doublecircle]
                }
                {rank=sink;
                    4386358440 [label="heat" style=filled fillcolor="#fcc5b3"]
                }
                4386358496 -> 4386358440 [style="dashed"]
            }
    
    
            subgraph cluster_4386358608 {
                label = "<<LightElement>>"
                style=solid
                {rank=source;
                    4386358664 [label="electricity" style=filled fillcolor="#b5fed9"]
                }
                {
                    4386358776 [label="off" style=filled fillcolor="#e2cbc1" shape=doublecircle]
                    4386358832 [label="on" style=filled fillcolor="#e2cbc1" shape=circle]
                }
                {rank=sink;
                    4386358720 [label="light" style=filled fillcolor="#fcc5b3"]
                }
                4386358776 -> 4386358832
                4386358832 -> 4386358776
                4386358776 -> 4386358720 [style="dashed"]
                4386358832 -> 4386358720 [style="dashed"]
            }
    
            4386358160 -> 4386357824
            4386357712 -> 4386358104
            4386358440 -> 4386358048
            4386358720 -> 4386357600
            4386357936 -> 4386358384 [style="dashed"]
            4386357936 -> 4386358664 [style="dashed"]
            4386357936 -> 4386357656 [style="dashed"]
        }
    
    }
    

    有希望的解决方案: 这是我想要的结局。请注意,在各自的子图中,绿色框都位于左侧,而红色框位于右侧。在这些元素之间应该有其他元素,由graphviz定位。

    How it should look like

    1 回复  |  直到 7 年前
        1
  •  2
  •   Craig    7 年前

    您可以通过添加不可见边来获得所需的布局,将三个输入连接到图的其余部分,以便graphviz布局算法可以正确计算其秩。可以通过添加 style=invis 到边缘格式。

    digraph MyGraph {
        node [fontsize=8  margin=".1,.01" width=.5 height=.5 shape=box]
        edge [fontsize=8]
        rankdir=LR;
        ranksep = .25;
        nodesep= .5;
    
    subgraph cluster_4386357488 {
        label = " <<GrowLamp>>"
        style=solid
        {rank=source;
            4386357544 [label="electricity" style=filled fillcolor="#b5fed9"]
            4386357712 [label="room_temperature" style=filled fillcolor="#b5fed9"]
            4386357768 [label="switch" style=filled fillcolor="#b5fed9"]
        }
        {
            4386357880 [label="off" style=filled fillcolor="#e2cbc1" shape=doublecircle]
            4386357936 [label="on" style=filled fillcolor="#e2cbc1" shape=circle]
            4386357656 [label="on_time" style=filled fillcolor="#d2ceef"]
        }
        {rank=sink;
            4386357600 [label="light" style=filled fillcolor="#fcc5b3"]
            4386357824 [label="temperature" style=filled fillcolor="#fcc5b3"]
        }
        4386357880 -> 4386357936
        4386357936 -> 4386357880
        #invisible edges added to achieve correct layout
        4386357544 -> 4386357880 [style="invis"]
        4386357712 -> 4386357880 [style="invis"]
        4386357768 -> 4386357880 [style="invis"]
    
        {
            subgraph cluster_4386357992 {
                label = "<<Adder>>"
                style=dashed
                {rank=source;
                    4386358048 [label="heat_in" style=filled fillcolor="#b5fed9"]
                    4386358104 [label="room_temp_in" style=filled fillcolor="#b5fed9"]
                }
                {
                    4386358216 [label="state" style=filled fillcolor="#e2cbc1" shape=doublecircle]
                }
                {rank=sink;
                    4386358160 [label="temperature" style=filled fillcolor="#fcc5b3"]
                }
                4386358216 -> 4386358160 [style="dashed"]
            }
    
    
            subgraph cluster_4386358328 {
                label = "<<HeatElement>>"
                style=solid
                {rank=source;
                    4386358384 [label="electricity" style=filled fillcolor="#b5fed9"]
                }
                {
                    4386358496 [label="on" style=filled fillcolor="#e2cbc1" shape=doublecircle]
                }
                {rank=sink;
                    4386358440 [label="heat" style=filled fillcolor="#fcc5b3"]
                }
                4386358496 -> 4386358440 [style="dashed"]
            }
    
    
            subgraph cluster_4386358608 {
                label = "<<LightElement>>"
                style=solid
                {rank=source;
                    4386358664 [label="electricity" style=filled fillcolor="#b5fed9"]
                }
                {
                    4386358776 [label="off" style=filled fillcolor="#e2cbc1" shape=doublecircle]
                    4386358832 [label="on" style=filled fillcolor="#e2cbc1" shape=circle]
                }
                {rank=sink;
                    4386358720 [label="light" style=filled fillcolor="#fcc5b3"]
                }
                4386358776 -> 4386358832
                4386358832 -> 4386358776
                4386358776 -> 4386358720 [style="dashed"]
                4386358832 -> 4386358720 [style="dashed"]
            }
    
            4386358160 -> 4386357824
            4386357712 -> 4386358104
            4386358440 -> 4386358048
            4386358720 -> 4386357600
            4386357936 -> 4386358384 [style="dashed"]
            4386357936 -> 4386358664 [style="dashed"]
            4386357936 -> 4386357656 [style="dashed"]
        }
    
    }
    }
    

    具有 dot.exe 版本2.38您应该得到如下图形:

    Nodes aligned correctly to graph source and sink.