9

我有一些相对简单的(自动生成的)graphviz dot 格式的图表。这些显示了通过状态机的路径,但是当我希望图形处于状态顺序时,dot 有一个稍微令人困惑的习惯,即决定两个节点必须处于同一等级。我尝试了很多设置(包括:nand:sweight下面列出的),但我无法说服 dot 将第三状态置于第四状态之上。

我对很多图表都有这个问题: dot 似乎有一些内部因素决定如果两个节点处于同一等级并且没有什么可以覆盖它会更好。我什至有代码指定一个节点应该是 a rank=sink,但是 dot 决定在它下面放置另一个节点。

有什么方法可以建议节点有序比任何其他约束更重要?

用于生成图表的代码如下所示:

digraph {
    ERROR [label="Error"];
    FirstSTATE [label="Initial State" URL="\ref FirstSTATE"];
    FirstSTATE -> SecondSTATE;
    SecondSTATE [label="Second State" URL="\ref SecondSTATE"];
    SecondSTATE -> ThirdSTATE;
    ThirdSTATE [label="Third State" URL="\ref ThirdSTATE"];
    FourthSTATE [label="Fouth State?" shape="diamond"];
    ThirdSTATE:s -> FourthSTATE:n [weight=50];
    FourthSTATE -> FifthSTATE [label="Yes" ];
    FourthSTATE -> ThirdSTATE [label="No"];
    FifthSTATE [label="Fifth State" URL="\ref FifthSTATE"];
    SixthSTATE [label="Sixth State?" shape="diamond"];
    SixthSTATE -> ERROR [label="Yes" ];
    SixthSTATE -> SeventhSTATE [label="No"];
    FifthSTATE -> SixthSTATE;
    SeventhSTATE [label="Seventh State" URL="\ref SeventhSTATE"];
    SeventhSTATE -> EighthSTATE;
    EighthSTATE [label="Eighth State" URL="\ref EighthSTATE"];
    NinthSTATE [label="Ninth State?" shape="diamond"];
    NinthSTATE -> TenthSTATE [label="Yes" ];
    NinthSTATE -> EighthSTATE [label="No"];
    EighthSTATE -> NinthSTATE;
    TenthSTATE [label="Tenth State" URL="\ref TenthSTATE"];
    EleventhSTATE [label="Eleventh State?" shape="diamond"];
    EleventhSTATE -> ERROR [label="Yes" ];
    EleventhSTATE -> TwelfthSTATE [label="No" ];
    TenthSTATE -> EleventhSTATE;
    TwelfthSTATE [label="Twelfth State" URL="\ref TwelfthSTATE"];
}

该图当前如下所示: 点图

4

2 回答 2

12

使用“约束=假”。

http://www.graphviz.org/doc/info/attrs.html#d:constraint

在您的图表中:

FourthSTATE -> ThirdSTATE [label="No" constraint=false] ;

你会得到:

digraph {
    ERROR [label="Error"];
    FirstSTATE [label="Initial State" URL="\ref FirstSTATE"];
    FirstSTATE -> SecondSTATE;
    SecondSTATE [label="Second State" URL="\ref SecondSTATE"];
    SecondSTATE -> ThirdSTATE;
    ThirdSTATE [label="Third State" URL="\ref ThirdSTATE"];
    FourthSTATE [label="Fouth State?" shape="diamond"];
    ThirdSTATE -> FourthSTATE;
    FourthSTATE -> FifthSTATE [label="Yes" ];
    FourthSTATE -> ThirdSTATE [label="No" constraint=false] ;
    FifthSTATE [label="Fifth State" URL="\ref FifthSTATE"];
    SixthSTATE [label="Sixth State?" shape="diamond"];
    SixthSTATE -> ERROR [label="Yes" ];
    SixthSTATE -> SeventhSTATE [label="No"];
    FifthSTATE -> SixthSTATE;
    SeventhSTATE [label="Seventh State" URL="\ref SeventhSTATE"];
    SeventhSTATE -> EighthSTATE;
    EighthSTATE [label="Eighth State" URL="\ref EighthSTATE"];
    NinthSTATE [label="Ninth State?" shape="diamond"];
    NinthSTATE -> TenthSTATE [label="Yes" ];
    NinthSTATE -> EighthSTATE [label="No"];
    EighthSTATE -> NinthSTATE;
    TenthSTATE [label="Tenth State" URL="\ref TenthSTATE"];
    EleventhSTATE [label="Eleventh State?" shape="diamond"];
    EleventhSTATE -> ERROR [label="Yes" ];
    EleventhSTATE -> TwelfthSTATE [label="No" ];
    TenthSTATE -> EleventhSTATE;
    TwelfthSTATE [label="Twelfth State" URL="\ref TwelfthSTATE"];
}
于 2009-07-09T15:22:50.343 回答
1

每当您想要一个向上的箭头时,请从上到下(即向后)编写边缘,然后添加dir=backhttps://graphviz.org/docs/attrs/dir/)以使箭头指向向上而不是向下。

所以而不是:

FourthSTATE -> ThirdSTATE [label="No"];

写:

ThirdSTATE -> FourthSTATE [dir=back, label="No"];

现在,由于所有边都从ThirdSTATEto 开始FourthSTATE,因此排名不再模棱两可,Graphviz 将可靠地放在ThirdSTATE上面FourthSTATE

这通常比使用更好,constraint=false因为constraint=false有时会给出非常摆动的边缘(好像它们没有参与图形布局?)。

于 2021-11-04T15:34:50.110 回答