在Graphviz状态机图中设置任意节点位置

时间:2015-11-30 13:14:40

标签: graphviz dot

Grapviz通常根据固定的样式格式化图表。

我想设置此图的节点位置:

standard Graphviz output

使用此代码生成

digraph finite_state_machine {
    # rankdir=LR;
    size="8,5"
    node [shape = doublecircle]; S E;
    node [shape = circle];
    S  -> S0   [ label = "0/-" ];
    S  -> S1   [ label = "1/-" ];

    S0 -> S1   [ label = "1/-" ];
    S1 -> S0   [ label = "0/-" ];

    S0 -> S00  [ label = "0/0" ];
    S1 -> S11  [ label = "1/1" ];

    S0 -> E  [ label = "$/-" ];
    S1 -> E  [ label = "$/-" ];

    S00 -> S00 [ label = "0/0" ];
    S11 -> S11 [ label = "1/1" ];

    S00 -> S1  [ label = "1/0" ];
    S11 -> S0  [ label = "0/1" ];

    S00 -> E [ label = "$/0" ];
    S11 -> E [ label = "$/1" ];


}

// http://www.graphviz.org/Gallery/directed/fsm.html
// dot -Tpng example_fsm.dot > example_fsm.png

看起来像这样: desired diagram

2 个答案:

答案 0 :(得分:1)

rank = same是你的朋友。我也增加了一些重量来增强对称性。

digraph finite_state_machine 
{
    size="8,5"
    { rank = same; S0  S1  }
    { rank = same; S00 S11 }

    node [shape = doublecircle]; 
    S; E;
    node [shape = circle];
    S0; S1; S00; S11;

    S  -> S0   [ label = "0/-" ];
    S  -> S1   [ label = "1/-" ];

    S0 -> S1   [ label = "1/-" ];
    S1 -> S0   [ label = "0/-" ];

    S0 -> S00  [ label = "0/0", weight = 8 ];
    S1 -> S11  [ label = "1/1", weight = 8 ];

    S0 -> E  [ label = "$/-" ];
    S1 -> E  [ label = "$/-" ];

    S00 -> S00 [ label = "0/0" ];
    S11 -> S11 [ label = "1/1" ];

    S00 -> S1  [ label = "1/0" ];
    S11 -> S0  [ label = "0/1" ];

    S00 -> E [ label = "$/0" ];
    S11 -> E [ label = "$/1" ];
}

产量

enter image description here

答案 1 :(得分:1)

作为替代解决方案,此解决方案使用的是constraintdir属性,而不是weight

digraph finite_state_machine {
    size="8,5"
    node [shape = doublecircle]; S E;
    node [shape = circle];
    S  -> S0   [ label = "0/-" ];
    S  -> S1   [ label = "1/-" ];

    S0 -> S00  [ label = "0/0" ];
    S1 -> S11  [ label = "1/1" ];

    S00 -> E [ label = "$/0" ];
    S11 -> E [ label = "$/1" ];

    S0 -> E  [ label = "$/-" ];
    S1 -> E  [ label = "$/-" ];

    S1 -> S00  [ label = "1/0", dir=back ];
    S0 -> S11  [ label = "0/1", dir=back ];

    edge[constraint=false];
    S0 -> S1   [ label = "1/-" ];
    S1 -> S0   [ label = "0/-" ];

    S00 -> S00 [ label = "0/0" ];
    S11 -> S11 [ label = "1/1" ];
}

Result as visualized with GraphvizFiddle