I am working on a pgfgantt
document where all timelines are governed by real dates and where the titlelist shows week days based on the TikZ calendar
library. I have used and modified the code from Custom calendar using TikZ to achieve this. The dates are converted to running numbers by using the pgfcalendardatetojulian
.
What I am now looking to do is to colour the background canvas of the Gantt plot so that weekdays are white and Sundays (or weekends, Sat-Sun and holidays) are highlighted by vertical (light) red stripes. In the picture from the MWE this is equal to the area between the two red dashed lines under each red S (for Sunday). This means using the calendar library to determine what parts of the background canvas should be coloured reddish. The question is how this can be done?
MWE:
\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage[utf8]{inputenc} \usepackage[T1]{fontenc}
\usepackage{pgfgantt} \usepackage{pgfcalendar}
\newcount\startdate
\newcount\daynum
\pgfcalendardatetojulian{2012-01-09}{\startdate}
%Redefinition from pgfcalendar.code.tex
\def\pgfcalendarweekdayveryshortname#1{%
\translate{\ifcase#1M\or T\or W\or T\or F\or S\or S\fi}%
}
\protected\def\zzz{% Mod from https://tex.stackexchange.com/q/87550/19384
\pgfcalendarjuliantodate{\numexpr\startdate\relax}{\year}{\month}{\day}%
\pgfcalendarjuliantoweekday\startdate\daynum
{\ifnum\daynum=6\color{red}%
\tiny\pgfcalendarweekdayveryshortname{\daynum}%
\else
\tiny\pgfcalendarweekdayveryshortname{\daynum}%
\fi}%
\global\advance\startdate1}
\begin{document}
\noindent\resizebox{\textwidth}{!}{
\begin{tikzpicture}
\begin{ganttchart}[vgrid={*5{black,dotted},*2{red,dashed}},hgrid,%
y unit chart=5mm,x unit=3mm]{20}
\gantttitle{Plan}{20} \\
\gantttitlelist[
title list options={var=\y,evaluate=\y as \x using
"{\zzz}"}
]{1,...,20}{1} \\
\ganttbar{Task 1}{2}{5} \\
\ganttlinkedbar{Task 2}{6}{8} \\
\ganttlinkedmilestone{Goal}{9}\\
\end{ganttchart}
\end{tikzpicture}
}
\end{document}
Resulting image but with a reddish shaded area added to show what I would like to achieve for all 'red days':
Best Answer
And so, after three years...
My approach is to draw
TikZ
rectangles on the background layer once the Gantt chart has been drawn.In the example below, I've highlighted the units used in the calendar, so that you can easily redefine the rectangles as you need. (For example, this approach could also be used to define horizontal stripes that would correspond to task groups.)
Note that the coloring of weekdays (Saturday in light red, and Sundays in bold red) is implemented here in a much lighter way that in your original code (i.e. directly in the definition of
\pgfcalendarweekdayletter
-- cf. line 27)What remains to do:
Edit: Now automatically computes the number of weeks, when does the first week-end starts, and handles correctly if the first day of the calendar is a Sunday.