I have problems to organize my subgraphs horizontal and the nodes inside the subgraphs vertical. All of them (subgraphs and nodes) are just on one line (horizontal or vertical).
digraph G {
rankdir = LR;
subgraph cluster_0 {
rankdir = TB;
node [style=filled];
label = "Title 1";
color=black
N1 -> N2;
}
subgraph cluster_1 {
rankdir = TB;
node [style=filled];
label = "Title 2";
color=black
N3 -> N4 -> N5;
}
subgraph cluster_2 {
rankdir = TB;
node [style=filled];
...
}
...
N2 -> N3;
...
N1 [label = "BA_A", fillcolor="green", shape="Msquare"]
N2 [label = "W2", fillcolor="green", shape="octagon"]
N3 [label = "BA_A", fillcolor="green", shape="Msquare"]
N4 [label = "W2", fillcolor="green", shape="octagon"]
N5 [label = "W2_ERROR", fillcolor="red", shape="octagon"]
N6 [label = "W3", fillcolor="green", shape="invtriangle"]
...
}
I also tried with {rank=same; N1; N3; ...;}. This take the nodes out of the subgraphs.
You can use something like that:
digraph G {
rankdir = LR;
subgraph cluster_0 {
{rank=same N1 N2}
label = "Title 1";
N1 -> N2;
}
subgraph cluster_1 {
{rank=same N3 N4 N5}
label = "Title 2";
N3 -> N4 -> N5;
}
subgraph cluster_2 {
node [style=filled];
label = "Title 3";
N6;
}
N2 -> N3;
N5 -> N6;
N1 [label = "BA_A", fillcolor="green", shape="Msquare"]
N2 [label = "W2", fillcolor="green", shape="octagon"]
N3 [label = "BA_A", fillcolor="green", shape="Msquare"]
N4 [label = "W2", fillcolor="green", shape="octagon"]
N5 [label = "W2_ERROR", fillcolor="red", shape="octagon"]
N6 [label = "W3", fillcolor="green", shape="invtriangle"]
}
In your case I'd prefer top-to-bottom layout
digraph G {
rankdir = TB;
node [style=filled];
subgraph cluster_0 {
N1 N2
label = "Title 1";
edge [dir = back]
N2 -> N1;
}
subgraph cluster_1 {
N3 N4 N5
label = "Title 2";
edge [dir = back]
N5 -> N4 -> N3;
}
subgraph cluster_2 {
label = "Title 3";
N6
}
N2 -> N3 [constraint=none];
N5 -> N6 [constraint=none];
N1 [label = "BA_A", fillcolor="green", shape="Msquare"]
N2 [label = "W2", fillcolor="green", shape="octagon"]
N3 [label = "BA_A", fillcolor="green", shape="Msquare"]
N4 [label = "W2", fillcolor="green", shape="octagon"]
N5 [label = "W2_ERROR", fillcolor="red", shape="octagon"]
N6 [label = "W3", fillcolor="green", shape="invtriangle"]
}
Related
I try to draw multiple finite state diagram in graphviz in only one diagram
For now i have this :
digraph D {
rankdir = LR;
node [shape = circle]
subgraph automaton_1 {
invis [shape = point style = invis]
0 [label =<S<SUB>0</SUB>>]
1 [label =<S<SUB>1</SUB>>]
2 [label =<S<SUB>2</SUB>>]
3 [label =<S<SUB>3</SUB>>]
4 [label =<S<SUB>4</SUB>>]
5 [label =<S<SUB>5</SUB>> peripheries = 2]
}
invis -> 0
0 -> 1[label = "a"]
1 -> 2[label = "b"]
0 -> 3[label = "b"]
3 -> 4[label = "c"]
2 -> 5[label = "d"]
4 -> 5[label = "d"]
subgraph automaton_2 {
b_invis [shape = point style = invis]
b_0 [label =<S<SUB>0</SUB>'>]
b_1 [label =<S<SUB>1</SUB>'>]
b_2 [label =<S<SUB>2</SUB>'>]
b_3 [label =<S<SUB>3</SUB>'>]
b_4 [label =<S<SUB>4</SUB>'>]
b_5 [label =<S<SUB>5</SUB>'> peripheries = 2]
}
b_invis -> b_0
b_0 -> b_1[label = "a"]
b_1 -> b_2[label = "b"]
b_0 -> b_3[label = "b"]
b_3 -> b_4[label = "c"]
b_2 -> b_5[label = "d"]
b_4 -> b_5[label = "d"]
invis -> b_invis[constraint=false, style=invis]
}
which give this result : image 1
But i want to have two independently aligned diagram, aligned like this : image 2
You can turn your subgraphs into actual clusters by adding cluster_ to their names. Inside clusters nodes from each of the subgraphs won't be affected by each other.
Here's the corrected code and the result (note that I've also added peripheries=0 for each cluster to remove borders:
digraph D {
rankdir = LR;
node [shape = circle]
subgraph cluster_automaton_1 {
peripheries=0
invis [shape = point style = invis]
0 [label =<S<SUB>0</SUB>>]
1 [label =<S<SUB>1</SUB>>]
2 [label =<S<SUB>2</SUB>>]
3 [label =<S<SUB>3</SUB>>]
4 [label =<S<SUB>4</SUB>>]
5 [label =<S<SUB>5</SUB>> peripheries = 2]
}
invis -> 0
0 -> 1[label = "a"]
1 -> 2[label = "b"]
0 -> 3[label = "b"]
3 -> 4[label = "c"]
2 -> 5[label = "d"]
4 -> 5[label = "d"]
subgraph cluster_automaton_2 {
peripheries=0
b_invis [shape = point style = invis]
b_0 [label =<S<SUB>0</SUB>'>]
b_1 [label =<S<SUB>1</SUB>'>]
b_2 [label =<S<SUB>2</SUB>'>]
b_3 [label =<S<SUB>3</SUB>'>]
b_4 [label =<S<SUB>4</SUB>'>]
b_5 [label =<S<SUB>5</SUB>'> peripheries = 2]
}
b_invis -> b_0
b_0 -> b_1[label = "a"]
b_1 -> b_2[label = "b"]
b_0 -> b_3[label = "b"]
b_3 -> b_4[label = "c"]
b_2 -> b_5[label = "d"]
b_4 -> b_5[label = "d"]
invis -> b_invis[constraint=false, style=invis]
}
I'm trying to recreate the following syntax diagram using Graphviz (which will eventually be embedded in Sphinx):
Using the DOT language, I defined the following diagram:
digraph numexpr {
bgcolor="transparent"
{rank = same;
p_0[shape=point];
n_1[shape=block, label="constant", group=g1];
p_1[shape=point]}
n_2[shape=block, label="enumerated-list", group=g1]
n_3[shape=block, label="reference", group=g1]
n_4[shape=block, label="function-call", group=g1]
n_5[shape=block, label="operator-expression", group=g1]
n_6[shape=block, label="iterative-expression", group=g1]
n_7[shape=block, label="conditional-expression", group=g1]
n_8[shape=block, label="logical-expression", group=g1]
{rank = same;
c_1[shape=circle, label="("];
n_9[shape=block, label="numerical-expression", group=g1];
c_2[shape=circle, label=")"]}
p_0 -> n_1 [arrowsize=.5]
p_0 -> n_2 [arrowsize=.5]
p_0 -> n_3 [arrowsize=.5]
p_0 -> n_4 [arrowsize=.5]
p_0 -> n_5 [arrowsize=.5]
p_0 -> n_6 [arrowsize=.5]
p_0 -> n_7 [arrowsize=.5]
p_0 -> n_8 [arrowsize=.5]
p_0 -> c_1 [arrowsize=.5]
c_1 -> n_9 [arrowsize=.5]
n_1 -> p_1 [arrowsize=.5]
n_2 -> p_1 [arrowsize=.5]
n_3 -> p_1 [arrowsize=.5]
n_4 -> p_1 [arrowsize=.5]
n_5 -> p_1 [arrowsize=.5]
n_6 -> p_1 [arrowsize=.5]
n_7 -> p_1 [arrowsize=.5]
n_8 -> p_1 [arrowsize=.5]
n_9 -> c_2 [arrowsize=.5]
c_2 -> p_1 [arrowsize=.5]
edge[style=invis];
n_1 -> n_2
n_2 -> n_3
n_3 -> n_4
n_4 -> n_5
n_5 -> n_6
n_6 -> n_7
n_7 -> n_8
n_8 -> n_9
}
Rendering as follows:
Close but no cigar. How can one manipulate the edges such that the render will look more similar to the original syntax diagram?
Here is an approximation. Done with ~225 lines of dot code and requiring a 3-step process:
dot -Tdot ... >somefile
hand editing somefile to add 18 horizontal edges and adding layout=neato
dot -Tpng somefile >somefile.png
If I had to do it again, I'd generate pos values for both nodes and edges.
digraph numexpr {
nodesep=.7
ranksep=.22
bgcolor="transparent"
node[pin=true]
subgraph clusterLeftSide {
peripheries=0
margin=30
node [shape=point width=.01 qlabel="" style=solid ordering=out]
nls1; nls2; nls3; nls4; nls5; nls6; nls7; nls8; nls9
ls1; ls2; ls3; ls4; ls5; ls6; ls7; ls8;
ls9 [style=invis]
node [shape=point width=.01 qlabel="" style=invis ordering=out]
fls1; fls2; fls3; fls4; fls5; fls6; fls7; fls8; fls9
edge [tailclip=false headclip=false dir=none]
{
rank=same
fls1->ls1 [style=solid]
ls1->nls1 [style=solid]
}
{
rank=same
fls2->ls2 [style=invis]
ls2->nls2 [style=invis]
}
{
rank=same
fls3->ls3 [style=invis]
ls3->nls3 [style=invis]
}
{
rank=same
fls4->ls4 [style=invis]
ls4->nls4 [style=invis]
}
{
rank=same
fls5->ls5 [style=invis]
ls5->nls5 [style=invis]
}
{
rank=same
fls6->ls6 [style=invis]
ls6->nls6 [style=invis]
}
{
rank=same
fls7->ls7 [style=invis]
ls7->nls7 [style=invis]
}
{
rank=same
fls8->ls8 [style=invis]
ls8->nls8 [style=invis]
}
{
rank=same
fls9->ls9 [style=invis]
ls9->nls9 [style=invis]
}
fls1->fls2->fls3->fls4->fls5->fls6->fls7->fls8->fls9 [style=invis]
node [shape=point width=.01 label="" style=zinvis]
edge [dir=none style=solid tailclip=false headclip=false]
ls1->ls2->ls3->ls4->ls5->ls6->ls7->ls8 [style=solid]
ls8->ls9 [style=invis]
}
subgraph clusterMain {
peripheries=0
margin=30
n1[shape=box label="constant",xleft=1 xright=1 ];
n2[shape=box label="enumerated-list",xleft=1 xright=1 ]
n3[shape=box label="reference",xleft=1 xright=1 ]
n4[shape=box label="function-call",xleft=1 xright=1 ]
n5[shape=box label="operator-expression",xleft=1 xright=1 ]
n6[shape=box label="iterative-expression",xleft=1 xright=1 ]
n7[shape=box label="conditional-expression",xleft=1 xright=1 ]
n8[shape=box label="logical-expression",xleft=1 xright=1 ]
{
rank = same;
c9a[shape=circle, label="(" xleft=1 ];
n9[shape=box width=2.7 label="numerical-expression" ];
c9b[shape=circle, label=")" xright=1 ]
c9a -> n9 -> c9b
}
edge[style=invis];
n1 -> n2
n2 -> n3
n3 -> n4
n4 -> n5
n5 -> n6
n6 -> n7
n7 -> n8
n8 -> n9
}
subgraph clusterRightSide {
peripheries=0
margin=30
node [shape=point width=.01 qlabel="" style=solid ordering=out]
nrs1; nrs2; nrs3; nrs4; nrs5; nrs6; nrs7; nrs8; nrs9
rs1; rs2; rs3; rs4; rs5; rs6; rs7; rs8;
rs9 [style=invis]
node [shape=point width=.01 qlabel="" style=invis ordering=out]
frs1; frs2; frs3; frs4; frs5; frs6; frs7; frs8; frs9
edge [tailclip=false headclip=false zdir=none]
{
rank=same
nrs1->rs1 [style=solid dir=none]
rs1->frs1 [style=solid]
}
{
rank=same
nrs2->rs2 [style=invis]
rs2->frs2 [style=invis]
}
{
rank=same
nrs3->rs3 [style=invis]
rs3->frs3 [style=invis]
}
{
rank=same
nrs4->rs4 [style=invis]
rs4->frs4 [style=invis]
}
{
rank=same
nrs5->rs5 [style=invis]
rs5->frs5 [style=invis]
}
{
rank=same
nrs6->rs6 [style=invis]
rs6->frs6 [style=invis]
}
{
rank=same
nrs7->rs7 [style=invis]
rs7->frs7 [style=invis]
}
{
rank=same
nrs8->rs8 [style=invis]
rs8->frs8 [style=invis]
}
{
rank=same
nrs9->rs9 [style=invis]
rs9->frs9 [style=invis]
}
nrs1->nrs2->nrs3->nrs4->nrs5->nrs6->nrs7->nrs8 [style=invis]
nrs8->nrs9 [style=invis]
node [shape=point width=.01 label="" style=zinvis]
edge [dir=none style=solid tailclip=false headclip=false]
rs1->rs2 [dir=back]
rs2->rs3->rs4->rs5->rs6->rs7->rs8 [style=solid]
rs8->rs9 [style=invis]
frs1->frs2->frs3->frs4->frs5->frs6->frs7->frs8->frs9 [style=invis]
}
edge [dir=none]
ls1:s->nls2:w
ls2:s->nls3:w
ls3:s->nls4:w
ls4:s->nls5:w
ls5:s->nls6:w
ls6:s->nls7:w
ls7:s->nls8:w
ls8:s->nls9:w
nrs2:e->rs1:s
nrs3:e->rs2:s
nrs4:e->rs3:s
nrs5:e->rs4:s
nrs6:e->rs5:s
nrs7:e->rs6:s
nrs8:e->rs7:s
nrs9:e->rs8:s
/* add these lines to the output of: dot -Tdot
edge [dir=forward]
nls1->n1
nls2->n2
nls3->n3
nls4->n4
nls5->n5
nls6->n6
nls7->n7
nls8->n8
nls9->c9a
edge [dir=none]
n1->nrs1
n2->nrs2
n3->nrs3
n4->nrs4
n5->nrs5
n6->nrs6
n7->nrs7
n8->nrs8
c9b->nrs9
*/
}
I want draw a superscript on Node in using of dot language.
Is this possible?
If yes, how can I do this?
If no, what tools should I ues to get graph like this?
The xlabel attribute creates an external label for a node or edge. The following will get you part of the way there. Once you have your entire graph created you can play with spacing to avoid overlaps.
graph {
node [shape = circle];
edge [style = dashed; minlen = 2];
NOP [style = dashed; xlabel = "0"];
node [label = "*"]; n1 n2 n3 n6 n8;
n1 [xlabel = "1"];
n2 [xlabel = "2"];
n3 [xlabel = "3"];
n6 [xlabel = "6"];
n8 [xlabel = "8"];
n10 [xlabel = "10"];
NOP -- n1;
NOP -- n2;
NOP -- n3;
NOP -- n6;
NOP -- n8;
n10 [label = "+"];
NOP -- n10;
}
I'm trying to get the following dot file to output two subgraphs. I want the bLoop node in cluster0 to align with the ISR struct in cluster 2. I'm using an invisible node to do this now, but with the unintended consequence of lot of gray space left in cluster0.
Is there a way to do what I want without the invisible node?
I can't post images yet, so here's the link.
digraph G {
ranksep=.75;
nodesep = 1.5;
node [shape = none]
node[fontsize=16,fixedsize=false,width=0.7,shape=rectangle];
edge[fontsize=16];
ratio=fill;
splines=false;
compound=true;
subgraph cluster0 {
node [style=filled];
style=filled;
color=lightgrey;
label = "Setup and Background Loop";
a0[label = "Peripheral Configs"];
a1[label = "Solar Library Block Configs"];
a2[label = "Enable Interrupts"];
bgLoop[label = "Start Background Loop"];
e0[shape=rectangle, style=invis, fixedsize=true, width=.01];
a0 -> a1 -> a2 -> bgLoop;
bgLoop ->e0[style=invis]
}
subgraph cluster1 {
node [style=filled, shape = "doublecircle"];
start
style="invis"
}
subgraph cluster2 {
node [shape=record,color=white];
style=filled;
color=lightgrey;
label = "ISRs";
struct1 [shape = record, color=white, label="{<f1> Slow ISR | <f2> Fast ISR }"];
}
concentrate = true;
struct1 -> bgLoop[lhead=cluster0, ltail=cluster4, constraint=true];
bgLoop -> struct1[lhead=cluster4, ltail=cluster0, constraint=true];
struct1 -> e0[style=invis, constraint=true];
start -> a0[lhead=cluster0];
}
you need helper nodes to get the correct rank for struct1.
digraph G {
ranksep=.75;
nodesep = 1.5;
node[fontsize=16,fixedsize=false,width=0.7,shape=rectangle];
edge[fontsize=16];
compound=true
subgraph cluster2 { rank="max"
node [shape=record,color=white];
style=filled;
color=lightgrey;
label = "ISRs";
struct1 [shape = record, color=white, label="{<f1> Slow ISR | <f2> Fast ISR }"];
}
subgraph cluster0 {
node [style=filled];
style=filled;
color=lightgrey;
label = "Setup and Background Loop";
a0[label = "Peripheral Configs"];
a1[label = "Solar Library Block Configs"];
a2[label = "Enable Interrupts"];
bgLoop[label = "Start Background Loop"];
a0 -> a1 -> a2 -> bgLoop;
}
subgraph cluster1 {
node [style=filled, shape = "doublecircle"];
start
style="invis"
}
{node [style=invis]; 0; 1; 2; 3; }
{edge [style=invis]; 0->1->2->3->struct1; }
struct1 -> bgLoop[lhead=cluster0, ltail=cluster2, constraint=false];
bgLoop -> struct1[lhead=cluster2, ltail=cluster0, constraint=false];
start -> a0[lhead=cluster0];
}
I use the following code to produce a graph using dot in Graphviz. I have manually included the coordinates of nodes, as I require four disjoint subgraphs placed adjacent to one another, as in the picture.
I would love to add labels under each of the subgraphs: $G_0$, $G_1$, etc. Adding label under subgraph creates a box and ignores my coordinates alignment. Is there any other way, like placing arbitrary text at specified coordinates? I use "dot -Teps -Kfdp -n trees -o t.eps" for compilation.
digraph Trees {
node [shape=circle, style="filled", fixedsize=true,width=0.6]; 0; 1;2; 3;4; 5;6; 7; 8;9;10;11;12;13;14;15;
0[pos = "0,1!"]
1[fillcolor=red, pos = "-1,2!"]
2[pos = "1,2!"]
3 [pos = "0,-0.5!"]
5[label=1, fillcolor=red, pos = "2,2!"]
4[label=0, fillcolor=red, pos = "3,1!"]
6[label=2, pos = "4,2!"]
7[label=3, pos = "3, -0.5!"]
9[label=1, fillcolor=red, pos = "5,2!"]
8[label=0, fillcolor=red, pos = "6,1!"]
10[label=2, pos = "7,2!"]
11[label=3, fillcolor=red, pos = "6, -0.5!"]
12[label=1, fillcolor=red, pos = "8,2!"]
13[label=0, fillcolor=green, pos = "9,1!"]
14[label=2, pos = "10, 2!"]
15[label=3, fillcolor=green, pos = "9, -0.5!"]
overlap=false;
fontsize=10;
subgraph 1{
edge [dir=none] 1->0 2->0 3->0;
}
subgraph 2{
edge [color=red] 5->4;
edge[color=black, dir=none] 6->4 7->4;
}
subgraph 3{
edge [color=red] 9->8 8->11;
edge [color=black, dir=none] 8->10;
}
subgraph 4{
edge [color=green] 12->13; 13->15;
edge [color=black, dir=none] 13->14;
}
}
The sub-graphs can be made disjoint using clusters and the dot layout engine. The same approach will also permit the introduction of cluster labels. They can be placed at the bottom of the cluster as required without creating dummy nodes.
This way, no absolute positions are required, and, the layout is automatically generated even if other nodes are added. The exact position of the nodes change, but the graphs remain topologically unchanged.
digraph Trees { node [shape = circle, style = "filled", fixedsize = true, width=0.4];
edge [dir = none];
layout = dot; overlap = false; fontsize = 10;
graph [labelloc = "b", penwidth = 0];
{ node [fillcolor = "red"];
1; 5 [label = 1]; 4 [label = 0]; 9 [label = 1];
8 [label = 0]; 11 [label = 3]; 12 [label = 1];
}
2; 0; 3; 6 [label = 2]; 7 [label = 3];
10 [label = 2]; 14 [label = 2];
{ node [fillcolor = "green"];
13 [label = 0]; 15 [label = 3];
}
subgraph cluster1{
label = "Subgraph 1";
{ 1; 2; } -> 0 -> 3;
}
subgraph cluster2{
label = "Subgraph 2";
5 -> 4 [color = red, dir = fwd];
6 -> 4 -> 7;
}
subgraph cluster3{
label = "Subgraph 3";
9 -> 8-> 11 [color=red, dir = fwd];
10 -> 8 [color=black];
}
subgraph cluster4{
label = "Subgraph 4";
12 -> 13 -> 15 [color=green, dir = fwd];
14-> 13;
}
}
Instead of using explicit node positions, you may use a simple directed graph combined with some rank constraints, invisible edges and text nodes instead of subgraph labels:
digraph Trees {
fontsize=10;
node [shape=circle, style="filled", fixedsize=true,width=0.6];
{rank=same;
a1[label=1, fillcolor=red];
a2[label=2];
a3[label=1, fillcolor=red];
a4[label=2];
a5[label=1, fillcolor=red];
a6[label=2];
a7[label=1, fillcolor=red];
a8[label=2];
}
node[label=0];
b1;
b2[fillcolor=red];
b3[fillcolor=red];
b4[fillcolor=green];
node[label=3];
c1;
c2;
c3[fillcolor=red];
c4[fillcolor=green];
node[shape=none, fillcolor=transparent];
d1[label="Label 1"];
d2[label="Label 2"];
d3[label="Label 3"];
d4[label="Label 4"];
edge[dir=none];
a1->b1;
a2->b1;
b1->c1;
c1->d1[style=invis];
a3->b2[dir=forward, fillcolor=red, color=red];
a4->b2;
b2->c2;
c2->d2[style=invis];
a5->b3[dir=forward, fillcolor=red, color=red];
a6->b3[dir=forward, fillcolor=red, color=red];
b3->c3;
c3->d3[style=invis];
a7->b4[dir=forward, fillcolor=green, color=green];
a8->b4[dir=forward, fillcolor=green, color=green];
b4->c4;
c4->d4[style=invis];
edge[style=invis];
a2 -> a3;
a4 -> a5;
a6 -> a7;
}