绘制简单序列图

绘制简单序列图

我需要绘制这种简单的序列图,但我不太擅长 TiZ:

样本

\begin{figure}[!ht]
\begin{center}
\begin{tikzpicture}[node distance=2cm,auto,>=stealth']
    \node[] (server) {server};
    \node[left = of server] (client) {client};
    \node[below of=server, node distance=5cm] (server_ground) {};
    \node[below of=client, node distance=5cm] (client_ground) {};
    %
    \draw (client) -- (client_ground);
    \draw (server) -- (server_ground);
    \draw[->] ([yshift=-1cm]client.south) -- ([yshift=-1cm]server.south);
    \draw[->] ([yshift=-1.5cm]server.south) -- ([yshift=-1.5cm]client.south);
\end{tikzpicture}

我还希望线条是直的并且在线条上有文字。

答案1

下次请提供最小工作示例(MWE)。这应该是起点。

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
  \begin{tikzpicture}
    \coordinate (a) at (0,0);
    \coordinate (b) at (0,1);
    \coordinate (c) at (1,0);
    \coordinate (d) at (1,1);
    \draw (a) -- (b)node[pos=1.1,scale=0.25]{Client} (c) -- (d)node[pos=1.1,scale=0.25]{Server};
    \draw[-stealth] ($(a)!0.75!(b)$) -- node[above,scale=0.25,midway]{Text}($(c)!0.75!(d)$);
    \draw[stealth-] ($(a)!0.65!(b)$) -- node[below,scale=0.25,midway]{Hey} ($(c)!0.65!(d)$);
  \end{tikzpicture}
\end{document}

在此处输入图片描述

代码片段可以这样修改:

\documentclass[tikz]{standalone}
\usetikzlibrary{calc,positioning,arrows}
\begin{document}
  \begin{tikzpicture}[node distance=2cm,auto,>=stealth']
    \node[] (server) {server};
    \node[left = of server] (client) {client};
    \node[below of=server, node distance=5cm] (server_ground) {};
    \node[below of=client, node distance=5cm] (client_ground) {};
    %
    \draw (client) -- (client_ground);
    \draw (server) -- (server_ground);
    \draw[->] ($(client)!0.25!(client_ground)$) -- node[above,scale=1,midway]{Text} ($(server)!0.25!(server_ground)$);
    \draw[<-] ($(client)!0.35!(client_ground)$) -- node[below,scale=1,midway]{Hey} ($(server)!0.35!(server_ground)$);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

如果你希望它更符合 UML,你可能需要看一下包pgf-umlsd它也适用于 TikZ。

% arara: pdflatex

\documentclass{article}
\usepackage{pgf-umlsd}

\begin{document}
\begin{figure}
  \centering
  \begin{sequencediagram}
    \newthread{A}{Client}{}
    \newinst[1]{B}{Server}{}
    \begin{call}{A}{Call()}{B}{}
    \end{call}
  \end{sequencediagram}
\end{figure}
\end{document}

在此处输入图片描述

答案3

只是为了玩 TikZ。

\documentclass[tikz,border=12pt,12pt]{standalone}

\begin{document}
\begin{tikzpicture}
    \draw (-3,0) -- (-3,-5) (3,0) -- (3,-5);
    \node at (-3,.3) {Client};
    \node at (3,.3) {Server};
    \draw[->] (-3,-1) -- node[midway,above] {Hey} (3,-1);
    \draw[<-] (-3,-2) -- node[midway,above] {You} (3,-2);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案4

这是使用普通 TikZ 的另一个更漂亮的解决方案。

\documentclass[tikz,border=10pt,12pt]{standalone}
\usetikzlibrary{arrows}
\begin{document}

\begin{tikzpicture}[>=stealth']

% Locations
\def\ClientToServer{++(6,0)}
\def\ServerToClient{++(-6,0)}
\def\Lifeline{++(0,-3)}

% Lifelines
\path (0,0)           node[draw] (Client) {Client}
      \ClientToServer node[draw] (Server) {Server};
\draw (Client) -- \Lifeline (Server) -- \Lifeline;

% Blocks
\path (Server)
      ++(0,-1) node (BeginProcess) {} node[below right] {Process}
      ++(0,-1) node (EndProcess)   {};
\filldraw[fill=blue!30] (BeginProcess.west) rectangle (EndProcess.east);

% Calls
\draw[->] (BeginProcess)\ServerToClient -- node[above] {Hey} (BeginProcess);
\draw[->] (EndProcess) -- node[above] {You} \ServerToClient;

\end{tikzpicture}

\end{document}

序列图

相关内容