1

I want to plot a stacked area chart. Stacked area chart => All values get stacked and the areas under the lines get's colored.

Something like this: enter image description here

My data in a minimalistic example:

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.12}
\usetikzlibrary{fillbetween}
\usepackage{filecontents}

\begin{filecontents*}{data.csv}
date,backlog,wip,finished
2015-01-06,54,27,3
2015-01-13,55,27,5
2015-01-20,55,27,5
2015-01-27,54,27,8
2015-02-03,54,27,8
2015-02-10,56,27,10
2015-02-17,56,25,12
2015-02-24,63,24,17
2015-03-02,63,21,17
2015-03-09,59,23,20
2015-03-16,59,25,21
2015-03-23,55,27,26
2015-03-30,55,30,26
2015-04-06,62,28,30
2015-04-13,62,28,30
2015-04-20,65,22,40
2015-04-27,65,22,40
2015-05-04,61,22,44
2015-05-11,61,20,47
2015-05-18,60,21,50
2015-05-25,59,21,50
\end{filecontents*}
\begin{document}
    \begin{tikzpicture}[]
    \begin{axis}[
    date coordinates in=x, 
    table/col sep=comma, 
    date ZERO=2015-01-06, 
    xticklabel={\day.\month.\year}, 
    xticklabel style={rotate=90, anchor=near xticklabel},
    xmin={2015-01-06}, 
    xmax={2015-05-25},
    ymin=0,
    ymax=140,
    max space between ticks=20
    ]

    \addplot table [mark=none,x=date,y=backlog] {data.csv};
    \addplot table [mark=none,x=date,y=wip] {data.csv};
    \addplot table [mark=none,x=date,y=finished] {data.csv};

    \end{axis}
    \end{tikzpicture}
\end{document}
Stefan Pinnow
  • 29,535
Spenhouet
  • 952

1 Answers1

4

In parameters of your axes you need to add area style and each plot end width \closedcycle:

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.12}
\usetikzlibrary{fillbetween}
\usepackage{filecontents}

\begin{filecontents*}{data.csv}
date,backlog,wip,finished
2015-01-06,54,27,3
2015-01-13,55,27,5
2015-01-20,55,27,5
2015-01-27,54,27,8
2015-02-03,54,27,8
2015-02-10,56,27,10
2015-02-17,56,25,12
2015-02-24,63,24,17
2015-03-02,63,21,17
2015-03-09,59,23,20
2015-03-16,59,25,21
2015-03-23,55,27,26
2015-03-30,55,30,26
2015-04-06,62,28,30
2015-04-13,62,28,30
2015-04-20,65,22,40
2015-04-27,65,22,40
2015-05-04,61,22,44
2015-05-11,61,20,47
2015-05-18,60,21,50
2015-05-25,59,21,50
\end{filecontents*}
\begin{document}
    \begin{tikzpicture}[]
    \begin{axis}[
    date coordinates in=x,
    table/col sep=comma,
    date ZERO=2015-01-06,
    xticklabel={\day.\month.\year},
    xticklabel style={rotate=90, anchor=near xticklabel},
    xmin={2015-01-06},
    xmax={2015-05-25},
    ymin=0,
    ymax=140,
    max space between ticks=20,
    stack plots=y,%   
    area style,
    ]
\addplot table [mark=none,x=date,y=backlog] {data.csv}
    \closedcycle;
\addplot table [mark=none,x=date,y=wip] {data.csv}
    \closedcycle;
\addplot table [mark=none,x=date,y=finished] {data.csv}
    \closedcycle;
    \end{axis}
    \end{tikzpicture}
\end{document}

Library fillbetween is not necessary, but if it is present it is not harmful.

Edit: If you like to change list of area colors, than do the following:

\pgfplotsset{
/pgfplots/area cycle list/.style={/pgfplots/cycle list={%
{red,fill=blue!30!white,mark=none},%
{blue,fill=red!30!white,mark=none},%
{yellow!60!black,fill=yellow!30!white,mark=none},%
{black,fill=gray,mark=none},
}
},
}

From this list follows, that in \addplot is not necessary add parameter: marks=none. It is sufficient:

\addplot table [x=date,y=backlog] {data.csv}
    \closedcycle;
\addplot table [x=date,y=wip] {data.csv}
    \closedcycle;
\addplot table [x=date,y=finished] {data.csv}
    \closedcycle;
Zarko
  • 296,517