use*_*448 7 key directed-graph legend graphviz
我试图在Graphviz中创建一个图例/键,其中不仅包含文本,还包含节点和边.虽然我已经阅读过这篇文章,但HTML表格似乎与我正在尝试的内容无关.
现在,我使用的代码是:
digraph G {
fontname="Helvetica";
labelloc=t;
rankdir=LR;
label="Course Graph";
node[style=filled, fontname="Helvetica", colorscheme=greens3, color=1];
subgraph cluster_key {
rank=min;
label="Key";
rankdir=LR;
kc1[label="Course", peripheries=2, color=2];
k1[shape=plaintext, style=solid, label="Required Course"]
prereq[label="Course 1"];
kc2[label="Course 2"];
prereq->kc2;
k2[shape=plaintext, style=solid, label="Course 1 is a prerequisite for Course 2"]
coreq1[label="Course 1"];
coreq2[label="Course 2"];
coreq1->coreq2[dir=both];
k3[shape=plaintext, style=solid, label="Course 1 and Course 2 are corequisite"]
or[style="dashed", color="black", shape="diamond", label="OR"];
or1[label="Course 1"];
or1 -> or[style="dashed", dir="none"];
or2[label="Course 2"];
or2 -> or[style="dashed", dir="none"];
kc3[label="Course 3"]
or->kc3;
k4[shape=plaintext, style=solid, label="You must take either Course 1 OR\nCourse 2 before taking Course 3"]
{ rank=min;k1 k2 k3 k4 }
}
c3[color=3, peripheries=2];
c4[color=3, peripheries=2];
c1->c2[dir=both];
c2->c3;
c4_reqs[style="dashed", color="black", shape="diamond", label="OR"];
c4_reqs->c4;
c2->c4_reqs[style="dashed", dir="none"];
c5->c4_reqs[style="dashed", dir="none"];
}
Run Code Online (Sandbox Code Playgroud)
这段代码的结果是:

但是我想要更像这样的东西 - 最好是更小的尺寸:

Pot*_*rca 10
你离我不远.通过一些小的调整我得到以下结果:

我做的最重要的改变是使用rank=source而不是rank=min让节点正确排列.
为了修复文本对齐,我习惯将文本对齐\r(左右对齐\l),并为所有明文节点提供相同的宽度.
整个代码看起来像这样(我添加了一些注释,我做了更改):
digraph G {
fontname="Helvetica";
labelloc=t;
rankdir=LR;
label="Course Graph";
node[style=filled, fontname="Helvetica", colorscheme=greens3, color=1];
subgraph cluster_key {
//rank=min; /* this doesn't really do anything for you */
label="Key";
//rankdir=LR; /* this is also not needed*/
kc1[label="Course", peripheries=2, color=2];
k1[shape=plaintext, style=solid, label="Required Course\r", width=3.5] // Add fixed width so all nodes line up
prereq[label="Course 1"];
kc2[label="Course 2"];
prereq->kc2;
k2[shape=plaintext, style=solid, label="Course 1 is a prerequisite for Course 2\r", width=3.5] // Add fixed width
coreq1[label="Course 1"];
coreq2[label="Course 2"];
coreq1->coreq2[dir=both];
k3[shape=plaintext, style=solid, label="Course 1 and Course 2 are corequisite\r", width=3.5] // Add fixed width
or[style="dashed", color="black", shape="diamond", label="OR"];
or1[label="Course 1"];
or1 -> or[style="dashed", dir="none"];
or2[label="Course 2"];
or2 -> or[style="dashed", dir="none"];
kc3[label="Course 3"]
or->kc3;
k4[shape=plaintext, style=solid, label="You must take either Course 1 OR\rCourse 2 before taking Course 3\r", width=3.5] // Add fixed width
{ rank=source;k1 k2 k3 k4 } // Use "source in stead of min
}
c3[color=3, peripheries=2];
c4[color=3, peripheries=2];
c1->c2[dir=both];
c2->c3;
c4_reqs[style="dashed", color="black", shape="diamond", label="OR"];
c4_reqs->c4;
c2->c4_reqs[style="dashed", dir="none"];
c5->c4_reqs[style="dashed", dir="none"];
}
Run Code Online (Sandbox Code Playgroud)
另外,通过将所有明文节点放在一起可以清除代码,因此不必更频繁地声明属性.这将使节点和秩属性的额外好处不被分成代码中的不同部分:
{
rank=source
node [shape=plaintext, style=solid, width=3.5]
k1 [label="Required Course\r"]
k2 [label="Course 1 is a prerequisite for Course 2\r"]
k3 [label="Course 1 and Course 2 are corequisite\r"]
k4 [label="You must take either Course 1 OR\rCourse 2 before taking Course 3\r"]
}
Run Code Online (Sandbox Code Playgroud)