【问题标题】:Creating Straight Edges in Graphviz在 Graphviz 中创建直边
【发布时间】:2011-10-30 06:15:45
【问题描述】:

我想使用 Graphviz 创建一个流程图(类似于 Visio)。这是一个示例有向图。

digraph start_up {
node [style = rounded]; 
node [shape = rect] start end;
node [style = ""];
node [shape = diamond] "USB\nCommand\nArrived";
start -> "Initialize\nCode";
"Initialize\nCode" -> "USB\nCommand\nArrived";
"USB\nCommand\nArrived" -> "USB\nCommand\nArrived" [label="No" tailport=w headport=n];
"USB\nCommand\nArrived" -> "Has USB 3.0\nInterface Been\nSelected" [label = "Yes"];
 "Has USB 3.0\nInterface Been\nSelected" -> end
}

问题是当我在 Graphviz 中呈现这个时,"USB\nCommand\nArrived" -> "USB\nCommand\nArrived" [label="No" tailport=w headport=n]; 创建的行 看起来很丑。我不介意曲线,但这条线看起来变形了。您可以在此处查看 Graphviz 创建的内容

有没有办法让它看起来更好?

【问题讨论】:

  • add line "graph [ splines = ortho ]" - 不是一个完整的答案,但可能会有所帮助,另请参阅 graphviz 文档中的属性 splines。

标签: graphviz dot flowchart


【解决方案1】:

我认为最好通过例子来学习。只需阅读我的 cmets,如果有任何不清楚的地方,我会很乐意回答。

作为侧节点: 虽然 graphviz 非常适合为大型数据集生成图形,但它对于创建诸如 ER 图、流程图和序列图之类的东西就不那么棒了。这是可能的并且相对简单,但是您必须花费大量时间来做出正确的事情通常是不合理的,因为您可以使用 Wsywig-GUI 建模工具在很短的时间内完成相同的事情。但是,您花在这方面的时间将帮助您学习语言的语法和属性,当您需要可视化一些大型或复杂的问题(GUI 建模工具将无用)时,这真的会派上用场。


digraph start_up {
    { 
/* fake levels (level0 -> level1) and support nodes
 *
 * graphviz to charts is what latex is to documents, 
 * sometimes you'll have to fight it.
 * This is typically done by defining levels and connection points that
 * don't really have anything to do with your graph, but are used to 
 * force the graph to appear in a certain way.
 */
        node [shape=none, /*label="."*/]; l1a; l2a; l3a; l4a; l5a; l6a;
        node [shape=square label="no"]; l20a; 
    }

    {   /* connectiong point for the no arrow above "arrived" */
        node [width=0 shape=point label=""];
        d1; no;
    }

    node [style = rounded]; 
    node [shape = rect] start end;
    node [style = ""];

    node [shape = diamond]; {
        node [label="USB\nCommand\nArrived"]; arrived; 
        node [label="Has USB 3.0\nInterface Been\nSelected"]; selected;
        node [label="Initialize\nCode"]; init;
    }

    start -> init; 
    /*init -> arrived; */
    init -> d1 [arrowhead=none]; 
            d1 -> arrived;

/* 
 * tricky part:
 * since nodes in a digrap go either from top to bottom or left to right, we 
 * can usually not connect (->) two nodes and have them appear on the same 
 * level unless the connection is specified within a block that has the 
 * parameter `rank' set to `same'
 */
            l20a->no [arrowhead=none];

    {   rank=same; no -> arrived [dir=back arrowtail=none]; }
    {   rank=same; l20a -> d1; }

    /*arrived       -> arrived;*/ /*  [label="No" tailport=w headport=n]; */
    arrived     -> selected [label = "Yes"];
    selected    -> end


    /* just to demonstrate */
    l1a-> l2a-> l3a-> l4a-> l5a-> l6a;
}

【讨论】:

  • 顺便说一下,plantUML 非常适合创建与代码相关的图表,它基于 graphviz。
  • 要让 PlantUML 绘制复杂的东西,比如使用正交边等漂亮的 UML 部署图,这恰好是难以置信的困难。
  • 我是否理解正确,您的回答建议始终使用包含简单链设置有向图级别的代理/假(理想情况下也是不可见的)节点,而不是依赖于级别执行与网格布局非常相似的事情由代理链设置并使用 rank=same 链接实际图列?
  • @AlexanderItes 是的。我不知道 2012 年的网格布局。这个解决方案并非完美无缺,而且公认相对复杂。你可能在这里有所收获。我很想看到一个更简单或更直接的解决方案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-16
  • 1970-01-01
相关资源
最近更新 更多