# How do I add a legend to a pgfgantt chart using the \node command?

I am referring to the answer of Alain Matthes to a question asked some time ago.

What I have is a gantt chart which shows the steps in a project. I am pretty much finished with it and I really like the chart design but I have one big issue: I need to add a legend to this chart.

What I found in the question mentioned above is the \node command.

So I tried this for hours now but I can't seem to get the solution Alain Matthes suggested working. Please read his answer before continuing. I want to have about 5 boxes inside the chart in the upper right. But all I get is 5 boxes that are slightly above and right from the previous box. What I tried was something like this:

\documentclass[12pt]{article}
\usepackage{pgfgantt}
\begin{document}
\begin{center}
\begin{figure}[H]
\noindent\resizebox{480pt}{!}{
\begin{tikzpicture}
\begin{ganttchart}
[
vgrid,
vgrid style/.style=dotted,
hgrid,
hgrid style/.style=dotted,
y unit title=0.5cm,
y unit chart=0.8cm,
title/.append style={shape=rectangle, fill=black!20},
title height=1,
bar/.append style={fill=blue!80},
bar height=.5,
bar label font=\normalsize\color{black!80},
group top shift=.6,
group height=.3,
group peaks height=.2,
]
{1}{35}
\gantttitle{Some Title}{35}\\\\
\gantttitlelist{1,...,35}{1}\\\\
\gantttitle{Plan}{7}\gantttitle{Execute}{11}\gantttitle{Stabilize}{4}\gantttitle{Finish}{13}\\

\ganttgroup{Plan}{1}{6}\\
\ganttbar[name=kickoff1]{Kick-Off Meeting}{1}{1}\\
%This looks like the result I want but in the wrong place:
\node[fill=blue,draw] at ([yshift=-20pt]current bounding box.south){Person A};
\node[fill=yellow,draw] at ([yshift=-20pt]current bounding box.south){Person B};
\node[fill=green,draw] at ([yshift=-20pt]current bounding box.south){Person C};
\node[fill=red,draw] at ([yshift=-20pt]current bounding box.south){Person D};
\node[fill=purple,draw] at ([yshift=-20pt]current bounding box.south){Person E};
%This is where I want it to be but arranged as above:
\node[fill=blue,draw] at ([yshift=-20pt]current bounding box.north east){Person A};
\node[fill=yellow,draw] at ([yshift=-20pt]current bounding box.north east){Person B};
\node[fill=green,draw] at ([yshift=-20pt]current bounding box.north east){Person C};
\node[fill=red,draw] at ([yshift=-20pt]current bounding box.north east){Person D};
\node[fill=purple,draw] at ([yshift=-20pt]current bounding box.north east){Person E};
\end{ganttchart}
\end{tikzpicture}
}
\end{figure}
\end{center}
\end{document}


I assume this is a really silly mistake but even after reading the official documentation of pgfgantt, I cant happen to understand how to place this boxes properly. Any help is highly appreciated as I am about to bite chunks out of my desk..

The following code is based on answer from cfr and solves the problem described above perfectly:

\documentclass[12pt]{article}
\usepackage{pgfgantt}
\begin{document}
\begin{center}
\begin{figure}[H]
\noindent\resizebox{480pt}{!}{
\begin{tikzpicture}
\begin{ganttchart}
[
vgrid,
vgrid style/.style=dotted,
hgrid,
hgrid style/.style=dotted,
y unit title=0.5cm,
y unit chart=0.8cm,
title/.append style={shape=rectangle, fill=black!20},
title height=1,
bar/.append style={fill=blue!80},
bar height=.5,
bar label font=\normalsize\color{black!80},
group top shift=.6,
group height=.3,
group peaks height=.2,
]
{1}{35}
\gantttitle{Some Title}{35}\\\\
\gantttitlelist{1,...,35}{1}\\\\
\gantttitle{Plan}{7}\gantttitle{Execute}{11}\gantttitle{Stabilize}{4}\gantttitle{Finish}{13}\\
\ganttgroup{Plan}{1}{6}\\
\ganttbar{Kick-Off Meeting}{1}{1}\\
\ganttset{bar/.append style={fill=yellow!80}}
\ganttbar{}{1}{1}\\
\ganttset{bar/.append style={fill=green!80}}
\ganttbar{}{1}{1}\\
\ganttset{bar/.append style={fill=red!80}}
\ganttbar{}{1}{1}\\
\ganttset{bar/.append style={fill=purple!80}}
\ganttbar{}{1}{1}\\
\ganttset{bar/.append style={fill=blue!80}}
%This is the solution that works:
\node (a) [fill=blue,draw,anchor=east] at ([xshift=-40pt]current bounding box.8){Person A};
\node (b) [fill=yellow,draw,anchor=west] at ([yshift=-20pt]a.south west){Person B};
\node (c) [fill=green,draw,anchor=west] at ([yshift=-20pt]b.south west){Person C};
\node (d) [fill=red,draw,anchor=west] at ([yshift=-20pt]c.south west){Person D};
\node[fill=purple,draw,anchor=west] at ([yshift=-20pt]d.south west){Person E};
\end{ganttchart}
\end{tikzpicture}
}
\end{figure}
\end{center}
\end{document}

• Welcome! Please post the code for your chart (or a mini-version of it) in the form of a complete example which we can copy-paste-compile and add the legend to. Right now, I'd have to learn the package and create a chart from scratch before I could even think about the question you've asked. And, you know, life is too short - especially when you know you're just duplicating something the person asking knows already how to do perfectly well.
– cfr
Apr 16, 2016 at 21:17
• I just added an MWE but you were faster. Will look into your solution now. Apr 16, 2016 at 21:52
• Thanks. I realised I could steal the MWE from Alain's answer once I figured out how to make it work with the current version of the package ;).
– cfr
Apr 16, 2016 at 21:56
• How do I know what does current bounding box refer to? What if I want to put some descriptive text on the right of \ganttbar{Kick-Off Meeting}{1}{1}\\? Sep 24, 2016 at 4:36

OK. I used Alain Matthes's chart, updating it for the current version of the package.

Each time you add a node, the current bounding box changes to include that node. As the picture widens, each node ends up wandering further to the right. The easiest thing is to name the first node you add and then add the next node relative to that one rather than relative to the bounding box.

For example:

\documentclass[border=10pt,tikz,multi]{standalone}
\usepackage{pgfgantt}
\begin{document}
% updated from Alain Matthes's answer at https://tex.stackexchange.com/a/64863/
\begin{ganttchart}[y unit title=0.4cm,
y unit chart=0.5cm,
vgrid,hgrid,
title label anchor/.style={below=-1.6ex},
title left shift=.05,
title right shift=-.05,
title height=1,
bar/.style={fill=gray!50},
incomplete/.style={fill=white},
progress label text={},
bar height=0.7,
group right shift=0,
group top shift=.6,
group height=.3,
group peaks height={.2}
]{1}{24}
%labels
\gantttitle{Week}{24} \\
\gantttitle{Monday}{4}
\gantttitle{Tuesday}{4}
\gantttitle{Wednesday}{4}
\gantttitle{Thursday}{4}
\gantttitle{Friday}{4}
\gantttitle{Saturday}{4} \\

%relations
\node (a) [fill=white,draw,anchor=west] at (current bounding box.north east){Box North East};
\node (b) [fill=red,draw,anchor=west] at ([yshift=-20pt]a.south west){Box North East};
\node (c) [fill=blue,draw,anchor=west] at ([yshift=-20pt]b.south west){Box North East};
\node (d) [fill=yellow,draw,anchor=west] at ([yshift=-20pt]c.south west){Box North East};
\node[fill=purple,draw,anchor=west] at ([yshift=-20pt]d.south west){Box North East};
\end{ganttchart}
\end{document}


• You made my day! Thank you so much! I couldn't find this last missing piece in the puzzle to find my error. I will need to do some little adjustments but the big issue is solved. I spend days to figure this out and you did it in like minutes. Apr 16, 2016 at 22:04
• I think you just need to think about what current bounding box means, but this is not obvious if things are unfamiliar and you're in the grip of a problem.
– cfr
Apr 16, 2016 at 22:11
• I had that in mind indeed but I couldn't find a way to work around it. I edited my question to be more clear so others can find this solution and help with their maybe similar problem. Apr 16, 2016 at 22:17
• Well, it is true that diagnosis is one thing and cure another ;).
– cfr
Apr 16, 2016 at 22:18
• Is there a way to make visible the current bounding box ? This would be a blessing when facing a problem such as above !
– Yves
May 3, 2017 at 18:13