Prevent overlapping records using graphviz and neato - graphviz

I am building a dot file to represent computer hardware and the physical connections to a network switch and displays. I have it looking ok when processed by the dot program but I think I really want it processed by neato to create a more "free form" picture as it starts to grom. Right now when I run my large file with neato, everything is overlapping.
I am trying to figure out the syntax on where to define the overlap attribute. Below is a subset of my dot file.
graph g {
node [shape=record,height=.1];
PC8[label="{{<GigE1>GigE1|<GigE2>GigE2}|{<name>PC8}|{<dvi1>dvi1|<dvi2>dvi2|<dvi3>dvi3|<dvi4>dvi4}}"];
PC9[label="{{<GigE1>GigE1|<GigE2>GigE2}|{<name>PC9}|{<dvi1>dvi1|<dvi2>dvi2|<dvi3>dvi3|<dvi4>dvi4}}"];
C1[label = "{{<dvi1>dvi1}|{<name>C1}}"];
C2[label = "{{<dvi1>dvi1}|{<name>C2}}"];
C3[label = "{{<dvi1>dvi1}|{<name>C3}}"];
C4[label = "{{<dvi1>dvi1}|{<name>C4}}"];
D1[label = "{{<dvi1>dvi1}|{<name>D1}}"];
D2[label = "{{<dvi1>dvi1}|{<name>D2}}"];
"PC8":dvi1 -- "C1":dvi1;
"PC8":dvi2 -- "C2":dvi1;
"PC8":dvi3 -- "C3":dvi1;
"PC8":dvi4 -- "C4":dvi1;
"PC9":dvi1 -- "D1":dvi1;
"PC9":dvi2 -- "D2":dvi1;
}

Well, as with most questions...soon after I posted the I figured out the answer. I needed to add graph [overlap=false]; at the top of the file.
Do it like this:
graph g {
overlap = false;
node [shape=record,height=.1];
/* ... */
}

Setting overlap to false will work for neato as the community wiki answer says; however, if the graph exhibits any kind of regularity or symmetry, [overlap=false] will often mess it up by jiggling the nodes around to make them not overlap.
Use [overlap=false] as a last resort.
All node overlaps that are outputted from neato can be viewed as occurring because the nodes are too big relative to the edges. You can make any overlaps go away by making the nodes smaller and preserve symmetry in the graph drawing by setting [overlap=scale]. Quoting the Neato user manual:
To improve clarity, it is sometimes helpful to eliminate overlapping
nodes or edges. One way to eliminate node overlaps is just to scale up
the layout (in terms of the center points of the nodes) as much as
needed. This is enabled by setting the graph attribute overlap=scale.
This transformation preserves the overall geometric relationships in
the layout, but in bad cases can require high scale factors
As the documentation says [overlap=scale] can result in graph drawings that are unacceptably large, but if it does not its output is generally going to be better looking than [overlap=false].

Related

graphviz - prevent nodes and edges overlapping without splines

I have a simple fdp graph laid out in graphviz, with a bunch of nodes (rectangle and diamond shaped) having fixed positions, and some of those nodes have child nodes (oval shaped) that I want to be arranged around them automatically.
I want all edges in it to be straight, with no curves or polylines or anything (i.e. as given by splines=false), but I also want those child nodes to not overlap with edges between nodes with fixed positions. The fixed positions are used as I need edges between the fixed nodes to be fully vertical or horizontal. This is something that I probably can achieve by iterating over different start values and hoping that the next one will not have overlaps, but so far this is a bit tiresome.
digraph G {
###GRAPH
layout=fdp
overlap=false
dpi=150
splines=false
inputscale=0.4
# fdp specific:
K=0.50
maxiter=2000
start=1251
###OBJECTS
node[shape=rectangle width=1.0 height=0.4 fixedsize=true]
vendor[label="Поставщик" pos="3,1!"
]
item[label="Товар" pos="0,2!"
]
post[label="Поставка" pos="2,2!"
]
check[label="Чек" pos="1,1!"
]
prod[label="Продажа" pos="0,0!"
]
###SUBOBJECTS
node[shape=oval width=1.5 height=0.5]
edge[arrowhead=none]
{
vendork[label=<<u>ID поставщика</u>>] "Название\nпоставщика" "Контакт\nдля связи"
}->vendor
{
itemk[label=<<u>ID товара</u>>]
Название Тип Подтип Характеристики "Розничная цена"
}->item
{
postk[label=<<u>ID записи</u>>] post1[label="ID товара"]
post2[label="Кол-во"] post3[label="Цена поставки"]
post4[label="Дата"] post5[label="Квитанция"]
}->post
{
prodk[label=<<u>ID продажи</u>>] prod1[label="ID товара"]
prod2[label="Кол-во"] prod3[label="Цена\nпродажи"]
prod4[label="Чек"]
}->prod
{
checkk[label=<<u>ID чека</u>>] Дата Кассир "Сумма" "НДС" "Итого" "Вид оплаты"
}->check
###CONNECTIONS
node[shape=diamond]
edge[arrowhead=open]
vendorpost[label="Поставляет" pos="3,2!"
]
vendorpost->vendor
vendorpost->post[arrowhead=openopen]
postitem[label="Поставлен" pos="1,2!"
]
postitem->item
postitem->post[arrowhead=openopen]
itemprod[label="Продан" pos="0,1!"
]
itemprod->item
itemprod->prod[arrowhead=openopen]
prodcheck[label="Включает" pos="1,0!"
]
prodcheck->prod[arrowhead=openopen]
prodcheck->check
}
(forgive the cyrillics)
I am compiling with no additional parameters (i.e. dot -Tpng graph.gv > graph.png). So far, I get this.
I've tried using neato instead as well, but, in most cases, it gives a pretty weird result, where it seems like it completely ignores the fixed node positions, and draws the child nodes whereever it wants:
I went through the list of attributes in the documentation, and so far none seem to address node/edge overlapping besides splines (unless I've missed something). What should I do to achieve the result I'm looking for?
There are several issues:
layout=fdp causes the fdp layout engine to be used, no matter what the command line says
Use neato -n if you want to nail down the node position (see below)
by adding a ! to the position you are nailing down the pos (it won't change)
dot is not useful here, it is directional (top-down)
If you remove all the !, I think you will get what you want.
So far, one solution I've found is by adding invisible stretched out nodes that cover each edge that is fixed(i.e. between two nodes with fixed positions) by appending this to the graph:
###NO VERTICAL/HORIZONTAL EDGE/NODE INTERSECT HACK
node[style=invis]
node[width=0.1 height=2.0]
t1[label="" pos="0,1.5!"]
t2[label="" pos="0,0.5!"]
t3[label="" pos="1,0.5!"]
t4[label="" pos="3,1.5!"]
node[width=2.0 height=0.1]
t5[label="" pos="2.5,2!"]
t6[label="" pos="1.5,2!"]
t7[label="" pos="0.5,2!"]
t8[label="" pos="0.5,0!"]
node[style=normal]
And this does what I want, i.e. removes all overlaps with fixed edges:
However, this is limited only to edges that are perfectly vertical/horizontal(note that there is still at least 1 overlap in "child" nodes), and overall seems like a really hacky solution as I have to add/change those "support beams" every time I add/change any more nodes to my graph. Is there a better solution to this?

Increasing rank spacing in graphviz

I have a graph laid out using D3 graphviz. It's laying out in a pretty dense way, and I'd like it to use more of the width of the screen.
I have tried adjusing the ranksep and the ratio, but neither seem to have any effect. I've also tried changing the node border, to no effect. Changing the edge minlength just makes the edges look floppy. I can try giving every node an invisible child, but that seems like a horrible hack.
For clarity, by rank, I mean the horizontal spacing between the columns of nodes. (The graph is laid out left to right.)
Ideally I'd be able to give it an aspect ratio and it'd make the most of that space to lay out the graph in a way that doesn't need too much acrobatics.
Am I missing something obvious?
I may well be using the options incorrectly
or the D3 graphviz implementation may not have those features?
Is there no good way to do this?
I have added :
size = "16.66,8.33!"; // 1200x600 at 72px/in, "!" to force
ratio = "fill"; // see https://graphviz.gitlab.io/_pages/doc/info/attrs.html#d:ratio
at the beginning of the graph declaration (just after digraph G {).
It renders a wider output.
Have a try and tell.

GraphViz Dot. How to distribute elements around a boxed size?

I have a dot graphviz file that geneates the following output:
http://www.qlands.com/other_files/test.png
However the output is organized vertically. If I setup the size to be for example 8.27 inches; How can I distribute the elements around a box of 8.27 x 8.27 inches?
Your graph is a directed graph layed out with dot from left to right, and the first rank contains many nodes which results in a very high image.
The main tool to break up graphs with this problem is unflatten:
unflatten is a preprocessor to dot that is used to improve the aspect
ratio of graphs having many leaves or disconnected nodes. The usual
layout for such a graph is generally very wide or tall. unflatten
inserts invisible edges or adjusts the minlen on edges to improve
layout compaction.
You may combine this with other tools and techniques to get the result you want:
Use the unflatten utility - please see this answer for a detailed example using unflatten.
Use invisible edges to introduce new ranks (basically what unflatten does automatically, but with human inspiration... example also here)
If you need the output to be of this exact size, be sure to understand graphviz's various attributes which have an impact on it, such as size, margin, ratio... (see also this and yet another answer providing details)
Finally, you could simply use a different layout (neato for example)

D3 show text for nodes when zoomed

The number of nodes in my d3 graph is too large. So I built a zoom mechanism in that graph. Now the problem is, I just cannot display text for each nodes since they will overlap each other. However when I zoom in to the nodes, the space is enough to display texts.
So how do I show texts when the space is enough to show all of them without overlapping?
I have had this same problem in the past. Unfortunately optimal label placement is not an easy problem. To mitigate overlap effects one option is to use a restricted force layout for label placement. You can also try using callouts to allow the labels to move farther away from the nodes.
In the past I have implemented a sort of greedy collision detection based algorithm that goes something like:
sort the labels in decreasing priority
for each label in the list // so most important first
if the label does not overlap any placed labels
place the label and add it to my collision data structure (e.g. quad tree)
else
hide the label
Obviously this will have some non-optimal cases and it can be slow if you have a lot of animations going on. But when you have the option to zoom in to see more label and if your absolute number of labels is not too high then it works quite well. There are also a number of obvious ways to speed it up like restricting testing to only labels within the view (but then you need to update on pan).
You may find some helpful suggestions here including an implementation of collision detection.

GraphViz horizontal order in presence of clusters

Ok, I know the question of ordering nodes in GraphViz has been hashed to near-death, but I haven't seen anyone address the question of ordering clusters. I tried all the tricks in the book:
The order of the nodes in the file is left-to-right. If I don't add clusters, dot simply does the right thing and shows them in the right order without any prompting. However, when clusters are added, dot shuffles (randomizes?) the order.
I added invisible edges to try and force the order between the sub-clusters; but it seems that as soon as sub-clusters are placed inside a cluster, dot decides on some specific order, and will not hesitate to have edges go all around the map to preserve it.
Since order-of-nodes and invisible edges failed me, I turned to trying to force the position of the nodes. This fails again, because only dot does clusters, and it ignores input positions. Running even the unmodified output of dot through fdp (with the generated positions) caused a crash, so I gave up on that direction as well.
Here is an example dot file which produces the image below. The green lines are the "invisible" edges I added unsuccessfully trying to force dot to order everything from left to right. If this was successful, the green lines would have gone from the anchor to the left through the nodes in order to the right, without crossing themselves. For example, each be.0 would be to the left of its sibling be.1, and similarly for tg-s). As you can see, dot shuffled the order of the sub-clusters (putting the .1 sibling to the left of the .0 one). It is as if it is doing it on purpose, due to some constraint, regardless of any edges. I can't find any way whatsoever to convince it to do otherwise. This is extremely frustrating because the generated diagrams are otherwise perfect for my needs.
(source: ben-kiki.org)
So. Is there any way at all to force dot to respect some order of clusters-within-a-cluster?
Edit: Looking further into this, it seems as though the default ordering for clusters is reversed from that of nodes. That is, normally (in a TB graph), a node that appears first in the text will tend to appear to the left of a node that appears later in the text; but it seems that a sub-cluster that appears first in the text will tend to appear to the right of a sub-cluster that appears later in the text. Now, if this was a hard-and-fast rule, life would be great; however, dot still sometimes (but less often) insist on rearranging the sub-clusters, regardless of any resulting crossed edges, seemingly "just because". So the question remains.
A bit late but here are some ideas for other that comes here:
(the example data is a bit to large to play around with)
anyway have you tried:
edge[constraint=false] on some of the black/red lines (constraint=false may course
some strange long edge routing though)
and/or edge[weight=1000] on the green lines (more difficult to get right),
you might also want to throw in a bit more scaffolding
you cant rank=same across clusters so if needed it must be on scaffolding nodes
here is a light example that successfully turns the order of half the nodes including clusters
digraph G {
subgraph cluster_sa{
edge[weight=1000]
subgraph cluster_s1a{ s1a1->s1a2 }
subgraph cluster_s2a{ s2a1->s2a2 }
subgraph cluster_s1a_{ s1a1_->s1a2_ }
subgraph cluster_s2a_{ s2a1_->s2a2_ }
s1a2->s1a1_
s2a2->s2a1_
}
{
edge[constraint=false]
subgraph cluster_ta{
subgraph cluster_t1a{ t1a1->t1a2 }
subgraph cluster_t2a{ t2a1->t2a2 }
subgraph cluster_t1a_{ t1a1_->t1a2_ }
subgraph cluster_t2a_{ t2a1_->t2a2_ }
t1a2->t1a1_
t2a2->t2a1_
}
edge[tailport=s headport=s]
s1a2_->t1a1
s2a2_->t2a1
}
edge[color=green]
node[color=green]
{rank=same s1a0->s2a0->t2a0->t1a0}
s1a0->s1a1
s2a0->s2a1
t1a0->t1a2_
t2a0->t2a2_
t1a2_->t1a1_->t1a2->t1a1
t2a2_->t2a1_->t2a2->t2a1
}
rendered on viz-js.com
some times it might also be usefull to connect node in reverese order (e.g. t2a2_->t2a1_[dir=back] instead of the above would actually be better as the green arrow could then be remove giving a straight black arrow) but I dont think that would help in your case

Resources