3

I have been using this great pie chart code: How to draw Bar & Pie Chart The code is from Bordaigorl.

I was wondering if anyone can see how it is possible to have two pie charts in the same environment with different sizes.

The part of the code that is responsible for this is "scale" when you call the function pie chart:

\begin{tikzpicture}
[
    pie chart,
    slice type={g}{giallo},
    slice type={A}{rosso},
    slice type={B}{blu},
    pie values/.style={font={\small}},
    scale=2
]

    \pie{t=1, A in power}{66/g,34/A}
    \pie[xshift=2.2cm]%
        {t=2, B in power}{33/g,33/B}
\end{tikzpicture}

My question is: does anyone see how to create two "types" of pies, one with scale=2 and another with scale=2.5, for example?

For the purpose of understanding, I will post an MWE based on the whole code created by Bordaigorl here. What I would like is to have the left pie chart in one scale and the pie chart on the right in another scale. Please note that I am not the one who designed this code and credit must be given to Bordaigorl:

\documentclass[border=10pt,multi,tikz]{standalone}
\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}

\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 (90: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}
}
\begin{document}
\begin{tikzpicture}
[
    pie chart,
    slice type={g}{giallo},
    slice type={A}{rosso},
    slice type={B}{blu},
    pie values/.style={font={\small}},
    scale=2
]

    \pie{1}{66/g,34/A}
    \pie[xshift=2.2cm]%
        {2}{33.3/g,33.3/B,33.3/A}
\end{tikzpicture}
\end{document}

I apologize if that is an obvious question. I've tried in 2 differents ways and they were all not succesful:

  1. I've tried to remove the scale from the preamble on tikz and added scale=2 when was calling pie function: \pie[xshift=2.2cm, scale=2]{2}{33.3/g,33.3/B,33.3/A}
  2. I've also tried to create two functions in the preamble of the document, one pie1 and another pie2. The code doesn't compile

Thank you!

2

Use a scope environment, i.e.

\begin{scope}[scale=2,xshift=2.2cm]
    \pie{2}{33.3/g,33.3/B,33.3/A}
\end{scope}

enter image description here

\documentclass[border=10pt,multi,tikz]{standalone}
\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}

\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 (90: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}
}
\begin{document}
\begin{tikzpicture}
[
    pie chart,
    slice type={g}{giallo},
    slice type={A}{rosso},
    slice type={B}{blu},
    pie values/.style={font={\small}},
%    scale=2
]

    \pie{1}{66/g,34/A}
\begin{scope}[scale=2,xshift=2.2cm]
    \pie{2}{33.3/g,33.3/B,33.3/A}
\end{scope}
\end{tikzpicture}
\end{document}
  • That is great, Torbjørn T.! It works perfectly! – Laura K Apr 27 '17 at 19:53
3

Actually, the \pie command already wraps the path in a scope environment so you can just do

\pie{1}{66/g,34/A}
\pie[xshift=4cm,scale=2]%
    {2}{33.3/g,33.3/B,33.3/A}

which produces

enter image description here

By the way, thanks for the credit ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.