%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Start of pgf-pie.sty
%
% Some LaTeX macros for pie chart by using PGF/Tikz package.
% Home page of project: http://pgf-pie.googlecode.com/
% Author: Xu Yuan <xuyuan.cn@gmail.com>
%

\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{pgf-pie}[2011/10/01 v0.1 Some LaTeX macros for pie
chart by using PGF/Tikz package.]

\RequirePackage{tikz}
\usetikzlibrary{shadows}

% args:
% #1: begin angle
% #2: end angle
% #3: inner label
% #4: outer label
% #5: explode
% #6: fill color
% #7: radius
\newcommand{\pgfpie@slice}[7]{
  \pgfmathparse{0.5*#1+0.5*#2}
  \let\midangle\pgfmathresult

  % slice
  \draw[thick,fill=#6, drop shadow] (\midangle:#5) -- ++(#1:#7) arc (#1:#2:#7) -- cycle;

  % outer label
  \node[label=\midangle:#4] at (\midangle:#7) {};

  % inner label
  \pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
  \let\temp\pgfmathresult
  \pgfmathparse{(max(\temp,-0.5) + 0.8)*#7}
  \let\innerpos\pgfmathresult
  \node at (\midangle:\innerpos) {#3};
}

\newcounter{pgfpie@angleBegin}
\newcounter{pgfpie@angleEnd}
\newcounter{pgfpie@explodeLength}
\newcounter{pgfpie@colormapLength}

\def\setexplode#1\pgfeov{\def\explode{#1}}
\pgfkeyslet{/explode/.@cmd}{\setexplode}

\def\setcolormap#1\pgfeov{\def\colormap{#1}}
\pgfkeyslet{/colormap/.@cmd}{\setcolormap}

\def\setradius#1\pgfeov{\def\radius{#1}}
\pgfkeyslet{/radius/.@cmd}{\setradius}

\newcommand{\pie}[2][]
{
  \pgfkeys{explode=0,
    colormap={blue!60, cyan!60, yellow!60, orange!60, red!60},
    radius=3}
  \pgfkeys{#1}

  \def\explodearray{{\explode}}
  \setcounter{pgfpie@explodeLength}{0}
  \foreach \e in \explode { \addtocounter{pgfpie@explodeLength}{1} }

  \setcounter{pgfpie@colormapLength}{0}
  \foreach \c in \colormap { \addtocounter{pgfpie@colormapLength}{1} }
  
  \setcounter{pgfpie@angleEnd}{0}


    \foreach \p/\t [count=\i from 0] in {#2}
    {
      \setcounter{pgfpie@angleBegin}{\value{pgfpie@angleEnd}}
      \addtocounter{pgfpie@angleEnd}{\p}

      % find explode
      \pgfmathparse{\explodearray[int(mod(\i,\value{pgfpie@explodeLength}))]}
      \let\e\pgfmathresult

      % find color
      \pgfmathparse{int(mod(\i,\value{pgfpie@colormapLength}))}
      \let\ci\pgfmathresult
      \foreach \c [count=\j from 0] in \colormap {
        \ifnum \j=\ci
          \xdef\thecolor{\c}
          \breakforeach
        \fi
      }

      \pgfpie@slice{\thepgfpie@angleBegin/100*360}
      {\thepgfpie@angleEnd/100*360}
      {\p\%}{\t}
      {\e}
      {\thecolor}
      {\radius}
    }
}

%%% End of pgf-pie.sty
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

