1

I am very happy with the solution to Pie chart labels, but I wonder how I can place the legend to the right of the pie. For that, I like the idea of Pie chart with joined legend position - to add, in my version, a style={legend to the east}. But I don't know how to edit it into this MWE.

So, the question is: how can I draw on this code

\tikzset{
legend to the south/.code={
\coordinate[xshift=-1.5cm,
yshift=(\value{pgfpie@sliceLength}*0.5+1)*0.5cm-4em] (legendpos) at
(current bounding box.south);},
}

to place the legend of the following MWE to the east?

\documentclass[11pt]{article}
\usepackage{tikz} % loads xcolor
\usetikzlibrary{arrows}


\usepackage[utf8]{inputenc}

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


\tikzset{
  chart/.style={
    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}},
  },
  pie chart/.style={
    chart,
    slice/.style={line cap=round, line join=round, very thick,draw=white},
    pie title/.style={font={\bfseries}},
    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{\radius}{1}
    \def\Centre{(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] \Centre
            -- +(\curA:\radius)
            arc (\curA:\nextA:\radius)
            -- cycle;

   % to determine direction of lines (left/right, up/down
   \pgfmathsetmacro{\ysign}{ifthenelse(mod(\midA,360)<=180,1,-1)}
   \pgfmathsetmacro{\xsign}{ifthenelse(mod(\midA-90,360)<=180,-1,1)}

   \begin{pgfonlayer}{foreground}
        \draw[*-,thin] \Centre ++(\midA:\radius/2) -- 
                               ++(\xsign*0.07*\radius,\ysign*0.2*\radius) -- 
                               ++(\xsign*\radius,0) 
                      node[above,near end,pie values,values of \s]{$\v\%$};
   \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={comet}{blu},
    slice type={legno}{rosso},
    slice type={coltello}{giallo},
    slice type={sedia}{viola},
    slice type={caffe}{verde},
    pie values/.style={font={\small}},
    scale=2
]

\pie{Sommerjobs in Österreich}{52/comet,12/legno,20/sedia,16/coltello}


\legend[shift={(-1cm,-1cm)}]{{Eisverkäufer}/comet, {Schwimmbad}/legno, {Zettelverteiler}/coltello}
\legend[shift={(1cm,-1cm)}]{{Kellner}/sedia}


\end{tikzpicture}
\end{document}
4
  • 2
    This is all included in the code you post. You only need to change the shift. Try \legend[shift={(2cm,1cm)}]{{Eisverk\"aufer}/comet,{Schwimmbad}/legno, {Zettelverteiler}/coltello,{Kellner}/sedia}.
    – user194703
    Commented May 23, 2020 at 21:29
  • absolutely, thanks a lot! hmm, if you post it as an answer I will accept it; or, alternatively, I guess, I should delete or close the question.
    – ingli
    Commented May 23, 2020 at 21:32
  • 1
    I leave the decision to you. Obviously I have tried out the suggestion so I would have a code that I could post.
    – user194703
    Commented May 23, 2020 at 21:34
  • please do post the code - will accept it
    – ingli
    Commented May 23, 2020 at 21:39

1 Answer 1

1

In the spirit of

Never repair a working code.

I only changed the shift parameters to get

\documentclass[11pt]{article}
\usepackage{tikz} % loads xcolor
\usetikzlibrary{arrows}


\usepackage[utf8]{inputenc}

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


\tikzset{
  chart/.style={
    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}},
  },
  pie chart/.style={
    chart,
    slice/.style={line cap=round, line join=round, very thick,draw=white},
    pie title/.style={font={\bfseries}},
    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{\radius}{1}
    \def\Centre{(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] \Centre
            -- +(\curA:\radius)
            arc (\curA:\nextA:\radius)
            -- cycle;

   % to determine direction of lines (left/right, up/down
   \pgfmathsetmacro{\ysign}{ifthenelse(mod(\midA,360)<=180,1,-1)}
   \pgfmathsetmacro{\xsign}{ifthenelse(mod(\midA-90,360)<=180,-1,1)}

   \begin{pgfonlayer}{foreground}
        \draw[*-,thin] \Centre ++(\midA:\radius/2) -- 
                               ++(\xsign*0.07*\radius,\ysign*0.2*\radius) -- 
                               ++(\xsign*\radius,0) 
                      node[above,near end,pie values,values of \s]{$\v\%$};
   \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={comet}{blu},
    slice type={legno}{rosso},
    slice type={coltello}{giallo},
    slice type={sedia}{viola},
    slice type={caffe}{verde},
    pie values/.style={font={\small}},
    scale=2
]

\pie{Sommerjobs in \"Osterreich}{52/comet,12/legno,20/sedia,16/coltello}


\legend[shift={(2cm,1cm)}]{{Eisverk\"aufer}/comet,{Schwimmbad}/legno, {Zettelverteiler}/coltello,{Kellner}/sedia}


\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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