You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Graphviz中如何调整盒子大小以确保文本始终在框内?

解决Graphviz中文本超出盒子的问题

你的Dot代码里用shape=plain搭配HTML表格构建UML类节点时,出现了文本溢出盒子的情况,核心原因是内部表格的属性限制了宽度适配。下面是两种直接有效的解决方法:

方法一:调整HTML表格属性,让盒子自动适配内容

移除内部表格的align="left"属性,并给内部表格添加width="100%",让它撑满外层单元格的空间,外层盒子就会根据内容自动调整到足够大的尺寸。

修改后的完整代码:

digraph UML_Class_diagram {
    graph [
        label="UML Class diagram"
        labelloc="t"
        fontname="Helvetica,Arial,sans-serif"
        splines=ortho, nodesep=1
        overlap=prism, overlap_scaling=0.1
    ]
    node [
        fontname="Helvetica,Arial,sans-serif"
        shape=record
        style=filled
        fillcolor=gray95
    ]
    edge [fontname="Helvetica,Arial,sans-serif"]
    edge [arrowhead=vee style=solid]

    ADN293[
        shape=plain
        label=<<table border="0" cellborder="1" cellspacing="0" cellpadding="4">
            <tr> <td> <b>CAx connector and function</b> </td> </tr>
            <tr> <td>
                <table border="0" cellborder="0" cellspacing="0" width="100%">
                    <tr> <td align="left" >+ loss performance relevant : Boolean : null</td> </tr>
                    <tr> <td align="left" >+ number of connectors : Integer (count) : ZeroToOne</td> </tr>
                    <tr> <td align="left" >+ Number of function groups : Integer (count) : null</td> </tr>
                    <tr> <td port="ss2" align="left" >- Function group : Reference</td> </tr>
                </table> 
            </td> </tr> 
        </table>> 
    ]
    edge [dir=back arrowtail=diamond]
    ADN293:ss2 -> AAS252 [xlabel=null] ;
    
    AAS252[
        shape=plain
        label=<<table border="0" cellborder="1" cellspacing="0" cellpadding="4">
            <tr> <td> <b>Function group</b> </td> </tr>
            <tr> <td>
                <table border="0" cellborder="0" cellspacing="0" width="100%">
                    <tr> <td align="left" >+ Number of functions : Integer (count) : null</td> </tr>
                    <tr> <td align="left" >+ type of function groups : String : null</td> </tr>
                    <tr> <td align="left" >+ Function group execution : String (translatable) : null</td> </tr>
                    <tr> <td align="left" >+ Function criterium : String : null</td> </tr>
                    <tr> <td align="left" >+ Number of parts relations : Integer (count) : null</td> </tr>
                    <tr> <td align="left" >+ Number of function groups : Integer (count) : null</td> </tr>
                </table> 
            </td> </tr> 
        </table>> 
    ]
    edge [dir=back arrowtail=diamond]
}

方法二:改用Graphiz原生record形状

如果不需要复杂的HTML排版,直接用Graphiz内置的shape=record更简单,它会自动计算内容宽度,确保所有文本都在盒子内。

示例代码:

digraph UML_Class_diagram {
    graph [
        label="UML Class diagram"
        labelloc="t"
        fontname="Helvetica,Arial,sans-serif"
        splines=ortho, nodesep=1
        overlap=prism, overlap_scaling=0.1
    ]
    node [
        fontname="Helvetica,Arial,sans-serif"
        shape=record
        style=filled
        fillcolor=gray95
    ]
    edge [fontname="Helvetica,Arial,sans-serif"]
    edge [arrowhead=vee style=solid]

    ADN293[label="{ <b>CAx connector and function</b> | + loss performance relevant : Boolean : null\n+ number of connectors : Integer (count) : ZeroToOne\n+ Number of function groups : Integer (count) : null\n<ss2> - Function group : Reference }"]
    
    edge [dir=back arrowtail=diamond]
    ADN293:ss2 -> AAS252 [xlabel=null] ;
    
    AAS252[label="{ <b>Function group</b> | + Number of functions : Integer (count) : null\n+ type of function groups : String : null\n+ Function group execution : String (translatable) : null\n+ Function criterium : String : null\n+ Number of parts relations : Integer (count) : null\n+ Number of function groups : Integer (count) : null }"]
    
    edge [dir=back arrowtail=diamond]
}

内容的提问来源于stack exchange,提问作者Joseph

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 00:45:35