Can I refer to a node using a short name in DOT - graphviz

I'm trying to respresent a binary tree with GraphViz using DOT, can I have a longer text string as a node's text but them just refer to it using a shortname when linking it?
I'm working with GraphViz through Pandoc and Pandoc-plot.
I've tried setting and node's ID, name, label and refering to it but it just doesn't work. In my document it just got the codeblock instead a rendered graph, I've tested it with a simple graph and it renders fine.
I've tried a graph like this in the GraphViz online editor and the node's with the long names don't get connected in the right way.
graph {
"Adam \n 01/01"[id="adam"]
"Bella \n01/02"[id="bella"]
"Eloise \n01/05"
adam -- bella
bella -- eloise
}

Looks like you mix up node names and node labels (by default the node name is equal to the node label, so in your case the node name is "Adam \n 01/01" and also the label is "Adam \n 01/01" so you have to refer to it like:
"Adam \n 01/01" -- "Bella \n01/02"
but this is quite cumbersome and error prone so looks like you are looking for (as indicated bu "short names" request):
graph {
adam [label="Adam \n 01/01"]
bella [label="Bella \n01/02"]
eloise [label ="Eloise \n01/05"]
adam -- bella
bella -- eloise
}

Related

Multiple graphs inside Graphviz DOT file

I have this Graphviz DOT graph:
digraph unit_test {
label="Unit test"
edge [fillcolor="#a6cee3" color="#1f78b4"]
node[shape="ellipse" style="filled" fillcolor="#1f77b4"]
start
end
node[shape="box" style="filled" fillcolor="#ff7f0e"]
process
subgraph cluster_process {
label = "Major logic"
process
}
start -> process
process -> end
}
The above renders as:
I have this second graph:
digraph details {
label = "Process details"
edge [fillcolor="#a6cee3" color="#1f78b4"]
node[shape="ellipse" style="filled" fillcolor="#1f77b4"]
start
end
node[shape="box" style="filled" fillcolor="#ff7f0e"]
details
subgraph cluster_details {
label = "Details"
details
}
start -> details
details -> end
}
Which renders to:
Problem
When I put the above two graphs inside the same DOT file named supporting.dot and I run dot -Tpng -o supporting.png supporting.dot command, terminal prints out some jiberish and the output image file won't contain both graphs, it just contains the first one. Is it possible to use multiple graphs inside a single DOT file? If so, what am I missing?
Question is unclear about what is to be accomplished, but maybe the following is a starting point
digraph G{
subgraph unit_test {
label="Unit test"
edge [fillcolor="#a6cee3" color="#1f78b4"]
node[shape="ellipse" style="filled" fillcolor="#1f77b4"]
start
end
node[shape="box" style="filled" fillcolor="#ff7f0e"]
process
subgraph cluster_process {
label = "Major logic"
process
}
start -> process
process -> end
}
subgraph details {
label = "Process details"
edge [fillcolor="#a6cee3" color="#1f78b4"]
node[shape="ellipse" style="filled" fillcolor="#1f77b4"]
start1 [label="start"]
end1 [label="end"]
node[shape="box" style="filled" fillcolor="#ff7f0e"]
details
subgraph cluster_details {
label = "Details"
details
}
start1 -> details
details -> end1
}
}
Note the naming / labels in the second subgraph.
Dot can't render 2 graphs into a single file, the output you see is probably the content of one of the graphs as a png.
In order to prevent that, you may run your graphs first through gvpack - something similar to:
gvpack -u supporting.dot | dot -Tpng -o supporting.png
This combines all graphs in supporting.dot into a single graph, which then is rendered with dot.
The layout of the graphs can be influenced by some more options of gvpack.
It is legal to have multiple graphs defined in one input file. You can then produce multiple output files using the -O option, like this:
dot -Tpng -O multi.gv
This will produce multi.gv.png and multi.gv.2.png
I got a better answer
http://www.bound-t.com/manuals/ref-manual.pdf
The -dot_dir option and the names of drawing files
The -dot option creates a single file that contains all drawings from one Bound-T run. If you
then use the dot tool to create a PostScript file, each drawing will go on its own page in the
PostScript file. However, dot can also generate graphical formats that do not have a concept of
"page" and then it may happen that only the first drawing is visible. If you want to use such
non-paged graphical formats it is better to create a directory (folder) to hold the drawing files
and use the Bound-T option -dot_dir instead of the option -dot. The -dot_dir option creates a
separate file for each drawing, named as follows:
• The call-graph of a root subprogram is put in a file called cg_R_nnn.dot, where R is the
link-name of the root subprogram, edited to replace most non-alphanumeric characters
with underscores '_', and nnn is a sequential number to distinguish root subprograms that
have the same name after this editing.
• If the call-graph of some root subrogram is recursive, Bound-T draws the joint call-graph of
all roots and puts it in a file called jcg_all_roots_001.dot.
• The flow-graph of a subprogram is put in a file called fg_S_nnn.dot, where S is the linkname of the subprogram, edited as above, and nnn is a sequential number to distinguish
subprograms that have the same name after this editing and also to distinguish drawings
that show different flow-graphs (execution bounds) for the same subprogram.
The sequential numbers nnn start from 1 and increment by 1 for each drawing file; the same
number sequence is shared by all types of drawings and all subprograms. For example, if we
analyse the root subprogram main?func that calls the two subprograms start$sense and
start$actuate, with the -dot_dir option and -draw options that ask for one flow-graph drawing
of each subprogram, the following drawing files are created:
• cg_main_func_001.dot for the call-graph of main?func
• fg_main_func_002.dot for the flow-graph of main?func
• fg_start_sense_003.dot for the flow-graph of start$sense
• fg_start_actuate_004.dot for the flow-graph of start$actuate.

Aligning Clusters within Graphviz

I am using Graphviz to automatically create an architecture diagram. I am having the following two problems and was hoping to get assistance.
I am using UUID to uniquely identify a component (example: "a5320de8-a320-11ea-bb37-0242ac130002" [label="Component A"]). When mapping A -> B, I'll get "Component A" -> b0c5e47c. Which is strange. The only way that I've been able to map UUID to UUID is to put quotes around them. Any suggestions?
I want to align clusters in a specific manner and specific direction. I've tried {rank=same; cluster_B, cluster_C, cluster_D}; and "9653369c-a322-11ea-bb37-0242ac130002" -> "aa31adb9-9621-40c2-855c-621832dd8c61" [style=invis] But neither work.
I have three sections within my dot file, they are:
Components (within this section, I list out all 100+ components and color code them based on a specific rule.
Clusters (within this section, I cluster the components into specific 'groupings')
Diagram or mapping (within this section, I then map the different components and clusters).
Here is a sample of my DOT file.
digraph architecture {
#graph [rankdir=LR]
compound=true;
#Compliant
node[fillcolor="#013220" style="filled" shape=square fontcolor="white"];
"a5320de8-a320-11ea-bb37-0242ac130002" [label="Component A"]
"b0c5e47c-a320-11ea-bb37-0242ac130002" [label="Component B"]
#Clusters
#Customer-facing client application cluster
subgraph cluster_A{
label="Client Apps";
"f7b3915d-6b3d-4d4c-bef0-bdabda915c03";
"9912de2b-739a-4c5c-834e-e0c3d09d70d1";
"16bb2066-9293-470e-99ec-c59d8426c0ab";
"641a6601-f4f6-4c06-baa6-e5e232f8abed";
"c5e92b09-a470-4fb6-af5c-e5f7dbeff919";
}
#Diagrams
"f7b3915d-6b3d-4d4c-bef0-bdabda915c03" -> {"35305026-d285-458c-85ad-7eae4e785e84", "76e0e679-42a6-47f0-9164-abc223da07fe"};
76e0e679-42a6-47f0-9164-abc223da07fe" -> "35305026-d285-458c-85ad-7eae4e785108";
}
I get something like:
However, I want to arrange the cluster is a specific way, like:
As you found out, hyphens are not legal characters in a node ID unless the string is quoted. If you want more info: https://www.graphviz.org/doc/info/lang.html
There is no straight-forward to align clusters. Sometime you can force desired alignment by embedding multiple clusters within another cluster to "shrink-wrap" them. For example embed B3 and B4 within cluster B34. But no guarantees.
You can use gvpr to reposition clusters (and their contents) but that can get pretty complex.

Cryptic dot error message for graph with big (sub)clusters

I've written a little tool dumping (in dot format) the dependency graph of a project where all the files living in the same directory are gathered in a cluster. When I try to generate a pdf containing the corresponding graph, dot starts to cry:
The command dot -Tpdf trimmedgraph.dot -o graph.pdf produces the cryptic error message Error: install_in_rank clusterReals virtual rank 21 i = 0 an = 0 which does not yield any result on google.
I've tried to edit trimmedgraph.dot manually: turning the subgraph clusterReals into Reals yields a file that can be compiled but all the content of my Reals/ directory is obviously not gathered anymore.
Is there a way to generate only dot-valid files (I was planning to send my patch upstream eventually but if I cannot guarantee that everything will be okay...)?
I've put the two versions of trimmedgraph.dot online but they are rather big and given that I have no idea where the problem is, I cannot really come up with a minimal file recreating the problem.
I think (but couldn't find any documentation about this) cluster names have to be unique within the entire dot file. In your file however, there are two subgraphs called clusterReals.
The solution is to make sure all cluster names are unique - since the name doesn't appear anywhere in the output, you may just use numbers when generating dot files.
A quick test shows that strange things happen when reusing the same cluster name:
digraph dependencies {
subgraph cluster0 {
label="First cluster 0";
Node1;
subgraph cluster0 {
label="Second cluster 0";
Node2;
}
}
subgraph cluster0 {
label="Third cluster 0";
Node3;
}
subgraph cluster1 {
label="Cluster 1";
Node4;
subgraph cluster0 {
label="Fourth cluster 0";
Node5;
}
}
}
All the cluster0 seem to be merged together (nodes, label), unless it is not possible because they're included by other clusters. At least that's what it looks like... Since the consequences are unpredictable (error in your case), I'd try to always use unique cluster names.

Get node text only if contain an attribute?

XPath problem.
I have these nodes:
[...]
<videos>
<video timestamp="201204271112">myVideo.avi</video>
<video>myVideo.avi</video>
<video timestamp="201204271113">myVideo.avi</video>
<video>myVideo.avi</video>
<video>myVideo.avi</video>
</videos>
<photos>
<photo timestamp="201204271112">myphoto.avi</video>
<photo>myphoto.avi</video>
<photo timestamp="201204271113">aphoto.avi</video>
<photo>myphoto.avi</video>
<photo>myphoto.avi</video>
</photos>
[...]
How can i get only node text that contains timestamp attribute?
I tried
//#timestamp
it returns ALL timestamps attribute only. And the text?
How can make a query that include all two conditions? AND condition.
Something like this:
//#text and //#timestamps
to get only
201204271112 - myVideo.avi
201204271113 - myVideo.avi
201204271113 - aphoto.avi
excluding other ones?
thanks.
How can i get only node text that contains timestamp attribute?
Could you mean //*[#timestamp]/text()? That selects all text nodes whose parents have the timestamp attribute.
The conditions are in XPaths, too (i.e. //video[#timestamp and text()] selects all video nodes that have both timestamp and some text nodes).
What you probably meant is a node-set union used with symbol |. To get both the timestamps and the text nodes, you'll need two queries unioned together: //#timestamp | //*[#timestamp]/text() gets all timestamps and all their text nodes. However, I don't think you can get it nicely aligned (there will be all timestamps first, then all text nodes).
You can try either iterating one by one with some kind of for loop and get both the timestamp and the text node via position, or you can just get all nodes that have a timestamp and dig their text out of them later (which is a preffered way).
The spec is a surprisingly good read on this.
You can match on attributes:
//video[#timestamp]/text()
//video = matches a node with name video anywhere in the treee
[#timestamp] is a predicate, meaning the node has to have this attribute
text() selects all text node children of the current node

How to insert mathematical symbols like Greek characters in a Graphviz dot file?

I have an unique problem. I am using dot to represent a graph which is generic in nature. So, instead of using numbers, I was planning to use symbols like greek letters like alpha, beta, etc. I am curious to know how can we label nodes/edges in .dot file using some symbols?
for example,
node1 -> node2 [label= <something here which will show up as symbol of beta> style=dashed]
You can use HTML-like a labels:
digraph G {
a [ label=<α>]
b [ label=<β>]
c [ label=<γ>]
a -> b -> c
}
will show alpha -> beta -> gamma:
You could also used named HTML references to make it even clearer (mentioned in a comment):
label=<I love α and β>
The surrounding <> indicate that the label should be parsed as a custom language that looks like an HTML subset: http://www.graphviz.org/doc/info/lang.html#html
Unicode characters
Sometimes you can get away with Unicode https://en.wikipedia.org/wiki/Greek_alphabet#Greek_in_Unicode
graph {
"α" -- "β"
}
Output:
You can also emulate some more math with Unicode, e.g.:
https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts
https://en.wikipedia.org/wiki/Mathematical_operators_and_symbols_in_Unicode
Tested on Ubuntu 16.10, graphviz 2.38.

Resources