如何将图例放置在 Graphviz 流程图中图表的左下角?

问题描述 投票:0回答:1

我正在使用 Graphviz 制作流程图,需要将图例放置在图表的左下角。下面是我当前使用的代码,我在其中添加了图例的子图。默认情况下,图例位于顶部,但我想将其移至左下角。

这是我的代码的简化版本:

enter image description here

digraph flowchart {
  
  # Graph
  graph [
    overlap = true
    fontsize = 10
  ]
  
  # Define styles for Datasets and Analyses
  node [fontname = 'Calibri Light', fontcolor = white, style = filled]
  
  # Datasets (boxes)
  node [shape = box, fillcolor = firebrick3]  # Set dataset style
  A
  B
  
  # Analyses (circles)
  node [shape = circle, fixedsize = true, width = .9, fillcolor = steelblue3]
  C
  D
  
  # Edges
  {A B}->C
  A->D
  
  # Subgraph for legend
  subgraph cluster_legend {
    graph [
      label = 'Legend'
      labelloc = 't'
      labeljust= 'l'
      fontname = 'Calibri Light'
      color = lightgrey
    ]
    
    # Global node style
    node [fontsize = 9]
    
    # Legend entry for Analysis
    node [shape = circle, fixedsize = true, fillcolor = steelblue3, width = .5, height = .3]
    legend_analysis [label = 'Analysis']
    
    # Legend entry for Data
    node [shape = box, fillcolor = firebrick3, width = .5, height = .5]
    legend_data [label = 'Data']
    
    # Make legend items horizontal
    {rank = same; legend_data; legend_analysis}
  }
}

有人可以建议我如何将图例移动到图表的左下角吗?

graphviz
1个回答
0
投票

这是一种方法。

有点复杂,但这样你就不需要不可见的节点/边来尝试将图例强制到所需的位置。

/*******************************************************************
  create a second graph containing the legend
  use gvpack to combine the two,
  then neato -n2 to create image
********************************************************************/
digraph flowchart {
  subgraph cluster_main {  //  sometimes helps w/ gvpack
  # Graph
  graph [
    //  overlap = true  //  dot ignores overlap
    fontsize = 10
  ]
  
  # Define styles for Datasets and Analyses
  node [fontname = "Calibri Light", fontcolor = white, style = filled]
  
  # Datasets (boxes)
  node [shape = box, fillcolor = firebrick3]  # Set dataset style
  A
  B
  
  # Analyses (circles)
  node [shape = circle, fixedsize = true, width = .9, fillcolor = steelblue3]
  C
  D
  
  # Edges
  {A B}->C
  A->D
  }
}
/**  now the legend  **/
digraph L {
 # Subgraph for legend
  subgraph cluster_legend {
    graph [
      label = "Legend"
      labelloc = "t"
      labeljust= "l"
      fontname = "Calibri Light"
      color = lightgrey
    ]
    
    # Global node style
    node [fontsize = 9]
    
    # Legend entry for Analysis
    node [shape = circle, fixedsize = true, fillcolor = steelblue3, width = .5, height = .3]
    legend_analysis [label = "Analysis"]
    
    # Legend entry for Data
    node [shape = box, fillcolor = firebrick3, width = .5, height = .5]
    legend_data [label = "Data"]
    
    # Make legend items horizontal
    {rank = same; legend_data; legend_analysis}
  }
}

和命令行(注意点输出格式为xdot):

dot myFile.gv | gvpack -array_il1 |neato -n2 -T...

给予:
enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.