Tikz 圆节点饼图

Tikz 圆节点饼图

我想要一个 tikz 圆圈节点作为饼图。我有以下代码:

\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}

在此处输入图片描述

我怎样才能用 60% 填充节点 1,用 30% 填充节点 2?

答案1

此解决方案使用path picture选项来填充圆的一部​​分。它不需要backgrounds库。

pienode样式由 2 个参数定义,第一个参数定义圆的最小尺寸,第二个参数是需要填充的百分比(0 到 100)。

\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}

在此处输入图片描述

答案2

你可以使用类似

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

这里1.0与 相同1.east0表示度数。 的半径arc设置为minimum size的一半node

backgrounds库可帮助您在节点本身后面绘制填充。

在此处输入图片描述

\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}

为了方便起见,可以将其包装在宏中,并包括节点/圆弧半径的计算:

\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}

相关内容