gypaetus
gypaetus

Reputation: 7359

Graphviz nodes rendering

I have a simple graphviz graph that is drawn as shown in the following image:

digraph G {
        "Model" -> "Task";
        "Task" -> "Worker 1" -> "Sink";
        "Task" -> "Worker 2" -> "Sink";
        "Task" -> "Worker 3" -> "Sink";
}       

enter image description here

Adding and edge between the sink and task nodes, the graph output is shown the following image:

    "Sink" -> "Model";

enter image description here

How could I add this edge and keep the original symmetric node layout shown in the first image?

Upvotes: 2

Views: 610

Answers (1)

Simon
Simon

Reputation: 10841

(Edited to improve my original answer)

Building on the answer to How to control subgraphs' layout in dot?, if we start by laying out the core digraph with dot, we can then use neato to add the additional edge. neato allows nodes that already have defined positions to be pinned using the -n parameter so one could do something like this (where symmetric.dot contains the original GraphViz digraph with the addition of graph [splines = true]):

#!/bin/bash
dot -Tdot symmetric.dot >symmetric1.dot
neato -n2 -Tpng symmetric1.dot -osymmetric1.png
sed 's/^}/"Sink" -> "Model";}/' <symmetric1.dot  >symmetric2.dot
neato -n2 -Tdot symmetric2.dot >symmetric3.dot
neato -n2 -Tpng symmetric3.dot -osymmetric3.png

This script:

  1. Uses dot to lay out the original digraph into a new .dot file.
  2. Uses neato -n2 to plot the original digraph. The -n2 option prevents neato from moving nodes that already have positions.
  3. Uses sed to insert the "Sink" -> "Model" edge.
  4. Uses neato -n2 to lay out the new edge (which is the only one without pos defined, and thus the only one not pinned) and plot it as a .png file.

symmetric1.png looks like this:

enter image description here

... and symmetric3.dot with the additional edge looks like this:

enter image description here

Upvotes: 1

Related Questions