3

First, I started using LaTeX way too late! Thanks for all the hints on this site.

I looked at the answer of the following question. How to draw Bar & Pie Chart

This chart uses the value 100 as maxvalue. I want to change it a bit, it should sum up all parameters and use that as the new 100%

Example of a pie chart (sum of 922 as 100%)

\pie{Pie XYZ}
{337/div,196/drog,191/verm,118/gew,47/staa}

Here is the original script with my idea in the comment. To be frank I have never use tex-script before, but just with googling I couldn't manage to get what I want.

Any help for a LaTeX noob higly appreciated.

\newcommand{\pie}[3][]{
    \begin{scope}[#1]
    \pgfmathsetmacro{\curA}{90}
    \pgfmathsetmacro{\r}{1}
    \def\c{(0,0)}
    \node[pie title] at (90:1.3) {#2};

% Sum all parameters for max value   
%   \newcounter{tmpSUM}
%   \foreach \v / \s in{#3}{    
%       \setcounter{tmpSUM}{\v}
%   }

    \foreach \v/\s in{#3}{
%       \pgfmathsetmacro{\deltaA}{\v / tmpSUM *360}
        \pgfmathsetmacro{\deltaA}{\v / 100 *360}

        \pgfmathsetmacro{\nextA}{\curA + \deltaA}
        \pgfmathsetmacro{\midA}{(\curA+\nextA)/2}

        \path[slice,\s] \c
            -- +(\curA:\r)
            arc (\curA:\nextA:\r)
            -- cycle;
       \pgfmathsetmacro{\d}{max((\deltaA * -(.5/50) + 1) , .5)}

        \begin{pgfonlayer}{foreground}
        \path \c -- node[pos=\d,pie values,values of \s]{$\v\%$} +(\midA:\r);
        \end{pgfonlayer}

        \global\let\curA\nextA
    }
    \end{scope}
}
1
  • Hate to be that guy, but Pie charts are terrible data visualization tools. stick with a horizontal bar chart, sorted from largest to smallest. Much easier to read and compare values. Commented Mar 1, 2021 at 3:16

3 Answers 3

3

Your commented solution is closer to the working one. The idea of looping through the values and accumulating the sum in tmpSUM is quite right but:

  1. you may want to use \pgfmath to support non-integer values
  2. the body of the \foreach is in a group so you need global assignments to make the value of the counter escape the scope of the for and be available afterwards (see this answer).

This can be accomplished by

\pgfmathsetmacro{\tmpSUM}{0}
\foreach \v/\s in{#3}{
    \pgfmathparse{\tmpSUM+\v}
    \global\let\tmpSUM\pgfmathresult
}

and then you can use

\pgfmathsetmacro{\deltaA}{\v / \tmpSUM * 360}
1
  • thx, will try it out! Commented Feb 3, 2014 at 13:28
6

A possible solution using PSTricks with siunitx is as follows:

\documentclass{article}

\usepackage{pstricks-add}
\usepackage[
  round-mode = places,
  round-precision = 0
]{siunitx}
\usepackage{xfp}

\newcommand*\total{\fpeval{\honey+\pollen+\water}}
\newcommand*\Honey{\fpeval{\honey/\total*100}}
\newcommand*\Pollen{\fpeval{\pollen/\total*100}}
\newcommand*\Water{\fpeval{\water/\total*100}}

\def\pieSlice[#1](#2)#3#4#5{%
  \rput(psChartI#2){\shortstack[c]{%
      \SI{#3}{\g}\strut\\[-1ex]
      \SI{#4}{\percent}\strut}}
  \ncline{psChartO#2}{psChart#2}
  \nput{#1}{psChartO#2}{#5}}

% parameters
\def\honey{1671}
\def\pollen{1015}
\def\water{621}

\begin{document}

\psset{nodesepB = -5pt}
\begin{pspicture}(-3,-3.5)(4.1,4.1)
  \psChart[
    userColor = {red!60, green!60, blue!60},
    chartNodeO = 1.2,
    shadow = true,
    shadowsize = 5pt,
    linewidth = 0
  ]{\honey,\pollen,\water}{}{3}
  \pieSlice[90](1){\honey}{\Honey}{Honey}
  \pieSlice[-90](2){\pollen}{\Pollen}{Pollen}
  \pieSlice[0](3){\water}{\Water}{Water}
\end{pspicture}

\end{document}

output

Update

I think I might have misunderstood your request; here is a version showing only the absolute values:

\documentclass{article}

\usepackage{pstricks-add}
\usepackage{siunitx}

\def\pieSlice[#1](#2)#3#4{%
  \rput(psChartI#2){\SI{#3}{\g}}
  \ncline{psChartO#2}{psChart#2}
  \nput{#1}{psChartO#2}{#4}}

% parameters
\def\honey{1671}
\def\pollen{1015}
\def\water{621}

\begin{document}

\begin{pspicture}(-3,-3.5)(4.1,4.1)
  \psChart[
    userColor = {red!60, green!60, blue!60},
    chartNodeO = 1.2,
    shadowsize = 5pt,
    shadow = true,
    linewidth = 0
  ]{\honey,\pollen,\water}{}{3}
  \pieSlice[90](1){\honey}{Honey}
  \pieSlice[-90](2){\pollen}{Pollen}
  \pieSlice[0](3){\water}{Water}
\end{pspicture}

\end{document}

output2

1
  • 1
    looks like a solution too, but i want to publish multiple pies in a row, therefore i go furter with the existing solution instead of a complete new one. thanks anyway, +1 Commented Feb 3, 2014 at 15:28
2

The wheelchart package, which I wrote, can be used.

The lines are obtained with the key lines.

The values are given by the first variable \WCvarA. These are placed in the slices with the key wheel data=\WCvarA.

enter image description here

\documentclass[border=6pt]{standalone}
\usepackage{wheelchart}
\begin{document}
\begin{tikzpicture}
\wheelchart[
  lines,
  pie,
  wheel data=\WCvarA
]{%
  337/orange/div,
  196/green/drog,
  191/magenta/verm,
  118/red/gew,
  47/blue/staa%
}
\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .