%%
%% This is file `csvpie.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% csvtools.dtx  (with options: `csvpie.sty,package')
%% Copyright (C) 2007 Nicola Talbot, all rights reserved.
%% If you modify this file, you must change its name first.
%% You are NOT ALLOWED to distribute this file alone. You are NOT
%% ALLOWED to take money for the distribution or use of either this
%% file or a changed version, except for a nominal charge for copying
%% etc.
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{csvpie}[2006/09/01 v1.0]
\newif\ifcolorpiechart
\colorpiecharttrue
\DeclareOption{color}{\colorpiecharttrue}
\DeclareOption{monochrome}{\colorpiechartfalse}
\ProcessOptions
\RequirePackage{csvtools}
\RequirePackage{tikz}
\RequirePackage{calc}
\RequirePackage{keyval}
\newlength\csvradius
\newlength\csvinner
\newlength\csvouter
\newlength\csvcutawayoffset
\newcounter{csvstartangle}
\newcounter{csvendangle}
\newcounter{csvangle}
\newcounter{csvpietotal}
\newcommand*{\csvpiedefaults}{radius=2cm,inner=0.25,outer=1.25,%
cutaway={},total=100,start=0,offset=0.1}
\newcommand{\csvpieouterlabel}{\field{1}}
\newcommand{\csvpieinnerlabel}{\field{2}\%}
\newcommand*{\csvpiecutaways}{}
\newcommand*{\csvpiesegmentcol}[2]{%
\expandafter\def\csname csvpie@segcol\romannumeral#1\endcsname{#2}}
\newcommand*{\csvpiesegcolname}[1]{%
\csname csvpie@segcol\romannumeral#1\endcsname}
\ifcolorpiechart
\csvpiesegmentcol{1}{red}
\csvpiesegmentcol{2}{green}
\csvpiesegmentcol{3}{blue}
\csvpiesegmentcol{4}{yellow}
\csvpiesegmentcol{5}{magenta}
\csvpiesegmentcol{6}{cyan}
\csvpiesegmentcol{7}{orange}
\csvpiesegmentcol{8}{white}
\else
\csvpiesegmentcol{1}{black!15}
\csvpiesegmentcol{2}{black!25}
\csvpiesegmentcol{3}{black!35}
\csvpiesegmentcol{4}{black!45}
\csvpiesegmentcol{5}{black!55}
\csvpiesegmentcol{6}{black!65}
\csvpiesegmentcol{7}{black!75}
\csvpiesegmentcol{8}{black!85}
\fi
\define@key{csvpie}{start}{\setcounter{csvstartangle}{#1}}
\define@key{csvpie}{total}{\setcounter{csvpietotal}{#1}}
\define@key{csvpie}{radius}{\setlength{\csvradius}{#1}}
\define@key{csvpie}{inner}{\setlength{\csvinner}{#1\csvradius}}
\define@key{csvpie}{outer}{\setlength{\csvouter}{#1\csvradius}}
\define@key{csvpie}{offset}{\setlength{\csvcutawayoffset}{#1\csvradius}}
\define@key{csvpie}{cutaway}{\renewcommand*{\csvpiecutaways}{#1}}
\providecommand*{\csv@startrow}{1}
\define@key{csvpie}{firstrow}{\renewcommand*{\csv@startrow}{#1}}
\newcommand{\csvpiechart}{%
\@ifstar{\def\@@pieapplyCSVfile{\applyCSVfile*}%
\def\csv@startrow{1}%
\@csvpiechart}{%
\def\@@pieapplyCSVfile{\applyCSVfile}%
\def\csv@startrow{2}%
\@csvpiechart}%
}
\newcommand*{\csvpiesetkeys}[1]{%
\edef\csv@piesk{\noexpand\setkeys{csvpie}{\csvpiedefaults,#1}}%
\csv@piesk}
\newcommand{\@csvpiechart}[3][]{%
{\csvpiesetkeys{#1}%
\edef\csv@startang{\number\c@csvstartangle}%
\csv@computeangles{#2}{#3}%
\setcounter{csvstartangle}{\csv@startang}%
\begin{tikzpicture}%
\@@pieapplyCSVfile[\csv@startrow]{#3}{%
\csvpiesegment{#2}}%
\end{tikzpicture}%
}}
\newcommand*{\csvpiesegment}[1]{%
\setcounter{csvstartangle}{%
\csname csv@sang@\romannumeral\c@csvrownumber\endcsname}%
\setcounter{csvangle}{%
\csname csv@angle@\romannumeral\c@csvrownumber\endcsname}%
\setcounter{csvendangle}{\value{csvangle} + \value{csvstartangle}}%
\setcounter{csvangle}{%
\csname csv@cut@angle\romannumeral\c@csvrownumber\endcsname}%
\ifthenelse{\value{csvangle}>180}{\addtocounter{csvangle}{-360}}{}%
\edef\@csv@shift{(\number\c@csvangle:%
\csname csv@cut@len\romannumeral\c@csvrownumber\endcsname)}%
\setcounter{csvangle}{%
\csname csv@angle@\romannumeral\c@csvrownumber\endcsname/2
+\value{csvstartangle}}%
\begin{scope}[shift={\@csv@shift}]%
\fill[color=\csvpiesegcolname\c@csvrownumber] (0,0) --
(\thecsvstartangle:\csvradius)
arc (\thecsvstartangle:\thecsvendangle:\csvradius) -- cycle;
\ifthenelse{\(\value{csvangle}>90 \and \value{csvangle}<270\)
\TE@or \value{csvangle}<-90}{%
\setcounter{csvstartangle}{\value{csvangle}-180}%
\draw (\thecsvangle:\csvinner)
node[left,rotate=\thecsvstartangle]{\csvpieinnerlabel};
\draw (\thecsvangle:\csvouter)
node[left,rotate=\thecsvstartangle]{\csvpieouterlabel};
}{%
\draw (\thecsvangle:\csvinner)
node[right,rotate=\thecsvangle]{\csvpieinnerlabel};
\draw (\thecsvangle:\csvouter)
node[right,rotate=\thecsvangle]{\csvpieouterlabel};
}%
\end{scope}%
}
\newcommand*{\csv@computeangles}[2]{%
\@@pieapplyCSVfile[\csv@startrow]{#2}{%
\csvsetsegmentparams{\c@csvrownumber}{#1}%
}%
\ifthenelse{\equal{\csvpiecutaways}{}}{}{\csvcomputeoffsets}}
\newcommand*{\csvcomputeoffsets}{%
\@for\csv@row:=\csvpiecutaways\do{%
\expandafter\@csv@set@off\csv@row-\relax
}}
\def\@csv@set@off#1-#2\relax{%
\ifthenelse{\equal{#2}{}}{%
\@@csv@set@off{#1}}{%
\@@csv@set@offr#1-#2\relax}%
}
\newcommand*{\@@csv@set@off}[1]{%
\setcounter{csvangle}{%
\csname csv@angle@\romannumeral#1\endcsname/2
+ \csname csv@sang@\romannumeral#1\endcsname}%
\expandafter\xdef\csname csv@cut@angle\romannumeral#1\endcsname{%
\number\c@csvangle}%
\expandafter
\gdef\csname csv@cut@len\romannumeral\csv@row\endcsname{%
\csvcutawayoffset}%
}
\newcount\@csv@seg
\def\@@csv@set@offr#1-#2-\relax{%
\ifnum#1>#2\relax
\PackageError{csvpie}{Segment ranges must go in ascending
order}{Try #2-#1 instead of #1-#2}%
\else
\setcounter{csvangle}{0}%
\@csv@seg=#1\relax
\whiledo{\not\(\@csv@seg > #2\)}{%
\addtocounter{csvangle}{%
\csname csv@angle@\romannumeral\@csv@seg\endcsname}%
\advance\@csv@seg by 1}%
\setcounter{csvangle}{\value{csvangle}/2
+ \csname csv@sang@\romannumeral#1\endcsname}%
\@csv@seg=#1\relax
\whiledo{\not\(\@csv@seg > #2\)}{%
\expandafter\xdef\csname csv@cut@angle\romannumeral\@csv@seg\endcsname{%
\number\c@csvangle}%
\expandafter
\gdef\csname csv@cut@len\romannumeral\@csv@seg\endcsname{%
\csvcutawayoffset}%
\advance\@csv@seg by 1}%
\fi
}
\newcommand*{\csvsetsegmentparams}[2]{%
\ifthenelse{\value{csvstartangle}>180}{%
\addtocounter{csvstartangle}{-360}}{}%
\ifthenelse{\value{csvstartangle}<-180}{%
\addtocounter{csvstartangle}{360}}{}%
\expandafter
\xdef\csname csv@sang@\romannumeral#1\endcsname{%
\number\c@csvstartangle}%
\setcounter{csvangle}{360*\real{#2}/\value{csvpietotal}}%
\addtocounter{csvstartangle}{\value{csvangle}}%
\expandafter
\xdef\csname csv@angle@\romannumeral#1\endcsname{%
\number\c@csvangle}%
\expandafter
\gdef\csname csv@cut@angle\romannumeral#1\endcsname{0}%
\expandafter
\gdef\csname csv@cut@len\romannumeral#1\endcsname{0cm}%
}
\endinput
%%
%% End of file `csvpie.sty'.
