我正在使用 tikzpicture 绘制序列图,结果决策节点看起来太高了。
有什么方法可以让这个节点短一点吗?我注意到没有最大高度属性,因此有这个问题。
这是我的决策节点的设置:
\tikzstyle{decision} = [diamond, minimum width=3cm, minimum height=1cm, text centered, text width=3cm, draw=black, fill=green!30]
以下是绘制图表的代码:
\begin{tikzpicture}[node distance=2cm]
\node (start) [startstop] {Start};
\node (init) [io, below of=start] {Initialize};
\node (schedule) [process, below of=init] {schedule(freq, [times])};
\node (rd) [process, below of=schedule] {record\_data(records, new\_record)};
\node (dec1) [decision, below of=rd, yshift=-0.5cm] {stressed yesterday?};
\node (pro2a) [process, below of=dec1, yshift=-0.5cm] {Process 2a};
\node (pro2b) [process, right of=dec1, xshift=3.5cm] {Process 2b};
\node (out1) [io, below of=pro2a] {Output};
\node (stop) [startstop, below of=out1] {Stop};
\draw [arrow] (start) -- (init);
\draw [arrow] (init) -- node[anchor=east] {freq=3} (schedule);
\draw [arrow] (schedule) -- (rd);
\draw [arrow] (rd) -- (dec1);
\draw [arrow] (dec1) -- node[anchor=east] {yes} (pro2a);
\draw [arrow] (dec1) -- node[anchor=south] {no} (pro2b);
\draw [arrow] (pro2b) |- (schedule);
\draw [arrow] (pro2a) -- (out1);
\draw [arrow] (out1) -- (stop);
\end{tikzpicture}
答案1
经过近两年(抱歉,我之前没有看到问题),对于 OP 来说可能已经晚了,但可能会对其他人有所帮助。
正如评论中已经指出的那样,无法tikz
定义任何节点形状的最大高度。但是,仍然可以采取一些措施来解决 OP 问题。
正如@Jakub G 回答中提到的,决策节点可以通过选项展平aspect=<amount>
。此时应该意识到,同时节点会变宽。因此,应该找到一些折衷方案。例如,手动将节点的文本拆分为两行并选择aspect=1.3
。
但从显示的图像可以得出结论,这不是主要的 OP 问题。decision
如果节点之间的距离得到正确管理,他可能会对节点大小感到满意。例如,通过使用库positioning
并使用其语法,below=of <node name>
可以定义节点边界之间的距离(因此决策节点不会与相邻节点重叠),而不是节点中心之间的距离,就像在弃用语法中所做的那样below of=<node name>
,因此当选定的距离太小时,它会与相邻节点重叠。
下面的 MWE 做出了这样的妥协。对于纵横比使用值 1.3`,对于节点边界之间的距离使用 6mm。添加了通用节点样式定义,可用于任何具有相同类型节点的流程图,并允许编写简洁而简短的流程图代码。
\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
chains,
positioning,
quotes,
shapes.geometric}
\makeatletter
\tikzset{FlowChart/.style={
suspend join/.code = {\def\tikz@after@path{}},
base/.style = {draw, fill=##1,
minimum height=9mm, text width=36mm,
align=center,
on chain, join=by arr
},
startstop/.style = {base=red!30},
process/.style = {base=orange!30, rounded corners},
decision/.style = {diamond, aspect=1.3, inner xsep=0pt,
draw, fill=green!30, align=center, % since not use "base"
on chain, join=by arr}, % since not use "base"
io/.style = {base=blue!30, trapezium, trapezium stretches body,
trapezium left angle=70, trapezium right angle=110},
arr/.style = {semithick,-Triangle},
every edge quotes/.style = {auto, font=\footnotesize}
} }
\makeatletter
\begin{document}
\begin{tikzpicture}[FlowChart,
node distance = 6mm and 12mm,
start chain = going below,
]
\node (start) [startstop] {Start};
\node (init) [io] {Initialize};
\node (schedule) [process]{schedule (freq, [times])};
\node (rd) [process] {record\_data\\ (records, new\_record)};
\node (dec1) [decision] {stressed\\ yesterday?};
\node (pro2a) [process] {Process 2a};
\node (out1) [io] {Output};
\node (stop) [startstop] {Stop};
\node (pro2b) [process, suspend join,
right=of dec1] {Process 2b};
\path (init) edge ["freq=3"] (schedule)
(dec1) edge ["yes"] (pro2a)
(dec1) edge [arr, "no"] (pro2b);
\draw[arr] (pro2b) |- (schedule);
\end{tikzpicture}
\end{document}