summaryrefslogtreecommitdiff
path: root/dev/pgf-pie.sty
blob: d85467fc5dbe159edea27107c068cc26de96a0fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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}

\newcommand{\pgfpie@slice}[6]{
  \pgfmathparse{0.5*#1+0.5*#2}
  \let\midangle\pgfmathresult

  \pgfmathparse{1}
  \let\radius\pgfmathresult
  % slice
  \draw[thick,fill=#6, drop shadow] (\midangle:#5) -- ++(#1:\radius) arc (#1:#2:\radius) -- cycle;

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

  % inner label
  \pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
  \let\temp\pgfmathresult
  \pgfmathparse{max(\temp,-0.5) + 0.8}
  \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}

\newcommand{\pie}[2][]
{
  \pgfkeys{explode=0,
    colormap={blue!60, cyan!60, yellow!60, orange!60, red!60}}
  \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}
  \begin{tikzpicture}[scale=3,
% x={(1cm,0cm)}, y={(0cm,0.5cm)}
]

    \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}
    }

  \end{tikzpicture}
}

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