Search code examples
graphviz

Is there a way to add arrow from a token/word to another in graphviz?


I'm wondering if it's possible to create something like this usint graphviz, where an arrow points from a token/word to the other, instead of a node.


Solution

  • It is possible to simulate (a lot of things) with tables, though it's usually very ugly in source code:

    digraph  {
        node [shape=plain]
        node1 [
            label=<
                <table cellspacing="0" bgcolor="#d0e2f2" cellborder="0">
                    <tr><td></td></tr>
                    <tr><td port="label">foo bar</td></tr>
                    <tr><td></td></tr>
                </table>>
        ]
         node2 [
            label=<
                <table cellspacing="0" bgcolor="#d0e2f2" cellborder="0">
                    <tr><td></td></tr>
                    <tr><td port="label">baz qux</td></tr>
                    <tr><td></td></tr>
                </table>>
        ]
    
        node1:label:n -> node2:label:n [constraint=false]
    }
    

    Result:


    What I did here:

    1. I used a plain node shape and HTML-like label syntax to create a table:
    node [shape=plain]
    node1 [
        label=<>
    ]
    
    1. I added 3 rows for my table, first and last one being empty:
    <tr><td></td></tr>
    <tr><td port="label">foo bar</td></tr>
    <tr><td></td></tr>
    
    1. The middle row contains the actual label. Also, to be able to point an edge to specific cell I've added a port to it: <td port="label">foo bar</td>.

    2. Finally when defining an edge I've specified the ports to be connected (documentation on ports):

    node1:label:n -> node2:label:n