1

I have a stacked bar chart and I would like to have the summation values displayed instead of the increment.

I have the following code:

\documentclass[border=0mm]{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{width=7cm,compat=newest}


\begin{document}

    \begin{tikzpicture}

\pgfplotstableread{

x Order                               05  1  15
%5mm----------------
0 5 4 39 31 
%4mm----------------
1 4 4 44 34 
%3mm----------------
2 3 4 53 38
%2mm----------------
3 2 5 73 22
%1.5mm----------------
4 1.5 7 85 8
%1mm----------------
5 1 12 83 5
%0.75mm----------------
6 0.75 17 79 4
%0.6mm----------------
7 0.6 21 75 4
}\datatable

\begin{axis}[
%    x post scale=\linewidth/\axisdefaultwidth,
    xlabel=x-Axis {[}\%{]},
    ytick=data,
    yticklabels from table={\datatable}{Order},
    height=6cm,
    width=8cm,
    xbar stacked,
    xmin=0,
    xmax=100,
    ymin=-1.5,
    ymax=8.5,
    ylabel=y-Axis {[}mm{]},
    area style,
    legend style={area legend,at={(0.5,-0.3)},anchor=north,legend columns=-1},
every node near coord/.style={
        check for zero/.code={
        \pgfkeys{/pgf/fpu=true}
        \pgfmathparse{\pgfplotspointmeta-4}
        \pgfmathfloatifflags{\pgfmathresult}{-}{
           \pgfkeys{/tikz/coordinate}
        }{}
        \pgfkeys{/pgf/fpu=false}
      }, check for zero, xshift=0, font=\footnotesize},
]

\addplot+[nodes near coords] table [x=05, y=x] {\datatable};
\addplot+[nodes near coords] table [x=1, y=x] {\datatable};
\addplot+[nodes near coords] table [x=15, y=x] {\datatable};

\end{axis}  

    \end{tikzpicture}


\end{document}

And that is the current result:

enter image description here

The desired result would look something like this:

enter image description here

It would also be nice if the total value is 100%, that it is not displayed. Thanks!

1

You almost had it, but only needed to calculate the values which you want to show in the nodes near coords.

For details on how to do this please have a look at the comments in the code.

% used PGFPlots v1.15
    % because of a bug in PGFPlots v1.15 and earlier you can't use a "loaded table"
    % to achieve what you want. So store the data in a file instead
    % <https://sourceforge.net/p/pgfplots/bugs/109/>
    \begin{filecontents*}{data.txt}
        x Order 05  1  15
        0 5     4   39 31
        1 4     4   44 34
        2 3     4   53 38
        3 2     5   73 22
        4 1.5   7   85 8
        5 1     12  83 5
        6 0.75  17  79 4
        7 0.6   21  75 4
    \end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        % use this `compat' level or higher to use the new feature of centering
        % the `nodes near coords' text
        compat=1.9,
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xmin=0,
        xmax=100,
        ymin=-1.5,
        ymax=8.5,
        xlabel=$x$-axis in \%,
        ylabel=$y$-axis in mm,
        ytick=data,
        yticklabels from table={data.txt}{Order},
        xbar stacked,
        every node near coord/.style={
            % adapted style to your needs to not show values equal or greater than 100
            check for hundert/.code={
                \pgfkeys{/pgf/fpu=true}
                \pgfmathparse{\pgfplotspointmeta-99}
                \pgfmathfloatifflags{\pgfmathresult}{+}{
                    \pgfkeys{/tikz/coordinate}
                }{}
                \pgfkeys{/pgf/fpu=false}
            },
            check for hundert,
            font=\footnotesize,
        },
        % moved common options here
        nodes near coords,
        table/y=x,
    ]
        \addplot table [x=05] {data.txt};
        \addplot table [
            x=1,
            % calculate the sum of the two values which you want to show
            % in the `nodes near coords'
            point meta=\thisrow{05} + \thisrow{1},
        ] {data.txt};
        \addplot table [
            x=15,
            point meta=\thisrow{05} + \thisrow{1} + \thisrow{15},
        ] {data.txt};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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