5

I would like a tikz circle node to be a pie chart. I have following code:

\tikzstyle{bus} = [draw, circle, minimum size=2em,inner sep=0pt]
\tikzstyle{busg} = [draw, circle, minimum size=2em,inner sep=0pt,color=red]
\begin{tikzpicture}[]
\node [busg] (1) {1};
\node [bus, above =8em of 1] (5) {5};
\node [busg, right=3em of 1] (2) {2};
\draw (1) -- (5);
\draw (1) -- (2);
\end{tikzpicture}

enter image description here

How can I fill node 1 with 60% and node 2 with 30%?

9

This solution uses a path picture option to fill part of the circle. It doesn't need backgrounds library.

The pienode style is defined with 2 arguments, the first defines circle minimum size and the second is the percentage (0 to 100) to be filled.

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{positioning, calc}

\begin{document}
\begin{tikzpicture}[
    bus/.style={draw, circle, minimum size=2em, inner sep=0pt},
    pienode/.style 2 args={
    circle, minimum size=#1, 
    draw=red, text=red,
    inner sep=0pt,
    path picture={\fill[red!30] (path picture bounding box.center) 
        -- ++(0:#1) arc[start angle=0, end angle=3.6*#2, radius=#1]
        --cycle;}}]

\node[pienode={2em}{60}] (1) {1};
\node[bus, above=8em of 1] (5) {5};
\node[pienode={2em}{25}, right=3em of 1] (2) {2};

\draw (1)--(5) (1)--(2);
\end{tikzpicture}
\end{document}

enter image description here

2
  • How could I make the white space inside the circle (where there's no red) be a different color? – Addison Klinke Jul 25 '20 at 22:17
  • Add option fill= color to the desired nodes – Ignasi Jul 26 '20 at 6:58
4

You can use something like

\fill [red!40] (1.center) -- (1.0) arc[start angle=0,delta angle=360*0.6,radius=1em] -- cycle;

Here 1.0 is the same as 1.east, the 0 indicates degrees. The radius of the arc is set to half the minimum size of the node.

The backgrounds library helps you with drawing the fill behind the node itself.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\tikzset{
 bus/.style={draw, circle, minimum size=2em,inner sep=0pt},
 busg/.style={draw, circle, minimum size=2em,inner sep=0pt,color=red}
}
\usetikzlibrary{positioning,backgrounds}
\begin{document}

\begin{tikzpicture}[]
\node [busg] (1) {1};
\node [bus, above=8em of 1] (5) {5};
\node [busg, right=3em of 1] (2) {2};
\draw (1) -- (5);
\draw (1) -- (2);\fill [red!40] (1.center) -- (1.0) arc[start angle=0,delta angle=360*0.6,radius=1em] -- cycle;

\begin{scope}[on background layer]
\fill [red!40] (1.center) -- (1.0) arc[start angle=0,delta angle=360*0.6,radius=1em] -- cycle;
\fill [red!40] (2.center) -- (2.0) arc[start angle=0,delta angle=360*0.3,radius=1em] -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}

For convenience one can wrap it in a macro, and include a calculation of the node/arc radius:

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\tikzset{
 bus/.style={draw, circle, minimum size=2em,inner sep=0pt},
 busg/.style={draw, circle, minimum size=2em,inner sep=0pt,color=red}
}
\usetikzlibrary{positioning,backgrounds,calc}
\newcommand\NodePie[4][red!40]{%
% optional argument #1: fill color
% #2: node name
% #3: start angle
% #4: fraction (e.g. 0.3 for 30%)
\fill let
\p1=(#2.center), \p2=(#2.east), \n1={veclen(\x2-\x1,0)}
in
[#1] (#2.center) -- (#2.#3) arc[start angle=#3,delta angle=360*#4,radius=\n1] -- cycle;
}
\begin{document}

\begin{tikzpicture}[]
\node [busg] (1) {1};
\node [bus, above=8em of 1] (5) {5};
\node [busg, right=3em of 1] (2) {2};
\draw (1) -- (5);
\draw (1) -- (2);

\begin{scope}[on background layer]
\NodePie{1}{0}{0.6}
\NodePie[blue!30]{2}{30}{0.3}
\end{scope}
\end{tikzpicture}
\end{document}

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.