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

DiagrammeR流程图垂直节点对齐与箭头优化技术咨询

Fixing Layout and Arrow Issues in DiagrammeR Flowchart

Got it, let's tackle those flowchart problems one by one. I've revised your code to address all the layout alignment, straight arrow, and optional polyline arrow requests:

library(DiagrammeR)
grViz("digraph flowchart {
  # Set global node defaults to avoid repetition
  node [fontname = Helvetica]
  
  # node type 1: starting and ending node
  node [shape = oval]
  start [label = '@@1']
  pro2 [label = '@@6']

  # node type 2: decision
  node [shape = diamond]
  dec1 [label = '@@2']
  dec2 [label = '@@3']
  dec3 [label = '@@4']

  # node type 3: process node
  node [shape = rectangle]
  pro1 [label = '@@5']
  pro3 [label = '@@7']

  # Rank adjustments for vertical alignment
  {rank = same; start dec1 dec2 pro1}  # Align START, decision1, decision2, process1 vertically
  {rank = same; pro3 dec3 pro2}        # Align process3, decision3, END vertically

  # Edge definitions with fixes
  edge[tailclip = true, headclip = true];
  
  # Basic flow
  start -> dec1 [dir=forward]
  dec1 -> dec2 [label = '', headport = 'n', tailport = 's']  # Straight vertical arrow
  dec1 -> pro3 [label = '', headport = 'n', tailport = 'e']
  
  # Fix decision2 to process1: straight vertical arrow
  dec2 -> pro1 [label = '', tailport = 's', headport = 'n']
  
  dec2 -> pro3 [label = '']
  
  # Fix process3 to decision3: straight vertical arrow
  pro3 -> dec3 [label = '', tailport = 's', headport = 'n']
  
  dec3 -> pro2 [label = '', tailport = 's', headport = 'n']  # Straight vertical arrow
  dec3 -> pro3 [label = '', headport = 'e', tailport = 'e']
  
  # Optional: Polyline arrow from process1 to decision1 (horizontal then vertical then horizontal)
  # Add a hidden dummy node to create the polyline path
  node [shape=point, style=invisible]
  dummy1 [pos=""]
  {rank = same; pro1 dummy1}  # Align dummy node with process1 horizontally
  pro1 -> dummy1 [dir=none]
  dummy1 -> dec1 [headport='w']
}
[1]: 'START'
[2]: 'decision 1'
[3]: 'decision 2'
[4]: 'decision 3'
[5]: 'process 1'
[6]: 'END'
[7]: 'process 3'
")

Key Changes Explained:

  1. Vertical Alignment & Straight Arrow for Process 1

    • Added {rank = same; start dec1 dec2 pro1} to ensure all these nodes sit on the same horizontal level, fixing the left offset of process1.
    • Updated the dec2 -> pro1 edge to use tailport = 's' and headport = 'n', forcing a straight vertical line instead of a curved one.
  2. Vertical Alignment & Straight Arrow for Decision 3 & END

    • Added {rank = same; pro3 dec3 pro2} to align process3, decision3, and END vertically.
    • Changed pro3 -> dec3 to use tailport = 's' and headport = 'n' for a straight vertical arrow.
  3. Polyline Arrow (Optional)

    • Added an invisible dummy node (dummy1) aligned horizontally with process1.
    • Split the pro1 -> dec1 arrow into two segments: first a horizontal line to the dummy node, then a vertical/horizontal line to dec1's west port, creating the desired polyline path.

This should give you a clean, aligned flowchart with all the requested arrow behaviors!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:21:22