2

I am trying to draw a pie chart from a csv file using pgf-pie. And it must be pdf-pie, because datapie does not work for me under Debian 11. It did work under Debian 10. So I'm trying pgf-pie and I'm not interested in other solutions with other packages.

\documentclass{article}

\usepackage{csvsimple}
\usepackage{pgf-pie}

\begin{document}
\begin{filecontents*}{fruit.csv}
Name,Quantity
"Apples",30
"Pears",25
"Lemons,Limes",40.5
"Peaches",34.5
"Cherries",20
\end{filecontents*}
\csvreader[]{fruit.csv}{Name = \Name, Quantity = \Quantity}{\Quantity/\Name,}

\begin{tikzpicture}
    \pie[polar, explode=0.1]{
        \csvreader[]{fruit.csv}{Name = \Name, Quantity = \Quantity}{\Quantity/\Name,}
    }
\end{tikzpicture}

\end{document}

The first \csvreader prints

30/”Apples”,25/”Pears”,34.5/”Peaches”,20/”Cherries”,

But that is wrong, because it doesn't print 40.5/"Lemons,Limes".

And the second \csvreader hangs pdflatex for ever with this error message:

! Use of \csv@reader doesn't match its definition.
\@ifnextchar ...eserved@d =#1\def \reserved@a {#2}
                                                  \def \reserved@b {#3}\futu...
l.2091  }

Why does this not work?

B. A. Sylla
  • 419
  • 3
  • 13

1 Answers1

2

For the first part the problem is that , is normally used as a separation character in the csv file, so you need to protect it inside a group:

\documentclass{article}

\usepackage{csvsimple}
\usepackage{pgf-pie}

\begin{filecontents*}[overwrite]{fruit.csv}
Name,Quantity
"Apples",30
"Pears",25
"Lemons{,}Limes",40.5
"Peaches",34.5
"Cherries",20
\end{filecontents*}

\begin{document}

\csvreader[]{fruit.csv}{Name = \Name, Quantity = \Quantity}{\Quantity/\Name,}


\end{document}

enter image description here

or you could use another separator to avoid the conflict:

\documentclass{article}

\usepackage{csvsimple}
\usepackage{pgf-pie}

\begin{filecontents*}[overwrite]{fruit.csv}
Name;Quantity
"Apples";30
"Pears";25
"Lemons,Limes";40.5
"Peaches";34.5
"Cherries";20
\end{filecontents*}

\begin{document}

\csvreader[separator=semicolon]{fruit.csv}{Name = \Name, Quantity = \Quantity}{\Quantity/\Name,}


\end{document}

And for the pie chart problem, you don't actually need the pgf-pie package. Instead you could use the example from the csvsimple documentation:

\documentclass{article}

\usepackage{csvsimple}
\usepackage{pgf-pie}

\begin{filecontents*}[overwrite]{fruit.csv}
Name,Quantity
"Apples",30
"Pears",25
"Lemons{,}Limes",40.5
"Peaches",34.5
"Cherries",20
\end{filecontents*}

\begin{document}

% From the csvsimple doc:

% This example needs the packages tikz, xcolor, calc
\definecolorseries{myseries}{rgb}{step}[rgb]{.95,.85,.55}{.17,.47,.37}
\resetcolorseries{myseries}%

% a pie slice
\newcommand{\slice}[4]{
  \pgfmathsetmacro{\midangle}{0.5*#1+0.5*#2}
  \begin{scope}
    \clip (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;
    \colorlet{SliceColor}{myseries!!+}%
    \fill[inner color=SliceColor!30,outer color=SliceColor!60] (0,0) circle (1cm);
  \end{scope}
  \draw[thick] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;
  \node[label=\midangle:#4] at (\midangle:1) {};
  \pgfmathsetmacro{\temp}{min((#2-#1-10)/110*(-0.3),0)}
  \pgfmathsetmacro{\innerpos}{max(\temp,-0.5) + 0.8}
  \node at (\midangle:\innerpos) {#3};
}

% sum of amounts
\csvreader[before reading=\def\mysum{0}]{fruit.csv}{Quantity=\Quantity}{%
  \pgfmathsetmacro{\mysum}{\mysum+\Quantity}%
}

% drawing of the pie chart
\begin{tikzpicture}[scale=3]%
\def\mya{0}\def\myb{0}
\csvreader[head to column names]{fruit.csv}{}{%
  \let\mya\myb
  \pgfmathsetmacro{\myb}{\myb+\Quantity}
  \slice{\mya/\mysum*360}{\myb/\mysum*360}{\Quantity}{\Name}
}
\end{tikzpicture}%

\end{document}

enter image description here