Groups of nodes with the same attributes in the GraphViz file

In the language dot GraphVizI want to describe a dual-mode network. Therefore, I have nodes of two different types. One group, for example, may contain people how to read, and another group may contain books that people read.

I want to give the nodes in these two groups different views ( shape, coloretc.). How to specify attributes for a group of nodes in a single expression. The goal is to be able to change the appearance of each group of nodes in one place, and not in all individual node descriptions.

This can be done with something like attribute inheritance, but I don't know if the language has dotthis concept.

+4
source share
2 answers

There are basically three possibilities.

  • set default attributes before creating node
    • globally - valid for all subsequent node creations
    • locally in the subgraph - valid for node creation only inside the subgraph
  • create nodes with explicit attributes
  • Assign attributes to a group of nodes after creation.

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


set default attributes globally before creating 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 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 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

+4
source

node edge. node -by- node .

:

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}
}

:

enter image description here

node:

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

    n1->n2->n3
}

:

enter image description here

+1

All Articles