17

I need to draw this kind of simple sequency diagram, but I'm not very good with TikZ:

sample

\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}

I also want the lines to be straight and have text over them.

3

6 Answers 6

13

Next time please provide a minimal working example (MWE). This should be the starting point.

\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}

enter image description here

And this is how your snippet can be modified:

\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}

enter image description here

0
31

If you want it to get a bit more UML conform, you might want to take a look on the package pgf-umlsd which works with TikZ as well.

% 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}

enter image description here

4

Just for fun with 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}

enter image description here

4
 \documentclass{article}
\usepackage{pgf-umlsd}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

\begin{document}
\begin{figure}
  \centering
    \scalebox{1}
    {  \begin{sequencediagram}
    \newthread{A}{\shortstack{Cliente\\ \\\begin{tikzpicture}
\node [copy shadow,fill=gray!20,draw=black,thick ,align=center] {Aplicaciones \\ Cliente};
\end{tikzpicture}}}{}
    \newinst[1]{B}{\shortstack{Servidor de \\ aplicaciones \\ \\ \begin{tikzpicture}
\node [fill=gray!20,draw=black,thick ,align=center] {Procesos};
\end{tikzpicture}}}{}
    \newinst[1]{C}{\shortstack{Servidor de \\  de Datos \\ \\\begin{tikzpicture}[shape aspect=.5]
\tikzset{every node/.style={cylinder, shape border rotate=90, draw,fill=gray!25}}
\node  at (1.5,0) {BD};
\end{tikzpicture}}}{}    
    \begin{call}{A}{}{B}{}
    \end{call}
    \begin{call}{B}{}{C}{}
    \end{call}
  \end{sequencediagram}
  }
\caption{Arquitectura en 3 capas} 
\end{figure}
\end{document}

enter image description here

1

Here's another prettier solution using plain 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}

Sequence diagram

1
  • Can you elaborate how will this solution extend to multiple entities, e.g. multiple client blocks?
    – Curious
    Oct 30, 2020 at 12:50
0

A PSTricks solution:

\documentclass{article}

\usepackage{pstricks}

\begin{document}

\begin{pspicture}(4.85,6.4)
  \psline(0.4,0)(0.4,6)
  \uput[90](0.40,6){client}
  \psline(4.4,0)(4.4,6)
  \uput[90](4.4,6){server}
  \psline{->}(0.4,5)(4.4,5)
  \uput[90](2.4,5){TextA}
  \psline{<-}(0.4,4)(4.4,4)
  \uput[90](2.4,4){TextB}
\end{pspicture}

\end{document}

output1

With text above and below the arrows:

\documentclass{article}

\usepackage{pstricks}

\begin{document}

\begin{pspicture}(4.85,6.4)
  \psline(0.4,0)(0.4,6)
  \uput[90](0.40,6){client}
  \psline(4.4,0)(4.4,6)
  \uput[90](4.4,6){server}
  \psline{->}(0.4,5)(4.4,5)
  \uput[90](2.4,5){TextA}
  \psline{<-}(0.4,4)(4.4,4)
  \uput[270](2.4,4){TextB}
\end{pspicture}

\end{document}

output2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .