如何在我的Graphviz流程图中创建一个循环?

9

我正在尝试创建一个有固定位置节点的流程图。我使用不可见的节点来尝试强制边的方向。我的图表如下所示。
由于我希望从节点 d 出来的线条与从 cd 的边缘是连续的(且直线),因此它还不太对。

请问我该如何使所有的线条都连接在一起呢?谢谢!

enter image description here

以下是我的代码:

digraph g1 {

  graph [splines=false];

  // invisible nodes
  node[fontsize=15, shape = box, width=3, height=0] ;
  i1 [ style="invis"];
  i2 [ style="invis"];
  i3 [ style="invis"];
  i4 [ style="invis"];

  node[fontsize=15, color = black, shape = box, width=3, height=1] ;
  a[color=blue, label="a"];
  b[color=green, label="b"];
  c[color=orange, label="c"]; 
  d[color=red, label="d"] ;       

  {rank=same; a -> b -> c};

  {rankdir = TB;    c -> i1[arrowhead=none];
        i1 -> d[label="  FOR EACH\n\n"]; 
        d -> i2[arrowhead=none];
  };

  {rank=same; i3 -> i2[arrowhead=none] };

  {rankdir = TB; 
    b -> i4[style="invis"];
    i4 -> i3[arrowhead=none];
  };

  {rank=same; i4 -> i1};

}

在保罗的评论下,我尝试使用node[fontsize=15, shape = box, label="", width=0, height=0, fixedsize=true],结果如下:

enter image description here


看起来无形节点的大小导致箭头在“半空中”开始。我会尝试像 node[shape = box, width=0, height=0 fixedsize=true] ; 这样的东西来定义无形节点,但我现在无法尝试它。 - PaulR
感谢 @PaulR;我已经根据您的建议编辑了问题。似乎将宽度和高度设置为零可以改变节点的位置。 - user2957945
啊,我明白了...也许你可以使用 ranksep 属性并设置为 "equally" 来影响间距。 - PaulR
谢谢@PaulR,我会试一下。 - user2957945
1个回答

15

使用shape = pointsminlen就能解决问题:

digraph g1 {

  graph [splines=false];

  // invisible nodes
  node[ shape = point, width=0, height=0] ;
  i1 [ style="invis"];
  i2 [ style="invis"];
  i3 [ style="invis"];
  i4 [ style="invis"];

  node[fontsize=15, color = black, shape = box, width=3, height=1] ;
  a[color=blue, label="a"];
  b[color=green, label="b"];
  c[color=orange, label="c"]; 
  d[color=red, label="d"] ;       

  {rank=same; a -> b -> c};

  c -> i1[arrowhead=none];
  i1 -> d[label="  FOR EACH\n\n"]; 
  d -> i2[arrowhead=none];

  {rank=same; i3 -> i2[arrowhead=none, minlen = 7 ] };

  b -> i4[style="invis"];
  i4 -> i3[arrowhead=none];

  {rank=same; i4 -> i1};

}
产出。

输入图像描述


谢谢 vaettchen,看起来不错。minlen=7 是试错出来的吗? - user2957945
1
是的,没错。它恰好是3 + 3 + 1(您的框的宽度乘以2,再加上1个空格),但不确定这是否是巧合... - vaettchen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接