[Tex/LaTex] pgfgantt: draw arrows (links) behind bars

pgfgantttikz-pgf

I want to draw a Gantt diagram using pgfgantt. With the default settings, it draws the bars first and the arrows/links second. Thereby, the bars are crossed by the arrows which I find utterly ugly. Is it possible to change this behaviour, e.g., to define the z-order of drawing ?

enter image description here

Minimal working example:

\documentclass{article}
\usepackage{pgfgantt}

\begin{document}
\begin{tikzpicture}
\begin{ganttchart}[bar/.append style={orange}, link/.append style={thick}]{1}{6}
  \ganttbar{Task 1}{1}{2} \\
  \ganttbar{Task 2}{3}{4} \\
  \ganttbar{Task 3}{5}{6}
  \ganttlink[link type=f-s]{elem0}{elem1}
  \ganttlink{elem0}{elem2}
\end{ganttchart}
\end{tikzpicture}
\end{document}

The example works with pgfgantt.sty version 4.0.

Best Answer

Setting the canvas to fill=none, one can simply use the backgrounds library and the on background layer key to draw the arrow behind “Task 2”. I also set the outer xsep to zero so that the f-s line is perfectly straight.

To avoid ambiguity about that arrow corssing under “Task 2” I would draw the arrow around as in Example Y below. (One could also think of keys that only add to already established link bulge value.)

Code X

\documentclass[tikz,convert=false]{standalone}
\usepackage{pgfgantt}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{ganttchart}[
  /pgf/outer xsep=+0pt,
  bar/.append style={orange},
  canvas/.append style={fill=none},
  link/.append style={thick}]{1}{6}
  \ganttbar{Task 1}{1}{2} \\
  \ganttbar{Task 2}{3}{4} \\
  \ganttbar{Task 3}{5}{6}
  \ganttlink[link type=f-s]{elem0}{elem1}
  \begin{scope}[on background layer]
    \ganttlink{elem0}{elem2}
  \end{scope}
\end{ganttchart}
\end{document}

Output X

enter image description here

Code Y

\documentclass[tikz,convert]{standalone}
\usepackage{pgfgantt}
\newganttlinktype{rdldr*}{%
  \draw [/pgfgantt/link]
    (\xLeft, \yUpper) --
    (\xLeft + \ganttvalueof{link bulge 1} * \ganttvalueof{x unit},
      \yUpper) --
    ($(\xLeft + \ganttvalueof{link bulge 1} * \ganttvalueof{x unit},
      \yUpper)!%
      \ganttvalueof{link mid}!%
      (\xLeft + \ganttvalueof{link bulge 1} * \ganttvalueof{x unit},
      \yLower)$) --
    ($(\xRight - \ganttvalueof{link bulge 2} * \ganttvalueof{x unit},
      \yUpper)!%
      \ganttvalueof{link mid}!%
      (\xRight - \ganttvalueof{link bulge 2} * \ganttvalueof{x unit},
      \yLower)$) --
    (\xRight - \ganttvalueof{link bulge 2} * \ganttvalueof{x unit},
      \yLower) --
    (\xRight, \yLower);%
}
\ganttset{
  link bulge 1/.link=/pgfgantt/link bulge,
  link bulge 2/.link=/pgfgantt/link bulge}
\begin{document}
\begin{ganttchart}[
  bar/.append style={orange},
  link/.append style={thick},
  link bulge=.5]{1}{6}
  \ganttbar{Task 1}{1}{2} \\
  \ganttbar{Task 2}{3}{4} \\
  \ganttbar{Task 3}{5}{6}
  \ganttlink{elem0}{elem1}
  \ganttlink[link type=rdldr*, link bulge 1=2.5, link mid=.75]{elem0}{elem2}
\end{ganttchart}
\begin{ganttchart}[
  bar/.append style={orange},
  link/.append style={thick},
  link bulge=.5]{1}{6}
  \ganttbar{Task 1}{1}{2} \\
  \ganttbar{Task 2}{3}{4} \\
  \ganttbar{Task 3}{5}{6}
  \ganttlink{elem0}{elem1}
  \ganttlink[link type=rdldr*, link bulge 2=2.5, link mid=.25]{elem0}{elem2}
\end{ganttchart}
\end{document}

Output Y

enter image description hereenter image description here