9.5 Drawing a pie chart

Pie charts are popular for showing proportions. A pie chart’s main characteristic is that all items usually sum up to 100 percent. They are displayed as segments of a disc.

How to do it…

We will use the pgf-pie package, which builds on TikZ and is specialized for generating pie charts. Follow these steps:

  1. Start with a document class:

  2. Load the pgf-pie package:

  3. Begin the document:

  4. Begin a TikZ picture, which will be the container of the pie chart:

  5. Draw the pie chart using this command:
     \pie [rotate = 180]
    {62/\TeX\ Live and Mac\TeX,
    32/MiK\TeX\ and Pro\TeX t, 6/Other \TeX}

  6. End the TikZ picture and the entire document:

  7. Compile and take a look at the result:

How it works…

The \pie command is the only command in the pgf-pie package. The syntax is as follows:

    \pie[options]{number1/text1, number2/text2, }

The backslashes in our example were just because of the \TeX macro and the following space.

Let’s take a look at the available options, with example values:

  • pos = 4,6: This positions the center at point (4,6). The default center is (0,0).
  • rotate = 90: This rotates the chart by 90 degrees.
  • radius = 5: This sets the chart’s radius size to 5. The default size is 3.
  • color = red: This chooses the color red for all the slices. The xcolor (and TikZ) syntax, such as red!80!black, will be understood.
  • color = {red!20, red!40, red!60}: This sets a specific red color value for each of the three slices.
  • explode = 0.1: This moves all the slices outwards by 0.1.
  • explode = { 0.2, 0, 0}: This moves only the first slice of the three outwards by 0.2.
  • sum = 50: This defines the reference sum as 50, instead of the default sum of 100.
  • sum = auto: This calculates the sum from the slice values.
  • scale font: This scales the font size according to the slice value.
  • before number = { \$ }: This inserts text before the values, in this case a dollar sign. It is empty by default.
  • after number = { percent }: This adds text after each value, in this case the word percent. With sum = 100, the default is %; otherwise, it is empty.
  • text = pin: This sets the text next to the slice, connected by a short line.
  • text = inside: This places the text within the slice.
  • text = legend: This produces a separate legend.
  • style = drop shadow: This adds a shadow below the chart.

There’s more…

The pie-chart package offers further chart designs. Let’s take a look at them, together with applying some of the styles we just covered.

Square chart

The square option gives a quadratic design. Adding the scale font and color option, we arrive at this:

      \pie [square, scale font,
color = {blue!10, blue!20, blue!40}] { ... }

With the values from our recipe, we get the following:

Square chart

Polar area chart

The polar option changes the layout so that the slices get equal angles but the radius represents the size. We add the explode and text=legend options:

      \pie [polar, explode=0.1, text=legend] { ... }

Thus, we get this output:

Cloud chart

The cloud option produces a set of discs whose sizes are according to the given values. This time, we put the text inside, scale it, and use a larger radius:

      \pie [cloud, text=inside, scale font, radius=6] { ... }

Now, the result is as follows: