用tikz中通过一个点的线连接两个节点

用tikz中通过一个点的线连接两个节点

我正在尝试使用 tikz 制作一个非常简单的流程图。

现在看起来是这样的: 当前流程图

以下是我需要的样子: 我想要的流程图

我找到了一个例子使用 tikz 在线绘制流程图,这是我的样式定义:

\tikzstyle{decision} = [diamond, draw, fill=blue!20, text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block}    = [rectangle, draw, fill=black!25, text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line}     = [draw, -latex']
\tikzstyle{cloud}    = [draw, ellipse,fill=red!20, node distance=3cm, minimum height=2em]

这是我的流程图代码:

\begin{figure}[!h]
    \centering
    \begin{tikzpicture}[node distance = 2cm, auto]
        \node [block] (st1) {Stage 1};
        \node [block, right of=st1, node distance=3cm] (st2) {Stage 2};
        \node [block, right of=st2, node distance=3cm] (st3) {Stage 3};
        \path [line] (st1) -- (st2);
        \path [line] (st2) -- (st3);
    \end{tikzpicture}
\end{figure}

如果我使用基于坐标的形状构建图像,那么添加所需的线条和文本会很耗时,但很简单。但由于我要添加这些节点并连接节点,所以我不知道如何添加带有 2 个弯曲的线条。

答案1

您可以使用相对坐标。

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{arrows}
\tikzset{decision/.style = {diamond, draw, fill=blue!20, text width=4.5em, text badly centered, node
                            distance=3cm, inner sep=0pt},
         block/.style    = {rectangle, draw, fill=black!25, text width=5em, text centered, rounded
                            corners, minimum height=4em},
         line/.style     = {draw, -latex'},
         cloud/.style    = {draw, ellipse,fill=red!20, node distance=3cm, minimum height=2em}
}
\begin{document}
    \begin{tikzpicture}[node distance = 2cm, auto]
        \node [block] (st1) {Stage 1};
        \node [block, right of=st1, node distance=3cm] (st2) {Stage 2};
        \node [block, right of=st2, node distance=3cm] (st3) {Stage 3};
        \path [line] (st1) -- (st2);
        \path [line] (st2) -- (st3);
        %% from st1 draw 1 unit down, then 2 units left (put a node), draw vertically up and then right to reach st1.west
        \path[line,dashed] (st1.south) -- ++(0,-1) -- +(-2,0) node[pos=0.5,below]{Blah1} |- (st1.west) ;
        %% draw 1 unit down from st3.south then horizontally left and up to reach st2.south.
        \path[line,dashed] (st3.south) -- +(0,-1) -|  (st2.south) node[pos=0.25,below]{Blah2} ;    \end{tikzpicture}
\end{document}

在此处输入图片描述

pos=<dimen>根据需要更改。我还用-|来绘制线条,这意味着draw horizontally first and then vertically。此外,使用\tikzset而不是\tikzstyle(已弃用)。

相关内容