1

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
ravi
  • 1,618
  • 3
    The syntax for the atan2 function changed recently: the arguments are now swapped. Try using atan2(\pgf@y,\pgf@x) instead of atan2(\pgf@x,\pgf@y). – Jake Sep 26 '14 at 13:04
  • @Jake : You are absolutely right. Thanks a ton. You may answer the question, if you have more info to share :) – ravi Sep 26 '14 at 13:39

3 Answers3

3

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

Bordaigorl
  • 15,135
2

A PSTricks solution:

\documentclass{article}

\usepackage{siunitx}
\usepackage{pstricks-add}

\def\slice[#1](#2)#3#4{
  \rput(psChartI#2){\SI{#3}{\tonne}}
  \ncline{psChartO#2}{psChart#2}
  \nput{#1}{psChartO#2}{#4}
}
% Use of the \slice macro:
% \slice%
% [position of outer node, relative to the line end-point]
% (slice number)
% {size of the slice, relative to the whole pie}
% {outer node}


\begin{document}

\begin{figure}
\def\before{54}
\def\under{594}
\def\after{890}
\centering
 \psset{
   unit = 2.8,
   nodesepB = -5pt
 }
  \begin{pspicture}(-1,-1.37)(1.5,1.4)
    \psChart[
      userColor = {red!70, green!70, blue!70},
      chartNodeO = 1.2,
      shadow = true,
      shadowsize = 5pt,
      linewidth = 0
    ]{\before, \under, \after}{}{}
    \slice[0](1){\before}{Before}
    \slice[90](2){\under}{Under}
    \slice[270](3){\after}{After}
  \end{pspicture}
 \caption{Distribution of something.}
 \label{fig:piechart}
\end{figure}
This is figure~\ref{fig:piechart}.

\end{document}

output

For more on the \psChart macro, see page 16 in the pstricks-add manual.

1

You may want to check this recent question. The shape of the pin can be modified but the basic idea is there

BambOo
  • 8,801
  • 2
  • 20
  • 47