GraphViz文件中具有相同属性的节点组

时间:2015-03-04 11:58:16

标签: grouping graphviz dot

GraphVizdot语言中,我想描述一个双模网络。所以我有两种不同类型的节点。例如,一个小组可以包含人们如何阅读,另一组可以包含人们正在阅读的书籍。

我想为这两个组中的节点赋予不同的外观(shapecolor等)。如何在一个语句中为一组节点指定属性。目的是能够在一个地方更改每组节点的外观,而不是在所有单个节点描述中。

这可以通过属性继承之类的东西来完成,但我不知道dot语言是否有这个概念。

2 个答案:

答案 0 :(得分:10)

原则上有三种可能性

  1. 在创建节点之前设置默认属性
    • 全局 - 对所有后续节点创建有效
    • 子图中的本地 - 仅对子图中的节点创建有效
  2. 创建具有显式属性的节点
  3. 创建后将属性分配给一组节点。
  4. 选项1和2每个节点只允许一个组,因为创建是单个事件。选项3允许为每个分配进行不同的分组。


    在创建节点之前全局设置默认属性

    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


    在创建节点之前在本地设置默认属性

    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


    创建具有显式属性的节点

    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


    在创建后将属性分配给一组节点

    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

答案 1 :(得分:2)

可以使用node关键字对图表中的所有节点执行此操作,也可以使用edge关键字对图表中的所有边缘执行此操作。这也可以在逐个节点或逐个边缘的基础上完成。

整个图表或子图的示例:

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

每个节点属性的示例:

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

    n1->n2->n3
}

会给出图表:

enter image description here