Use macro to draw inside pgfplot/axis

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}

\pgfmathparse{#1/365+#2/12+#3}
\pgfmathparse{#4/365+#5/12+#6}

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

}

\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.

-
Use directly pgfgantt package. – percusse Sep 20 '12 at 18:02

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}
\pgfmathparse{#4/365+#5/12+#6}

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