在Graphviz有向图中对齐节点

时间:2019-04-14 13:40:41

标签: graphviz

我有以下Graphviz代码:

digraph {
  Technique [shape = box];
  Path [shape = box];
  KnowledgeObservation [shape = box, label = "Knowledge\nObservation"];
  ManagementIntervention [shape = box, label = "Management\nIntervention"];
  ResultsModification [shape = box, label = "Results\nModification"];
  SharedCode [label = "Shared Code"];
  MediatedRelationship [label = "Mediated Relationship"];
  Art -> Technique;
  Therapy -> Path;
  {Technique Path} -> KnowledgeObservation -> ManagementIntervention -> ResultsModification;
  {MediatedRelationship SharedCode} -> {KnowledgeObservation ResultsModification}

  subgraph {
    rank = same
    Technique -> Path [dir = none]
  }

  subgraph {
    rank = same
    SharedCode
    ManagementIntervention
    MediatedRelationship     
  }
}

当前产生以下输出:

Graphviz

如何将“管理干预”与“知识观察”和“结果修改”垂直对齐?

“共享代码”应移到“管理干预”的左侧。

“中介关系”应移至“管理干预”的右侧。

“共享代码”,“管理干预”和“中介关系”应保持水平对齐。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

无需子图即可实现;最重要的修改是线条

{ rank = same; SharedCode -> ManagementIntervention -> MediatedRelationship[ style = invis ] }

这不仅使三个节点保持在正确的级别,而且还保持在所需的顺序内。

总的来说,这里的代码

digraph 
{
  // node definition
  Art Therapy;
  Technique[ shape = box ];
  Path[ shape = box ];
  KnowledgeObservation[ shape = box, label = "Knowledge\nObservation" ];
  ManagementIntervention[ shape = box, label = "Management\nIntervention" ];
  ResultsModification[ shape = box, label = "Results\nModification" ];
  SharedCode[ label = "Shared Code" ];
  MediatedRelationship[ label = "Mediated Relationship" ];

  // edges
  Art -> Technique;
  Therapy -> Path;
  { rank = same; Technique -> Path [dir = none] }
  { Technique Path} -> KnowledgeObservation -> ManagementIntervention -> ResultsModification;
  { rank = same; SharedCode -> ManagementIntervention -> MediatedRelationship[ style = invis ] } 

  { MediatedRelationship SharedCode } -> { KnowledgeObservation ResultsModification }
}

给你

enter image description here

根据我的理解,这就是您要寻找的东西。

仍然我建议将这三行代码替换为

KnowledgeObservation -> { SharedCode MediatedRelationship }[ dir = back ];
SharedCode -> ResultsModification;
MediatedRelationship -> ResultsModification;

原因是,一旦您的图变得更加复杂,graphviz就会识别并维护层次关系,而不是以令人惊讶的方式解释不明确的指令。

答案 1 :(得分:0)

如何将“管理干预”与“知识观察”和“结果修改”垂直对齐?

这可以通过增加边缘的weight来实现。 weight较高的边缘往往比较低的边缘更直,更短。

“中介关系”应移到“管理干预”的右侧。

您可以通过定义节点的顺序来控制它。如果在“共享代码”之前在“ emem”之前定义“管理干预”,则将首先绘制(MI)。例如,在SI的左侧。

“共享代码”,“管理干预”和“中介关系”应保持水平对齐。

使用rank =same子图属性,您做对了。尽管我已经将子图放在节点定义的时刻了。这样可以缩短源大小,并将等级规格限制在定义节点的位置,这对于可读性(所有内容都在一个位置中说明)很有帮助。


您修改的示例:

digraph {
  Technique [shape = box];
  Path [shape = box];
  KnowledgeObservation [shape = box, label = "Knowledge\nObservation"];
  ResultsModification [shape = box, label = "Results\nModification"];
  subgraph {
    rank=same

    ManagementIntervention [shape = box, label = "Management\nIntervention"];
    MediatedRelationship [label = "Mediated Relationship"];
    SharedCode [label = "Shared Code"];
  }
  Art -> Technique;
  Therapy -> Path;
  {Technique Path} -> KnowledgeObservation
  KnowledgeObservation -> ManagementIntervention -> ResultsModification [weight=3]
  {MediatedRelationship SharedCode} -> {KnowledgeObservation ResultsModification}

  subgraph {
    rank = same
    Technique -> Path [dir = none]
  }
}

结果: