8

Hi
Can I get graphviz to color the edges in a way that identifies the direction? For example, the part of the edge near its source node might be blue and then it gradually shades away to red as it nears the target node. Or are there any other graphing tools (like graphviz) that can do this?

Any help in this regard would be much appreciated.

Puneet
  • 193
  • 1
  • 2
  • 7

2 Answers2

11

Well, i don't know anything about your current dot file, so i'll have to make a couple of assumptions. To deal with the easy case first, distinguishing the the direction of an edge is the job of the arrowhead (points to) and arrowtail (points from). Your graph is only going to render those if your graph type is a directed graph, which you set at the top of your dot file, e.g.,

digraph G {
    node[style=filled, color=cornflowerblue, fontcolor=white, fontsize=10, 
          fontname='Helvetica']
    edge[arrowhead=vee, arrowtail=inv, arrowsize=.7, color=maroon, fontsize=10,
          fontcolor=navy]

    a1 -> a2;
    a2 -> a4 [taillabel="TL     "];
    a2 -> a5 [headlabel="       HL"];
    a4 -> a6 [label="  ordinary edge label"]
}

If you've already crated a directed graph but for some reason you want an additional indicator to show edge direction, then the only relevant edge attributes i can think of are the headlabel and taillabel attributes, which allows you to specify which end of an edge a label is placed. The small dot file above will render this graph:

alt text

Miikka
  • 4,573
  • 34
  • 47
doug
  • 69,080
  • 24
  • 165
  • 199
  • 1
    Thanks for your reply :) I knew this feature of graphviz, but the thing is that my graph is a 128-node circular graph, with a lot of connections between them. So, the arrow heads aren't very informative about the direction of a connection, as the head of an incoming arrow and tail of an outgoing arrow at a node get clustered and don't show clearly. That is why I am exploring other options to differentiate between source and target for connections – Puneet Dec 10 '10 at 04:43
  • i mention two 'features' in my answer, not one; particularly given the peculiar nature of your graph, the second of these is likely to be useful in your case. – doug Dec 10 '10 at 06:03
  • How do we change the individual edge color? – MuneshSingh Oct 19 '18 at 13:12
5

Since 2012 and 2.30.0, Graphviz supports multiple colors for edges: color="blue;0.5:red" should make the half closer to the source blue and the half closer to the target red. More precisely:

if colorList has no fractions, the edge is drawn using parallel splines or lines, one for each color in the list, in the order given.

The head arrow, if any, is drawn using the first color in the list, and the tail arrow, if any, the second color. This supports the common case of drawing opposing edges, but using parallel splines instead of separately routed multiedges.

If any fraction is used, the colors are drawn in series, with each color being given roughly its specified fraction of the edge.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • It doesn't work as OP intended. Instead of a blue head and red tail, it creates an edge with two separate lines, one blue and one red. – SOFe Oct 29 '20 at 07:40
  • Thanks, it works great after the edit. Although it's still not a gradient, at least it changes the color. – SOFe Oct 31 '20 at 17:22
  • Yes, it doesn't seem like gradient is supported. I wonder how much effort it would be to add... – Alexey Romanov Oct 31 '20 at 18:54