You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PlantUML/Graphviz中实现框图从上到下垂直对齐

解决PlantUML框图垂直对齐问题

问题分析

你的代码存在两个核心问题导致布局不符合预期:

  1. 重复定义了store1至store4节点(全局和cluster_0中各定义一次),引发布局冲突;
  2. 未对各层级节点的垂直顺序做明确约束,加上cluster_0内的rank=same设置,导致节点水平排列而非垂直对齐。

解决方案

通过以下调整实现从上到下的垂直对齐:

  • 删除全局块中重复的store1至store4定义,仅保留cluster_0内的节点定义;
  • 移除cluster_0中的rank=same,让内部节点垂直排列;
  • 使用rank约束语句强制节点按从上到下的顺序排列,配合边的连接关系固定层级。

修改后的完整代码

@startuml
digraph dfd{
  node[shape=record]
  
  scheduler[label="<f0> scheduler"];
  store5 [label="<f0> firstTotalTimeBlockAddress|<f1> D"];
  store6 [label="<f0> secondTotalTimeBlockAddress|<f1> D"];
  store7 [label="<f0> firstTotalTime|<f1> D"];
  store8 [label="<f0> secondTotalTime|<f1> D"];
  store9 [label="<f0> firstTotalTimeBlockAddress|<f1> D"];
  store10 [label="<f0> secondTotalTimeBlockAddress|<f1> D"];
  store11 [label="<f0> firstTotalTime|<f1> D"];
  store12 [label="<f0> secondTotalTime|<f1> D"];
  proc1 [label="{<f0> 1|<f1> totalTimeRead\n\n\n}" shape=Mrecord];
  proc2 [label="{<f0> 2|<f1> updateTotalTime\n\n\n}" shape=Mrecord];

  subgraph cluster_0 {
    store1 [label="<f0> firstTotalTimeBlockAddress|<f1> D"];
    store2 [label="<f0> secondTotalTimeBlockAddress|<f1> D"];
    store3 [label="<f0> firstTotalTime|<f1> D"];
    store4 [label="<f0> secondTotalTime|<f1> D"];
    label = "process #1";
  }

  # 强制垂直层级顺序
  {rank=same; scheduler}
  {rank=same; store1}
  {rank=same; store2}
  {rank=same; store3}
  {rank=same; store4}
  {rank=same; proc1}
  {rank=same; store5}
  {rank=same; store6}
  {rank=same; store7}
  {rank=same; store8}
  {rank=same; proc2}
  {rank=same; store9}
  {rank=same; store10}
  {rank=same; store11}
  {rank=same; store12}

  # 定义连接关系,固定垂直流向
  scheduler -> store1
  store1 -> store2
  store2 -> store3
  store3 -> store4

  store1:f1 -> proc1:f0;
  store2:f1 -> proc1:f0;
  store3:f1 -> proc1:f0;
  store4:f1 -> proc1:f0;

  proc1:f1 -> store5:f0;  
  proc1:f1 -> store6:f0;  
  proc1:f1 -> store7:f0;  
  proc1:f1 -> store8:f0;  

  store5 -> store6
  store6 -> store7
  store7 -> store8

  store5:f1 -> proc2:f0;
  store6:f1 -> proc2:f0;
  store7:f1 -> proc2:f0;
  store8:f1 -> proc2:f0;

  proc2:f1 -> store9:f0;  
  proc2:f1 -> store10:f0;
  proc2:f1 -> store11:f0;  
  proc2:f1 -> store12:f0;

  store9 -> store10
  store10 -> store11
  store11 -> store12
}
@enduml

关键调整说明

  1. 移除重复节点:删除全局块中与cluster_0重复的节点定义,避免布局冲突;
  2. 层级约束:通过{rank=same; 节点}将每个节点单独设置为同一水平层,配合节点间的直接连接(如scheduler -> store1),强制从上到下的垂直排列;
  3. 保留功能连接:原有的数据流向连接(如store1:f1 -> proc1:f0)全部保留,确保逻辑关系不变。

内容的提问来源于stack exchange,提问作者kerem0101

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 20:35:38