📚 The CoCalc Library - books, templates and other resources
cocalc-examples / martinthoma-latex-examples / presentations / Gute-Frage-Schluesselqualifikation / LaTeX / pgf-pie.sty
132941 viewsLicense: OTHER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%1% Start of pgf-pie.sty2%3% Some LaTeX macros for pie chart by using PGF/Tikz package.4% Home page of project: http://pgf-pie.googlecode.com/5% Author: Xu Yuan <[email protected]>6%78\NeedsTeXFormat{LaTeX2e}[1999/12/01]9\ProvidesPackage{pgf-pie}[2011/10/02 v0.2 Some LaTeX macros for pie10chart by using PGF/Tikz package.]1112\RequirePackage{tikz}13\RequirePackage{ifthen}14\RequirePackage{scalefnt}151617% args:18% #1: begin angle19% #2: end angle20% #3: number21% #4: label22% #5: explode23% #6: fill color24% #7: radius25% #8: center26\newcommand{\pgfpie@slice}[8]{27\pgfmathparse{0.5*(#1)+0.5*(#2)}28\let\midangle\pgfmathresult2930\path (#8) -- ++(\midangle:#5) coordinate(O);3132\pgfmathparse{#7+#5}33\let\radius\pgfmathresult3435% slice36\draw[line join=round, fill=#6, \style] (O) -- ++(#1:#7) arc (#1:#2:#7) -- cycle;3738\pgfmathparse{min(((#2)-(#1)-10)/110*(-0.3),0)}39\let\temp\pgfmathresult40\pgfmathparse{(max(\temp,-0.5) + 0.8)*#7}41\let\innerpos\pgfmathresult4243\ifthenelse{\equal{\pgfpie@text}{inside}}44{45% label and number together46\path (O) -- ++(\midangle:\innerpos) node47{\scalefont{#3}\shortstack{#4\\\beforenumber#3\afternumber}};48}49{50% label51\iflegend52\else53\path (O) -- ++ (\midangle:\radius)54node[inner sep=0, \pgfpie@text=\midangle:#4]{};55\fi5657% number58\path (O) -- ++(\midangle:\innerpos) node59{\scalefont{#3}\beforenumber#3\afternumber};60}61}6263\newcommand{\pgfpie@findColor}[1]64{65\pgfmathparse{int(mod(#1,\value{pgfpie@colorLength}))}66\let\ci\pgfmathresult67\foreach \c [count=\j from 0] in \color {68\ifnum \j=\ci69\xdef\thecolor{\c}70\thecolor71\breakforeach72\fi73}74}7576\newcommand{\pgfpie@findExplode}[1]77{78\pgfmathparse{int(mod(#1,\value{pgfpie@explodeLength}))}79\let\ei\pgfmathresult80\foreach \e [count=\j from 0] in \explode {81\ifnum \j=\ei82\xdef\theexplode{\e}83\breakforeach84\fi85}86}8788% #1: bottom left point89% #2: size90% #3: number91% #4: color92% #5: text93\newcommand{\pgfpie@square}[5]94{95\ifthenelse{\equal{\pgfpie@text}{inside}}96{97\draw[fill=#4, \style] (#1) rectangle node98{\scalefont{#3}\shortstack{#5\\\beforenumber#3\afternumber}} ++(#2);99}100{101\draw[fill=#4, \style] (#1) rectangle node102{\scalefont{#3}\beforenumber#3\afternumber} ++(#2);103}104}105106% #1: pos107% #2: radius108% #3: number109% #4: color110% $5: style111% $6: label112\newcommand{\pgfpie@cloud}[6]113{114\draw[fill=#4, #5] (#1) circle[radius=#2];115\ifthenelse{\equal{\pgfpie@text}{inside}}116{117\node at (#1) {\scalefont{#3}\shortstack{#6\\\beforenumber#3\afternumber}};118}119{120\node at (#1) {\scalefont{#3}\beforenumber#3\afternumber};121}122}123124\newlength{\pgfpie@angleEnd}125\newcounter{pgfpie@explodeLength}126\newcounter{pgfpie@colorLength}127\newcounter{pgfpie@sliceLength}128129\def\setexplode#1\pgfeov{\def\explode{#1}}130\pgfkeyslet{/explode/.@cmd}{\setexplode}131132\def\setcolor#1\pgfeov{\def\color{#1}}133\pgfkeyslet{/color/.@cmd}{\setcolor}134135\def\setradius#1\pgfeov{\def\radius{#1}}136\pgfkeyslet{/radius/.@cmd}{\setradius}137138\def\setpos#1\pgfeov{\def\pos{#1}}139\pgfkeyslet{/pos/.@cmd}{\setpos}140141\def\setstyle#1\pgfeov{\def\style{#1}}142\pgfkeyslet{/style/.@cmd}{\setstyle}143144\def\setbeforenumber#1\pgfeov{\def\beforenumber{#1}}145\pgfkeyslet{/before number/.@cmd}{\setbeforenumber}146147\def\setafternumber#1\pgfeov{\def\afternumber{#1}}148\pgfkeyslet{/after number/.@cmd}{\setafternumber}149150\def\settext#1\pgfeov{\xdef\pgfpie@text{#1}}151\pgfkeyslet{/text/.@cmd}{\settext}152153\def\setsum#1\pgfeov{\xdef\pgfpie@sum{#1}}154\pgfkeyslet{/sum/.@cmd}{\setsum}155156\def\setrotate#1\pgfeov{\xdef\rotate{#1}}157\pgfkeyslet{/rotate/.@cmd}{\setrotate}158159\newif\ifpolar160\pgfkeys{/polar/.is if=polar}161162\newif\iflegend163164\newif\ifsquare165\pgfkeys{/square/.is if=square}166167\newif\ifcloud168\pgfkeys{/cloud/.is if=cloud}169170\newif\ifscalefont171\pgfkeys{/scale font/.is if=scalefont}172\let\scalefontorg\scalefont173\renewcommand{\scalefont}[1]174{175\ifscalefont176\pgfmathparse{#1 / \pgfpie@sum * 3 + 0.9}177\scalefontorg{\pgfmathresult}178\fi179}180181\newcommand{\pie}[2][]182{183% load default parameters184\pgfkeys{185explode=0,186color={blue!60, cyan!60, yellow!60, orange!60, red!60,187blue!60!cyan!60, cyan!60!yellow!60, red!60!cyan!60,188red!60!blue!60, orange!60!cyan!60},189radius=3,190pos={0,0},191style={thick},192before number=,193after number=,194text=label,195sum=100,196rotate=0,197polar=false,198square=false,199cloud=false,200scale font=false,201}202% load user's parameters203\pgfkeys{#1}204% add percentage automatically205\ifthenelse{\equal{\pgfpie@sum}{100}}206{207\pgfkeys{after number=\%}208\pgfkeys{#1}209}{}210211% legend or not212\ifthenelse{\equal{\pgfpie@text}{legend}}213{\legendtrue}214{\legendfalse}215216% handle sum217\ifthenelse{\equal{\pgfpie@sum}{auto}}218{219% sum all input220\xdef\pgfpie@sum{0}221\foreach \p/\t in {#2}222{223\pgfmathparse{\pgfpie@sum + \p}224\xdef\pgfpie@sum{\pgfmathresult}225}226}227{}228229% init counters230\setcounter{pgfpie@explodeLength}{0}231\foreach \e in \explode { \addtocounter{pgfpie@explodeLength}{1} }232233\setcounter{pgfpie@colorLength}{0}234\foreach \c in \color { \addtocounter{pgfpie@colorLength}{1} }235236\pgfmathsetlength{\pgfpie@angleEnd}{0}237238\setcounter{pgfpie@sliceLength}{0}239\foreach \p/\e in {#2} { \addtocounter{pgfpie@sliceLength}{1} }240241\ifsquare242%%%%%%%%%% SQUARE PIE BEGIN %%%%%%%%%%%243\pgfmathparse{\radius*2}244\xdef\verticalLength{\pgfmathresult}245\xdef\horizontalLength{\pgfmathresult}246\path (\pos) -- ++(-\radius, -\radius) coordinate (start);247\pgfmathparse{\verticalLength * \horizontalLength / \pgfpie@sum}248\let\squareUnit\pgfmathresult249250% drawing loop251\foreach \p/\t [count=\i from 0] in {#2}252{253\pgfpie@findColor{\i}254255\ifthenelse{\lengthtest{\verticalLength cm > \horizontalLength cm}}256{257\pgfmathparse{\p * \squareUnit / \horizontalLength}258\let\height\pgfmathresult259260\pgfpie@square{start}{\horizontalLength,\height}261{\p}262{\thecolor}263{\t}264%label265\iflegend266\else267\ifthenelse{\equal{\pgfpie@text}{inside}}268{}269{270\path (start) -- ++(\horizontalLength,\height*0.5) node[inner271sep=0, \pgfpie@text=0:\t]{};272}273\fi274275\pgfmathparse{\verticalLength - \height}276\xdef\verticalLength{\pgfmathresult}277\path (start) -- ++(0, \height) coordinate (start);278}279{280\pgfmathparse{\p * \squareUnit / \verticalLength }281\let\width\pgfmathresult282283\pgfpie@square{start}{\width,\verticalLength}284{\p}285{\thecolor}286{\t}287%label288\iflegend289\else290\ifthenelse{\equal{\pgfpie@text}{inside}}291{}292{293\path (start) -- ++(\width*0.5,\verticalLength) node[inner294sep=0, \pgfpie@text=90:\t]{};295}296\fi297298\pgfmathparse{\horizontalLength - \width}299\xdef\horizontalLength{\pgfmathresult}300\path (start) -- ++(\width, 0) coordinate (start);301}302}303304%%%%%%%%%% SQUARE PIE END %%%%%%%%%%%305\else306\ifcloud307%%%%%%%%%% CLOUD PIE BGEIN %%%%%%%%%%%308% drawing loop309\foreach \p/\t [count=\i from 0] in {#2}310{311% find explode312\pgfpie@findExplode{\i}313\def\cloudGap{\theexplode + 0.1}314\pgfmathparse{sqrt(\p / \pgfpie@sum) * \radius}315\let\cloudR\pgfmathresult316\ifnum \i = 0317% first cloud318\coordinate (O) at (\pos);319\xdef\cloudRone{\cloudR}320\xdef\cloudExtendDir{180+\rotate}321\else322\ifnum \i = 1323% second cloud324\xdef\cloudRtwo{\cloudR}325\xdef\cloudExtendDir{45+\rotate}326\path (O) -- ++(\cloudExtendDir:\cloudRone+\cloudGap+\cloudRtwo) coordinate (O);327\else328% next cloud329\pgfmathparse{\cloudRone+\cloudGap+\cloudRtwo}330\let\la\pgfmathresult331\pgfmathparse{\cloudRone+\cloudGap+\cloudR}332\let\lb\pgfmathresult333\pgfmathparse{\cloudRtwo+\cloudGap+\cloudR}334\let\lc\pgfmathresult335\pgfmathparse{\la^2+\lc^2-\lb^2}336\let\tmp\pgfmathresult337\pgfmathparse{180 - acos(\tmp / 2 / \la / \lc)}338\let\cloudRot\pgfmathresult339\ifodd \i340\pgfmathparse{-\cloudRot}341\let\cloudRot\pgfmathresult342\fi343\pgfmathparse{\cloudExtendDir - \cloudRot}344\xdef\cloudExtendDir{\pgfmathresult}345\path (O) -- ++(\cloudExtendDir:\lc) coordinate (O);346\xdef\cloudRone{\cloudRtwo}347\xdef\cloudRtwo{\cloudR}348\fi349\fi350351% find color352\pgfpie@findColor{\i}353354\pgfpie@cloud{O}{\cloudR}{\p}355{\thecolor}{\style}{\t}356357% label358\iflegend359\else360\ifthenelse{\equal{\pgfpie@text}{inside}}361{}362{363\path (O) -- ++(\cloudExtendDir:\cloudR)364node[inner sep=0, \pgfpie@text=\cloudExtendDir:\t] {};365}366\fi367}368%%%%%%%%%% CLOUD PIE BGEIN %%%%%%%%%%%369\else370%%%%%%%%%% CIRCLE PIE BGEIN %%%%%%%%%%%371\ifpolar372\xdef\maxValue{0}373\foreach \p/\e in {#2} {374\ifnum \maxValue < \p375\xdef\maxValue{\p}376\fi377}378\pgfmathparse{\pgfpie@sum / \value{pgfpie@sliceLength}}379\xdef\polarangle{\pgfmathresult}380\pgfmathparse{\radius / sqrt(\maxValue)}381\xdef\polarRadiusUnit{\pgfmathresult}382\else383\xdef\theradius{\radius}384\fi385386\xdef\pgfpie@angleBegin{\the\pgfpie@angleEnd}387% drawing loop388\foreach \p/\t [count=\i from 0] in {#2}389{390\pgfmathsetlength{\pgfpie@angleEnd}{\pgfpie@angleBegin}391\ifpolar392% Polar area diagram393\pgfmathaddtolength{\pgfpie@angleEnd}{\polarangle}394\pgfmathparse{sqrt(\p) * \polarRadiusUnit}395\xdef\theradius{\pgfmathresult}396\else397% normal pie398\pgfmathaddtolength{\pgfpie@angleEnd}{\p}399\fi400401% find explode402\pgfpie@findExplode{\i}403404% find color405\pgfpie@findColor{\i}406\pgfpie@slice{\pgfpie@angleBegin/\pgfpie@sum*360+\rotate}407{\the\pgfpie@angleEnd/\pgfpie@sum*360+\rotate}408{\p}409{\t}410{\theexplode}411{\thecolor}412{\theradius}413{\pos}414\xdef\pgfpie@angleBegin{\the\pgfpie@angleEnd}415}416%%%%%%%%%% CIRCLE PIE END %%%%%%%%%%%417\fi418\fi419420% legend421\iflegend422\coordinate[xshift=0.8cm,423yshift=(\value{pgfpie@sliceLength}*0.5+1)*0.5cm] (legendpos) at424(current bounding box.east);425426\begin{scope}[node distance=0.5cm]427\foreach \p/\t [count=\i from 0] in {#2}428{429\pgfpie@findColor{\i}430\node[draw, fill=\thecolor, \style, below of=legendpos, label=0:\t] (legendpos) {};431}432\end{scope}433\fi434}435436%%% End of pgf-pie.sty437%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%438439440441