Thursday, 22 August 2013

How can I get a stacked bar with single values and sum on top.

How can I get a stacked bar with single values and sum on top.

I have a stacked pgfplot like this:
But I want the single values inside the bars and the total sum on top of
the bar (this graphic is 'handmade'):

How can I get this result?
My attempt up to now:
\documentclass[border=5mm] {standalone}
\usepackage{pgfplots, pgfplotstable}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title={Test stacked},
ybar stacked, ymin=0,
bar width=10mm,
symbolic x coords={a,b,c,d},
xtick=data,
nodes near coords,
nodes near coords align={anchor=north},%Move values in bar
every node near coord/.style={
},
]
%Active
\addplot [fill=blue] coordinates {
({a},15)
({b},25)
({c},35)
({d},15)};
%Inactive
\addplot [fill=red] coordinates {
({a},60)
({b},50)
({c},40)
({d},30)};
\legend{Active,Inactive}
\end{axis}
\end{tikzpicture}
\end{document}
This writes the added values inside the bar (not the single values) and
the total sum is missing.
The TeX-code is generated, it would be no problem for me to write the
total in my TeX-code, there is no need to calculate it with tikz.

No comments:

Post a Comment