%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% 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}
\RequirePackage{ifthen}

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

  \path (#8) -- ++(\midangle:#5) coordinate(O);

  \pgfmathparse{#7+#5}
  \let\radius\pgfmathresult
  
  % slice
  \draw[line join=round, fill=#6, #9] (O) -- ++(#1:#7) arc (#1:#2:#7) -- cycle;

  % outer label
  \path (O) -- ++ (\midangle:\radius) node[inner sep=0, \text=\midangle:#4]{};

  % 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
  \path (O) -- ++(\midangle:\innerpos) node {#3};
}

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

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

\def\setcolor#1\pgfeov{\def\color{#1}}
\pgfkeyslet{/color/.@cmd}{\setcolor}

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

\def\setpos#1\pgfeov{\def\pos{#1}}
\pgfkeyslet{/pos/.@cmd}{\setpos}

\def\setstyle#1\pgfeov{\def\style{#1}}
\pgfkeyslet{/style/.@cmd}{\setstyle}

\def\setbeforenumber#1\pgfeov{\def\beforenumber{#1}}
\pgfkeyslet{/before number/.@cmd}{\setbeforenumber}

\def\setafternumber#1\pgfeov{\def\afternumber{#1}}
\pgfkeyslet{/after number/.@cmd}{\setafternumber}

\def\settext#1\pgfeov{\xdef\text{#1}}
\pgfkeyslet{/text/.@cmd}{\settext}

\def\setsum#1\pgfeov{\xdef\sum{#1}}
\pgfkeyslet{/sum/.@cmd}{\setsum}

\def\setrotate#1\pgfeov{\xdef\rotate{#1}}
\pgfkeyslet{/rotate/.@cmd}{\setrotate}

\newcommand{\pie}[2][]
{
  \pgfkeys{
    explode=0,
    color={blue!60, cyan!60, yellow!60, orange!60, red!60},
    radius=3,
    pos={0,0},
    style={thick},
    before number=,
    after number=\%,
    text=label,
    sum=100,
    rotate=0,
  }
  \pgfkeys{#1}

  \ifthenelse{\equal{\sum}{auto}}
  {
    % sum all input
    \xdef\sum{0}
    \foreach \p/\t in {#2}
    {
      \pgfmathparse{\sum + \p}
      \xdef\sum{\pgfmathresult}
    }
  }
  {}


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

  \setcounter{pgfpie@colorLength}{0}
  \foreach \c in \color { \addtocounter{pgfpie@colorLength}{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{int(mod(\i,\value{pgfpie@explodeLength}))}
    \let\ei\pgfmathresult
    \foreach \e [count=\j from 0] in \explode {
      \ifnum \j=\ei
      \xdef\theexplode{\e}
      \breakforeach
      \fi
    }

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

    \pgfpie@slice{\thepgfpie@angleBegin/\sum*360+\rotate}
    {\thepgfpie@angleEnd/\sum*360+\rotate}
    {\beforenumber \p \afternumber}
    {\t}
    {\theexplode}
    {\thecolor}
    {\radius}
    {\pos}
    {\style}
  }
}

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

