# Example: Pie chart

Published 2007-05-21 | Author: Robert Vollmert

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

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.

% Pie chart
% Author: Robert Vollmert
\documentclass{article}

\usepackage{calc}
\usepackage{ifthen}
\usepackage{tikz}
\begin{document}
\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}}
\slice{\thea/100*360}
{\theb/100*360}
{\p\%}{\t}
}

\end{tikzpicture}

\end{document}


• #1 Kjell Magne Fauske, April 29, 2009 at 9:04 p.m.

Updated example so that it is compatible with PGF 2.0.

• #2 Krzysztof Swientek, June 26, 2009 at 9:19 a.m.

Hello,

I've tried to download the Pie Chart example but it is impossible. When I used

wget http://media.texample.net/tikz/examples/PDF/pie-chart.pdf

ERROR: 403: Forbidden.

The same is with tex file

Regards K.

• #3 A. Rubio, September 30, 2009 at 8:54 p.m.

Looks like there's a broken link on the figure. Otherwise, I've found the site to be fantastic!

Regards, A.R.

• #4 Kjell Magne Fauske, September 30, 2009 at 9:06 p.m.

Sorry about that. Forgot to set the correct access rights. Fixed now.

• #5 Paco, October 23, 2009 at 5:19 p.m.

It would be fine if it handled non-integer values: currently e.g. 49.5 plus 50.5 equals 100 but the pies are drawn as if they summed up ~ 99, so decimal digits after comma are silently ignored, correct me if I'm wrong

• #6 iraysyvalo, October 25, 2009 at 10:26 p.m.

Paco,

You're right. It's because a counter is used in the example to add up slice percentages.

I don't know on the other hand if there is any way to solve this easily.

• #7 Anna, June 4, 2010 at 7:20 p.m.

Hi, the pie chart looks perfect! However, I have one problem, I can't seem to solve. When I input the second chart in file, the slice of the first value is not drawn (i.e. percent and label are there, but the circle line and gray coloring are missing).

thanks,

• #8 John Wright, August 18, 2010 at 6:38 p.m.

Anna, this is caused by the reuse of the counters which have to be reinitialized. I have wrapped up Vollmert's great example into a single macro call along with providing for color specification and auto scaling of input values (they need not be percents). I'll upload that soon. -john

• #9 Marco , September 30, 2010 at 2:12 p.m.

Hey.

Any chance to get this in 3D?? Thanks

• #10 Izyan, September 18, 2012 at 3:39 a.m.

Thank you for the coding.... I've tried and it really work.... Thank God!!!!!!!!!!!!!!