Tikz-PGF – How to Set Text and Its Pin in Correct Orientation in a TikZ Pie Chart

tikz-pgf

I am modifying the pie chart, which is available here. In case of small slices, text inside the pie chart overlaps to each other. So I am trying to write the text and value outside the chart. Below is my modified latex code-

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}

\makeatletter
\def\tikz@auto@anchor{%
    \pgfmathtruncatemacro\angle{atan2(\pgf@x,\pgf@y)-90}
    \edef\tikz@anchor{\angle}%
}
\makeatother

\begin{document}

\begin{figure}[h]
    \def\angle{0}
    \def\radius{2}
    \def\cyclelist{{"orange","blue","gray","red","green","cyan","magenta"}}

    \centering
    \newcount\cyclecount \cyclecount=-1
    \newcount\ind \ind=-1
    \begin{tikzpicture}[nodes = {font=\small}]
      \foreach \percent/\name in {
        2/3,
        2/4,
        2/5,
        18/7,
        13/8,
        63/9,
        } {
          \ifx\percent\empty\else               % If \percent is empty, do nothing
            \global\advance\cyclecount by 1     % Advance cyclecount
            \global\advance\ind by 1            % Advance list index
            \ifnum6<\cyclecount                 % If cyclecount is larger than list
              \global\cyclecount=0              %   reset cyclecount and
              \global\ind=0                     %   reset list index
            \fi
            \pgfmathparse{\cyclelist[\the\ind]} % Get color from cycle list
            \edef\color{\pgfmathresult}         %   and store as \color
            \draw[fill={\color!50},draw={\color}] (0,0) -- (\angle:\radius)
              arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
            \node[pin={[pin distance=1cm]\angle+0.5*\percent*3.6:{\name~[\percent\%]}}]
              at (\angle+0.5*\percent*3.6:\radius) {};
            \pgfmathparse{\angle+\percent*3.6}  % Advance angle
            \xdef\angle{\pgfmathresult}         %   and store in \angle
          \fi
        };
    \end{tikzpicture}
\end{figure}
\end{document}

I have followed some answers given for How can I force TikZ pin angle? and pgfplot pin length but they are not working well. Below is the screenshot of generated pdf-

enter image description here

I have following questions-

  1. How to orient the pin properly i.e. direction towards the center of circle
  2. The angle at which text and pin are oriented, must be half of the angle of that slice

Best Answer

I would avoid using the pin option (you have to compute positions anyway!)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}

\begin{document}

\begin{figure}[h]
    \def\angle{0}
    \def\radius{2}
    \def\labelradius{3}
    \def\cyclelist{{"orange","blue","gray","red","green","cyan","magenta"}}

    \centering
    \newcount\cyclecount \cyclecount=-1
    \newcount\ind \ind=-1
    \begin{tikzpicture}[nodes = {font=\small}]
      \foreach \percent/\name in {
        2/3,
        2/4,
        2/5,
        18/7,
        13/8,
        63/9,
        } {
          \ifx\percent\empty\else               % If \percent is empty, do nothing
            \global\advance\cyclecount by 1     % Advance cyclecount
            \global\advance\ind by 1            % Advance list index
            \ifnum6<\cyclecount                 % If cyclecount is larger than list
              \global\cyclecount=0              %   reset cyclecount and
              \global\ind=0                     %   reset list index
            \fi
            \pgfmathparse{\cyclelist[\the\ind]} % Get color from cycle list
            \edef\color{\pgfmathresult}         %   and store as \color
            \draw[fill={\color!50},draw={\color}] (0,0) -- (\angle:\radius)
              arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
            % \node[pin={[pin distance=1cm]\angle+0.5*\percent*3.6:{\name~[\percent\%]}}]
            %   at (\angle+0.5*\percent*3.6:\radius) {};
            \draw[draw=gray, shorten >=2pt] (\angle+0.5*\percent*3.6:\labelradius) node {\name~[\percent\%]} edge (\angle+0.5*\percent*3.6:\radius);
            \pgfmathparse{\angle+\percent*3.6}  % Advance angle
            \xdef\angle{\pgfmathresult}         %   and store in \angle
          \fi
        };
    \end{tikzpicture}
\end{figure}
\end{document}

preview

Related Question