summaryrefslogtreecommitdiff
path: root/dev/pgf-pie.sty
blob: 717be85909c9ca49faa6020de6bfef9fb4f7fcfd (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% 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}

% 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);
  % slice
  \draw[fill=#6, #9] (O) -- ++(#1:#7) arc (#1:#2:#7) -- cycle;

  % outer label
  \path (O) -- ++ (\midangle:#7) node[label=\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}

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

  \def\explodearray{{\explode}}
  \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{\explodearray[int(mod(\i,\value{pgfpie@explodeLength}))]}
    \let\e\pgfmathresult

    % 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/100*360}
    {\thepgfpie@angleEnd/100*360}
    {\p\%}
    {\t}
    {\e}
    {\thecolor}
    {\radius}
    {\pos}
    {\style}
  }
}

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