2

I have the following Gantt chart, but I want to make the following modifications to it.

  1. There is currently a link from WP1 to WP2 (first and second bar). I want to keep that but instead of having a solid link between the two, I want it to be dashed line link.
  2. I want to add two custom links between WP1-WP3 and WP2-WP3. More precisely, the links should start from middle of Q1 part of WP1 and WP3 bars and end at the beginning of WP3 bars. Moreover, the links should have double arrow heads (i.e., arrow on both sides of the link). This is shown pictorially below.

Any ideas how to achieve these?

\documentclass[a4paper,11pt]{article}
\usepackage{xcolor}
\usepackage{tikz}
\tikzset{>=latex}
\usepackage{pgfgantt}
\usepackage{relsize}

\definecolor{cbg}{HTML}{fdf6e3} 
\definecolor{c1}{HTML}{CB4B16}
\definecolor{c2}{HTML}{586E75}
\definecolor{c3}{HTML}{6C71C4}
\definecolor{c4}{HTML}{D33682}

\newlength{\mynodedist}
\setlength{\mynodedist}{2pt}

\newlength{\mytextwidth}
\setlength{\mytextwidth}{50pt}

\newlength{\mytextheight}
\setlength{\mytextheight}{50pt}

\newcommand{\filllegendentry}[1]{\begin{tikzpicture}[scale=0.5, transform shape] \filldraw[draw=#1, fill=#1, thin] (0,0) rectangle (0.8,0.4); \end{tikzpicture}}
\newcommand{\legendentry}[2]{\begin{tikzpicture}[scale=0.5, transform shape] \filldraw[pattern color=#1, draw=#1, fill=#1, pattern=#2, thin] (0,0) rectangle (0.8,0.4); \end{tikzpicture}}

\begin{document}

\begin{figure}[ht]
\centering
\begin{ganttchart}[
    x unit=1cm,
    y unit title=0.5cm,
    y unit chart=0.6cm,
    hgrid=true,
    vgrid={{dotted},{solid},*7{dotted},{solid},{dotted},{solid}},
    title label anchor/.style={below=-1.6ex},
    title height=1,
    incomplete/.style={fill=white},
    progress label text={},
    bar height=0.75,
]{1}{12}
  \gantttitle{\textbf{Time Schedule}}{12} \\
  \gantttitle{\textbf{Year 1}}{4}
  \gantttitle{\textbf{Year 2}}{4}
  \gantttitle{\textbf{Year 3}}{4}\\
  \gantttitle{Q1}{1}
  \gantttitle{Q2}{1}
  \gantttitle{Q3}{1}
  \gantttitle{Q4}{1}
  \gantttitle{Q1}{1}
  \gantttitle{Q2}{1}
  \gantttitle{Q3}{1}
  \gantttitle{Q4}{1}
  \gantttitle{Q1}{1}
  \gantttitle{Q2}{1}
  \gantttitle{Q3}{1}
  \gantttitle{Q4}{1}\\
  \ganttbar[bar height=0.6,bar/.append style={fill=c1, draw=c1},name=WP1]{\textbf{WP1}}{1}{6}\\
  \ganttbar[bar height=0.6,bar/.append style={fill=c2, draw=c2},name=WP2]{\textbf{WP2}}{3}{8}\\
  \ganttbar[bar height=0.6,bar/.append style={fill=c3, draw=c3}]{\textbf{WP3}}{2}{3}
  \ganttbar[bar height=0.6,bar/.append style={fill=c3, draw=c3}]{\textbf{}}{6}{7}\\
  \ganttbar[bar height=0.6,bar/.append style={fill=c4, draw=c4}]{\textbf{WP4}}{9}{12}\\
  \ganttlink{WP1}{WP2}
\end{ganttchart}
\caption[Gantt Chart of the Project]{Gantt chart of the project.}
\label{fig:gantt}
\end{figure}

\end{document}

enter image description here

2 Answers 2

4

Changing the line type can be done by altering the /pgfgantt/link style, for easier access I define the /pgfgantt/links = <styles> key so that you can just write links = dashed (or whatever dash pattern you like).

I do a similar thing with barsbar. I also set the drawing of the bars to none since you always use the same color for both.

PGFGantt comes with a link type dr however, it uses the south anchor by default.

I'll define a dr* link type that also uses the value of on bottom fraction to find the proper position on the southern border of the node. (It would be nice, if we could reference the date this should come out but that needs more work.)

We need to evaluate that value at least once because it is used literally in low-level TeX calculations (which would only allow decimal fractions) but I want to use fractions like 1/12 because it makes it really easy to specify the middle of a column. This is done by the .evaluated handler that also solves an expansion issue that would present otherwise.

Code

\documentclass[tikz]{standalone}
%\documentclass[a4paper,11pt]{article}
%\usepackage{xcolor}
%\usepackage{tikz}
\tikzset{>=latex}
\usepackage{pgfgantt}

\definecolor{cbg}{HTML}{fdf6e3} 
\definecolor{c1}{HTML}{CB4B16}
\definecolor{c2}{HTML}{586E75}
\definecolor{c3}{HTML}{6C71C4}
\definecolor{c4}{HTML}{D33682}

\ganttset{
  links/.style={link/.append style={#1}},
  bars/.style={bar/.append style={#1}},
  start fraction/.initial=.5}
\newganttlinktype{dr*}{%
  \ganttsetstartanchor{on bottom/.evaluated=% a bit annoying
    \pgfkeysvalueof{/pgfgantt/on bottom fraction}}%
  \ganttsetendanchor{west}%
  \draw [/pgfgantt/link] (\xLeft, \yUpper) -- (\xLeft, \yLower)
    node [pos=.5, /pgfgantt/link label node] {\ganttlinklabel} -- (\xRight, \yLower);}
\begin{document}
%\begin{figure}[ht]
%\centering
\begin{ganttchart}[
    x unit=1cm,
    y unit title=0.5cm,
    y unit chart=0.6cm,
    hgrid=true,
    vgrid={{dotted},{solid},*7{dotted},{solid},{dotted},{solid}},
    title label anchor/.style={below=-1.6ex},
    title height=1,
    incomplete/.style={fill=white},
    progress label text={},
    bar height=0.75,
    bars={draw=none}, % ← !
]{1}{12}
  \gantttitle{\textbf{Time Schedule}}{12} \\
  \gantttitle{\textbf{Year 1}}{4}
  \gantttitle{\textbf{Year 2}}{4}
  \gantttitle{\textbf{Year 3}}{4}\\
  \foreach \y in {1, 2, 3}
    \foreach \Q in {1, 2, 3, 4} {\gantttitle{Q\Q}{1}} \\
  \ganttbar[bar height=0.6, bars=c1, name=WP1]{\textbf{WP1}}{1}{6}\\
  \ganttbar[bar height=0.6, bars=c2, name=WP2]{\textbf{WP2}}{3}{8}\\
  \ganttbar[bar height=0.6, bars=c3]{\textbf{WP3}}{2}{3}
  \ganttbar[bar height=0.6, bars=c3]{\textbf{}}{6}{7}\\
  \ganttbar[bar height=0.6, bars=c4]{\textbf{WP4}}{9}{12}\\
  \ganttlink[links=dashed]{WP1}{WP2}
  \ganttlink[links=<->, link type=dr*, on bottom fraction=1/12]{WP1}{elem2}
  \ganttlink[links=<->, link type=dr*, on bottom fraction=5/12]{WP2}{elem3}
\end{ganttchart}
%\caption[Gantt Chart of the Project]{Gantt chart of the project.}
%\label{fig:gantt}
%\end{figure}
\end{document}

Output

enter image description here

2
  • Thanks, this is quite a clean solution!
    – pixel
    Jul 31, 2023 at 10:48
  • 1
    @pixel I've updated the answer so that it uses PGFGantt tools without reinventing the wheel. Jul 31, 2023 at 11:00
3

For a dashed link, add dashed to the link style.

\ganttlink[link/.append style=dashed]{...}{...}

For the other type of links, you could add a new link type, related. At the moment, I didn't find a way to make the end position parametric or automatic, so we need to separate definitions depending on where the link should end (on bottom=0.1 vs. on bottom=0.4). Maybe you want to ask a separate question to improve that.

\newganttlinktype{related1}{
  \ganttsetstartanchor{on left=0.5}
  \ganttsetendanchor{on bottom=0.1}
  \draw[/pgfgantt/link,<->] (\xLeft,\yUpper) -| (\xRight,\yLower);
}
\newganttlinktype{related4}{
  \ganttsetstartanchor{on left=0.5}
  \ganttsetendanchor{on bottom=0.4}
  \draw[/pgfgantt/link,<->] (\xLeft,\yUpper) -| (\xRight,\yLower);
}

Then, you can add the links as

\ganttlink[link type=related1]{WP3a}{WP1}
\ganttlink[link type=related4]{WP3b}{WP2}

(after adding the names WP3a and WP3b to the corresponding gantt bars). enter image description here

\documentclass[a4paper,11pt]{article}
\usepackage{xcolor}
\usepackage{tikz}
\tikzset{>=latex}
\usepackage{pgfgantt}
\newganttlinktype{related1}{
  \ganttsetstartanchor{on left=0.5}
  \ganttsetendanchor{on bottom=0.1}
  \draw[/pgfgantt/link,<->] (\xLeft,\yUpper) -| (\xRight,\yLower);
}
\newganttlinktype{related4}{
  \ganttsetstartanchor{on left=0.5}
  \ganttsetendanchor{on bottom=0.4}
  \draw[/pgfgantt/link,<->] (\xLeft,\yUpper) -| (\xRight,\yLower);
}

\usepackage{relsize}

\definecolor{cbg}{HTML}{fdf6e3} 
\definecolor{c1}{HTML}{CB4B16}
\definecolor{c2}{HTML}{586E75}
\definecolor{c3}{HTML}{6C71C4}
\definecolor{c4}{HTML}{D33682}

\newlength{\mynodedist}
\setlength{\mynodedist}{2pt}

\newlength{\mytextwidth}
\setlength{\mytextwidth}{50pt}

\newlength{\mytextheight}
\setlength{\mytextheight}{50pt}

\newcommand{\filllegendentry}[1]{\begin{tikzpicture}[scale=0.5, transform shape] \filldraw[draw=#1, fill=#1, thin] (0,0) rectangle (0.8,0.4); \end{tikzpicture}}
\newcommand{\legendentry}[2]{\begin{tikzpicture}[scale=0.5, transform shape] \filldraw[pattern color=#1, draw=#1, fill=#1, pattern=#2, thin] (0,0) rectangle (0.8,0.4); \end{tikzpicture}}

\begin{document}

\begin{figure}[ht]
\centering
\begin{ganttchart}[
    x unit=1cm,
    y unit title=0.5cm,
    y unit chart=0.6cm,
    hgrid=true,
    vgrid={{dotted},{solid},*7{dotted},{solid},{dotted},{solid}},
    title label anchor/.style={below=-1.6ex},
    title height=1,
    incomplete/.style={fill=white},
    progress label text={},
    bar height=0.75,
]{1}{12}
  \gantttitle{\textbf{Time Schedule}}{12} \\
  \gantttitle{\textbf{Year 1}}{4}
  \gantttitle{\textbf{Year 2}}{4}
  \gantttitle{\textbf{Year 3}}{4}\\
  \gantttitle{Q1}{1}
  \gantttitle{Q2}{1}
  \gantttitle{Q3}{1}
  \gantttitle{Q4}{1}
  \gantttitle{Q1}{1}
  \gantttitle{Q2}{1}
  \gantttitle{Q3}{1}
  \gantttitle{Q4}{1}
  \gantttitle{Q1}{1}
  \gantttitle{Q2}{1}
  \gantttitle{Q3}{1}
  \gantttitle{Q4}{1}\\
  \ganttbar[bar height=0.6,bar/.append style={fill=c1, draw=c1},name=WP1]{\textbf{WP1}}{1}{6}\\
  \ganttbar[bar height=0.6,bar/.append style={fill=c2, draw=c2},name=WP2]{\textbf{WP2}}{3}{8}\\
  \ganttbar[bar height=0.6,bar/.append style={fill=c3, draw=c3},name=WP3a]{\textbf{WP3}}{2}{3}
  \ganttbar[bar height=0.6,bar/.append style={fill=c3, draw=c3},name=WP3b]{\textbf{}}{6}{7}\\
  \ganttbar[bar height=0.6,bar/.append style={fill=c4, draw=c4}]{\textbf{WP4}}{9}{12}\\
  \ganttlink[link/.append style=dashed]{WP1}{WP2}
  \ganttlink[link type=related1]{WP3a}{WP1}
  \ganttlink[link type=related4]{WP3b}{WP2}
\end{ganttchart}
\caption[Gantt Chart of the Project]{Gantt chart of the project.}
\label{fig:gantt}
\end{figure}

\end{document}
3
  • Thanks a lot, this works nicely!
    – pixel
    Jul 31, 2023 at 10:35
  • Ah, that's how that is supposed to work. I think we can combine our answers so that they don't reinvent the wheel and have it parametric. Jul 31, 2023 at 10:48
  • @Qrrbrbirlbel Makes sense, both of your answers are very nice!
    – pixel
    Jul 31, 2023 at 10:50

You must log in to answer this question.

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