Graphviz:在左侧对齐节点而不是居中(有答案)

时间:2018-02-22 10:09:39

标签: nodes graphviz rank

我制作了一个python脚本,用graphviz绘制目录结构。 为了正确渲染图形,我使用rankdir = LR。但是,具有相同等级的节点居中。

见下图。

Graph result

正如您所看到的,“。gitignore”,“。travis.yml”等节点由于其标签而不具有相同的宽度。 我希望无论宽度如何,左侧都有相同等级的节点。

我见过其他相关帖子,例如this one,但我不想强制节点宽度。

还有其他办法吗?

提前感谢您的回答并度过愉快的一天。

P.S .: 以下是我的图表代码示例:

strict digraph projetcStructure { 
    graph [overlap=false, splines=ortho, ranksep=0.05]
    edge[arrowhead=none, color=black]
    node[fontname="DejaVu Sans Mono", fontsize=14]
    "e0ec0f89f0745e25b725768b304be71e" [shape=folder, style=filled, colorscheme=ylgnbu3, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/file-directory.png" SCALE="TRUE"/></TD><TD>TechnicalReport</TD></TR></TABLE>>, color=2]
    "77a7da2a6f864418dd23f49f9c5e9804" [shape=note, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/git-merge.png" SCALE="TRUE"/></TD><TD>.gitignore</TD></TR></TABLE>>, color=1]
    "373d5e485b88f76e79d5bab15a88759e" [shape="point", width=0, height=0]
    "8e58ce33bdaaa1db79bab7080019c6fe" [shape=note, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/travis.png" SCALE="TRUE"/></TD><TD>.travis.yml</TD></TR></TABLE>>, color=1]
    "b56aa426f8ad0d8a7704c909f2473b70" [shape="point", width=0, height=0]
    "98a5f187b3527ca7f00643e7907b3453" [shape=note, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/book.png" SCALE="TRUE"/></TD><TD>LICENSE</TD></TR></TABLE>>, color=1]
    "07adcea039d69fb282b22eebd8daf537" [shape="point", width=0, height=0]
    "ede2c50901cc59eeb8987d630d055b22" [shape=note, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/terminal.png" SCALE="TRUE"/></TD><TD>makeScript.sh</TD></TR></TABLE>>, color=1]
    "8f182d8be99f90432a2bec25a864bf19" [shape="point", width=0, height=0]
    "53ccb88dccecc95c401d180aa34ac0ac" [shape=note, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/note.png" SCALE="TRUE"/></TD><TD>README.rst</TD></TR></TABLE>>, color=1]
    "f6ffc62db5cefc5b4a1eb2e6a3731802" [shape="point", width=0, height=0]
    "3f917d2353bb6c1c156b18a78f23cf71" [shape=note, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/file.png" SCALE="TRUE"/></TD><TD>requirements.txt</TD></TR></TABLE>>, color=1]
    "acc1004c581f3b603d2a33092ffcfe61" [shape="point", width=0, height=0]
    "2d05acf91c352a7ac491e18dcf65fc9c" [shape=note, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/settings.png" SCALE="TRUE"/></TD><TD>setup.cfg</TD></TR></TABLE>>, color=1]
    "8e1760a00144fca5272f341da0873674" [shape="point", width=0, height=0]
    "b78eb5bb77cb04f62506859fb1ab76f8" [shape=note, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/terminal.png" SCALE="TRUE"/></TD><TD>test.sh</TD></TR></TABLE>>, color=1]
    "cc60f62b1b3d0cf8c266074b7139b567" [shape="point", width=0, height=0]
    "3512568f6984d40e6353aa13b5029dc3" [shape=folder, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/mark-github.png" SCALE="TRUE"/></TD><TD>.github</TD></TR></TABLE>>, color=1]
    "88ae0eb0f89e79ddff8e2b1fe470a488" [shape="point", width=0, height=0]
    "9b43ae74e93bdb3dadace6084e333a27" [shape=folder, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/file-directory.png" SCALE="TRUE"/></TD><TD>foldertree</TD></TR></TABLE>>, color=1]
    "2f9f172522c65ab785d2c23db909b36a" [shape="point", width=0, height=0]
    "e7925cdfdefd09112b8b3c8b5ddde087" [shape=folder, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/file-directory.png" SCALE="TRUE"/></TD><TD>generated</TD></TR></TABLE>>, color=1]
    "0b54fbb48c26181df9c8a5fd3bab1049" [shape="point", width=0, height=0]
    "69d5f4cf4e091e008cb1f4de43692b4d" [shape=folder, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/file-directory.png" SCALE="TRUE"/></TD><TD>TechnicalReportGenerator</TD></TR></TABLE>>, color=1]
    "c4f859f4d369226348a4eb08adab0afc" [shape="point", width=0, height=0]
    "0ab55395ce4ef62f7a9f5f72201ce0ae" [shape=folder, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/file-directory.png" SCALE="TRUE"/></TD><TD>worklog</TD></TR></TABLE>>, color=1]
    "9b4f2a57b302961cd3509dd307121cfa" [shape="point", width=0, height=0]
    "77a7da2a6f864418dd23f49f9c5e9804" [shape=note, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/git-merge.png" SCALE="TRUE"/></TD><TD>.gitignore</TD></TR></TABLE>>, color=1]
    "8e58ce33bdaaa1db79bab7080019c6fe" [shape=note, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/travis.png" SCALE="TRUE"/></TD><TD>.travis.yml</TD></TR></TABLE>>, color=1]
    "98a5f187b3527ca7f00643e7907b3453" [shape=note, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/book.png" SCALE="TRUE"/></TD><TD>LICENSE</TD></TR></TABLE>>, color=1]
    "ede2c50901cc59eeb8987d630d055b22" [shape=note, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/terminal.png" SCALE="TRUE"/></TD><TD>makeScript.sh</TD></TR></TABLE>>, color=1]
    "53ccb88dccecc95c401d180aa34ac0ac" [shape=note, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/note.png" SCALE="TRUE"/></TD><TD>README.rst</TD></TR></TABLE>>, color=1]
    "3f917d2353bb6c1c156b18a78f23cf71" [shape=note, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/file.png" SCALE="TRUE"/></TD><TD>requirements.txt</TD></TR></TABLE>>, color=1]
    "2d05acf91c352a7ac491e18dcf65fc9c" [shape=note, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/settings.png" SCALE="TRUE"/></TD><TD>setup.cfg</TD></TR></TABLE>>, color=1]
    "b78eb5bb77cb04f62506859fb1ab76f8" [shape=note, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/terminal.png" SCALE="TRUE"/></TD><TD>test.sh</TD></TR></TABLE>>, color=1]
    "3512568f6984d40e6353aa13b5029dc3" [shape=folder, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/mark-github.png" SCALE="TRUE"/></TD><TD>.github</TD></TR></TABLE>>, color=1]
    "9b43ae74e93bdb3dadace6084e333a27" [shape=folder, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/file-directory.png" SCALE="TRUE"/></TD><TD>foldertree</TD></TR></TABLE>>, color=1]
    "e7925cdfdefd09112b8b3c8b5ddde087" [shape=folder, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/file-directory.png" SCALE="TRUE"/></TD><TD>generated</TD></TR></TABLE>>, color=1]
    "69d5f4cf4e091e008cb1f4de43692b4d" [shape=folder, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/file-directory.png" SCALE="TRUE"/></TD><TD>TechnicalReportGenerator</TD></TR></TABLE>>, color=1]
    "0ab55395ce4ef62f7a9f5f72201ce0ae" [shape=folder, style=filled, colorscheme=ylgnbu3, width=3.7555555555555555, label=<<TABLE><TR><TD WIDTH="20" HEIGHT="20" FIXEDSIZE="TRUE"><IMG SRC="/mnt/e/Documents/3eme/TechnicalReport/foldertree/octicons/file-directory.png" SCALE="TRUE"/></TD><TD>worklog</TD></TR></TABLE>>, color=1]
    {rank=same;  "373d5e485b88f76e79d5bab15a88759e" "b56aa426f8ad0d8a7704c909f2473b70" "07adcea039d69fb282b22eebd8daf537" "8f182d8be99f90432a2bec25a864bf19" "f6ffc62db5cefc5b4a1eb2e6a3731802" "acc1004c581f3b603d2a33092ffcfe61" "8e1760a00144fca5272f341da0873674" "cc60f62b1b3d0cf8c266074b7139b567" "88ae0eb0f89e79ddff8e2b1fe470a488" "2f9f172522c65ab785d2c23db909b36a" "0b54fbb48c26181df9c8a5fd3bab1049" "c4f859f4d369226348a4eb08adab0afc" "9b4f2a57b302961cd3509dd307121cfa"}
"e0ec0f89f0745e25b725768b304be71e" -> "373d5e485b88f76e79d5bab15a88759e"
"373d5e485b88f76e79d5bab15a88759e" -> "b56aa426f8ad0d8a7704c909f2473b70"
"b56aa426f8ad0d8a7704c909f2473b70" -> "07adcea039d69fb282b22eebd8daf537"
"07adcea039d69fb282b22eebd8daf537" -> "8f182d8be99f90432a2bec25a864bf19"
"8f182d8be99f90432a2bec25a864bf19" -> "f6ffc62db5cefc5b4a1eb2e6a3731802"
"f6ffc62db5cefc5b4a1eb2e6a3731802" -> "acc1004c581f3b603d2a33092ffcfe61"
"acc1004c581f3b603d2a33092ffcfe61" -> "8e1760a00144fca5272f341da0873674"
"8e1760a00144fca5272f341da0873674" -> "cc60f62b1b3d0cf8c266074b7139b567"
"cc60f62b1b3d0cf8c266074b7139b567" -> "88ae0eb0f89e79ddff8e2b1fe470a488"
"88ae0eb0f89e79ddff8e2b1fe470a488" -> "2f9f172522c65ab785d2c23db909b36a"
"2f9f172522c65ab785d2c23db909b36a" -> "0b54fbb48c26181df9c8a5fd3bab1049"
"0b54fbb48c26181df9c8a5fd3bab1049" -> "c4f859f4d369226348a4eb08adab0afc"
"c4f859f4d369226348a4eb08adab0afc" -> "9b4f2a57b302961cd3509dd307121cfa"
    {rank=same;  "77a7da2a6f864418dd23f49f9c5e9804" "8e58ce33bdaaa1db79bab7080019c6fe" "98a5f187b3527ca7f00643e7907b3453" "ede2c50901cc59eeb8987d630d055b22" "53ccb88dccecc95c401d180aa34ac0ac" "3f917d2353bb6c1c156b18a78f23cf71" "2d05acf91c352a7ac491e18dcf65fc9c" "b78eb5bb77cb04f62506859fb1ab76f8" "3512568f6984d40e6353aa13b5029dc3" "9b43ae74e93bdb3dadace6084e333a27" "e7925cdfdefd09112b8b3c8b5ddde087" "69d5f4cf4e091e008cb1f4de43692b4d" "0ab55395ce4ef62f7a9f5f72201ce0ae"}
"373d5e485b88f76e79d5bab15a88759e" -> "77a7da2a6f864418dd23f49f9c5e9804"
"b56aa426f8ad0d8a7704c909f2473b70" -> "8e58ce33bdaaa1db79bab7080019c6fe"
"07adcea039d69fb282b22eebd8daf537" -> "98a5f187b3527ca7f00643e7907b3453"
"8f182d8be99f90432a2bec25a864bf19" -> "ede2c50901cc59eeb8987d630d055b22"
"f6ffc62db5cefc5b4a1eb2e6a3731802" -> "53ccb88dccecc95c401d180aa34ac0ac"
"acc1004c581f3b603d2a33092ffcfe61" -> "3f917d2353bb6c1c156b18a78f23cf71"
"8e1760a00144fca5272f341da0873674" -> "2d05acf91c352a7ac491e18dcf65fc9c"
"cc60f62b1b3d0cf8c266074b7139b567" -> "b78eb5bb77cb04f62506859fb1ab76f8"
"88ae0eb0f89e79ddff8e2b1fe470a488" -> "3512568f6984d40e6353aa13b5029dc3"
"2f9f172522c65ab785d2c23db909b36a" -> "9b43ae74e93bdb3dadace6084e333a27"
"0b54fbb48c26181df9c8a5fd3bab1049" -> "e7925cdfdefd09112b8b3c8b5ddde087"
"c4f859f4d369226348a4eb08adab0afc" -> "69d5f4cf4e091e008cb1f4de43692b4d"
"9b4f2a57b302961cd3509dd307121cfa" -> "0ab55395ce4ef62f7a9f5f72201ce0ae"

}

[编辑]

经过一些研究并感谢@TomServo,我已经能够“修复”节点宽度。

我使用以下代码。可以找到ImageFont doc here

font = ImageFont.truetype('DejaVuSansMono.ttf', FONT_SIZE)
width = font.getsize("YOUR STRING TO MEASURE HERE")[0]
# convert pixels to inches and add extra space for icons and margins
# icon size is defined as points which is a 72's of an inch
width = width / GRAPHVIZ_DPI + ((1 / 72) * 20 * 2)

然后结果:

Better graph

0 个答案:

没有答案