是否可以在甘特图的 x 轴下方添加文本。例如,假设我有一张包含两个任务的简单甘特图,
\begin{ganttchart}[vgrid, hgrid]{12}
\gantttitle{Title}{12} \\
\ganttbar{Task 1}{0}{3} \\
\ganttbar{Task 2}{3}{10}
\end{ganttchart}
我想在甘特图中的某些垂直网格线下方添加文本。例如,我想在任务 1 结束的垂直网格线下方添加文本“T_1”,并在任务 2 开始的垂直网格线下方添加“T_2”。这可以吗?
答案1
这里有一种方法可以使用Paul Gessler's nice answer
到如何在甘特图中的指定日期处绘制垂直线?:
\documentclass{article}
\usepackage{pgfgantt}
\makeatletter
\@gtt@keydef{anyday}{none}
\@gtt@keydef{anyday offset}{1}
\@gtt@stylekeydef{anyday rule}{dashed, line width=1pt}
\@gtt@keydef{anyday label font}{\normalfont}
\@gtt@stylekeydef{anyday label node}{%
anchor=north, font=\ganttvalueof{anyday label font}%
}
\newcount\gtt@anyday@slot
\newcommand\gantText[2]{%
\gtt@tsstojulian{#1}{\gtt@anyday@slot}%
\gtt@juliantotimeslot{\gtt@anyday@slot}{\gtt@anyday@slot}%
\pgfmathsetmacro\y@upper{%
\gtt@lasttitleline * \ganttvalueof{y unit title}%
}%
\pgfmathsetmacro\y@lower{%
\gtt@lasttitleline * \ganttvalueof{y unit title}%
+ (\gtt@currentline - \gtt@lasttitleline - 1)%
* \ganttvalueof{y unit chart}%
}%
\pgfmathsetmacro\x@mid{%
(\gtt@anyday@slot - 1 + \ganttvalueof{anyday offset})%
* \ganttvalueof{x unit}%
}%
\node [/pgfgantt/anyday label node]
at (\x@mid pt, \y@lower pt) {#2};
}
\makeatother
\begin{document}
\begin{ganttchart}[vgrid, hgrid]{1}{12}
\gantttitle{Title}{12} \\
\ganttbar{Task 1}{1}{3} \\
\ganttbar{Task 2}{3}{12} \\
\gantText{3}{$T_1$}
\gantText{2}{$T_2$}
\end{ganttchart}
\end{document}