Graphviz + Dot 经验分享

Graphviz + dot 经验分享,不定期更新。

cluster 去除边框


subgraph 中设置 peripheries=0

节点对齐


节点对齐可以用 rankgroupweightconstraint 多种方法实现。

rank 方法

rank 的方向由 rankdir 决定,例如 rankdir=LR 表示从左至右,也就是节点依次从左往右排列。如果指定两个节点有相同的 rank,则它们在 rankdir 垂直的方向上是对齐的。例如:

digraph G {
    rankdir=LR
    rank=same { a c }
    a -> b -> c
}

group 方法

拥有相同 group 的节点在布局时,他们之间的边会尽量保持直线。因此可以用来对齐节点。

原文:If the end points of an edge belong to the same group, i.e., have the same group attribute, parameters are set to avoid crossings and keep the edges straight.

示例:

digraph G {
    rankdir=LR
    
    a [group=g1]
    b [group=g1]
    d [group=g1]
    
    a -> b -> c -> d
    b -> d
    a -> c
}

weight 方法

dot 中,weight 的值只能是整数,值越大,边越短、越直、越垂直。

原文:In dot, the heavier the weight, the shorter, straighter and more vertical the edge is.

示例:

digraph G {
    rankdir=TB

    a -> b -> c -> d
    b -> d [weight=9]
    a -> c [weight=0]
}

constraint 方法

将边的 constraint 设置为 false 会导致这条边在 rank 计算中被忽略。其结果表现为边的头尾节点拥有相同的 rank, 与 rank=same 有类似效果又不完全相同,因此也可以用来对齐节点。

原文:If false, the edge is not used in ranking the nodes.

示例:

digraph G {
  a -> c;
  a -> b;
  b -> c [constraint=false];
}

注意以下示例与 rank=same 的行为差异:

digraph G {
  a -> c;
  a -> b [constraint=false];
  b -> c [constraint=false];
}

猜你喜欢

转载自blog.csdn.net/ZML086/article/details/122238353
dot