如何在甘特图中使用条件?

如何在甘特图中使用条件?

我一直在尝试在甘特图中使用条件,以便自动更改颜色。在这种情况下,我只想将进度小于 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}

有人能帮我吗?

答案1

使用该etoolbox包,您可以修改\ganttbar命令以根据第一个参数(包含progress=x)设置填充颜色。使用\pretocmd,此测试将添加到命令的开头。

要修补实际\ganttbar命令,\pretocmd必须以稍微复杂的方式使用(参见请指导一下patchcmd和xpatch的使用progress=x)。还请注意,测试中不会解析键值对\ifstrequal,即使用此方法无法进行数值比较(较小、较大)。

梅威瑟:

\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}

结果:

在此处输入图片描述

相关内容