2

I am currently using pgfgantt to add a Gantt chart in a Latex document. To improve readability am trying to alternate the background color of the rows. So that Group1, Task2 and Final task have a background color whilst task1 and milestone have a different background color.

Any pointers how can I achieve it ?

\begin{ganttchart}[
    hgrid,
    vgrid]{1}{12}[
  \gantttitle{2011}{18} \\
  \gantttitlelist{1,...,12}{1} \\
  \ganttgroup{Group 1}{1}{7} \\
  \ganttbar{Task 1}{1}{2} \\
  \ganttlinkedbar{Task 2}{3}{7} \ganttnewline
  \ganttmilestone{Milestone}{7} \ganttnewline
  \ganttbar{Final Task}{8}{12}
  \ganttlink{elem2}{elem3}
  \ganttlink{elem3}{elem4}
\end{ganttchart}

sample gantt chart

kpenza
  • 59

1 Answers1

5

Inspired by How to colour the pgfgantt canvas based on calendar dates, you can use a style list for hgrid to do this.

Style lists for hgrid/vgrid are described on page 8 in the manual (for v4.0 of pgfgantt, dated 2013/06/01). Similar to column specifications for tables, you can write for example

hgrid={ *{2}{blue}, *{3}{red} }

to make the first two grid lines, blue, and the next three red. This pattern is then repeated. The trick used below is to set the line width to the height of the rows, obtained via \ganttvalueof{y unit chart}, and then shift it upwards by half this.

output of code

\documentclass{article}
\usepackage{pgfgantt}
\tikzset{
   oddlines/.style={
        line width=\ganttvalueof{y unit chart},
        yshift=-0.5*\ganttvalueof{y unit chart},
        opacity=0.2,
        red
   },
  evenlines/.style={
      oddlines,
      blue
  }
}
\begin{document}
\begin{ganttchart}[
   hgrid={
      *{1}{oddlines},
      *{1}{evenlines},
    },
    vgrid
]{1}{12}
  \gantttitle{2011}{12} \\
  \gantttitlelist{1,...,12}{1} \\
  \ganttgroup{Group 1}{1}{7} \\
  \ganttbar{Task 1}{1}{2} \\
  \ganttlinkedbar{Task 2}{3}{7} \ganttnewline
  \ganttmilestone{Milestone}{7} \ganttnewline
  \ganttbar{Final Task}{8}{12}
  \ganttlink{elem2}{elem3}
  \ganttlink{elem3}{elem4}
\end{ganttchart}

\end{document}
Torbjørn T.
  • 206,688