Created
January 18, 2014 22:07
-
-
Save chrishuan9/8497360 to your computer and use it in GitHub Desktop.
This example shows how to draw a basic pie chart. Note that labels are automatically aligned and placed in a smart way. This makes the code more complicated. However, charts can now bee drawn without worrying about overlapping labels. http://www.texample.net/tikz/examples/pie-chart/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Pie chart | |
% Author: Robert Vollmert | |
\documentclass{article} | |
\usepackage{calc} | |
\usepackage{ifthen} | |
\usepackage{tikz} | |
%%%< | |
\usepackage{verbatim} | |
\usepackage[active,floats,tightpage]{preview} | |
\PreviewEnvironment{tikzpicture} | |
\setlength\PreviewBorder{5pt}% | |
%%%> | |
\begin{document} | |
\begin{comment} | |
:Title: Pie chart | |
:Tags: Charts, Foreach | |
This example shows how to draw a basic pie chart. Note that labels are automatically | |
aligned and placed in a smart way. This makes the code more complicated. However, | |
charts can now bee drawn without worrying about overlapping labels. | |
:Author: Robert Vollmert | |
\end{comment} | |
\newcommand{\slice}[4]{ | |
\pgfmathparse{0.5*#1+0.5*#2} | |
\let\midangle\pgfmathresult | |
% slice | |
\draw[thick,fill=black!10] (0,0) -- (#1:1) arc (#1:#2:1) -- 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}; | |
} | |
\begin{tikzpicture}[scale=3] | |
\newcounter{a} | |
\newcounter{b} | |
\foreach \p/\t in {20/type A, 4/type B, 11/type C, | |
49/type D, 16/other} | |
{ | |
\setcounter{a}{\value{b}} | |
\addtocounter{b}{\p} | |
\slice{\thea/100*360} | |
{\theb/100*360} | |
{\p\%}{\t} | |
} | |
\end{tikzpicture} | |
\end{document} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment