Search code examples
cellgraphviz

Graphviz/ Table/ How to merge cells


I would like to draw a graph like this -

Expected graph

I have Graphviz code like this -

digraph G {
    
    
"test" [
    label = <<table border="0" cellspacing="0">
    <tr>
        <td port="f0" border="1" bgcolor="darkorange">TEST</td>
        <td port="f1" border="1" bgcolor="darkorange"></td>
    </tr>
    <tr>
        <td port="f2" border="1" bgcolor="cyan">A</td>
                 <td>
                    <table border="0" cellspacing="0">
                        <tr><td port="f3" border="1" bgcolor="azure">A1</td></tr>
                        <tr><td port="f4" border="1" bgcolor="azure">A2</td></tr>
                        <tr><td port="f5" border="1" bgcolor="azure">A3</td></tr>                         
                    </table>
                </td>      
    </tr>          
    <tr>
        <td port="f5" border="1" bgcolor="gray">Else</td>
        <td port="f6" border="1" bgcolor="gray"></td>
    </tr>
</table>>
shape = "none"
];

    
}

But it gives the graph like this

output of the above graphviz code

Would you please suggest how can we tweak the code to achieve the objective - merging f0, f1 on top and f5,f6 at bottom?


Solution

  • You can use HTML <td>s with colspan and rowspan attributes in GraphViz. These allow one cell to span multiple columns and/or rows inside a table.

    This also simplifies your digraph, as only one table is needed.

    digraph G {
        
        
    "test" [
        label = <<table border="0" cellspacing="0">
        <tr>
            <td colspan="2" port="f0" border="1" bgcolor="darkorange">TEST</td>
        </tr>
        <tr>
            <td rowspan="3" port="f5" border="1" bgcolor="blue">A</td>
            <td port="f6" border="1" bgcolor="white">A1</td>
        </tr>
        <tr>
            <td port="f6" border="1" bgcolor="white">A2</td>
        </tr>
        <tr>
            <td port="f6" border="1" bgcolor="white">A3</td>
        </tr>
        <tr>
            <td colspan="2" port="f0" border="1" bgcolor="grey">Else</td>
        </tr>
    </table>>
    shape = "none"
    ];
        
    }
    

    This gives you the following basic output, which you can then customize for spacing, line colors, etc:

    enter image description here