[Tex/LaTex] How to add legend to custom pie chart

chartslegendmacrostikz-pgf

I've found here this code and it's pretty working for me, but I had problem with too long labels (5-6 words), so I've removed name labels from code and I would like to add legend on the right side of the pie chart which will be corresponding with colors used in pie chart.

\documentclass{article}
\usepackage{tikz}
\usepackage{color}
\usepackage{xcolor}
\usetikzlibrary{arrows}
\begin{document}

% Adjusts the size of the wheel:
\def\innerradius{1.8cm}
\def\outerradius{2.2cm}

% The main macro
\newcommand{\wheelchartwithlegend}[1]{
  % Calculate total
  \pgfmathsetmacro{\totalnum}{0}
  \foreach \value/\colour/\name in {#1} {
      \pgfmathparse{\value+\totalnum}
      \global\let\totalnum=\pgfmathresult
  }

  \begin{tikzpicture}

    % Calculate the thickness and the middle line of the wheel
    \pgfmathsetmacro{\wheelwidth}{\outerradius-\innerradius}
    \pgfmathsetmacro{\midradius}{(\outerradius+\innerradius)/2}

    % Rotate so we start from the top
    \begin{scope}[rotate=90]

    % Loop through each value set. \cumnum keeps track of where we are in the wheel
    \pgfmathsetmacro{\cumnum}{0}
    \foreach \value/\colour/\name in {#1} {
          \pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}

          % Calculate the percent value
          % \pgfmathsetmacro{\percentage}{\value/\totalnum*100}
                    \pgfmathsetmacro{\percentage}{\value}
          % Calculate the mid angle of the colour segments to place the labels
          \pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}

          % This is necessary for the labels to align nicely
          \pgfmathparse{
             (-\midangle<180?"west":"east")
          } \edef\textanchor{\pgfmathresult}
          \pgfmathsetmacro\labelshiftdir{1-2*(-\midangle>180)}

          % Draw the color segments. Somehow, the \midrow units got lost, so we add 'pt' at the end. Not nice...
          \fill[\colour] (-\cumnum:\outerradius) arc (-\cumnum:-(\newcumnum):\outerradius) --
          (-\newcumnum:\innerradius) arc (-\newcumnum:-(\cumnum):\innerradius) -- cycle;

          % Draw the data labels
          \draw  [*-,thin] node [append after command={(\midangle:\midradius pt) -- (\midangle:\outerradius + 1ex) -- (\tikzlastnode)}] at (\midangle:\outerradius + 1ex) [xshift=\labelshiftdir*0.5cm,inner sep=0pt, outer sep=0pt, ,anchor=\textanchor]{\pgfmathprintnumber{\percentage}\%};

          % Set the old cumulated angle to the new value
          \global\let\cumnum=\newcumnum
      }
    \end{scope}
  \end{tikzpicture}
}


\definecolor{A0}{HTML}{A4DA90}
\definecolor{A1}{HTML}{81C269}
\definecolor{A2}{HTML}{74AA61}

\definecolor{B0}{HTML}{ECEB80}
\definecolor{B1}{HTML}{CFCD56}

\definecolor{C0}{HTML}{9D7AB3}
\definecolor{C1}{HTML}{845A9E}

\definecolor{D0}{HTML}{7E8BB4}
\definecolor{D1}{HTML}{5E6FA0}

\definecolor{E0}{HTML}{8D80B7}
\definecolor{E1}{HTML}{6F61A3}

\wheelchartwithlegend{
    49/A0/{\footnotesize  Lorem ipsum dolor sit amet},
    44/B0/{\footnotesize  Ut enim ad minim veniam},
    50/C0/{\footnotesize  Quis nostrud exercitation ullamco laboris},
    36/D0/{\footnotesize  Nisi ut aliquip ex ea commodo consequat},
    28/E0/{\footnotesize  Duis aute irure dolor in reprehenderit },
    11/A1/{\footnotesize  Voluptate velit esse cillum},
    28/B1/{\footnotesize  Dolore eu fugiat nulla pariatur},
    31/C1/{\footnotesize  Excepteur sint occaecat cupidatat non proident, sunt.}/
}
\end{document}

currently this produces something like this:

pie-chart

Best Answer

There are just three small additions to your code:

  1. \foreach [count=\i,remember=\i as \j (initially 0)] ..: Each legend entry is a node named L-\i, and is placed using the previous one as reference. Hence we need to get \i minus one somehow. The remember statement is one way of doing that.

  2. A coordinate that determines the upper left corner of the first legend entry:

    \coordinate (L-0) at (\outerradius+5mm,-\outerradius-2.5cm);
    

    The use of \outerradius is to position the legend relative to the wheel.

  3. A \node and \fill to make the legend:

    % add legend node
    \node [anchor=north west,text width=5cm,font=\footnotesize] (L-\i) at (L-\j.south west) {\name};
    % draw legend image
    \fill [fill=\colour] ([xshift=-3pt,yshift=1mm]L-\i.north west) rectangle ++(-2mm,5mm);
    

    Adjust text width to your needs.

    (Because I added font=\footnotesize, I removed all the \footnotesize commands from input to the macro.)

Oh, and since tikz loads xcolor, which in turn loads color, I removed the two latter packages from your preamble.

output of code

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

% Adjusts the size of the wheel:
\def\innerradius{1.8cm}
\def\outerradius{2.2cm}



\definecolor{A0}{HTML}{A4DA90}
\definecolor{A1}{HTML}{81C269}
\definecolor{A2}{HTML}{74AA61}

\definecolor{B0}{HTML}{ECEB80}
\definecolor{B1}{HTML}{CFCD56}

\definecolor{C0}{HTML}{9D7AB3}
\definecolor{C1}{HTML}{845A9E}

\definecolor{D0}{HTML}{7E8BB4}
\definecolor{D1}{HTML}{5E6FA0}

\definecolor{E0}{HTML}{8D80B7}
\definecolor{E1}{HTML}{6F61A3}

% The main macro
\newcommand{\wheelchartwithlegend}[1]{
  % Calculate total
  \pgfmathsetmacro{\totalnum}{0}
  \foreach \value/\colour/\name in {#1} {
      \pgfmathparse{\value+\totalnum}
      \global\let\totalnum=\pgfmathresult
  }

  \begin{tikzpicture}

    % Calculate the thickness and the middle line of the wheel
    \pgfmathsetmacro{\wheelwidth}{\outerradius-\innerradius}
    \pgfmathsetmacro{\midradius}{(\outerradius+\innerradius)/2}

    % Rotate so we start from the top
    \begin{scope}[rotate=90]

    % add coordinate to define the upper left starting point of the legend entries
    \coordinate (L-0) at (\outerradius+5mm,-\outerradius-2.5cm);

    % Loop through each value set. \cumnum keeps track of where we are in the wheel
    \pgfmathsetmacro{\cumnum}{0}
    \foreach [count=\i,remember=\i as \j (initially 0)] \value/\colour/\name in {#1} {
          \pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}

          % Calculate the percent value
          % \pgfmathsetmacro{\percentage}{\value/\totalnum*100}
                    \pgfmathsetmacro{\percentage}{\value}
          % Calculate the mid angle of the colour segments to place the labels
          \pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}

          % This is necessary for the labels to align nicely
          \pgfmathparse{
             (-\midangle<180?"west":"east")
          } \edef\textanchor{\pgfmathresult}
          \pgfmathsetmacro\labelshiftdir{1-2*(-\midangle>180)}

          % Draw the color segments. Somehow, the \midrow units got lost, so we add 'pt' at the end. Not nice...
          \fill[\colour] (-\cumnum:\outerradius) arc (-\cumnum:-(\newcumnum):\outerradius) --
          (-\newcumnum:\innerradius) arc (-\newcumnum:-(\cumnum):\innerradius) -- cycle;

          % Draw the data labels
          \draw  [Circle-,thin] node [append after command={(\midangle:\midradius pt) -- (\midangle:\outerradius + 1ex) -- (\tikzlastnode)}] at (\midangle:\outerradius + 1ex) [xshift=\labelshiftdir*0.5cm,inner sep=0pt, outer sep=0pt, ,anchor=\textanchor]{\pgfmathprintnumber{\percentage}\%};

          % add legend node
          \node [anchor=north west,text width=5cm,font=\footnotesize] (L-\i) at (L-\j.south west) {\name};
          % draw legend image
          \fill [fill=\colour] ([xshift=-3pt,yshift=1mm]L-\i.north west) rectangle ++(-2mm,5mm);


          % Set the old cumulated angle to the new value
          \global\let\cumnum=\newcumnum
      }
    \end{scope}
  \end{tikzpicture}
}


\begin{document}



\wheelchartwithlegend{
    49/A0/{Lorem ipsum dolor sit amet},
    44/B0/{Ut enim ad minim veniam},
    50/C0/{Quis nostrud exercitation ullamco laboris},
    36/D0/{Nisi ut aliquip ex ea commodo consequat},
    28/E0/{Duis aute irure dolor in reprehenderit },
    11/A1/{Voluptate velit esse cillum},
    28/B1/{Dolore eu fugiat nulla pariatur},
    31/C1/{Excepteur sint occaecat cupidatat non proident, sunt.}
}
\end{document}
Related Question