2

I have to include quite small values into my pie chart. Unfortunately the values are quite difficult to decipher. Is there an easy way to set small values (or all values) outside the pie and connect the values and the slices with a string?

\documentclass[
a4paper,
captions=tableheading
]
{scrbook}
%%%
\usepackage[
backend=biber %%%% !!! BIBER !!!
]{biblatex}
\usepackage{caption}
\usepackage{tikz}   
\usepackage{pgf-pie}        
\usepackage{pgfplots}
\usepackage{booktabs}                   
\usepackage{pgfplotstable}
\usepackage{siunitx}                            
\usetikzlibrary{patterns}
\usepgfplotslibrary{statistics} 
\sisetup{output-decimal-marker={,},group-separator={\,},}
\def\ScanPercentage#1\afternumber{\SI{#1}{\percent}}
%%%
\begin{document}
\begin{figure}
\textcolor{black!50}{\rule{\linewidth}{0.25pt}}
\begin{center}
\begin{tikzpicture}
\pie[
before number=\ScanPercentage,
after number={},
radius=3.00,  
text=legend, 
rotate=0,
pie values/.style={font={\scriptsize}},
pie legend/.style={font={\scriptsize}}
]
{
38.3/car,
25.0/aircraft,
13.3/boat,
6.7/horse,
1.7/goat,
0.8/ostrich,
14.2/na
}
\end{tikzpicture}
\end{center}
\textcolor{black!50}{\rule{\linewidth}{0.25pt}}
\vspace*{-10pt}
\caption[Pie Chart]
{\textbf{Pie Chart}}
\end{figure}
\end{document}  

In addition to that: Does a predefined "schema" exist, where tikz defines automatically grayscale "colours" for filling the different slices?

TRJW
  • 450

1 Answers1

4

I only can answer the second part of the question. Torbjørn T. kindly informed me that there is a manual. I do not think one can easily move the numbers out without rewriting the macro. The second part of your question is easy: just add a list of colors after color=. In what follows, there is an ad hoc attempt of redefining the macro for the slices. The result is far from perfect. I am certainly not a TikZ expert, so what I am saying now has to be taken with a grain of salt. Yet I can't help from noting that many of the things done in that code could be done in an arguably more elegant and flexible way. I just do a minimal damage repair here. There is a key Move Out, which, if it is different from 1, will move the numbers out. The result is far from optimal, also because I continue to hard code things. I have seen much nicer pie charts on this site like e.g. this one, so to me the most promising way to go seems to be to adapt one of these.

\documentclass[
a4paper,
captions=tableheading
]
{scrbook}
\usepackage{caption}
\usepackage{tikz}   
\usepackage{pgf-pie}        
\usepackage{siunitx}                            
\sisetup{output-decimal-marker={,},group-separator={\,},}
\def\ScanPercentage#1\afternumber{\SI{#1}{\percent}}
%%%
\newif\ifOut
%\Debugtrue
\Outfalse
\pgfkeys{/pgf/.cd,
Move Out/.initial=1,
Move Out=1,
Minimum Angle/.initial=0}
\newcounter{myslice}
\makeatletter
\renewcommand{\pgfpie@slice}[8]{\stepcounter{myslice}
  \pgfmathparse{0.5*(#1)+0.5*(#2)}
  \let\midangle\pgfmathresult

  \path (#8) -- ++(\midangle:#5) coordinate(O);

  \pgfmathparse{#7+#5}
  \let\radius\pgfmathresult

  \pgfmathsetmacro{\SwitchOff}{ifthenelse(#2-#1<\pgfkeysvalueof{/pgf/Minimum
  Angle},1,0)}

  % slice
  \draw[line join=round, fill=#6, \style] (O) -- ++(#1:#7) arc (#1:#2:#7) -- cycle;

  \pgfmathparse{min(((#2)-(#1)-10)/110*(-0.3),0)}
  \let\temp\pgfmathresult
  \pgfmathtruncatemacro{\itest}{ifthenelse(\pgfkeysvalueof{/pgf/Move Out}==1,1,0)}
  \ifnum\itest=1
  \pgfmathsetmacro\innerpos{((max(\temp,-0.5)+0.8)*#7)}
  \else
  \pgfmathsetmacro\innerpos{\pgfkeysvalueof{/pgf/Move Out}*#7*(1+0.2*sin(90*\themyslice))}
  \fi
  \ifthenelse{\equal{\pgfpie@text}{inside}}
  {
    % label and number together
    \path (O) -- ++(\midangle:\innerpos) node
    {\scalefont{#3}\shortstack{#4\\\beforenumber#3\afternumber}};
  }
  {
    % label
    \iflegend
    \else
    \path (O) -- ++ (\midangle:\radius)
    node[inner sep=0, \pgfpie@text=\midangle:#4]{};
    \fi

    % number
    \ifnum\SwitchOff=1
    \else
     \ifnum\itest=1
      \path (O) -- ++(\midangle:\innerpos) node
      {\scalefont{#3}\beforenumber#3\afternumber};
     \else
      \path (O) -- ++(\midangle:\innerpos)  node (pie-N-\themyslice)
      {\scalefont{#3}\beforenumber#3\afternumber};
      \draw[latex-] (\midangle:{0.85*#7}) -- (pie-N-\themyslice);
     \fi
    \fi
  }
}
\makeatother
\begin{document}
\begin{figure}
\textcolor{black!50}{\rule{\linewidth}{0.25pt}}
\begin{center}
\begin{tikzpicture}
\pie[
before number=\ScanPercentage,
after number={},
radius=3.00,  
text=legend, 
rotate=0,
/pgf/Move Out=1.5,
/pgf/Minimum Angle=8,
color={gray!10,gray!20,gray!30,gray!40,gray!50,gray!60,gray!70},
pie values/.style={font={\scriptsize}},
pie legend/.style={font={\scriptsize}}
]
{
38.3/car,
25.0/aircraft,
13.3/boat,
6.7/horse,
1.7/goat,
0.8/ostrich,
14.2/na
}
\path (0,6);
\end{tikzpicture}
\end{center}
\textcolor{black!50}{\rule{\linewidth}{0.25pt}}
\vspace*{-10pt}
\caption[Pie Chart]
{\textbf{Pie Chart}}
\end{figure}
\end{document}  

enter image description here

As for the things raised in the comments:

  1. I added a key Minimum Angle. Whenever the angle of a segment is smaller that this angle, the label will be supressed. For Minimum Angle=8, this kills 0,8% and 1,7%.
  2. I added \path (0,6);, which moves the pie a bit down. If you use \path (0,7);, the pie will move further down.
  • 3
    Not sure what you mean by official, but pgf-pie is on CTAN (manual there as well) and in MikTeX. It's not in TeX Live however, I think because it doesn't have a license. – Torbjørn T. Sep 16 '18 at 19:53
  • @TorbjørnT. I see. Thanks! I didn't know that. –  Sep 16 '18 at 20:25
  • The linked pie chart being “nicer” is rather subjective. In my opinion it is much worse because the additional 3D, drop shadow, and color shading adds absolutely zero information to the chart. – Henri Menke Sep 17 '18 at 02:00
  • @HenriMenke As you say, it is subjective. I was actually referring to the code. However, if you find a nicer way to repair this style file, I will be happy to remove my answer. Of course, if you want to complain that some well-structured code also achieves some features that you do not regard necessary, I do not have anything intelligent to say about that. –  Sep 17 '18 at 02:09
  • Is it possible to let the values 0,8% and 1,7% disappear!? (opacity=0)? I am using now "text=pin" and so I could put the values behind the label. – TRJW Sep 17 '18 at 08:36
  • add: How can I insert a vspace above the pie and below the line in the top!? When I put a \vspace*{10pt} this does not work! – TRJW Sep 17 '18 at 12:47
  • @TRJW I made an update. –  Sep 17 '18 at 15:09