5

Based on a previous approach from Gonzalo Medina, I want to modify a simple gantt-chart to add the responsibilities of project workers to the belonging \ganttbar.


Screenshot of the current state:

Example image


The challenge

Depending on the length of the inserted text, the bar can look very ugly (e.g. in case of "Sandra, Michael, Thomas"). Now my idea is to provide additional positioning options to specify the position of those "extra labels" above or below their belonging bar.

Due to the available progress-option of regular bars, it would be very nice to combine all options together in one command. The dream result could look like that:

\textganttbar[progress=00, name=test1]{Text in outside label}{Text in bar}{3}{8}

How could I get such a behaviour?

A different option from Qrrbrbirlbel unfortunately doesn't provide the necessary indepencence from the progress-label.


Minimum Working Example (MWE):

\documentclass{standalone}
\usepackage{pgfgantt}

\newcommand\textganttbar[4]{%
    \ganttbar{#1}{#3}{#4}
    \ganttbar[inline,bar label font=\footnotesize]
    {#2}{#3}{#4}
}

\begin{document}
    \begin{ganttchart}{1}{12}
        \gantttitle{2011}{12} \\
        \gantttitlelist{1,...,12}{1} \\

        \textganttbar{Task 1}{Lisa, Harry}{1}{5} \\
        \textganttbar{Task 2}{Jenny, George}{2}{6} \\
        \textganttbar{Task 3}{Lindy, Rob}{8}{12}\\
        \textganttbar{Task 4}{Sandra, Michael, Thomas}{7}{10}\\

        \ganttbar[progress=40]{Task 4}{3}{8}\\

%       Dream solution:
%       \textganttbar[progress=00, name=element1]{Task 4}{Worker names}{3}{8}
    \end{ganttchart}
\end{document}
TeXnician
  • 33,589
Dave
  • 3,758

1 Answers1

8

Here are two possible definitions of \textganttbar. In both cases the names are set in a node, instead of an inline bar. And in both cases the (first) optional argument goes to the \ganttbar, so you can use progress=X,name=Y as usual. I can't say the placement of the node is done very elegantly though. It is done by appending alias=tmp to the bar style, and then using tmp as a coordinate.

The first one is the least flexible. What it does is to measure the length of the workers' names, and if it is longer than the length of the bar, the names are placed above the bar.

The second one is more flexible, in that it has a second optional argument that goes to the style of the node. I also defined two additional styles, above bar and below bar, intended to do exactly what they say. They make use of the alias defined for the bars.

output of code

\documentclass{standalone}
\usepackage{pgfgantt}
\usepackage{xparse}

\newcommand\textganttbar[5][]{%
    \ganttbar[#1,bar/.append style={alias=tmp}]{#2}{#4}{#5}
    \path 
    let
    \p1=(tmp.west),\p2=(tmp.east),
    \n1={\x2-\x1},\n2={width("#3")},
    \n3={ifthenelse(\n1>\n2,90,270)}
    in
    node [anchor=\n3,font=\footnotesize] at (tmp.north) {#3};
}

\NewDocumentCommand\textganttbarB{O{} O{} mmmm}{%
    \ganttbar[#1,bar/.append style={alias=tmp}]{#3}{#5}{#6}
    \node [font=\footnotesize,at={(tmp)},#2]  {#4};
}

\tikzset{
  above bar/.style={
    at={(tmp.north)},anchor=south
    },
  below bar/.style={
    at={(tmp.south)},anchor=north
    }
}

\begin{document}
    \begin{ganttchart}{1}{12}
        \gantttitle{2011}{12} \\
        \gantttitlelist{1,...,12}{1} \\

        % if you only supply one optional argument to \textganttbarB, that goes to
        % the \ganttbar. Hence, if you only want to pass arguments to the node,
        % you need an empty set of brackets
        \textganttbarB[][above bar,blue,fill=red]{Task 1}{Lisa, Harry}{1}{5} \\
        \textganttbarB[name=t2,progress=50]{Task 2}{Jenny, George}{2}{6} \\

        \textganttbar[name=t3]{Task 3}{Lindy, Rob}{8}{12}\\
        \textganttbar[progress=20]{Task 4}{Sandra, Michael, Thomas}{7}{10}\\

        \ganttbar[progress=40]{Task 4}{3}{8}\\

    \end{ganttchart}
\end{document}
Torbjørn T.
  • 206,688
  • Thank you very much Torbjørn! The second solution is really great! In combination with a little yshift it is easily possible to adjust the names to the bar - nice! – Dave Nov 27 '17 at 02:17