如何全局定义甘特图中不同的任务栏颜色?

如何全局定义甘特图中不同的任务栏颜色?

我一直在使用这个甘特图示例这里。我想添加用户特定的彩色条。本质上,我不想必须输入bar/.append style={fill=green}每个新的条命令。我只想将一个简单的命令参数传递给\ganttbar。作为示例

\documentclass[border=10pt]{standalone}%{article}
%\usepackage[margin=1cm]{geometry}
\usepackage{pgfgantt}
\usepackage{graphicx}
\usepackage{xcolor}
%\usetikzlibrary{positioning}

\ganttset{group/.append style={orange},
milestone/.append style={red},
progress label node anchor/.append style={text=red}}

\newcommand{\USERtask}{bar/.append style={fill=green}}

\begin{document}

%    \begin{figure}
%    \centering
     \begin{ganttchart}[%Specs
     y unit title=0.5cm,
     y unit chart=0.7cm,
     vgrid,hgrid,
     title height=1,
%     title/.style={fill=none},
     title label font=\bfseries\footnotesize,
     bar/.style={fill=blue},
     bar height=0.7,
%   progress label text={},
     group right shift=0,
     group top shift=0.7,
     group height=.3,
     group peaks width={0.2},
     inline]{1}{24}
    %labels
    \gantttitle{A two-years project}{24}\\  % title 1
    \gantttitle[]{2013}{12}                 % title 2
    \gantttitle[]{2014}{12} \\              
    \gantttitle{Q1}{3}                      % title 3
    \gantttitle{Q2}{3}
    \gantttitle{Q3}{3}
    \gantttitle{Q4}{3}
    \gantttitle{Q1}{3}
    \gantttitle{Q2}{3}
    \gantttitle{Q3}{3} 
    \gantttitle{Q4}{3}\\
    % Setting group if any
    \ganttgroup[inline=false]{Group 1}{1}{5}\\ 
    \ganttbar[progress=10,inline=false]{Planning}{1}{4}\\
    \ganttmilestone[inline=false]{Milestone 1}{9} \\

    \ganttgroup[inline=false]{Group 2}{6}{12} \\
    %
    % QUESTION IS HERE
    %\ganttbar[progress=20,inline=false]{test1}{10}{19} \\ 
    \ganttbar[progress=20,inline=false,\USERtask]{test1}{10}{19} \\
    %
    %
    \ganttmilestone[inline=false]{Milestone 2}{17} \\
    \ganttbar[progress=5,inline=false]{test2}{11}{20} \\
    \ganttmilestone[inline=false]{Milestone 3}{22} \\       

    \ganttgroup[inline=false]{Group 3}{13}{24} \\ 
    \ganttbar[progress=90,inline=false]{Task A}{13}{15} \\ 
    \ganttbar[progress=50,inline=false, bar progress label node/.append style={below left= 10pt and 7pt}]{Task B}{13}{24} \\ \\
    \ganttbar[progress=30,inline=false]{Task C}{15}{16}\\ 
    \ganttbar[progress=70,inline=false]{Task D}{18}{20} \\ 
\end{ganttchart}
%    \caption{Gantt diagram for 2013--2014 Project}
%\end{figure}
\end{document}

我尝试创建一个新命令

\newcommand{\USERtask}{bar/.append style={fill=green}}

然后在文档甘特图环境中,我尝试调用命令

\ganttbar[progress=20,inline=false,\USERtask]{test1}{10}{19}

但它会出现一个错误“我不知道键‘/pgfgantt/bar/.append style={fill=green}’并且我将忽略它。”

我怎样才能传递全局参数\ganttbar[ ]而不是到处输入\ganttbar[bar/.append style={fill=green}]

答案1

最简单的方法是使用样式而不是宏。我不确定为什么不选择这种方式 - 你已经拥有许多样式,它们的作用并无不同。

\documentclass[border=10pt]{standalone}
\usepackage{pgfgantt}

\ganttset{
  group/.append style={orange},
  milestone/.append style={red},
  progress label node anchor/.append style={text=red},
  user task/.style={bar/.append style={fill=green}},
}

\newcommand{\USERtask}{bar/.append style={fill=green}}

\begin{document}
     \begin{ganttchart}[%Specs
     y unit title=0.5cm,
     y unit chart=0.7cm,
     vgrid,hgrid,
     title height=1,
     title label font=\bfseries\footnotesize,
     bar/.style={fill=blue},
     bar height=0.7,
     group right shift=0,
     group top shift=0.7,
     group height=.3,
     group peaks width={0.2},
     inline]{1}{24}
    %labels
    \gantttitle{A two-years project}{24}\\  % title 1
    \gantttitle[]{2013}{12}                 % title 2
    \gantttitle[]{2014}{12} \\
    \gantttitle{Q1}{3}                      % title 3
    \gantttitle{Q2}{3}
    \gantttitle{Q3}{3}
    \gantttitle{Q4}{3}
    \gantttitle{Q1}{3}
    \gantttitle{Q2}{3}
    \gantttitle{Q3}{3}
    \gantttitle{Q4}{3}\\
    % Setting group if any
    \ganttgroup[inline=false]{Group 1}{1}{5}\\
    \ganttbar[progress=10,inline=false]{Planning}{1}{4}\\
    \ganttmilestone[inline=false]{Milestone 1}{9} \\

    \ganttgroup[inline=false]{Group 2}{6}{12} \\
    %
    % QUESTION IS HERE
    %\ganttbar[progress=20,inline=false]{test1}{10}{19} \\
    \ganttbar[progress=20,inline=false,user task]{test1}{10}{19} \\
    %
    %
    \ganttmilestone[inline=false]{Milestone 2}{17} \\
    \ganttbar[progress=5,inline=false]{test2}{11}{20} \\
    \ganttmilestone[inline=false]{Milestone 3}{22} \\

    \ganttgroup[inline=false]{Group 3}{13}{24} \\
    \ganttbar[progress=90,inline=false]{Task A}{13}{15} \\
    \ganttbar[progress=50,inline=false, bar progress label node/.append style={below left= 10pt and 7pt}]{Task B}{13}{24} \\ \\
    \ganttbar[progress=30,inline=false]{Task C}{15}{16}\\
    \ganttbar[progress=70,inline=false]{Task D}{18}{20} \\
\end{ganttchart}
\end{document}

绿色条

如果你想要指定颜色,而不是总是绿色,你可以使用

user task/.style={bar/.append style={fill=#1}},

然后说

user task=green

做同样的事情,这显然更灵活一些,可能更接近你真正想要做的事情。

相关内容