0

I've been trying use a conditional inside my Gantt chart in order to become the process of change color automatically. In this case, I just wanna color red the bars that have progress minor them 100%.

\documentclass{article}

\usepackage{etoolbox}
\usepackage{parskip}
\usepackage{tikz}
\usepackage{pgfgantt}
\usepackage{ifthen}
\newcounter{myWeekNum}
\stepcounter{myWeekNum}

\newcommand{\myWeek}{\themyWeekNum
\stepcounter{myWeekNum}
\ifnum\themyWeekNum=53
\setcounter{myWeekNum}{1}
\else\fi
}


\newcommand{\setvalue}[2]{
\ifdefined #1
\renewcommand{#1}{#2}
\else
\newcommand{#1}{#2}
\fi
}

\setvalue{\x}{100}

\setvalue{\GreenColor}{green}

\begin{document}


\setcounter{myWeekNum}{1}
\ganttset{%
calendar week text={\myWeek{}}%
}
%

\begin{ganttchart}[         
            vgrid={*{6}{draw=none}, dotted},
            link mid=.25, 
            link bulge=4.3,
            progress=today,
            today={\the\year-\the\month-\the\day},
            today rule/.style={draw=blue, ultra thick},
            today label=\today,
            bar/.append style={fill=green},                                  %I wanna replace this line for the next line
            %bar/.append style={[fill="\ifthenelse{\x<100}{green}{red}"]},   %Problem
            %bar/.append style={fill=\GreenColor},                           %Test line
            bar incomplete/.append style={fill=white},
            x unit=.055cm,
            y unit title=.7cm,
            y unit chart=.6cm,
            time slot format=isodate,
            time slot format/start date=2017-12-01, title label font=\scriptsize
            ]{2018-01-13}{2018-05-30}
            \ganttset{bar height=.6}
            \gantttitlecalendar{year, month=shortname, week} \\

            \ganttgroup{Group}{2018-01-13}{2018-05-17} \\
            \ganttbar[progress=100]{Task1}{2018-01-13}{2018-05-17}\\
            \ganttbar[progress=60]{Task2}{2018-02-13}{2018-05-17}\\
            \ganttbar[progress=20]{Task3}{2018-03-13}{2018-05-17} \\
            \ganttbar[progress=90]{Task4}{2018-04-13}{2018-05-17} \\

            \ganttlink[link mid=.6]{elem0}{elem1}
            \ganttlink[link mid=.6]{elem1}{elem2}
            \ganttlink[link mid=.6]{elem2}{elem3}


   \end{ganttchart}     
\end{document}

Can someone help me?

1 Answers1

0

With the etoolbox package you can modify the \ganttbar command to set the fill color based on the first argument (containing progress=x). Using \pretocmd, this test will added at the beginning of the command.

To patch the actual \ganttbar command, \pretocmd must be used in a somewhat complicated way (see Please tutor the usage of patchcmd and xpatch). Note also that the key-value pair progress=x is not parsed in the \ifstrequal test, i.e., a numerical comparison (smaller, larger) would not work using this approach.

MWE:

\documentclass{article}

\usepackage{etoolbox}
\usepackage{pgfgantt}

\begin{document}

\expandafter\pretocmd\csname\string\ganttbar\endcsname{
\ifstrequal{#1}{progress=100}{%
\ganttset{bar/.append style={fill=green}}%
}{%
\ganttset{bar/.append style={fill=red}}%
}%
}{}{}

\begin{ganttchart}[         
            vgrid={*{6}{draw=none}, dotted},
            link mid=.25, 
            link bulge=4.3,
            progress=today,
            today={\the\year-\the\month-\the\day},
            today rule/.style={draw=blue, ultra thick},
            today label=\today,
            bar incomplete/.append style={fill=white},
            group progress label node/.append style={right=5pt},
            bar progress label node/.append style={right=5pt},
            x unit=.055cm,
            y unit title=.7cm,
            y unit chart=.6cm,
            time slot format=isodate,
            time slot format/start date=2017-12-01, title label font=\scriptsize
            ]{2018-01-13}{2018-05-30}
            \ganttset{bar height=.6}
            \gantttitlecalendar{year, month=shortname} \\

            \ganttgroup{Group}{2018-01-13}{2018-05-17} \\
            \ganttbar[progress=100]{Task1}{2018-01-13}{2018-05-17}\\
            \ganttbar[progress=60]{Task2}{2018-02-13}{2018-05-17}\\
            \ganttbar[progress=20]{Task3}{2018-03-13}{2018-05-17} \\
            \ganttbar[progress=90]{Task4}{2018-04-13}{2018-05-17} \\

            \ganttlink[link mid=.6]{elem0}{elem1}
            \ganttlink[link mid=.6]{elem1}{elem2}
            \ganttlink[link mid=.6]{elem2}{elem3}
   \end{ganttchart}     
\end{document}

Result:

enter image description here

Marijn
  • 37,699