Removing unused rows from pgfgantt gantt chart

overleafpdftexpgfgantt

I am preparing a gantt chart in Overleaf using the pgfgrantt package. I use the bar and milestone top shift options to shift them up and squeeze them closer to one another. However, this leave a lot of unused rows at the bottom of the chart, as shown in the attached image. How can I get rid of these rows? The code below can reproduce the attached chart.enter image description here

\begin{ganttchart}[
    vgrid,
    hgrid,
   milestone top shift=-2.45,
    milestone right shift=0.3,
    group top shift = -0.05,
    bar height = 0.2,bar label node/.append style={align=right}
    ]{1}{24}
  \gantttitle{2022}{3}   \gantttitle{2023}{12} 
  \gantttitle{2024}{9} \\
  \gantttitlelist{10,11,12}{1}  \gantttitlelist{1,...,12}{1} \gantttitlelist{1,...,9}{1}\\
  \ganttgroup{a}{1}{11.5} \\
  \ganttbar[bar top shift=-0.6]{b}{1}{2.5}{jj} \\
  \ganttlinkedbar[bar height=0.2, bar top shift=-1.2]{c}{3.5}{8.5} \\
  \ganttlinkedbar[bar top shift=-1.8]{d}{8.5}{10.5} \\
  \ganttlinkedmilestone{e}{11} 
  %%
  \ganttgroup[group top shift = -1.6]{2\\f}{7}{15} \\
  \ganttbar[bar height = 0.4, bar top shift=-2.1]{g \\[-3pt]h}{7}{8}{jj} \\
  \ganttlinkedbar[bar height=0.4, bar top shift=-2.2]{i \\[-3pt] j}{9}{11} \\
  \ganttlinkedbar[bar height = 0.4, bar top shift=-2.3]{k \\[-3pt] l}{12}{14} \\
  \ganttlinkedmilestone[milestone top shift = -2.6]{m}{15}
  %%
  \ganttgroup[group top shift = -1.6]{n}{3}{23} \\
  \ganttbar[bar height = 0.2, bar top shift=-2.1]{p}{3}{5}{jj} \\
  \ganttlinkedmilestone[milestone top shift = -2.7]{o}{6} \\
  \ganttlinkedbar[bar height=0.2, bar top shift=-3.1]{q}{15}{18} \\
  \ganttlinkedbar[bar height = 0.4, bar top shift=-3.5]{r \\[-3pt] s}{19}{22} \\
  \ganttlinkedmilestone[milestone top shift = -3.8]{t}{23}
\end{ganttchart}

Best Answer

The height of the chart is computed automatically from the number of \ganttbar, \ganttgroup, ... commands by multiplying it with a fixed length, y unit chart. So even if you squeeze some rows by manually shifting them, the height of the chart will remain unaffected. I recommend to set y unit chart to a smaller value and not to shift the bars manually.

\begin{ganttchart}[..., y unit chart = 7mm, ...]

Other problems: You shouldn't use \\ to break lines in the label, but \ganttalignnewline. (Alternatively, you can set newline shortcut to false, but then have to use \ganttnewline to separate the lines in the chart).

\ganttbar{g \ganttalignnewline[-3pt]h}{7}{8}{jj}

Finally, line breaks don't seem to work in the label of \ganttgroup either way. If you really need two lines there, typeset the label in a \savebox.

\newsavebox\twof
\savebox\twof{\bfseries\begin{tabular}{@{}r@{}}2\\[-3pt]f\end{tabular}}
...
\ganttgroup{\usebox\twof}{7}{15}

enter image description here

\documentclass{article}
\usepackage{pgfgantt}
\begin{document}
\newsavebox\twof
\savebox\twof{\bfseries\begin{tabular}{@{}r@{}}2\\[-3pt]f\end{tabular}}
\begin{ganttchart}[
    vgrid,
    hgrid,
    bar height = 0.2,
    bar label node/.append style={align=right},
    y unit chart = 7mm
    ]{1}{24}
  \gantttitle{2022}{3} \gantttitle{2023}{12} \gantttitle{2024}{9} \\
  \gantttitlelist{10,11,12}{1}  \gantttitlelist{1,...,12}{1} \gantttitlelist{1,...,9}{1}\\
  \ganttgroup{a}{1}{11.5} \\
  \ganttbar{b}{1}{2.5}{jj} \\
  \ganttlinkedbar{c}{3.5}{8.5} \\
  \ganttlinkedbar{d}{8.5}{10.5} \\
  \ganttlinkedmilestone{e}{11} \\
  %%
  \ganttgroup{\usebox\twof}{7}{15} \\
  \ganttbar[bar height = 0.4]{g \ganttalignnewline[-3pt]h}{7}{8}{jj} \\
  \ganttlinkedbar[bar height=0.4]{i \ganttalignnewline[-3pt] j}{9}{11} \\
  \ganttlinkedbar[bar height = 0.4]{k \ganttalignnewline[-3pt] l}{12}{14} \\
  \ganttlinkedmilestone{m}{15}\\
  %%
  \ganttgroup{n}{3}{23} \\
  \ganttbar{p}{3}{5}{jj} \\
  \ganttlinkedmilestone{o}{6} \\
  \ganttlinkedbar{q}{15}{18} \\
  \ganttlinkedbar{r \ganttalignnewline[-3pt] s}{19}{22} \\
  \ganttlinkedmilestone{t}{23}
\end{ganttchart}
\end{document}
Related Question