Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groups of nodes with the same attributes in GraphViz file

In the dot language of GraphViz I want to describe a 2-mode network. So I have nodes of two different types. One group for example could contains people how read and the other group could contain the books being read by the people.

I want to give the nodes in these two groups different looks (shape, color, etc). How can I specify the attributes for a group of nodes in one statement. The aim is to be able to change the look for each group of nodes in one place, not in all individual node descriptions.

This could be done with something like attribute inheritance, but I don't know whether the dot language has this concept.

like image 946
halloleo Avatar asked Mar 04 '15 11:03

halloleo


People also ask

What is the shape of a node?

Shape nodes Holds an object's geometry attributes or attributes other than the object's transform node attributes. A shape node is the child of a transform node. A transform node has only one shape node.

What is rank in Graphviz?

Ranks and Subgraphs To work out the layout, Graphviz uses a system it calls "ranks". Each node is assigned a higher rank than the highest ranked node that point to it. If your rank direction is set to left to right ( rankdir=LR ), then nodes with a higher rank are placed further to the right.

What is Graphviz Neato?

NEATO is a program that makes layouts of undirected graphs following the. filter model of DOT. Its layout heuristic creates virtual physical models and runs an iterative solver to find low energy configurations.

How do you make a graph on Graphviz?

Create a graph object, assemble the graph by adding nodes and edges, and retrieve its DOT source code string. Save the source code to a file and render it with the Graphviz installation of your system. Use the view option/method to directly inspect the resulting (PDF, PNG, SVG, etc.) file with its default application.


2 Answers

In principal there are three possibilities

  1. set default attributes before creating a node
    • globally - valid for all following node creations
    • locally in a subgraph - valid for node creations within subgraph only
  2. create nodes with explicit attributes
  3. assign attributes to a group of nodes after creation.

Options 1 and 2 allow only one group per node as creation is a single event. Option 3 allows different grouping for each assignment.


set default attributes globally before creating a node

digraph {
  x // node with current defaults

  // set default
  node [shape=box color=red]
  // create with default values
  a1, a2

  // set default
  node [shape=circle color=blue]
  // create with default values
  b1, b2

  y // node with current defaults

  x->{a1 a2}
  a1->{b1 b2}
  a2->{b1 b2}
  {b1,b2}->y
}

enter image description here


set default attributes locally before creating a node

digraph {
  x // node with current defaults

  {
      // set default
      node [shape=box color=red]
      // create with default values
      a1, a2
  }

  {
      // set default
      node [shape=circle color=blue]
      // create with default values
      b1, b2
  }

  y // node with current defaults

  x->{a1 a2}
  a1->{b1 b2}
  a2->{b1 b2}
  {b1,b2}->y
}

enter image description here


create nodes with explicit attributes

digraph {
  x // node with current defaults

  // create with explicit attributes
  a1, a2 [shape=box color=red]

  // create with explicit attributes
  b1, b2 [shape=circle color=blue]

  y // node with current defaults

  x->{a1 a2}
  a1->{b1 b2}
  a2->{b1 b2}
  {b1,b2}->y
}

enter image description here


assign attributes to a group of nodes after creation

digraph {
  x // node with current defaults

  // create with default values
  a1, a2, b1, b2

  // assign shape
  a1, a2 [shape=box]
  b1, b2 [shape=circle]

  // assign color
  a1, b2 [color=red]
  b1, a2 [color=blue]

  y // node with current defaults

  x->{a1 a2}
  a1->{b1 b2}
  a2->{b1 b2}
  {b1,b2}->y
}

enter image description here

like image 152
stefan Avatar answered Sep 24 '22 22:09

stefan


This can be done for all nodes in a graph with the node keyword, or for all edges in graph with the edge keyword. This can also be done on a node-by-node or edge-by-edge basis.

Example for a whole graph or subgraph:

digraph
{
  subgraph readers
  {
      node[shape=box; color=red;]
      r1; r2; r3;
  }

  subgraph books
  {
      node[shape=circle; color=blue;]
      b1; b2; b3;
  }
  r1->{b1 b2}
  r2->{b2 b3}
  r3->{b1 b2 b3}
}

This would give you the graph:

enter image description here

Example of per node attributes:

digraph
{
    n1[shape=triangle];
    n2[shape=star];
    n3[shape=square];

    n1->n2->n3
}

Would give the graph:

enter image description here

like image 27
bn. Avatar answered Sep 24 '22 22:09

bn.