Graphviz水平对齐顶部/底部的节点

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

我想画一个这样的图。

enter image description here

我想了很久,我只能生成下面的图片。

enter image description here

digraph G {
    node [shape=plaintext]
//    newrank=true;

    subgraph part1 {
        {rank=same state0, state9, state6}
        subgraph nest1 {
            state0 [label=<
                    <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
                    <TR><TD PORT="title" bgcolor="yellow">0</TD></TR>
                    <TR><TD PORT="text1" bgcolor="red">S&rarr; ・E$</TD></TR>
                    <TR><TD PORT="text2" bgcolor="green">E&rarr; ・E+T <br/> E&rarr;・T </TD></TR>
                    <TR><TD PORT="text3" bgcolor="green">T&rarr; ・id <br/> T&rarr;・(E)</TD></TR>
                    </TABLE>>];
        }

        subgraph nest2 {
            state6 [label=<
                    <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
                    <TR><TD PORT="title" bgcolor="yellow">6</TD></TR>
                    <TR><TD PORT="text1" bgcolor="red">T&rarr;(・E) </TD></TR>
                    <TR><TD PORT="text2" bgcolor="green">E&rarr; ・E+T <br/> E&rarr;・T </TD></TR>
                    <TR><TD PORT="text3" bgcolor="green">T&rarr; ・id <br/> T&rarr;・(E)</TD></TR>
                    </TABLE>>];
        }

        subgraph nest3 {
            state9 [label=<
                    <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
                    <TR><TD PORT="title" bgcolor="yellow">9</TD></TR>
                    <TR><TD PORT="text" bgcolor="red">E&rarr;T・ </TD></TR>
                    </TABLE>>];

            state5 [label=<
                    <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
                    <TR><TD PORT="title" bgcolor="yellow">5</TD></TR>
                    <TR><TD PORT="text" bgcolor="red">T&rarr;id・ </TD></TR>
                    </TABLE>>];
        }

//        {rank=same; nest1; nest2; nest3;}
//        nest1 -> nest2 -> nest3 [style=invis]
    }

    subgraph part2 {
        {rank=same state1, state3, state7}
        state1 [label=<
                <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
                <TR><TD PORT="title" bgcolor="yellow">1</TD></TR>
                <TR><TD PORT="text" bgcolor="red">S&rarr;E・$<br/>E&rarr;E・+T</TD></TR>
                </TABLE>>];

        state7 [label=<
                <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
                <TR><TD PORT="title" bgcolor="yellow">7</TD></TR>
                <TR><TD PORT="text1" bgcolor="red">T&rarr;(E・) </TD></TR>
                <TR><TD PORT="text2" bgcolor="green">E&rarr; E・+T</TD></TR>
                </TABLE>>];

        state3 [label=<
                <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
                <TR><TD PORT="title" bgcolor="yellow">3</TD></TR>
                <TR><TD PORT="text1" bgcolor="red">E&rarr;E+・T </TD></TR>
                <TR><TD PORT="text2" bgcolor="green">T&rarr;・id <br/> T&rarr;・(E) </TD></TR>
                </TABLE>>];
    }

    subgraph part3 {
        {rank=same state2, state4, state8}

        state2 [label=<
                <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
                <TR><TD PORT="title" bgcolor="yellow">2</TD></TR>
                <TR><TD PORT="text" bgcolor="red">S&rarr;E$・ </TD></TR>
                </TABLE>>];

        state8 [label=<
                <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
                <TR><TD PORT="title" bgcolor="yellow">8</TD></TR>
                <TR><TD PORT="text" bgcolor="red">T&rarr;(E)・ </TD></TR>
                </TABLE>>];

        state4 [label=<
                <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
                <TR><TD PORT="title" bgcolor="yellow">4</TD></TR>
                <TR><TD PORT="text" bgcolor="red">E&rarr;E+T・ </TD></TR>
                </TABLE>>];
    }

//    state0:s -> state1:n [label="E"]
//    state0:title:e -> state9:title:w [label="T"]
//    state0:text3:e -> state5:text:w [label="id"]
//    state0:text2:e -> state6:text2:w [label="("]

    state0:s -> state1:n [label="E"]
    state0 -> state9 [label="T"]
    state0 -> state5 [label="id"]
    state0 -> state6 [label="(", weight=100]

    state1:s -> state2:n [label="$"]
    state1:title:e -> state3:title:w [label="+"]

    state3:s -> state4:n [label="T"]
    state3:n -> state5:s [label="id"]
//    state3:title:e -> state6:s [label="("]
    state3:title:e -> state6 [label="("]

//    state6:text3:w -> state5:text:e [label="id"]
//    state6:s -> state7:n [label="E"]
//    state6:title:w -> state9:title:e [label="T"]
//    state6:title:n -> state6:e [label="("]

    state6:s -> state7:n [label="E"]
    state6 -> state9 [label="T"]
    state6 -> state5 [label="id"]
    state6 -> state6 [label="("]

    state7:text2:w -> state3:text2:e [label="+"]
    state7:s -> state8:n [label=")"]

    // ------------ Invisible edges to order vertically node groups
    state9:s -> state5:n [style=invis]
    state5:s -> state3:n [style=invis]
    state3:s -> state4:n [style=invis]

    state6:s -> state7:n [style=invis]
    state7:s -> state8:n [style=invis]
    // ---------------------------------------------------

    // below two lines make sure the order 1 3 7 | 2 4 8 is right
    state1 -> state3 -> state7 [style=invis]
    state2 -> state4 -> state8 [style=invis]

    state0:text1:e -> state9:text1:w [style=invis]
    state9:text1:e -> state6:text1:w [style=invis]
}

我想调整0 9 5 6部分。这意味着我需要排名相同0 9/5 6,我尝试过使用子图,但它没有解决问题。

有没有办法水平对齐两个节点并保持顶部/底部对齐像HTML图像?

enter image description here

graphviz dot
1个回答
1
投票

您可以申请几个修复程序。 splines=ortho将边缘改为垂直和水平线。执行此操作时,边缘标签必须重做为xlabel=而不是label=

enter image description here

但是,9和5没有正确放置。如果使用neato而不是dot,则可以将节点准确定位在所需的位置。然后你可以删除所有的子图和不可见的节点和权重,并执行以下操作:

enter image description here

graphviz代码将是:

digraph G {
    splines=ortho;
    node [shape=plaintext]

    state0 [label=<
            <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
            <TR><TD PORT="title" bgcolor="yellow">0</TD></TR>
            <TR><TD PORT="text1" bgcolor="red">S&rarr; ・E$</TD></TR>
            <TR><TD PORT="text2" bgcolor="green">E&rarr; ・E+T <br/> E&rarr;・T </TD></TR>
            <TR><TD PORT="text3" bgcolor="green">T&rarr; ・id <br/> T&rarr;・(E)</TD></TR>
            </TABLE>>, pos="0,4!"];

    state6 [label=<
            <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
            <TR><TD PORT="title" bgcolor="yellow">6</TD></TR>
            <TR><TD PORT="text1" bgcolor="red">T&rarr;(・E) </TD></TR>
            <TR><TD PORT="text2" bgcolor="green">E&rarr; ・E+T <br/> E&rarr;・T </TD></TR>
            <TR><TD PORT="text3" bgcolor="green">T&rarr; ・id <br/> T&rarr;・(E)</TD></TR>
            </TABLE>>, pos="4,4!"];

    state9 [label=<
            <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
            <TR><TD PORT="title" bgcolor="yellow">9</TD></TR>
            <TR><TD PORT="text" bgcolor="red">E&rarr;T・ </TD></TR>
            </TABLE>>, pos="2,4.7!"];

    state5 [label=<
            <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
            <TR><TD PORT="title" bgcolor="yellow">5</TD></TR>
            <TR><TD PORT="text" bgcolor="red">T&rarr;id・ </TD></TR>
            </TABLE>>, pos="2,3.3!"];

    state1 [label=<
            <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
            <TR><TD PORT="title" bgcolor="yellow">1</TD></TR>
            <TR><TD PORT="text" bgcolor="red">S&rarr;E・$<br/>E&rarr;E・+T</TD></TR>
            </TABLE>>, pos="0,1.5!"];

    state7 [label=<
            <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
            <TR><TD PORT="title" bgcolor="yellow">7</TD></TR>
            <TR><TD PORT="text1" bgcolor="red">T&rarr;(E・) </TD></TR>
            <TR><TD PORT="text2" bgcolor="green">E&rarr; E・+T</TD></TR>
            </TABLE>>, pos="4,1.5!"];

    state3 [label=<
            <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
            <TR><TD PORT="title" bgcolor="yellow">3</TD></TR>
            <TR><TD PORT="text1" bgcolor="red">E&rarr;E+・T </TD></TR>
            <TR><TD PORT="text2" bgcolor="green">T&rarr;・id <br/> T&rarr;・(E) </TD></TR>
            </TABLE>>, pos="2,1.5!"];

    state2 [label=<
            <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
            <TR><TD PORT="title" bgcolor="yellow">2</TD></TR>
            <TR><TD PORT="text" bgcolor="red">S&rarr;E$・ </TD></TR>
            </TABLE>>, pos="0,0!"];

    state8 [label=<
            <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
            <TR><TD PORT="title" bgcolor="yellow">8</TD></TR>
            <TR><TD PORT="text" bgcolor="red">T&rarr;(E)・ </TD></TR>
            </TABLE>>, pos="4,0!"];

    state4 [label=<
            <TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="1" CELLSPACING="0">
            <TR><TD PORT="title" bgcolor="yellow">4</TD></TR>
            <TR><TD PORT="text" bgcolor="red">E&rarr;E+T・ </TD></TR>
            </TABLE>>, pos="2,0!"];

    state0:s -> state1:n [xlabel="E"]
    state0 -> state9 [xlabel="T"]
    state0:e -> state5:w [xlabel="id"]
    state0 -> state6 [xlabel="("]

    state1:s -> state2:n [xlabel="$"]
    state1:title:e -> state3:title:w [xlabel="+"]

    state3:s -> state4:n [xlabel="T"]
    state3:n -> state5:s [xlabel="id"]
    state3:title:e -> state6 [xlabel="("]

    state6:s -> state7:n [xlabel="E"]
    state6 -> state9 [xlabel="T"]
    state6 -> state5 [xlabel="id"]
    state6 -> state6 [xlabel="("]

    state7:text2:w -> state3:text2:e [xlabel="+"]
    state7:s -> state8:n [xlabel=")"]
}

References

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.