甘特图:标题包含在画布中

甘特图:标题包含在画布中

我有一张甘特图,其中我想将标题包含在画布中,这意味着我的图表中有几个标题,它们应该作为部分使用。我希望将标题放在任务描述和栏上方的中心。

这是我目前所拥有的:

\documentclass[a4paper, 11pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb} % amsmath loads amstext
\usepackage[english, ngerman]{babel}
\usepackage[hmargin=2.5cm,vmargin=2.5cm]{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{pgfgantt}

\begin{document}

\begin{center}

\begin{tikzpicture} % added

\begin{ganttchart}[%
    x unit=0.8cm,
    y unit title=0.8cm,
    y unit chart=0.6cm,
    vgrid,
    hgrid,
    bar/.append style={fill=MidnightBlue},
    milestone/.append style={fill=SkyBlue, rounded corners=3pt},
    bar label font=\footnotesize, % reduce font size
    milestone label font=\footnotesize\itshape, % reduce font size
    bar label node/.append style={align=right},
    milestone label node/.append style={align=right}, 
    canvas/.append style={name=canvas}
]{1}{12}

\gantttitle{\textbf{YEAR 1}: Entry Phase}{12} \\
\gantttitlelist{1,...,12}{1} \\
\ganttbar{Literature: research and review}{1}{7} \\
\ganttbar{Writing of exposé}{4}{6} \\
\ganttmilestone{Public presentation}{7} \\
\ganttbar{Adaption of experiment 1}{11}{12} \\
\ganttbar{Conference participation\\\& networking}{11}{12} \\ % <- added a linebreak in the label text

\makeatletter
\advance\gtt@currentline by 1
\makeatother

\gantttitle{\textbf{YEAR 2}: Research Phase}{12} \\
\gantttitlelist{1,...,12}{1} \\

\makeatletter
\advance\gtt@currentline by -2
\makeatother

\ganttbar{Experiment 1: conduction}{1}{2} \\
\ganttbar{Planning of experiment 2}{3}{4} \\
\ganttbar{Organizing a workshop}{9}{11} \\
\ganttmilestone{Workshop}{11}

\end{ganttchart}

\draw[/pgfgantt/canvas,fill=none]
    ([yshift=-0.5\pgflinewidth]canvas.north west) --
    (canvas.north west -| current bounding box.west) |-
    ([yshift=0.5\pgflinewidth]canvas.south west);

\end{tikzpicture}

\end{center}

\end{document}

我在 pgf-gantt 手册中发现了这一点:

\newif\ifgtt@includetitle

\ganttset{%

include title in canvas/.is if=gtt@includetitle,%

include title in canvas

}

但我不确定如何将其融入我的代码中。另外,您有什么建议可以让我的图表看起来更漂亮一点吗?

提前致谢!

最好的,丽莎

答案1

可以使用选项手动调整标题title left skip。它需要一个数字,该数字被解释为时间段宽度的因子。必须将它提供给要扩展的所有标题。

不幸的是,在设置标题时,描述的宽度尚不清楚。因此必须手动设置该值。如果太低,标题栏将无法一直延伸到左侧。请注意,如果太大,描述的宽度将扩大。

为了减轻痛苦,我\titleleftshiftganttchart环境中定义了一个宏并将其用于相关的标题。

以下是代码:

\documentclass[a4paper, 11pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb} % amsmath loads amstext
\usepackage[english, ngerman]{babel}
\usepackage[hmargin=2.5cm,vmargin=2.5cm]{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{pgfgantt}

\begin{document}

\begin{center}

\begin{tikzpicture} % added

\begin{ganttchart}[%
    x unit=0.8cm,
    y unit title=0.8cm,
    y unit chart=0.6cm,
    vgrid,
    hgrid,
    bar/.append style={fill=MidnightBlue},
    milestone/.append style={fill=SkyBlue, rounded corners=3pt},
    bar label font=\footnotesize, % reduce font size
    milestone label font=\footnotesize\itshape, % reduce font size
    bar label node/.append style={align=right},
    milestone label node/.append style={align=right}, 
    canvas/.append style={name=canvas}
]{1}{12}
% set value here
\def\titleleftshift{-5.9}

% added title left shift option
\gantttitle[title left shift=\titleleftshift]{\textbf{YEAR 1}: Entry Phase}{12} \\
\gantttitlelist{1,...,12}{1} \\
\ganttbar{Literature: research and review}{1}{7} \\
\ganttbar{Writing of exposé}{4}{6} \\
\ganttmilestone{Public presentation}{7} \\
\ganttbar{Adaption of experiment 1}{11}{12} \\
\ganttbar{Conference participation\\\& networking}{11}{12} \\ % <- added a linebreak in the label text

\makeatletter
\advance\gtt@currentline by 1
\makeatother

% added title left shift option
\gantttitle[title left shift=\titleleftshift]{\textbf{YEAR 2}: Research Phase}{12} \\
\gantttitlelist{1,...,12}{1} \\

\makeatletter
\advance\gtt@currentline by -2
\makeatother

\ganttbar{Experiment 1: conduction}{1}{2} \\
\ganttbar{Planning of experiment 2}{3}{4} \\
\ganttbar{Organizing a workshop}{9}{11} \\
\ganttmilestone{Workshop}{11}

\end{ganttchart}

\draw[/pgfgantt/canvas,fill=none]
    ([yshift=-0.5\pgflinewidth]canvas.north west) --
    (canvas.north west -| current bounding box.west) |-
    ([yshift=0.5\pgflinewidth]canvas.south west);

\end{tikzpicture}

\end{center}

\end{document}

结果如下:

在此处输入图片描述

相关内容