首页 > 解决方案 > 如何用 GraphViz 分离节点的图片和标签?

问题描述

我正在尝试使用 GraphViz 显示带有图像和标签的图形。我想在图像下显示标签(参见labelloc="b"图表上的选项),但不知何故它不起作用。标签和图像重叠。

知道我缺少什么吗?

下面是我正在使用的 DOT 代码和当前结果。

谢谢!

具有 3 个节点、背景图像和标签的 VizGraph

digraph {
graph [compound=true, labelloc="b"];
node [shape=box];
edge [dir=none];

Label1[label="Label1",image="images/Avatar1.png"];
Label2[label="Label2",image="images/Avatar2.png"];
Label3[label="Label3",image="images/Avatar3.png"];

{ 
    rank=same;
    Label1 -> h0 -> Label2;
    h0[shape=circle,label="",height=0.01,width=0.01];
}
{
    h0_0;
    h0_0[shape=circle,label="",height=0.01,width=0.01];
}

h0 -> h0_0;
h0_0 -> Label3;
}

标签: graphviz

解决方案


UPD:您只需将imagepos属性添加到您的解决方案中height

digraph {
graph [compound=true, labelloc="b"];
node [shape=box];
edge [dir=none];

Label1[
    label="Label1"
    height="2.1"
    imagepos="tc"
    labelloc="b"
    image="images/Avatar1.png"
];
Label2[
    label="Label2"
    height="2.1"
    imagepos="tc"
    labelloc="b"
    image="images/Avatar2.png"
];
Label3[
    label="Label3"
    height="2.1"
    imagepos="tc"
    labelloc="b"
    image="images/Avatar3.png"
];

{ 
    rank=same;
    Label1 -> h0 -> Label2;
    h0[shape=circle,label="",height=0.01,width=0.01];
}
{
    h0_0;
    h0_0[shape=circle,label="",height=0.01,width=0.01];
}

h0 -> h0_0;
h0_0 -> Label3;
}

结果:


或者你也可以使用类似 HTML 的标签,特别是表格:

digraph {
graph [compound=true, labelloc="b"];
node [shape=box];
edge [dir=none];

Label1 [
    shape=plain
    label=<
        <table cellspacing="0" border="0" cellborder="1">
            <tr><td><img src="images/Avatar1.png" /></td></tr>
            <tr><td>Label1</td></tr>
        </table>
    >
];
Label2 [
    shape=plain
    label=<
        <table cellspacing="0" border="0" cellborder="1">
            <tr><td><img src="images/Avatar2.png" /></td></tr>
            <tr><td>Label2</td></tr>
        </table>
    >
];
Label3 [
    shape=plain
    label=<
        <table cellspacing="0" border="0" cellborder="1">
            <tr><td><img src="images/Avatar3.png" /></td></tr>
            <tr><td>Label3</td></tr>
        </table>
    >
];

{
    rank=same;
    Label1 -> h0 -> Label2;
    h0[shape=circle,label="",height=0.01,width=0.01];
}
{
    h0_0;
    h0_0[shape=circle,label="",height=0.01,width=0.01];
}

h0 -> h0_0;
h0_0 -> Label3;
}

代码有点复杂(乍一看),但作为奖励,您可以更灵活地控制边界。结果:


推荐阅读