自动将甘特图扩展至页面的文本宽度

自动将甘特图扩展至页面的文本宽度

我希望几乎每个甘特图都适合自动地到页面的文本宽度。这个问题中的大词是自动的。这就是它与其他类似问题的不同之处。

文字宽度时隙数图表中未指定。解决方案应足够灵活以处理此问题。

也许有解决办法计算 x unit?下面的 MWE 有 122 个时隙。所以我会设置x unit\textwidth divided by 122。有办法吗?

\documentclass{article}
\usepackage[showframe]{geometry} % show page borders
\usepackage{pgfgantt}

\begin{document}
\noindent
\begin{ganttchart}
    [
        x unit=1.23mm,              % timeslot width
        time slot format=isodate    % Datumsformat
    ]
    {2017-04-01}{2017-07-31}        % from-to (122 days)

    \gantttitle{Gantt-Chart with 122 timeslots}{122}\\
    \ganttbar{Short}{2017-05-01}{2017-06-01}\\
    \ganttbar{Long Long}{2017-05-25}{2017-06-04}\\
\end{ganttchart}
\end{document}

答案1

根据聊天讨论,这是艾伦答案的改编,实现了他试图实现的语法。

% addaswyd o ateb Alan Munn: https://tex.stackexchange.com/questions/364944/expand-a-gantt-chart-automatically-to-textwidth-of-a-page/364969#364969 a chwestiwn buhtz: https://tex.stackexchange.com/questions/364944/expand-a-gantt-chart-automatically-to-textwidth-of-a-page
\documentclass[a4paper]{article}
\usepackage[showframe]{geometry} % show page borders
\parindent=0pt
\usepackage{pgfgantt}
\makeatletter
\tikzset{%
  /pgfgantt/time slots/.code={%
    \tikzset{%
      /pgfgantt/time slots/.cd,
      #1,
      /pgfgantt/.cd,
    }%
  },
  /pgfgantt/time slots/.search also={/pgfgantt},
  /pgfgantt/time slots/.cd,
  width/.store in=\ts@width,
  width=\textwidth,
  slots/.store in=\totaltimeslots,
  slots=20,
  label width/.store in=\ts@labelwidth,
  label width=25mm,
  calc x unit/.code n args=3{%
    \pgfmathsetmacro\ts@xunit{(#1-#2-0.6667em-2*\pgflinewidth)/#3}%
    \tikzset{%
      /pgfgantt/x unit=\ts@xunit pt,
    }%
  },
  widest/.code={%
    \pgfmathsetmacro\ts@wdlabel{width("#1")}%
    \tikzset{/pgfgantt/time slots/label width=\ts@wdlabel pt}%
  },
  calc x unit aux width/.style={/pgfgantt/time slots/calc x unit={#1}{\ts@labelwidth}{\totaltimeslots}},
  calc x unit aux label width/.style={/pgfgantt/time slots/calc x unit={\ts@width}{#1}{\totaltimeslots}},
  calc x unit aux slots/.style={/pgfgantt/time slots/calc x unit={\ts@width}{\ts@labelwidth}{#1}},
  width/.forward to=/pgfgantt/time slots/calc x unit aux width,
  slots/.forward to=/pgfgantt/time slots/calc x unit aux slots,
  label width/.forward to=/pgfgantt/time slots/calc x unit aux label width,
}
\makeatother
\begin{document}
\begin{ganttchart}
    [
        time slots={slots=122, widest=Long Long},
        time slot format=isodate,
    ]
    {2017-04-01}{2017-07-31}        % from-to (122 days)

    \gantttitle{Gantt-Chart with 122 timeslots}{\totaltimeslots}\\
    \ganttbar{Short}{2017-05-01}{2017-06-01}\\
    \ganttbar{Long Long}{2017-05-25}{2017-06-04}\\
\end{ganttchart}

\begin{ganttchart}
    [
        time slots/slots=15,
        time slots/widest=Foo,
        time slots/width=.9\linewidth,
        time slot format=isodate,
    ]
    {2017-04-01}{2017-04-15}        % from-to (122 days)
    \gantttitle{Gantt-Chart with 15 timeslots}{15}\\
    \ganttbar{Foo}{2017-05-25}{2017-06-04}
\end{ganttchart}
\end{document}

答案2

使用calc包根据需要设置长度。由于需要在命令中手动设置时间段数\ganttitle,因此我创建了一个ganttchart样式timeslots,该样式以图表的时间段数和最长\gantttitle标签的文本(逗号分隔)作为参数,然后使用该文本计算宽度。它还设置了一个宏的值\timeslots,然后可以在命令中使用\gantttitle

我已将代码添加为ganttchart样式,这样就可以指定它,而不必像上一版本那样使用自己的宏(有关详细信息,请参阅编辑历史记录)。我还为您希望自动调整大小的宽度添加了长度。对于您的特定示例,它设置为\textwidth,但这允许您将其设置为您喜欢的任何宽度。

用法:[ ..., timeslots={<number>,<text of longest label>}, ... ]

\usepackage[]{geometry} % show page borders
\parindent=0pt
\usepackage{pgfgantt}
\usepackage{calc}
\newlength{\myunitx}
\newlength{\autosizewidth}
\setlength{\autosizewidth}{\textwidth} % this allows you to change the value for autosizing
\newcommand*{\timeslots}{}
\ganttset{
timeslots/.code args={#1,#2}{\setlength{\myunitx}{(\autosizewidth-\widthof{#2}-1em)/#1}
                 \renewcommand{\timeslots}{#1}\pgfkeys{pgfgantt/x unit=\myunitx}}
}
\begin{document}

\begin{ganttchart}
    [
        timeslots={122,Long Long},  % timeslots
        time slot format=isodate    % Datumsformat
    ]
    {2017-04-01}{2017-07-31}        % from-to (122 days)

    \gantttitle{Gantt-Chart with 122 timeslots}{\timeslots}\\
    \ganttbar{Short}{2017-05-01}{2017-06-01}\\
    \ganttbar{Long Long}{2017-05-25}{2017-06-04}\\
    \end{ganttchart}

\begin{ganttchart}
    [
        timeslots={15,Foo},         % timeslots
        time slot format=isodate    % Datumsformat
    ]
    {2017-04-01}{2017-04-15}        % from-to (122 days)
    \gantttitle{Gantt-Chart with 15 timeslots}{15}\\
    \ganttbar{Foo}{2017-05-25}{2017-06-04}
\end{ganttchart}
\end{document}

代码输出

相关内容