TikZ: Advanced Commands
Drawing a pie chart
This second section on TikZ will cover slightly more
challenging commands and will guide you through making charts.
So let us start right away with a pie chart:
CODE
\documentclass[12pt]{article}
\title{Pie Chart: Step 1}
\date{}
\usepackage{tikz}
\begin{document}
\maketitle
\begin{center}
\begin{tikzpicture}
\filldraw
[line width= 3pt, opacity= 1, color= gray!30!white, draw= black] (0,0) circle (5cm);
\end{tikzpicture}
end{center}
\end{document}
So far, so easy. We just drew a circle like in the previous subsection.
The next step is still going to be about what we have already learnt
as we're going to draw lines to determine the slices of our pie chart:
CODE
\documentclass[12pt]{article}
\title{Pie Chart: Step 2}
\date{}
\usepackage{tikz}
\begin{document}
\maketitle
\begin{center}
\begin{tikzpicture}
\filldraw
[line width= 3pt, opacity= 1, color= gray!30!white, draw= black]
(0,0) circle (5cm);
\filldraw
[line width= 1.2pt, opacity= 1, color= yellow, draw= black]
(0,0) -- (-5,0) arc (180:135:5cm);
\filldraw
[line width= 1.2pt, opacity= 1, color= yellow!60!brown, draw= black]
(0,0) -- (5,0) arc (0:135:5cm);
\filldraw
[line width= 1.2pt, opacity= 1, color= purple!60!pink, draw= black]
(0,0) -- (-5,0) arc (180:255:5cm);
\draw[line width= 1.2pt] (0,0) -- (-1.3,-4.8);
\draw[line width= 1.2pt] (0,0) -- (-3.55,3.55);
\end{tikzpicture}
\end{center}
\end{document}
These are still all commands that we've seen and used already but we can see
that their options have been used to their full extent to make the best use out of them.
Our
arc
now actually uses the first of its last specification set,
which basically swaps the side from which the angle gets drawn if we change it from
0 to 180; what's more about it, is that the angle gets generated differently.
To draw a 45 degree angle we had to "flip" our angle with 180 and then use 135
so that the 45 degree angle would be drawn on the opposite side.
The behaviour isn't as simple, but since we're looking to simplify LaTex
and not make it more difficult, we can generalize and say that if we flip the angle
with the 180 value then the degree of the angle begins at 90 and not 0 anymore;
because of this arc
requires a bit of trial and error to get right at first
so you might want to experiment around with it more before actually implementing it.
Other than arc
we also drew lines to close our slices and
there isn't much new about that.
To show what can happen if you mess up the "flip value" that we've used,
you can find the following examples of what went wrong while our pie chart
was being written:
CODE
\documentclass[12pt]{article}
\title{Failed Examples}
\date{}
\usepackage{tikz}
\begin{document}
\maketitle
\begin{center}
\begin{tikzpicture}
\filldraw
[line width= 3pt, opacity= 1, color= gray!30!white, draw= black]
(0,0) circle (5cm);
\filldraw
[line width= 1.2pt, opacity= 1, color= yellow, draw= black]
(0,0) -- (-5,0) arc (180:135:5cm);
\filldraw
[line width= 1.2pt, opacity= 1, color= yellow!60!brown, draw= black]
(0,0) -- (5,0) arc (0:135:5cm);
\filldraw
[line width= 1.2pt, opacity= 1, color= purple!60!pink, draw= black]
(0,0) -- (-5,0) arc (360:255:5cm);
\draw[line width= 1.2pt] (0,0) -- (-1.3,-4.8);
\draw[line width= 1.2pt] (0,0) -- (-3.55,3.55);
\end{tikzpicture}
\end{center}
\begin{center}
\begin{tikzpicture}
\filldraw
[line width= 3pt, opacity= 1, color= gray!30!white, draw= black]
(0,0) circle (5cm);
\filldraw
[line width= 1.2pt, opacity= 1, color= yellow, draw= black]
(0,0) -- (-5,0) arc (270:135:5cm);
\filldraw
[line width= 1.2pt, opacity= 1, color= yellow!60!brown, draw= black]
(0,0) -- (5,0) arc (0:135:5cm);
\filldraw
[line width= 1.2pt, opacity= 1, color= purple!60!pink, draw= black]
(0,0) -- (-5,0) arc (180:255:5cm);
\draw[line width= 1.2pt] (0,0) -- (-1.3,-4.8);
\draw[line width= 1.2pt] (0,0) -- (-3.55,3.55);
\end{tikzpicture}
\end{center}
\begin{center}
\begin{tikzpicture}
\filldraw
[line width= 3pt, opacity= 1, color= gray!30!white, draw= black]
(0,0) circle (5cm);
\filldraw
[line width= 1.2pt, opacity= 1, color= yellow, draw= black]
(0,0) -- (-5,0) arc (180:135:5cm);
\filldraw
[line width= 1.2pt, opacity= 1, color= yellow!60!brown, draw= black]
(0,0) -- (5,0) arc (255:135:5cm);
\filldraw
[line width= 1.2pt, opacity= 1, color= purple!60!pink, draw= black]
(0,0) -- (-5,0) arc (180:255:5cm);
\draw[line width= 1.2pt] (0,0) -- (-1.3,-4.8);
\draw[line width= 1.2pt] (0,0) -- (-3.55,3.55);
\end{tikzpicture}
\end{center}
\end{document}
Next up we're going to see how to insert text on our chart via the
node
command:
CODE
\documentclass[12pt]{article}
\title{Pie Chart: Step 3}
\date{}
\usepackage{tikz}
\begin{document}
\maketitle
\begin{center}
\begin{tikzpicture}
\filldraw[line width= 3pt, opacity= 1, color= gray!30!white, draw= black]
(0,0) circle (5cm);
\filldraw[line width= 1.2pt, opacity= 1, color= yellow, draw= black]
(0,0) -- (-5,0) arc (180:135:5cm);
\filldraw[line width= 1.2pt, opacity= 1, color= yellow!60!brown, draw= black]
(0,0) -- (5,0) arc (0:135:5cm);
\filldraw[line width= 1.2pt, opacity= 1, color= purple!60!pink, draw= black]
(0,0) -- (-5,0) arc (180:255:5cm);
\draw[line width= 1.2pt] (0,0) -- (-1.3,-4.8);
\draw[line width= 1.2pt] (0,0) -- (-3.55,3.55);
\draw (1,2.5) node[font= \Large] {Pie I have not eaten};
\draw (1.8,-2.5) node[font= \Large] {Pie I have eaten};
\draw (-3,1) node[font= \Large] {Pie I have cut};
\draw (-2.3,-2) node[font= \Large, text width= 3cm] {Pie with magical properties};
\end{tikzpicture}
\end{center}
\end{document}
Author's note:over the internet you will find different ways of
typing nodes, this is the one I'm most comfortable with but if you find
a way that better fits your style feel free to follow it instead.
So now we've successfully added text to our chart with
So now we've successfully added text to our chart with
node
.
As you can see it works mostly the same as the other commands, you
first \draw
, you give the coordinates and then you specify
that it is a node
, we then added options to make the font
bigger in the last node
a text width
was specified
in order to prevent the text to overflow outside the chart.
Other than that, by giving the option draw= *color*
(where color is a color of your choice), a default rectangle box will be drawn
around your node
and you can then change the shape of said box
by typing circle
as an option, for instance, or any other shape
supported by TikZ or by other additional packages you might find useful.
Surely there are statistic packages that will let you create charts of this
kind with percentages at the ready and such. But for the sake of simplicity
you can obtain the same result by drawing it yourself and calculating the
percentage with a calculator. In this case we mostly took precise angles,
such as 45 degrees, but in a real chart you might have to find the right
value to insert with your node
.