我很高兴玩彩色饼图Henri Menke 的分享非常好。我根据自己的需要做了一些调整。但是,当我想为\percent
或\name
条目着色时,仍然会出现故障。见下文:我试图使用text={\color}
选项(参见实例“警告”)用当前颜色为相关文本着色来实现这一点\color
。但是,它变得疯狂,我不明白为什么。请注意,这没有帮助。这很奇怪,因为或\textcolor{\color}{\name}
选项工作得很好。fill={\color!50}
draw={\color}
% Pie chart with colors
% Author: Henri Menke
\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\def\angle{0}
\def\radius{3}
\def\cyclelist{{"orange","blue","red","green","black","pink"}}
\newcount\cyclecount
\cyclecount=-1
\newcount\ind
\ind=-1
\begin{tikzpicture}[cap=round,join=round]
\foreach \percent/\name in {
20/term 1,
20/term 2,
20/term 3,
20/term 4,
20/term 5,
} {
\ifx\percent\empty\else % If \percent is empty, do nothing
\global\advance\cyclecount by 1 % Advance cyclecount
\global\advance\ind by 1 % Advance list index
\ifnum5<\cyclecount % If cyclecount is larger than list
\global\cyclecount=0 % reset cyclecount and
\global\ind=0 % reset list index
\fi
\pgfmathparse{\cyclelist[\the\ind]} % Get color from cycle list
\edef\color{\pgfmathresult} % and store as \color
% Draw angle and set labels
\draw[fill={\color!50},draw=white] (0,0) -- (\angle:\radius) arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
\node[text={\color}] at (\angle+0.5*\percent*3.6:0.8*\radius) {\percent}; % WARNING
\draw (\angle+0.5*\percent*3.6:\radius)--(\angle+0.5*\percent*3.6:1.2*\radius)node[circle,fill=white,inner sep=0pt,text={\color}]{\name}; % WARNING
\filldraw (\angle+0.5*\percent*3.6:\radius) circle (1pt);
\pgfmathparse{\angle+\percent*3.6} % Advance angle
\xdef\angle{\pgfmathresult} % and store in \angle
\fi
};
\end{tikzpicture}
\end{document}
答案1
\color
我猜重新定义(已经存在设置文本颜色)不是一个好主意。使用不同的名称,例如\mycolor
:
\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\def\angle{0}
\def\radius{3}
\def\cyclelist{{"orange","blue","red","green","black","pink"}}
\newcount\cyclecount
\cyclecount=-1
\newcount\ind
\ind=-1
\begin{tikzpicture}[cap=round,join=round]
\foreach \percent/\name in {
20/term 1,
20/term 2,
20/term 3,
20/term 4,
20/term 5,
} {
\ifx\percent\empty\else % If \percent is empty, do nothing
\global\advance\cyclecount by 1 % Advance cyclecount
\global\advance\ind by 1 % Advance list index
\ifnum5<\cyclecount % If cyclecount is larger than list
\global\cyclecount=0 % reset cyclecount and
\global\ind=0 % reset list index
\fi
\pgfmathparse{\cyclelist[\the\ind]} % Get color from cycle list
\edef\mycolor{\pgfmathresult} % and store as \mycolor
% Draw angle and set labels
\draw[fill={\mycolor!50},draw=white] (0,0) -- (\angle:\radius) arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
\node[text={\mycolor}] at (\angle+0.5*\percent*3.6:0.8*\radius) {\percent}; % WARNING
\draw (\angle+0.5*\percent*3.6:\radius)--(\angle+0.5*\percent*3.6:1.2*\radius)node[circle,fill=white,inner sep=0pt,text={\mycolor}]{\name}; % WARNING
\filldraw (\angle+0.5*\percent*3.6:\radius) circle (1pt);
\pgfmathparse{\angle+\percent*3.6} % Advance angle
\xdef\angle{\pgfmathresult} % and store in \angle
\fi
};
\end{tikzpicture}
\end{document}