如何防止绘制某些节点和边?

如何防止绘制某些节点和边?

我有一个非常简单的图表:

digraph G {
  "for" -> "initial assignment"
  "initial assignment" -> "condition"
  "condition" -> "code" [color=red]
  "condition" -> "end" [color=red]
  "code" -> "final assignment" [color=red]
  "final assignment" -> "condition" [color=red]
}

在此处输入图片描述

但是,节点必须按照特定顺序绘制,即从上到下。(红色箭头将是红色的,因为它们不符合此特定顺序。)

其中一种方法是:

digraph G {

  {rank = same; "$1"; "for"}
  {rank = same; "$2"; "initial assignment"}
  {rank = same; "$3"; "condition"}
  {rank = same; "$4"; "final assignment"}
  {rank = same; "$5"; "code"}
  {rank = same; "$6"; "end"}

  "$1" -> "$2" -> "$3" -> "$4" -> "$5" -> "$6"    

  "for" -> "initial assignment"
  "initial assignment" -> "condition"
  "condition" -> "code" [color=red]
  "condition" -> "end" [color=red]
  "code" -> "final assignment" [color=red]
  "final assignment" -> "condition" [color=red]

}

在此处输入图片描述

但是,这会在图表上显示那些额外的节点,而我宁愿没有这些节点。如何在不绘制节点和边的情况下定义它们?

(我可以将边缘定义为白色,将节点文本定义为白色等,但 graphviz 仍会为它们分配空间,这在透明 png 图像上不起作用。此外,图层不适用于所有输出格式(例如 .png),并且这些节点所需的空间仍然已分配。我们不考虑这些解决方案。)

答案1

史蒂芬·诺斯Graphviz 的作者,建议使用[style=invis]

digraph G {

  {
    node [style=invis]
    edge [style=invis]
    "$1" -> "$2" -> "$3" -> "$4" -> "$5" -> "$6"    
  }

  {rank = same; "$1"; "for"}
  {rank = same; "$2"; "initial assignment"}
................................................................................

输出

这并不能完全解决问题,因为仍然为这些碎片分配了空间,但我不得不猜测这已经是最好的结果了。

答案2

为什么不使用节点本身来建议特定的顺序?

有向图G
{
  {
    边缘[style=invis]

    一个[标签=“为”]
    b[label="初始分配"]
    c[标签="条件"]
    d[label="最终作业"]
    e[label="代码"]
    f[标签="结束"]

    a->b->c->d->e->f
  }

  a->b->c
  c -> e [颜色=红色]
  c -> f [颜色=红色]
  e -> d -> c [颜色=红色]
}

答案3

只需在第一个例子中添加一个隐藏的关系,这样“结束”就被排在“最终分配”之后:

  "final assignment" -> "end" [style=invis]

因此就变成:

digraph G {
  "for" -> "initial assignment"
  "initial assignment" -> "condition"
  "condition" -> "code" [color=red]
  "condition" -> "end" [color=red]
  "code" -> "final assignment" [color=red]
  "final assignment" -> "condition" [color=red]
  "final assignment" -> "end" [style=invis]
}

在此处输入图片描述

答案4

Graphviz 提供了两个边属性:dir = back 和 minlen = 3,可以应用这两个属性来生成所需的图形,而无需使用隐藏节点和边来明确强制节点排名。生成图形需要额外的簿记,但这可以编码在源模型中并易于获取。

dir属性用于在不改变节点的层次顺序的情况下反转边的逻辑意义,并minlen强制边的最小长度(以节点等级为单位)。

digraph Critical {
  "for" -> "initial assignment"
  "initial assignment" -> "condition"
  "condition" -> "code" [color=red]
  "condition" -> "end" [color=red, minlen = 3] 
  "final assignment" ->"code" [dir = back, color=red]
  "condition" -> "final assignment" [dir = back, color=red]
}

相关内容