Graphviz绘制数组数据结构

时间:2016-11-11 04:24:56

标签: graphviz

我可以使用"记录"绘制一个像这样的数组:

graph G{
node [shape = record];
node0 [fontsize=13,  label ="A[0]|A[1]|A[2]"];
}

但我怎么能画出这样的东西呢 http://sites.tufts.edu/comp15/files/2013/06/pic2_1darrays1.png

特别是,如何为数组中的每个单元格添加索引号,如0,1,2,3,4,5。

我应该使用哪种节点形状?

2016/11/11更新

好的,我根据https://stackoverflow.com/a/37986662/5374561

的答案得到了它

代码在这里:

digraph so
{
rankdir=LR;
    subgraph cluster0
    {
        rank = same{ Array notes }
        color = white;
        Array [ shape = record, label = "{ A | B | C | D }"] ;
        notes [ shape = record, color = white, label = "{ 0 | 1 | 2 | 3 }" ];
        Array -> notes[ style = invis ];
    }
    nodesep = .0;
 }

但结果并不完美。还有其他方法吗?

2016年8月/ 9月更新

来自tequlia2pop的解决方案(谢谢)接近原始图片,但来自"指针"到"价值观"应该是直线。

1 个答案:

答案 0 :(得分:1)

digraph {
    node [shape=plaintext, fontcolor=red, fontsize=18];
    "Pointers:" -> "Values:" -> "Indices:" [color=white];

    node [shape=record, fontcolor=black, fontsize=14, width=4.75, fixedsize=true];
    pointers [label="<f0> A | <f1> A+1 | <f2> A+2 | <f3> A+3 | <f4> A+4 | <f5> A+5", color=white];
    values [label="<f0> A[0] | <f1> A[1] | <f2> A[2] | <f3> A[3] | <f4> A[4] | <f5> A[5]", color=blue, fillcolor=lightblue, style=filled];
    indices [label="0 | 1 | 2 | 3| 4 | 5", color=white];

    { rank=same; "Pointers:"; pointers }
    { rank=same; "Values:"; values }
    { rank=same; "Indices:"; indices }

    edge [color=blue];
    pointers:f0 -> values:f0;
    pointers:f1 -> values:f1;
    pointers:f2 -> values:f2;
    pointers:f3 -> values:f3;
    pointers:f4 -> values:f4;
    pointers:f5 -> values:f5;
}

产生

image link