在节点的左侧和右侧添加标签

时间:2021-06-24 14:29:10

标签: graphviz

我有这个输入:

graph {
"1" -- "11"
"11" -- "111"
"1" -- "12"
"12" -- "121"
"12" -- "122"
}

哪个会产生这个图

enter image description here

是否可以在节点的左侧和右侧添加标签,这样输出将是这样的?

enter image description here

无需严格使用graphviz/DOT

2 个答案:

答案 0 :(得分:0)

使用来自和到同一节点的边标签进行一点graphviz“hack”是可能的,但隐藏边(笔宽=0):

graph {
    edge [penwidth=0];
    "1":w -- "1":w  [taillabel="1"]
    "1":e -- "1":e  [taillabel="12"]
    "11":n -- "11":w  [taillabel="21"] // north - west
    "11":e -- "11":e  [taillabel="21"]
    "12":w -- "12":w  [taillabel="1"]
    "12":e -- "12":e  [taillabel="6"]
    "111":w -- "111":w  [taillabel="1"]
    "111":e -- "111":e  [taillabel="6"]
    "121":w -- "121":w  [taillabel="1"]
    "121":e -- "121":e  [taillabel="6"]
    "122":w -- "122":w  [taillabel="1"]
    "122":e -- "122":e  [taillabel="6"]
    edge [penwidth=1];
    
    "1" -- "11"
    "11" -- "111"
    "1" -- "12"
    "12" -- "121"
    "12" -- "122"
}

graphviz graph output

答案 1 :(得分:0)

如果您对节点形状很灵活,这里有一个使用类似 html 的节点 https://www.graphviz.org/doc/info/shapes.html#html 的解决方案。有点冗长,但很容易以编程方式生成。

graph {
    graph [splines=false]
    node  [shape=plaintext]
    N1 [label=<
      <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
        <TR>
      <TD BORDER="0">1</TD><TD PORT="p1">1</TD><TD BORDER="0">12</TD>
    </TR>
      </TABLE>>];
    N2 [label=<
      <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
        <TR>
      <TD BORDER="0">21</TD><TD PORT="p2">11</TD><TD BORDER="0">21</TD>
    </TR>
      </TABLE>>];
    N3 [label=<
      <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
        <TR>
      <TD BORDER="0">1</TD><TD PORT="p3">12</TD><TD BORDER="0">6</TD>
    </TR>
      </TABLE>>];
   N4 [label=<
      <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
        <TR>
      <TD BORDER="0">1</TD><TD PORT="p4">11</TD><TD BORDER="0">6</TD>
    </TR>
      </TABLE>>];
   N5 [label=<
      <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
        <TR>
      <TD BORDER="0">1</TD><TD PORT="p5">121</TD><TD BORDER="0">6</TD>
    </TR>
      </TABLE>>];
   N6 [label=<
      <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
        <TR>
      <TD BORDER="0">1</TD><TD PORT="p6">122</TD><TD BORDER="0">6</TD>
    </TR>
      </TABLE>>];
    
      N1:p1--N2:p2
      N1:p1--N3:p3
      N2:p2--N4:p4
      N3:p3--N5:p5
      N3:p3--N6:p6
}

给予:
enter image description here