堆积条形图中的总和值而不是增量

堆积条形图中的总和值而不是增量

我有一个堆积条形图,我想显示总和值而不是增量。

我有以下代码:

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

这就是目前的结果:

在此处输入图片描述

期望的结果看起来应该是这样的:

在此处输入图片描述

如果总价值为 100%,则不显示该值,这样就更好了。谢谢!

答案1

您几乎已经知道了,但只需要计算您想要在 中显示的值nodes near coords

有关如何执行此操作的详细信息,请查看代码中的注释。

% 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}

该图显示了上述代码的结果

相关内容