Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to implement a gantt chart function inside pgfplots. To draw the tasks, I have a macro that calculates the start and finish time of a task. However, when I use multiple tasks, the values inside are overridden and as a result it only shows a single task.

\documentclass[10pt]{article}
\usepackage{pgfplots}

\begin{document}

\newcommand{\task}[7]{
    \pgfmathparse{#1/365+#2/12+#3}
    \edef\taskstart{\pgfmathresult}
    \pgfmathparse{#4/365+#5/12+#6}
    \edef\taskfinish{\pgfmathresult}

    \pgfmathparse{#7-0.4}
    \edef\bottom{\pgfmathresult}
    \pgfmathparse{#7+0.4}
    \edef\top{\pgfmathresult}

    \draw[blue,fill=blue!50!white] (axis cs:\taskstart,\bottom) rectangle (axis cs:\taskfinish,\top);
}

\begin{tikzpicture}
    \begin{axis}[xmin=2011,xmax=2013,ymin=0,ymax=10] 
        \task{26}{1}{2011}{23}{6}{2011}{1} % this is not drawn
        \task{26}{1}{2012}{23}{6}{2012}{3} % only this is drawn
    \end{axis}
\end{tikzpicture}

\end{document}

I know that this is a question of expansion, but I have to admit that I do not know how to handle it within the axis.

share|improve this question
    
Use directly pgfgantt package. –  percusse Sep 20 '12 at 18:02

1 Answer 1

up vote 5 down vote accepted

The problem arises as you correctly have noticed due to the expansion of the variables. pgfplots collects all statements until \end{axis} and then executes them. Thus you need to make sure that the commands that are to contain data are containing the correct data! Your problem is that the data \taskstart, \bottom, \taskfinish and \top are defined to be the last value. That is from \task<2>.

Remember to consider all commands in TikZ or pgfplots terms to be already fully expanded for you to get the correct plot.

You can solve the problem by using this \newcommand:

\newcommand{\task}[7]{
    \pgfmathparse{#1/365+#2/12+#3}
    \edef\taskstart{\pgfmathresult}
    \pgfmathparse{#4/365+#5/12+#6}
    \edef\taskfinish{\pgfmathresult}

    \pgfmathparse{#7-0.4}
    \edef\bottom{\pgfmathresult}
    \pgfmathparse{#7+0.4}
    \edef\top{\pgfmathresult}
    \edef\tmp{%
        \noexpand\draw[blue,fill=blue!50!white] (axis cs:\taskstart,\bottom) rectangle
        (axis cs:\taskfinish,\top);}
    \tmp
}

The resulting image will be:

enter image description here

share|improve this answer
    
Thank you very much, this solved the problem. –  Martin L. Sep 20 '12 at 23:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.