In Graphviz, how do I align an edge to the top center of a node?
Asked Answered
D

3

7

In Graphviz / dot, is it possible to get the edge to connect exactly in the top center of a node? Reading the dot guide, I thought tailport and headport would help me, but adding those make no difference and sometimes get me weirder results.

This is what I'm getting:

sample graph

And this is what I'm looking for:

wanted results

The code I used to get the (incorrect) graph is:

digraph G {

  graph [splines = ortho];
  node [shape = box];
  edge [dir = none];

  {
    rank = same

    A
    AB [shape = point]
    B

    A -> AB
    AB -> B
  }

  {
    rank = same
    point1 [shape = point]
    point2 [shape = point]
    point3 [shape = point]
  }

  AB -> point1

  // The following section if to make the nodes appear in 
  // the correct order, not sure if there's a better way
  {
    edge [style = invisible]
    rank = same
    C
    D
    E
    F
    C -> D
    D -> E
  }

  point2 -> point1
  point2 -> C
  point1 -> point3
  point3 -> E
  point1 -> D

}
Disloyalty answered 16/12, 2014 at 12:28 Comment(3)
Copy-pasting your code into graphvizFiddle yields the correct result. Are you using a recent version of graphviz ?Campanulaceous
@Campanulaceous thanks! turns out the most recent (2.38) version isn't working properly in Mac OS X Yosemite, I had to downgrade to 2.36 as noted in the download page: graphviz.org/Download_macos.php If you post this as an answer I'll accept it, thanks again!Disloyalty
I'm glad you figured it out! Since you did all the work, you should add & accept your own answer ;)Campanulaceous
D
4

Turns out the most recent (2.38) version isn't working properly in Mac OS X Yosemite, I had to downgrade to 2.36 as noted in the download page.

Disloyalty answered 18/12, 2014 at 16:52 Comment(0)
F
6

Notes

  1. splines=ortho doesn't support tailport or headport (see: "Graphviz Issue Tracker - 0002142: ortho plots do not respect ports. also arrowheads seem to go the wrong way.")
  2. You can use hidden nodes, but don't use them on the lateral nodes (like C or F on the example below)

Image

Result

Code

This code works even with more than 3 child nodes and it's compatible with Graphviz 2.38. Useful for Org Chart (even if it's not perfect if you have many levels - I'm still try to reduce asymmetries).

 graph {
    splines=ortho;
    {0, 1, 2, 3 [width=0, shape=point, style=invis];}
    {rank=same; 1 -- 2 -- 3;}
    0 -- 2;
    node [shape=box];
    {rank=same; A -- 0 -- B;}
    1 -- C;
    1 -- D;
    3 -- E;
    3 -- F;
}
Fagoting answered 30/4, 2016 at 9:36 Comment(0)
D
4

Turns out the most recent (2.38) version isn't working properly in Mac OS X Yosemite, I had to downgrade to 2.36 as noted in the download page.

Disloyalty answered 18/12, 2014 at 16:52 Comment(0)
S
0

I see the same effect under Windows7 - did not try other platforms.

I tried fiddling with the 'port positions', See http://www.graphviz.org/docs/attr-types/portPos/

e.g. changing the last lines of your code to

  point2 -> point1
  point2 -> C:nw
  point1 -> point3
  point3 -> E:ne
  point1 -> D

gives a (slightly) better positioning of the connecting lines.

Safranine answered 14/10, 2015 at 18:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.