2013-10-21 46 views
7

我想創建一個Graphviz中的圖例/密鑰,其中不僅包含文本,還包含節點和邊。雖然我讀過this post,但HTML表格似乎不適用於我想要做的事情。Graphviz圖例/密鑰與節點

現在,我使用的代碼是:

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"]; 

} 

這段代碼的結果是:

The result of this code is

,但我想更多的東西這樣的 - 尺寸最好小於:

ebut I would like something more like

回答

9

你並不遙遠。隨着一些小的調整,我得到了以下結果:

enter image description here

我所做的最重要的變化是使用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"]; 

} 

在一個側面說明,該代碼可以被清理了一下:

整個代碼看起來像這樣(我加了一些意見,我所做的更改)明文節點放在一起,所以不需要更頻繁地聲明屬性。這將具有節點和等級屬性不會被分成代碼中的不同部分的附加益處:

{ 
     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"] 
    }