From cac24b96a2690e8b2bc0f7cb4f55a53f9cc38579 Mon Sep 17 00:00:00 2001
From: Xu Yuan <xu@informatik.hu-berlin.de>
Date: Sun, 2 Oct 2011 00:12:43 +0200
Subject: legend

---
 dev/pgf-pie.sty | 47 ++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 38 insertions(+), 9 deletions(-)

(limited to 'dev/pgf-pie.sty')

diff --git a/dev/pgf-pie.sty b/dev/pgf-pie.sty
index 84cbb1c..e26c4fb 100644
--- a/dev/pgf-pie.sty
+++ b/dev/pgf-pie.sty
@@ -36,7 +36,11 @@ chart by using PGF/Tikz package.]
   \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]{};
+  \iflegend
+  \else
+    \path (O) -- ++ (\midangle:\radius)
+    node[inner sep=0, \text=\midangle:#4]{};
+  \fi
 
   % inner label
   \pgfmathparse{min(((#2)-(#1)-10)/110*(-0.3),0)}
@@ -46,6 +50,19 @@ chart by using PGF/Tikz package.]
   \path (O) -- ++(\midangle:\innerpos) node {#3};
 }
 
+\newcommand{\pgfpie@findColor}[1]
+{
+  \pgfmathparse{int(mod(#1,\value{pgfpie@colorLength}))}
+  \let\ci\pgfmathresult
+  \foreach \c [count=\j from 0] in \color {
+    \ifnum \j=\ci
+    \xdef\thecolor{\c}
+    \thecolor
+    \breakforeach
+    \fi
+  }
+}
+
 \newcounter{pgfpie@angleBegin}
 \newcounter{pgfpie@angleEnd}
 \newcounter{pgfpie@explodeLength}
@@ -85,6 +102,8 @@ chart by using PGF/Tikz package.]
 \newif\ifpolar
 \pgfkeys{/polar/.is if=polar}
 
+\newif\iflegend
+
 \newcommand{\pie}[2][]
 {
   % load default parameters
@@ -106,6 +125,11 @@ chart by using PGF/Tikz package.]
   % load user's parameters
   \pgfkeys{#1}
 
+  % legend or not
+  \ifthenelse{\equal{\text}{legend}}
+  {\legendtrue}
+  {\legendfalse}
+
   % handle sum
   \ifthenelse{\equal{\sum}{auto}}
   {
@@ -169,14 +193,7 @@ chart by using PGF/Tikz package.]
     }
 
     % 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@findColor{\i}
 
     \pgfpie@slice{\thepgfpie@angleBegin/\sum*360+\rotate}
     {\thepgfpie@angleEnd/\sum*360+\rotate}
@@ -188,6 +205,18 @@ chart by using PGF/Tikz package.]
     {\pos}
     {\style}
   }
+
+  % legend
+  \iflegend
+    \coordinate[xshift=0.8cm] (legendpos) at (current bounding box.north east);
+    \begin{scope}[node distance=0.5cm]
+    \foreach \p/\t  [count=\i from 0] in {#2}
+    {
+      \pgfpie@findColor{\i}
+      \node[draw, fill=\thecolor, \style, below of=legendpos, label=0:\t] (legendpos) {};
+    }
+    \end{scope}
+  \fi
 }
 
 %%% End of pgf-pie.sty
-- 
cgit v1.2.3

