Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force two nodes to occupy the same rank in Graphviz?

Tags:

graphviz

Using ruby-graphviz, I've created a graph that looks like this (border added to emphasize rendering boundaries):

Before

What I really want is for A and K to line up together at the top (or left, if rankdir="LR"). So I added an invisible node (call it X), and added invisible edges from X to A and K. And here's what I got:

After

X, XA, and XK have no labels, and style set to 'invis'. X has height, width, and margin set to 0, and fixedsize set to true. XA and XK have minlen, len, and penwidth set to 0.

But there's still that empty space at the top. Is there any way to get rid of it, short of cropping after the fact?

like image 854
mbklein Avatar asked Sep 23 '11 07:09

mbklein


1 Answers

You do not need invisible nodes to achieve this.

This is the dot syntax to force the same rank for two nodes:

{rank=same; A; K;}

This is called a subgraph.

I don't know ruby-graphviz, I'm not sure how to create a subgraph - but there is an example on github:

c2 = g.subgraph { |c|
  c[:rank => "same"]
  c.mysite[:label => "\nexample.com\n ", :shape => "component", :fontname => "Arial"]
  c.dotgraph[:label => "\ndotgraph.net\n ", :shape => "component", :fontname => "Arial"]
}
like image 113
marapet Avatar answered Oct 24 '22 06:10

marapet