添加新箭头时如何保持图形形式?

时间:2021-07-05 16:15:06

标签: graphviz

我有一个这样的图表:

enter image description here

问题 1:有没有办法在顶部水平对齐两列?

如果我想要一个箭头 a5 -> a1,那么图表将是:

enter image description here

问题 2:有没有办法让图表像以前一样干净?

代码如下:

digraph {
    rankdir="LR";
// overlap=false;
    nodesep="0.2";
    ranksep="0.4";
    fontsize = 25
    labelloc="t";
    fontname="Lato";
    node [ shape="plaintext" style="filled, rounded" fontname="Lato" margin=0.2 ]
    edge [ fontname="Lato" color="#2B303A" ]
    
    subgraph cluster_0 {
        style=filled;
        color=lightgrey;
        label = "Column 1";
        fontsize = 20
        node [style=filled,color=white];
        a1
        a2
        a3
        a4
        a5
        
    }

    subgraph cluster_1 {
        node [style=filled];
        color=blue
        label = "Column 2";
        fontsize = 20
        // labeljust=r
        // labelloc=b
        b1
        b2
        b3
        b4
    }

    a1 -> b1
    b1 -> a2
    a2 -> b2
    b2 -> a3
    a3 -> b3
    b3 -> a4
    a4 -> b4
    b4 -> a5
    a5 -> a1
}

2 个答案:

答案 0 :(得分:1)

需要两个小步骤:​​

(1) 将所有 a 节点置于同一等级。如果不这样做,graphviz 会在 a1a5 之间建立层级关系,这就是您的图表显示的内容。

(2) 为 a1b1 之间的边添加一些额外的权重,使其保持笔直。

digraph {
    rankdir="LR";
// overlap=false;
    nodesep="0.2";
    ranksep="0.4";
    fontsize = 25
    labelloc="t";
    fontname="Lato";
    node [ shape="plaintext" style="filled, rounded" fontname="Lato" margin=0.2 ]
    edge [ fontname="Lato" color="#2B303A" ]
    
    subgraph cluster_0 {
        style=filled;
        color=lightgrey;
        label = "Column 1";
        fontsize = 20
        node [style=filled,color=white];
        {rank = same; a1 a2 a3 a4 a5 }        // !!!
    }

    subgraph cluster_1 {
        node [style=filled];
        color=blue
        label = "Column 2";
        fontsize = 20
        // labeljust=r
        // labelloc=b
        b1
        b2
        b3
        b4
    }

    a1 -> b1[ weight = 10 ];        // !!!
    b1 -> a2
    a2 -> b2
    b2 -> a3
    a3 -> b3
    b3 -> a4
    a4 -> b4
    b4 -> a5
    a5 -> a1
}

现在生产

enter image description here

答案 1 :(得分:1)

我会通过以下两个更改来解决这个问题:

  • 使用 constraint=false 以免将 a5a1 的边弄乱

  • 使用 group 属性来建议节点之间的直边(与边连接时应该放置在一条直线上的节点的属性值相同)

    digraph {
      rankdir="LR";
    // overlap=false;
      nodesep="0.2";
      ranksep="0.4";
      fontsize = 25
      labelloc="t";
      fontname="Lato";
      node [ shape="plaintext" style="filled, rounded" fontname="Lato" margin=0.2 ]
      edge [ fontname="Lato" color="#2B303A" ]
    
      subgraph cluster_0 {
          style=filled;
          color=lightgrey;
          label = "Column 1";
          fontsize = 20
          node [style=filled,color=white];
          a1 [group=1]
          a2
          a3
          a4
          a5
    
      }
    
      subgraph cluster_1 {
          node [style=filled];
          color=blue
          label = "Column 2";
          fontsize = 20
          // labeljust=r
          // labelloc=b
          b1 [group=1]
          b2
          b3
          b4
      }
    
      a1 -> b1
      b1 -> a2
      a2 -> b2
      b2 -> a3
      a3 -> b3
      b3 -> a4
      a4 -> b4
      b4 -> a5
      a5 -> a1 [constraint=false]
    }
    

graphviz output

相关问题