1

I'm trying to align these three pie charts, side by side, and to have the same legend for the three but with a title for each. But the changes that I have done, didn't work. This is my code:

\documentclass[oneside,12pt]{scrbook} %scrbook has more options than article

 \usepackage{tikz}


\usepackage{pgf-pie}
\usepackage{pgfplots}
\usepackage{bchart}

\usepackage{tikz,stackengine}
\def\stackalignment{r}
\setstackgap{S}{2pt}

\begin{document}

    \newcounter{a}
    \newcounter{b}

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

    \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{e}
            \newcounter{f}
            \foreach \p/\t in {
                31/Cash, 
                69/Private Equities}
            {
                \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}

    \begin{figure} [!htbp]
        \begin{center}
            \begin{tikzpicture}[scale=3]

            \newcounter{g}
            \newcounter{h}
            \foreach \p/\t in {
                45/Absolute Return Bonds, 
                45/Emerging Market Debt,
                7/Private Equities,
                3/Property (3\%)}
            {
                \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}

    \begin{figure} [!htbp]
        \begin{center}
            \begin{tikzpicture}[scale=3]

            \newcounter{i}
            \newcounter{j}
            \foreach \p/\t in {
                61/Absolute Return Bonds, 
                20/Emerging Market Debt,
                5/Private Equities,
                10/Property,
                4/Hedge Funds}
            {
                \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}

thanks in advance.

2 Answers 2

4

Preamble

\usepackage{tikz,stackengine}

\definecolor{rosso}{RGB}{220,57,18}
\definecolor{giallo}{RGB}{255,153,0}
\definecolor{blu}{RGB}{102,140,217}
\definecolor{verde}{RGB}{16,150,24}
\definecolor{viola}{RGB}{153,0,153}
\definecolor{cielo}{RGB}{102,120,217}

\makeatletter

\tikzstyle{chart}=[
legend label/.style={font={\scriptsize},anchor=west,align=left},
legend box/.style={rectangle, draw, minimum size=5pt},
axis/.style={black,semithick,->},
axis label/.style={anchor=east,font={\tiny}},
]

\tikzstyle{bar chart}=[
chart,
bar width/.code={
    \pgfmathparse{##1/2}
    \global\let\bar@w\pgfmathresult
},
bar/.style={very thick, draw=white},
bar label/.style={font={\bf\small},anchor=north},
bar value/.style={font={\footnotesize}},
bar width=.75,
]

\tikzstyle{pie chart}=[
chart,
slice/.style={line cap=round, line join=round, very thick,draw=white},
pie title/.style={font={\bf}},
slice type/.style 2 args={
    ##1/.style={fill=##2},
    values of ##1/.style={}
}
]

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}


\newcommand{\pie}[3][]{
    \begin{scope}[#1]
        \pgfmathsetmacro{\curA}{90}
        \pgfmathsetmacro{\r}{1}
        \def\c{(0,0)}
        \node[pie title] at (270:1.3) {#2};
        \foreach \v/\s in{#3}{
            \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}
}

\newcommand{\legend}[2][]{
    \begin{scope}[#1]
        \path
        \foreach \n/\s in {#2}
        {
            ++(0,-10pt) node[\s,legend box] {} +(5pt,0) node[legend label] {\n}
        }
        ;
    \end{scope}
}

Document

\begin{figure}[!tbp]
    \centering

\begin{tikzpicture}
[
pie chart,
slice type={Cash}{blu},
slice type={Private Equities}{rosso},
slice type={Absolute Return Bonds}{giallo},
slice type={Emerging Market Debt}{viola},
slice type={Property}{verde},
slice type={Hedge Funds}{cielo},
pie values/.style={font={\small}},
scale=2
]

\pie{2008}{31/Cash, 
    69/Private Equities}
\pie[xshift=2.2cm,values of coltello/.style={pos=1.1}]%
{2009}{45/Absolute Return Bonds, 
    45/Emerging Market Debt,
    7/Private Equities,
    3/Property}
\pie[xshift=4.4cm,values of caffe/.style={pos=1.1}]%
{2010}{61/Absolute Return Bonds, 
    20/Emerging Market Debt,
    5/Private Equities,
    10/Property,
    4/Hedge Funds}

\legend[shift={(0cm,-1.5cm)}]{{Cash}/Cash, {Private Equities}/Private Equities, {Absolute Return Bonds}/Absolute Return Bonds}
\legend[shift={(3cm,-1.5cm)}]{{Emerging Market Debt}/Emerging Market Debt, {Property}/Property,{Hedge Funds}/Hedge Funds}

\end{tikzpicture}

\caption[Example \textit{pie plots} ]{Example \textit{pie plots}.}

\end{figure}

enter image description here

1
  • thanks for the help, but maybe I didn't explain myself well, I want the pie graphs to be how they are on my code. So they can be consistent with the rest of the work. Isn't it possible? Commented Jul 27, 2016 at 8:13
0

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

First, the data are stored in the list \WCdata and the colors are stored in the list \WCcolors using the package listofitems.

A command \WCtest is defined of which the output depends on whether the percentage is larger than 3. This command is used in the keys data and wheel data. Here, the percentage is obtained with \WCperc.

By using the key wheel data pos={\WCpercentage<10?0.85:0.6}, the position of the wheel data is 0.85 if the percentage is smaller than 10 and 0.6 otherwise.

The first \wheelchart is positioned with the key at={(-7,0)}. The second is positioned at the initial coordinate (0,0) and the third uses the key at={(7,0)}.

The first variable corresponds to the value and the second variable corresponds to the item number for the lists \WCdata and \WCcolors. For example, the first \wheelchart has values 31 and 69 and the item numbers are 2 and 5.

The legend is placed in a separate \wheelchart. Here we set the keys data=, slices style={fill=none} and wheel data={} so that no slices are drawn and no data or wheel data are placed.

The legend is determined by the key legend. This is a tabular which is placed in a \node. The rows are determined by the key legend row. The result of all rows can be obtained with \WClegend.

enter image description here

\documentclass[border=6pt]{standalone}
\usepackage{listofitems}
\readlist\WCdata{Absolute Return Bonds,Cash,Emerging Market Debt,Hedge Funds,Private Equities,Property}
\readlist\WCcolors{orange,cyan,magenta,blue,red,green}
\usepackage{wheelchart}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
\def\WCtest#1#2{%
  \pgfmathparse{\WCpercentage>3?"#1":"#2"}%
  \pgfmathresult%
}
\pgfkeys{
  /wheelchart,
  anchor xsep=30,
  counterclockwise,
  data=\WCtest{\WCdata[\WCvarB]}{\WCdata[\WCvarB] (\WCperc)},
  radius={0}{2},
  slices style={
    \WCcolors[\WCvarB],
    draw=black,
    line join=bevel
  },
  start angle=0,
  wheel data=\WCtest{\WCperc}{},
  wheel data pos={\WCpercentage<10?0.85:0.6}
}
\wheelchart[
  at={(-7,0)}
]{31/2,69/5}
\wheelchart{45/1,45/3,7/5,3/6}
\wheelchart[
  at={(7,0)}
]{61/1,20/3,5/5,10/6,4/4}
\wheelchart[
  data=,
  legend columns=2,
  legend row={\tikz\fill[{\WCcolors[\WCcount]},draw=black] (0,0) rectangle (0.3,0.3); & \WCdata[\WCcount]},
  legend={
    \node[anchor=north] at (0,-3.5) {%
      \begin{tabular}{*{2}{l@{ }l}}%
      \WClegend%
      \end{tabular}%
    };
  },
  slices style={fill=none},
  total count=\WCdatalen,
  wheel data={}
]{}
\end{tikzpicture}
\end{document}

You must log in to answer this question.

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