1

I constructed a pie chart based on a code I found on the internet and I already tried to make some changes because it's all messy and I wanted, when the percentags are too small (<3%) to have outside text and a line coming from the inside to this outside text.

My code is:

\newcommand{\slice}[4]{
        \pgfmathparse{0.5*#1+0.5*#2}
        \let\midangle\pgfmathresult

        % slice
        \draw[thick,fill=black!10] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;

        % outer label
        \node[label=\midangle:#4] at (\midangle:1) {};

        % inner label
        \pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
        \let\temp\pgfmathresult
        \pgfmathparse{max(\temp,-0.5) + 0.8}
        \let\innerpos\pgfmathresult
        \node at (\midangle:\innerpos) {#3};
    }
    \begin{figure} [!htbp]
    \begin{center}
        \begin{tikzpicture}[scale=3]

        \newcounter{c}
        \newcounter{d}
        \foreach \p/\t in {18/Absolute Return Bonds, 8/Cash,  1/Eurozone equities, 6/Global Equities, 1/Hedge Funds (Mercer MLAS fund), 29/Index-Linked euro govts (AS), 7/Private Equity, 4/Euro Corporate Bonds (>10yrs), 25/Property, 2/Others}
        {
            \setcounter{a}{\value{b}}
            \addtocounter{b}{\p}
            \slice{\thea/100*360}
            {\theb/100*360}
            {\p\%}{\t}
        }

        \end{tikzpicture}
    \end{center}

    \caption{Alocation of the amount invested in each sub-asset class}
    \label{fi2}
\end{figure}

Could u give me some hints? :)

1 Answers1

1

See below for ENHANCED SOLUTION

Here I propose several changes. First, I changed the in-pie label to \ifnum\p>3\relax\p\%\fi. This way, it will only place the "number%" label inside the pie if the percent is greater than 3%. Otherwise, you should make the percentage an explicit part of the label itself.

Secondly, I use \stackon and \stackunder to extend rules towards the pie.

\documentclass{article}
\newcounter{a}
\newcounter{b}
\usepackage{tikz,stackengine}
\def\stackalignment{r}
\setstackgap{S}{2pt}
\begin{document}

\newcommand{\slice}[4]{
        \pgfmathparse{0.5*#1+0.5*#2}
        \let\midangle\pgfmathresult

        % slice
        \draw[thick,fill=black!10] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;

        % outer label
        \node[label=\midangle:#4] at (\midangle:1) {};

        % inner label
        \pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
        \let\temp\pgfmathresult
        \pgfmathparse{max(\temp,-0.5) + 0.8}
        \let\innerpos\pgfmathresult
        \node at (\midangle:\innerpos) {#3};
    }
    \begin{figure} [!htbp]
    \begin{center}
        \begin{tikzpicture}[scale=3]

        \newcounter{c}
        \newcounter{d}
        \foreach \p/\t in {
          18/Absolute Return Bonds, 
          8/Cash,  
          1/\stackunder{Eurozone equities (1\%)}{\rule{.8pt}{4.5ex}}, 
          6/\stackunder{Global Equities}{\rule{.8pt}{2.5ex}}, 
          1/\stackunder{Hedge Funds (Mercer MLAS fund)(1\%)}{\rule{.8pt}{1ex}}, 
          29/Index-Linked euro govts (AS), 
          7/Private Equity, 
          4/\stackon{Euro Corporate Bonds ($>$10yrs)}{\rule{.8pt}{1.5ex}}, 
          25/Property, 
          2/Others (2\%)}
        {
            \setcounter{a}{\value{b}}
            \addtocounter{b}{\p}
            \slice{\thea/100*360}
            {\theb/100*360}
            {\ifnum\p>3\relax\p\%\fi}{\t}
        }

        \end{tikzpicture}
    \end{center}

    \caption{Alocation of the amount invested in each sub-asset class}
    \label{fi2}
\end{figure}
\end{document}

enter image description here

ENHANCED SOLUTION

Here I introduce

\pielabelon[<H-shift>]{<Label text>}{<stem-height>}
\pielabelunder[<H-shift>]{<Label text>}{<stem-height>}

The MWE:

\documentclass{article}
\newcounter{a}
\newcounter{b}
\usepackage{tikz,stackengine}
\def\stackalignment{r}
\newcommand\pielabelon[3][0pt]{%
  \abovebaseline[#3]{#2%
  \makebox[-\dimexpr8pt+#1]{\smash{%
  \stackunder{}{\rule{1pt}{\dimexpr#3+2.5ex}}}}}
}
\newcommand\pielabelunder[3][0pt]{%
  \belowbaseline[#3]{#2%
  \makebox[-\dimexpr8pt+#1]{\smash{%
  \stackon{\rule{0pt}{1.3ex}}{\rule{1pt}{\dimexpr#3+2.5ex}}}}}
}
\begin{document}

\newcommand{\slice}[4]{
        \pgfmathparse{0.5*#1+0.5*#2}
        \let\midangle\pgfmathresult

        % slice
        \draw[thick,fill=black!10] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;

        % outer label
        \node[label=\midangle:#4] at (\midangle:1) {};

        % inner label
        \pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
        \let\temp\pgfmathresult
        \pgfmathparse{max(\temp,-0.5) + 0.8}
        \let\innerpos\pgfmathresult
        \node at (\midangle:\innerpos) {#3};
    }
    \begin{figure} [!htbp]
    \begin{center}
        \begin{tikzpicture}[scale=3]

        \newcounter{c}
        \newcounter{d}
        \foreach \p/\t in {
          18/Absolute Return Bonds, 
          8/Cash,  
          1/\pielabelon{Eurozone equities (1\%)}{4.5ex}, 
          6/\pielabelon{Global Equities}{2.5ex}, 
          1/\pielabelon[6pt]{Hedge Funds (Mercer MLAS fund)(1\%)}{1ex}, 
          29/Index-Linked euro govts (AS), 
          7/Private Equity, 
          4/\pielabelunder[4pt]{Euro Corporate Bonds ($>$10yrs)}{2.5ex}, 
          25/Property, 
          2/Others (2\%)}
        {
            \setcounter{a}{\value{b}}
            \addtocounter{b}{\p}
            \slice{\thea/100*360}
            {\theb/100*360}
            {\ifnum\p>3\relax\p\%\fi}{\t}
        }

        \end{tikzpicture}
    \end{center}

    \caption{Alocation of the amount invested in each sub-asset class}
    \label{fi2}
\end{figure}
\end{document}

enter image description here