![甘特图问题](https://linux22.com/image/389075/%E7%94%98%E7%89%B9%E5%9B%BE%E9%97%AE%E9%A2%98.png)
我有两个问题:
- 图表的标题
gantt
包含我不想要的虚线。 - 是否还可以提供任务栏的进度?如果可以,该怎么做?(请参见下面的第二张图以了解更多信息,该图片取自 pgfgantt 包)
gantt
可以找到使用的包这里
非常感谢所有的帮助。
问候
\documentclass[border=12pt]{standalone}
\usepackage{gantt}
\newcommand{\defcolor}{blue}
\begin{document}
\begin{gantt}[drawledgerline=true]{18}{24}
% vertical, horizontal 'boxes'
\begin{ganttitle} % First title: Year
\numtitle{2019}{1}{2019}{24}
% start, label, width
\end{ganttitle}
\begin{ganttitle} % Second title: Months
\titleelement{Feb}{4}
\titleelement{Mar}{4}
\titleelement{Apr}{4}
\titleelement{May}{4}
\titleelement{Jun}{4}
\titleelement{Jul}{4}
\end{ganttitle}
\begin{ganttitle} % Third title: Weeks
\numtitle{6}{1}{29}{1}
% start, skip, end, width
\end{ganttitle}
%---------------------------------------
\ganttmilestone{Task A}{0} % Milestone: Label, position
%---------------------------------------
%---------------------------------------
% Task B
%---------------------------------------
\ganttgroup{Task B}{0}{6} % start, width
%
\ganttbar[color=\defcolor]{Task B}{0}{1}
\ganttbarcon[color=\defcolor]{Task B}{1}{1}
\ganttbarcon[color=\defcolor]{Task B}{1}{4}
\ganttbarcon[color=\defcolor]{Task B}{3}{3}
%
\ganttmilestonecon{Task B}{6}
%---------------------------------------
%---------------------------------------
% Task C
%---------------------------------------
\ganttgroup{Task C}{6}{15} % start, width
%
\ganttbar[color=\defcolor]{Task C}{6}{2}
\ganttbarcon[color=\defcolor]{Task C}{8}{8}
\ganttbarcon[color=\defcolor]{Task C}{15}{3}
\ganttbarcon[color=\defcolor]{Task C}{16}{4}
%
\ganttmilestonecon{Task C}{20}
%
\ganttbarcon[color=\defcolor]{Task C}{20}{1}
%
\ganttmilestonecon{Task C}{21}
%---------------------------------------
\end{gantt}
\end{document}
答案1
去除虚假的虚线
这是第一个问题的答案。在我看来,这是软件包中的错误导致的。在绘制垂直线的命令中,坐标y
指定不正确。可以使用xpatch
软件包进行修补:
\documentclass[border=12pt]{standalone}
\usepackage{gantt}
\usepackage{xpatch}
\xpatchcmd{\gantt}{-0.5}{-1.5}{}{}
\newcommand{\defcolor}{blue}
\begin{document}
\begin{gantt}[drawledgerline=true]{18}{24}
% vertical, horizontal 'boxes'
\begin{ganttitle} % First title: Year
\numtitle{2019}{1}{2019}{24}
% start, label, width
\end{ganttitle}
\begin{ganttitle} % Second title: Months
\titleelement{Feb}{4}
\titleelement{Mar}{4}
\titleelement{Apr}{4}
\titleelement{May}{4}
\titleelement{Jun}{4}
\titleelement{Jul}{4}
\end{ganttitle}
\begin{ganttitle} % Third title: Weeks
\numtitle{6}{1}{29}{1}
% start, skip, end, width
\end{ganttitle}
%---------------------------------------
\ganttmilestone{Task A}{0} % Milestone: Label, position
%---------------------------------------
%---------------------------------------
% Task B
%---------------------------------------
\ganttgroup{Task B}{0}{6} % start, width
%
\ganttbar[color=\defcolor]{Task B}{0}{1}
\ganttbarcon[color=\defcolor]{Task B}{1}{1}
\ganttbarcon[color=\defcolor]{Task B}{1}{4}
\ganttbarcon[color=\defcolor]{Task B}{3}{3}
%
\ganttmilestonecon{Task B}{6}
%---------------------------------------
%---------------------------------------
% Task C
%---------------------------------------
\ganttgroup{Task C}{6}{15} % start, width
%
\ganttbar[color=\defcolor]{Task C}{6}{2}
\ganttbarcon[color=\defcolor]{Task C}{8}{8}
\ganttbarcon[color=\defcolor]{Task C}{15}{3}
\ganttbarcon[color=\defcolor]{Task C}{16}{4}
%
\ganttmilestonecon{Task C}{20}
%
\ganttbarcon[color=\defcolor]{Task C}{20}{1}
%
\ganttmilestonecon{Task C}{21}
%---------------------------------------
\end{gantt}
\end{document}
添加完成栏
这是第二个问题的答案。我percent
在命令中添加了一个键\ganttbar
,用于提供完成百分比(值范围为 0-1)。默认值为 1。我还为完成标签的位置添加了一个键。默认值为,right
但也可以指定为left
。最后有一个键plabeltext
,它定义在完成金额后打印出来的字符串。它预设为,% Complete
但可以使用进行更改\presetkeys{ganttbarx}{plabeltext=<text>}{}
。
这是一个完整的例子:
\documentclass[border=12pt]{standalone}
\usepackage{gantt}
\usepackage{xpatch}
\xpatchcmd{\gantt}{-0.5}{-1.5}{}{}
\makeatletter
\newlength{\gantttmpc}
\newlength{\ganttbarpercentage}
\define@key{ganttbarx}{percent}{\def\ganttbarpercent{#1}\pgfmathparse{int(round(\ganttbarpercent*100))}\let\ganttbarpercentage\pgfmathresult}
\define@key{ganttbarx}{percentpos}{\def\@tmp{left}\def\ganttbarpercentpos{#1}\ifx\ganttbarpercentpos\@tmp\def\ganttbarpercentanchor{\gantttmpa}\else\def\ganttbarpercentanchor{\gantttmpb}\fi}
\define@key{ganttbarx}{plabeltext}{\def\ganttbarplabeltext{#1}}
\presetkeys{ganttbarx}{pattern=north west lines,color=black,percent=1,plabeltext={\% Complete},percentpos=right}{}
\renewcommand{\ganttbar}[4][]{
\setlength{\gantttmpa}{#3\ganttunitlength}
\setlength{\gantttmpb}{#4\ganttunitlength}
\setlength{\gantttmpc}{#4\ganttunitlength}
\setlength{\gantttmpb}{\gantttmpa+\gantttmpb}
\ifKV@ganttx@drawledgerline
\draw[dotted] (0,\value{ganttnum}-0.2) -- (\value{ganttwidth}*\ganttunitlength,\value{ganttnum}-0.2);
\fi
\node at (0,\value{ganttnum}) [anchor=base east] {%
\ganttfontsize #2%
};
\setkeys{ganttbarx}{#1}{%
\setlength{\gantttmpc}{\gantttmpa+\ganttbarpercent\gantttmpc}
\draw[] (\gantttmpa,\value{ganttnum}+0.1) rectangle (\gantttmpb,\value{ganttnum}+0.5);
\node at (\ganttbarpercentanchor,\value{ganttnum}+0.25) [\ganttbarpercentpos=.5\ganttunitlength] {\ganttbarpercentage\ganttbarplabeltext};
\fill[pattern=\ganttbarpattern,pattern color=\ganttbarcolor] (\gantttmpa,\value{ganttnum}+0.1) rectangle (\gantttmpc,\value{ganttnum}+0.5);
}
\setlength{\ganttlastx}{\gantttmpb}
\addtocounter{ganttnum}{-1}
}
\makeatother
\newcommand{\defcolor}{blue}
\begin{document}
\begin{gantt}[drawledgerline=true]{18}{24}
% vertical, horizontal 'boxes'
\begin{ganttitle} % First title: Year
\numtitle{2019}{1}{2019}{24}
% start, label, width
\end{ganttitle}
\begin{ganttitle} % Second title: Months
\titleelement{Feb}{4}
\titleelement{Mar}{4}
\titleelement{Apr}{4}
\titleelement{May}{4}
\titleelement{Jun}{4}
\titleelement{Jul}{4}
\end{ganttitle}
\begin{ganttitle} % Third title: Weeks
\numtitle{6}{1}{29}{1}
% start, skip, end, width
\end{ganttitle}
%---------------------------------------
\ganttmilestone{Task A}{0} % Milestone: Label, position
%---------------------------------------
%---------------------------------------
% Task B
%---------------------------------------
\ganttgroup{Task B}{0}{6} % start, width
%
\ganttbar[color=\defcolor,percent=.25]{Task B}{0}{1}
\ganttbarcon[color=\defcolor,percent=.4]{Task B}{1}{1}
\ganttbarcon[color=\defcolor,percent=.75]{Task B}{1}{4}
\ganttbarcon[color=\defcolor,percent=.6]{Task B}{3}{3}
%
\ganttmilestonecon{Task B}{6}
%---------------------------------------
%---------------------------------------
% Task C
%---------------------------------------
\ganttgroup{Task C}{6}{15} % start, width
%
\ganttbar[color=\defcolor,percent=.8]{Task C}{6}{2}
\ganttbarcon[color=\defcolor,percent=.75,percentpos=left]{Task C}{8}{8}
\ganttbarcon[color=\defcolor]{Task C}{15}{3}
\ganttbarcon[color=\defcolor]{Task C}{16}{4}
%
\ganttmilestonecon{Task C}{20}
%
\ganttbarcon[color=\defcolor,percentpos=left]{Task C}{20}{1}
%
\ganttmilestonecon{Task C}{21}
%---------------------------------------
\end{gantt}
\end{document}