pgfgantt 突出显示周末并在里程碑上画垂直线

pgfgantt 突出显示周末并在里程碑上画垂直线

我想在甘特图上突出显示周末行,但我不确定如何做到这一点。

我也想在我的每一个里程碑上画一条垂直线。

是否可以将图表拆分到多张 A4 页面上?

我当前的文档如下所示:

\documentclass[
    12pt,       % font size
    oneside,    % one-sided pages
]{article}

\usepackage[
    a0paper,    % paper format
    landscape   % orientation
]{geometry}

% Encoding
\usepackage[utf8]{inputenc} % utf8
\usepackage[T1]{fontenc}    % 8-bit font encoding

% Language
\usepackage[ngerman]{babel} % languages

% Color
\usepackage{xcolor}

% Gantt Chart
\usepackage{pgfgantt}

% 
\def\pgfcalendarweekdayletter#1{
    %
    \ifcase#1Mo\or Di\or Mi\or Do\or Fr\or \textcolor{red}{Sa}\or \textcolor{red}{So}\fi
    %
}

\begin{document}

    \selectlanguage{ngerman}

    \begin{ganttchart}[
            hgrid,
            vgrid,
            x unit=7mm,
            time slot format=little-endian,
            today={\the\day.\the\month.\the\year},
            progress=today,
            group incomplete/.append style={fill=gray},
            group left shift=0,
            group right shift=0,
            group height=.2,
            group peaks tip position=0,
            group peaks width=0.2,
            group peaks height=.2,
            %group label node/.append style={left=.6cm},
            %group progress label font=\bfseries\small,
        ]{18.09.2017}{31.01.2018}
        \gantttitlecalendar{year, month=name, weekday=letter, day} \\

        \ganttmilestone[progress label text={}]{Abgabe Schlussbericht}{22.12.2017} \\
        \ganttmilestone[progress label text={}]{Abgabe Poster}{29.01.2018} \\

        \ganttgroup{Vorbereitung}{18.09.2017}{30.09.2017} \\
        \ganttbar{Projektauftrag verfassen}{20.09.2017}{24.09.2017} \\
        \ganttbar{Fragenkatalog erstellen}{22.09.2017}{27.09.2017} \\

        \ganttgroup{Wissensaufbau}{20.09.2017}{10.10.2017} \\
        \ganttbar{Bücher \& Papers suchen}{20.09.2017}{04.10.2017} \\
        \ganttbar{Konzepte erarbeiten}{28.09.2017}{08.10.2017} \\

        \ganttgroup{Evaluation des Konzepts}{05.10.2017}{25.10.2017} \\

        \ganttgroup{Zwischenpräsentation}{30.10.2017}{24.11.2017} \\

        \ganttgroup{Abschlusspräsentation}{18.12.2017}{19.01.2018} \\

    \end{ganttchart}
\end{document}

答案1

分割甘特图必须手动完成,正如 Torbjørn T. 已经提到的那样。里程碑的线条也不会内置到 中pgfgantt。周末可以用

vgrid={*4{dotted},{red,dotted},dotted,{red,dotted}}

这适用于从周一开始的甘特图。对于其他工作日,必须使用不同的模式。

pgfgantt可以扩展来做你想做的事情。

下面的代码提供了以下新选项:

  • 虚拟网格周刊需要两个参数,用于垂直线。第二个参数用于周五和周六以及周日和周一之间的线。第一个参数用于所有其他线。它将覆盖使用 完成的设置vgrid,即使后者位于此选项之后。无默认值。
  • 周末背景设置周六和周日的背景颜色。如果设置为,则none周末不着色。默认值:weekend background=none
  • 里程碑线是一个标志。没有参数 时,true将绘制里程碑线。有 时,false将不绘制线条。可以为整个图表和单个里程碑提供此选项。可以为里程碑本地更改此设置。里程碑线可能会被今天的线条覆盖。默认值:milestone line=false
  • 里程碑线条风格设置里程碑线的样式。只能为整个图表设置,不能为单个里程碑设置。默认值:milestone line style/.style={blue,thick}

以下代码进行了修补pgfgantt,以使新代码在正确的时间执行。但这意味着,该代码可能无法与旧版本或新版本兼容pgfgantt(此处使用的当前版本:2013/06/01 v4.0)。

在此处输入图片描述

代码:

\documentclass[
    12pt,       % font size
    oneside,    % one-sided pages
]{article}

\usepackage[
    a0paper,    % paper format
    landscape,  % orientation
    margin=1cm
]{geometry}

% Encoding
\usepackage[utf8]{inputenc} % utf8
\usepackage[T1]{fontenc}    % 8-bit font encoding

% Language
\usepackage[ngerman]{babel} % languages

% Color
\usepackage{xcolor}

% Gantt Chart
\usepackage{pgfgantt}

% 
\def\pgfcalendarweekdayletter#1{
    %
    \ifcase#1Mo\or Di\or Mi\or Do\or Fr\or \textcolor{red}{Sa}\or \textcolor{red}{So}\fi
    %
}

\makeatletter
\usepackage{etoolbox}

% patching pgfgnatt
% insert hook before drawing the grid
\patchcmd{\endganttchart}{\ifgtt@vgrid}{\gtt@before@grid\ifgtt@vgrid}{}{%
    \GenericWarning{(preamble)\@spaces\@spaces\@spaces\@spaces}{Package preamble Warning: patching ganttchart environment did not work (2).}}
% insert hook after drawing the grid
\patchcmd{\endganttchart}{\def\@tempa{none}}{\gtt@after@grid\def\@tempa{none}}{}{%
    \GenericWarning{(preamble)\@spaces\@spaces\@spaces\@spaces}{Package preamble Warning: patching ganttchart environment did not work (3).}}
% insert macro for collecting data for milestone lines
% crazy patching, because LaTeX internally defines '\\gtt@chartelement' for macros with optional arguments
\expandafter\patchcmd\expandafter{\csname\string\gtt@chartelement\endcsname}%
    {\gtt@tsstojulian{#4}{\gtt@right@slot}}{\gtt@store@ms@line{#5}\gtt@tsstojulian{#4}{\gtt@right@slot}}{}{%
    \GenericWarning{(preamble)\@spaces\@spaces\@spaces\@spaces}{Package preamble Warning: patching ganttchartelement did not work.}}

% for drawing vgrid lines differently befoe and after weekend
\newif\ifgtt@vgrid@weekend
\gtt@vgrid@weekendfalse
\newcommand*{\gtt@vgridweek@assemblestyle}{%
    \ifgtt@vgrid\ifgtt@vgrid@weekend
        \pgfcalendarjuliantoweekday{\gtt@startjulian}{\@tempcntb}%
        \ifcase\@tempcntb
            \edef\gtt@vgridstyle{*4{\gtt@vgridstyle@week},*1{\gtt@vgridstyle@weekend},%
                *1{\gtt@vgridstyle@week},*1{\gtt@vgridstyle@weekend}}%
        \or
            \edef\gtt@vgridstyle{*3{\gtt@vgridstyle@week},*1{\gtt@vgridstyle@weekend},%
                *1{\gtt@vgridstyle@week},*1{\gtt@vgridstyle@weekend},*1{\gtt@vgridstyle@week}}%
        \or
            \edef\gtt@vgridstyle{*2{\gtt@vgridstyle@week},*1{\gtt@vgridstyle@weekend},%
                *1{\gtt@vgridstyle@week},*1{\gtt@vgridstyle@weekend},*2{\gtt@vgridstyle@week}}%
        \or
            \edef\gtt@vgridstyle{*1{\gtt@vgridstyle@week},*1{\gtt@vgridstyle@weekend},%
                *1{\gtt@vgridstyle@week},*1{\gtt@vgridstyle@weekend},*3{\gtt@vgridstyle@week}}%
        \or
            \edef\gtt@vgridstyle{*1{\gtt@vgridstyle@weekend},*1{\gtt@vgridstyle@week},%
                *1{\gtt@vgridstyle@weekend},*4{\gtt@vgridstyle@week}}%
        \or
            \edef\gtt@vgridstyle{*1{\gtt@vgridstyle@week},*1{\gtt@vgridstyle@weekend},%
                *4{\gtt@vgridstyle@week},*1{\gtt@vgridstyle@weekend}}%
        \or
            \edef\gtt@vgridstyle{*1{\gtt@vgridstyle@weekend},*4{\gtt@vgridstyle@week},%
                *1{\gtt@vgridstyle@weekend},*1{\gtt@vgridstyle@week}}%
        \fi
    \fi\fi
}

% color the weekend
\newcommand*{\gtt@weekend@draw}{%
    \def\@tempa{none}%
    \ifx\gtt@weekend@background\@tempa\else
        \pgfcalendarjuliantoweekday{\gtt@startjulian}{\@tempcntb}%
        \gtt@currgrid=1\relax%
        \global\advance\gtt@chartwidth by-1\relax%
        \foreach \x in {0,...,\gtt@chartwidth} {%
            \pgfmathsetcount{\@tempcnta}{mod(\x+\@tempcntb,7)}%
            \ifnum\@tempcnta>4\relax
                \expandafter\fill\expandafter[\gtt@weekend@background]
                    (\x * \ganttvalueof{x unit}, \y@upper pt) rectangle%
                    (\x * \ganttvalueof{x unit} + \ganttvalueof{x unit}, \y@lower pt);%
            \fi
        }%
        \global\advance\gtt@chartwidth by1\relax%
        % redraw the canvas node, but without filling it
        \node [/pgfgantt/canvas, minimum width=\x@size pt,
            minimum height=\y@size pt, fill=none]
            at (\x@size pt / 2, \y@mid pt) {};%

    \fi
}

%lines for milestones
\newif\ifgtt@milestone@line
\newcommand*{\gtt@milestone@lines}{}
\newcommand*{\gtt@store@ms@line}[1]{%
    \def\@tempa{#1}\def\@tempb{milestone}%
    \ifx\@tempa\@tempb
        \ifgtt@milestone@line
            \xdef\gtt@milestone@lines{\gtt@milestone@lines,\the\gtt@left@slot}%
        \fi
    \fi
}

\newcommand*{\gtt@draw@milestone@lines}{%
    \ifx\gtt@milestone@lines\@empty\else
        \expandafter\gtt@@draw@milestone@lines\gtt@milestone@lines\gtt@msl@end%
    \fi
}

\newcommand*{\gtt@@draw@milestone@lines}{}
\def\gtt@@draw@milestone@lines,#1\gtt@msl@end{%
    \foreach \x in {#1}{%
        \draw[/pgfgantt/milestone line style]
            (\x * \ganttvalueof{x unit}, \y@upper pt) --
            (\x * \ganttvalueof{x unit}, \y@lower pt);
    }
}

% fill the hooks
\newcommand*{\gtt@before@grid}{\gtt@vgridweek@assemblestyle\gtt@weekend@draw}
\newcommand*{\gtt@after@grid}{\gtt@draw@milestone@lines}

% options
\ganttset{%
    vgridweek/.code 2 args={%
        \gtt@vgridtrue\gtt@vgrid@weekendtrue
        \def\gtt@vgridstyle@week{#1}%
        \def\gtt@vgridstyle@weekend{#2}%
    },
    weekend background/.store in=\gtt@weekend@background,
    weekend background=none,
    milestone line/.is if=gtt@milestone@line,
    milestone line=false,
    milestone line style/.style={blue,thick},
}
\makeatother

\begin{document}

    \selectlanguage{ngerman}

    \begin{ganttchart}[
            hgrid,
            %vgrid={*4{dotted},{red,dotted},dotted,{red,dotted}},
            vgridweek={dotted}{red},
            weekend background=yellow,
            milestone line,
            %milestone line style/.style={cyan,line width=3pt},
            x unit=7mm,
            time slot format=little-endian,
            today={\the\day.\the\month.\the\year},
            progress=today,
            group incomplete/.append style={fill=gray},
            group left shift=0,
            group right shift=0,
            group height=.2,
            group peaks tip position=0,
            group peaks width=0.2,
            group peaks height=.2,
            %group label node/.append style={left=.6cm},
            %group progress label font=\bfseries\small,
        ]{18.09.2017}{20.10.2017}
        \gantttitlecalendar{year, month=name, weekday=letter, day} \\

        \ganttmilestone[progress label text={}]{Abgabe Schlussbericht}{05.10.2017} \\
        \ganttmilestone[progress label text={},milestone line=false]{Test-Milestone}{12.10.2017} \\
        \ganttmilestone[progress label text={}]{Abgabe Poster}{16.10.2017} \\
        % milestone lines are also drawn at start or end of the gnattchart
        %\ganttmilestone[progress label text={}]{Abgabe Poster}{20.10.2017} \\
        %\ganttmilestone[progress label text={}]{Abgabe Poster}{17.09.2017} \\

        \ganttgroup{Vorbereitung}{18.09.2017}{30.09.2017} \\
        \ganttbar{Projektauftrag verfassen}{20.09.2017}{24.09.2017} \\
        \ganttbar{Fragenkatalog erstellen}{22.09.2017}{27.09.2017} \\

        \ganttgroup{Wissensaufbau}{20.09.2017}{10.10.2017} \\
        \ganttbar{Bücher \& Papers suchen}{20.09.2017}{04.10.2017} \\
        \ganttbar{Konzepte erarbeiten}{28.09.2017}{08.10.2017} \\

        \ganttgroup{Evaluation des Konzepts}{05.10.2017}{17.10.2017} \\

        %\ganttgroup{Zwischenpräsentation}{30.10.2017}{24.11.2017} \\
        %\ganttgroup{Abschlusspräsentation}{18.12.2017}{19.01.2018} \\

    \end{ganttchart}
\end{document}

相关内容