Use Graphviz to show two tables adjacent to each other with lines between cells

冷暖自知 提交于 2021-02-10 15:43:49

问题


I would like to show the diagram below, except with the two tables on the same horizontal level. I would like the second table to be shown to the right of the first table and not below it like it is currently.

The current code for graphviz that I have is:

digraph G  {

   node [shape=record, fontname="Arial"];

   set1 [label = "{Blue Crosses | B1 | B2 | B3 | B4 }|{ Square |<b1>  Left |<b2> Left |<b3> Right | Left }"];
   set2 [label = "{Blue Crosses |<b1> B1 |<b2> B2 |<b3> B3 }|{ Coordinates | (1, 1) | (2, 2) | (4, 2) }"];

  set1:b1 -> set2:b1;
  set1:b2 -> set2:b2;
  set1:b3 -> set2:b3;

}

回答1:


In order to achieve what you want, you need to add the line

{ rank = same; set1 set2 }

to your code after you have created the nodes. However, in this situation, you will find that graphviz gets confused and doesn't recognise the ports anymore. So instead of using record = shape you will need to re-code your graph with HTML-like labels. I have done that for the example provided (thanks for a clear question with code and desired outcome, by the way):

digraph G  
{
   node[ shape = none, fontname = "Arial" ];

    set1[ label=<
    <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
        <TR>
            <TD>Blue Crosses</TD>
            <TD>Square</TD>
        </TR>
        <TR>
            <TD>B1</TD>
            <TD PORT="b1">Left</TD>
        </TR>
        <TR>
            <TD>B2</TD>
            <TD PORT="b2">Left</TD>
        </TR>
        <TR>
            <TD>Right</TD>
            <TD PORT="b3">Right</TD>
        </TR>
        <TR>
            <TD>B4</TD>
            <TD>Left</TD>
        </TR>
    </TABLE>>];

    set2[ label=<
    <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
        <TR>
            <TD>Blue Crosses</TD>
            <TD>Coordinates</TD>
        </TR>
        <TR>
            <TD PORT="b1">B1</TD>
            <TD>(1, 1)</TD>
        </TR>
        <TR>
            <TD PORT="b2">B2</TD>
            <TD>(2, 2)</TD>
        </TR>
        <TR>
            <TD PORT="b3">B3</TD>
            <TD>(4, 2)</TD>
        </TR>
    </TABLE>>];    

    # layout
    nodesep = 2;                /* increase distance between nodes */
    { rank = same; set1 set2 }

    set1:b1 -> set2:b1;
    set1:b2 -> set2:b2;
    set1:b3 -> set2:b3;
}

yields



来源:https://stackoverflow.com/questions/53462525/use-graphviz-to-show-two-tables-adjacent-to-each-other-with-lines-between-cells

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!