6

I would like to build macro environment for a Pie Chart that I have. So that I can use several times easily. I already have a code that produces Pie chart/Donut Chart. It would be a great if there would be macro for arc angle/size, colour, arc label, and label for a central circular node. I would appreciate all the help. Here, is my MWE:

\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[font=\sffamily\bfseries\large, text=white, border/.style={line width=12mm}]
 \foreach \angle/\colour/\name [remember=\angle as \first (initially 0)] in {40/blue, 90/cyan, 160/green!30!black, 200/green, 240/orange, 295/red!70, 360/teal}{
    \draw[\colour, border] (\first:1.5cm) 
         arc[start angle=\first, end angle=\angle, radius=1.5cm];
    \draw[white, line width=1mm] (\first:0.8)--++(\first:1.8);
    }
    \node[line width=1mm, draw, circle, minimum width=1.7cm, white, fill=blue!80] {A};
    \node at (20:1.5cm) {A1};
    \node at (62:1.6cm) {A2};
    \node at (125:1.5cm) {A3};
    \node at (180:1.5cm) {A4};
    \node at (221:1.5cm) {A5};
    \node at (268:1.5cm) {A6};
    \node at (330:1.5cm) {A7};
\end{tikzpicture}
\end{document}

Here is the pie chart that my current MWE producing:

This is the pie chart my code is producing at this moment.

3

It seems that my code is useful. Nice!

This code can be adapted two a \newcommand with two parameters. \mypie has two parameters, the first one is the list of end-angle/color/contents triplets and the second one and option to be added two central node (which can be fill=..., node contents=...).

\documentclass[tikz,border=2mm]{standalone}
%\usepackage{xcolor} %<- already loaded by TikZ
\usepackage{tikz}

\newcommand{\mypie}[2]{
\begin{tikzpicture}[font=\sffamily\bfseries\large, 
                    text=white, border/.style={line width=12mm}]
\foreach \angle/\colour/\name [remember=\angle as \first (initially 0)] in {#1}{
    \draw[\colour, border] (\first:1.5cm) 
         arc[start angle=\first, end angle=\angle, radius=1.5cm];
    \node[white] at ({(\angle+\first)/2}:1.5) {\name};
    \draw[white, line width=1mm] (\first:0.8)--++(\first:1.8);
}
\node[line width=1mm, draw, circle, minimum width=1.7cm, white, #2];
\end{tikzpicture}
}

\begin{document}

\mypie{40/blue/A1, 90/cyan/A2, 160/green!30!black/A3, 
       200/green/A4, 240/orange/A5, 295/red!70/A6, 
       360/teal/A7}{fill=red!30, node contents=A}

\mypie{45/blue/A1, 100/cyan/A2, 200/green!30!black/A3, 
       300/green/A4, 360/orange/A5}{fill=purple!60, node contents=Z}

\end{document}

enter image description here

  • Can we include \begin{tikzpicture}[font=\sffamily\bfseries\large, text=white, border/.style={line width=12mm}] and \end{tikzpicture} in Macro. – Santosh Mar 21 '16 at 19:32
  • @user3669801 Yes. Answer updated. – Ignasi Mar 22 '16 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.