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

hal*_*leo 7 grouping dot graphviz

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

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

这可以通过属性继承来完成,但我不知道dot语言是否具有这个概念.

ste*_*fan 13

原则上有三种可能性

  1. 在创建节点之前设置默认属性
    • 全局 - 对所有后续节点创建有效
    • 在子图中本地 - 仅对子图内的节点创建有效
  2. 创建具有显式属性的节点
  3. 在创建后将属性分配给一组节点.

选项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
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


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

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
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


创建具有显式属性的节点

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
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


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

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
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 哇,这个很全面。谢谢! (2认同)

bn.*_*bn. 5

这可以针对具有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}
}
Run Code Online (Sandbox Code Playgroud)

这会给你图表:

在此处输入图片说明

每个节点属性的示例:

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

    n1->n2->n3
}
Run Code Online (Sandbox Code Playgroud)

会给图表:

在此处输入图片说明

  • 谢谢,这也帮助了我。最初吸引我的是我添加了子图,指定了节点创建之后的属性。需要在子图中**创建节点以接收这些属性。 (2认同)