Distance between edges and nodes in graphviz - graphviz

How do I configure the distance between edges and nodes (red), i.e. the outer shape of a node(blue)?

Here is a technique that you can consider that avoids the need to create a custom node shape. There may be some matters of taste that you may need to address further in order to get exactly what you want.
The advantage of this technique, using HTML-like labels, is that varying the space inside and outside the rectangle becomes a simple matter of changing the inner number of points (here 4) and outer number of points (here 16) respectively.
digraph {
node [shape=none]
O [label=<
<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="16" CELLPADDING="4">
<TR><TD WIDTH="70">\N</TD></TR>
</TABLE>
>]
{a,b,c,d} -> O
}
Result:
You can change the BORDER and CELLBORDER parameters to show or hide the rectangles. You can adjust their thicknesses although only in multiples of 1 point. I have used the WIDTH parameter to force a width-to-height ratio that causes the aligning of all the arrow tips. Otherwise some of the tips would meet the sides of the invisible outer rectangle.

Like the other answer said, this isn't really an easy feat. Using a combination of height, width, fixedsize, labelloc, and margin node parameters, you can probably get any desired effect you'd like. Margin is best is you're looking to expand that distance, but to minimize it you need to use the other params. For example, this graph would have the arrowheads almost touching the 'O' node's text.
digraph {
node [shape="none" width=.2 height=.2 fixedsize="true" labelloc="top"];
a -> O;
b -> O;
c -> O;
d -> O;
}
Alternatively, if you really want to put in effort, you could create a custom node shape, and do whatever you'd like to it.

This is not possible AFAIK.
The only option available is using headclip to have the edge go to the center of the node or to the edge of the outer shape (as you did in your example).
You may try to add some padding to a node by putting it in a cluster and have the edges clip at the (invisible) border of the cluster:
digraph {
compound=true;
subgraph cluster1 {
style=invis;
2;
}
1 -> 2[lhead=cluster1];
}
Hopefully somebody knows a better hack for this...

Related

Graph in Graph Extraction

I want to draw a graph with "dot" format. The graph I want is a (sub)graph in a graph style. The subgraph (a child) has internal graph, and the (parent) graphs are connected among the parents, and does not connect to children that are connected only in the subgraph.
Could you please guide how writing such style with the dot-format, with an example?
Good news:
creating your "parent nodes" is pretty easy. Graphviz, (dot) calls them "cluster subgraphs" (or clusters). (well documented on p. 19 of https://www.graphviz.org/pdf/dotguide.pdf) see below for an example.
Bad news:
explicitly sizing clusters is very difficult. Dot sizes a cluster to be just big enough to contain its nodes.
explicitly positioning clusters is also quite difficult. Dot positions clusters based on the "best" positioning of the component nodes, specifically the edges that connect nodes within the various clusters.
drawing edges from one cluster to another is a kludge. You define an edge from a node contained within one cluster to another node contained within a different cluster and instruct dot to clip that edge so it appears to be from cluster to cluster. This uses the attribute compound=true. (again, read the documentation listed above)
Finally: the fdp engine allows cluster-to-cluster edges, but you lose the directionality of the edges
Drawn with dot:
digraph C {
graph [compound=true] // allows edges to/from clusters
// create an extra cluster to try to keep clusterA above clusterB
subgraph clusterWrapper {
graph [peripheries=0] // no box around
subgraph clusterA {
graph [peripheries=1] // box around
a -> c
b->c
c->d
}
subgraph clusterB {
graph [peripheries=1] // box around
f->g
e->g
}
} // end of wrapper
subgraph clusterC {
edge [dir=none ] // no arrowheads
h->k
i->k
i->l
j->l
{rank=same h->i->j [style=invis]}
}
b->f [ltail=clusterA, lhead=clusterB];
l:s->b:n [ltail=clusterC, lhead=clusterA];
l->f [style=invis weight=50]
}
Giving:

Increase curvature of connecting arcs

Consider:
digraph D {
0[pos="0,0!"]
1[pos="0,5!"]
0 -> 1[label=0.25]
1 -> 0[label=0.50]
}
Rendered under neato engine, this gives:
Is there a way to increase/control the curvature of the connecting arcs so that the rendering is something like the hand-drawn annotations below:
Edited to add: While I don't have to stick with neato, it is important in my application that the node pos coordinate values be upheld.
[there may be better ways, but edges are pretty hard to wrangle]
This solution requires neato -n (see https://graphviz.org/faq/#FaqDotWithNodeCoords) and (https://graphviz.org/docs/attrs/pos/)
Different versions of neato seem to produce different results when previous answer is input. This input file should work with older & newer versions of neato.
digraph D {
// next lines from the Graphviz FAQ
overlap=false
splines=true
0[pos="0,0!"]
1[pos="0,5!"]
// stick an invisible node in the middle, to force the edges out
half [pos="0,2.5!" width=1.1 height=1 style=invis]
0:nw -> 1:sw [label=0.25]
1:se -> 0:ne [label=0.50]
}
Giving:

In GraphViz, make node positions within a subgraph independent from nodes in other subgraphs?

Using dot, the basic layout puts nodes into layers. If you create subgraphs, it groups related nodes inside of rectangles, but the nodes are still in layers, and those layers are influenced by the nodes outside of the subgraph.
Sometimes, this is great. But sometimes, when a subgraph is an independent visual entity, it might be nice to be able to lay out its content without respect to the layers of the other parts of the graph. Take for example the following:
digraph x {
subgraph one {
a [ label="a\nvery\nlong\nlabel" ]
b [ label="another\nvery\nlong\nlabel" ]
c [ label="still\nmore\nlong\nlabels" ]
a -> b - > c
}
subgraph two {
w -> x -> y -> z
}
}
Because of the long labels, the nodes in subgraph one will take up a lot of space. But because of the layer-based layout, the nodes in subgraph two will be vertically aligned with the corresponding nodes from subgraph one.
Is there a way to make it layout subgraph two as if subgraph one did not exist?
Not directly with dot. Dot does rank alignment across the entire graph. However here are two ways to get close to what you want:
use neato -Goverlap=false -Gmode=hier to approximate a dot layout:
or split the various parts into separate graphs, use dot -Tdot to layout each, then use gvpack (https://graphviz.org/pdf/gvpack.1.pdf) to combine the parts into a single output. Like so:
dot -Tdot ComboGraph1a.gv >ComboGraph1a.dot
dot -Tdot ComboGraph1b.gv >ComboGraph1b.dot
gvpack -array_ib2 ComboGraph1?.dot |neato -Tpng -n2 >oooo.png
Giving:

How to force neato engine to reverse node order?

I'm trying to draw a graph where the shorter word "Myth" is in the top right, however the neato engine is recalcitrant and refuses to accept pos inputs (with or without !) and rank inputs. When appending ! to my pos inputs, it will just stretch the graph out really far, and keep "Hominids" in the top row, while putting "Myth" in the bottom.
digraph G {
layout="neato";
center="true";
fontpath="C:\Windows\Fonts";
node [fontsize="14"];
node [fontname="Abode Jenson Pro"];
graph [size="10,10!"];
rankdir=LR;
meme [shape=ellipse,label="Myth",rank="min",pos="3,0!"];
gene [shape=ellipse,label="Hominids",rank="max",pos="7,10!"];
meme -> gene [arrowsize="0.5",label="increases \n survival of",fontsize="8",constraint="true"];
gene -> meme [arrowsize="0.5",label=" generate \n venerate",fontsize="8",constraint="false"];
}
Graphviz counts coordinates as on the orthogonal graph with 0 being at the bottom left corner. So in pos attribute first coordinate is distance from left side to right →, and the second one — from bottom to top ↑.
Look at you current coordinate values:
So it's logical that your nodes appear as on the picture: you've forced their positions to be so! If you inverse the pos values you will get the result you need:
digraph G {
layout="neato";
center="true";
fontpath="C:\Windows\Fonts";
node [fontsize="14"];
node [fontname="Abode Jenson Pro"];
graph [size="10,10!"];
meme [shape=ellipse,label="Myth",rank="min",pos="7,10"];
gene [shape=ellipse,label="Hominids",rank="max",pos="3,0"];
meme -> gene [arrowsize="0.5",label="increases \n survival of",fontsize="8",constraint="true"];
gene -> meme [arrowsize="0.5",label=" generate \n venerate",fontsize="8",constraint="false"];
}
Also note that rankdir attribute you used, won't have any effect in neato. As stated in the documentation it only works with dot layout.

Add extra edges to a digraph in Graphviz

I have a .dot digraph which shows a graph as I want (depicting relationship between some tables). I want to add redundant edges to the graph (to represent redundant relationships in the database which exist only to write queries less effortly). These redundat edges, which will be written in a "dotted" style, must not change the deployment of nodes in the graph.
In other words, there's edges which must affect the node positions to print the graph pretty, and other edges which must to be added after the node positions are already computed (which will be styled differently --light gray, dotted, etc; to show that they're not main edges).
Is there options in Graphviz to specify "extra" edges?
Use constraint=false and color=gray on those additional edges.
digraph G {
a -> b -> c -> d;
d -> a [constraint=false,color=gray]
a -> z -> x -> d;
}
Play with that on http://graphviz.it/#/rhlWBZsz

Resources