示例 1
我发现这网络上不错的饼图作为模板:
最小工作示例(MWE):
% Pie chart
% Author: Robert Vollmert
\documentclass{article}
\usepackage{calc}
\usepackage{ifthen}
\usepackage{tikz}
\begin{document}
\newcommand{\slice}[4]{
\pgfmathparse{0.5*#1+0.5*#2}
\let\midangle\pgfmathresult
% slice
\draw[thick,fill=black!10] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;
% outer label
\node[label=\midangle:#4] at (\midangle:1) {};
% inner label
\pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
\let\temp\pgfmathresult
\pgfmathparse{max(\temp,-0.5) + 0.8}
\let\innerpos\pgfmathresult
\node at (\midangle:\innerpos) {#3};
}
\begin{tikzpicture}[scale=3]
\newcounter{a}
\newcounter{b}
\foreach \p/\t in {20/type A, 4/type B, 11/type C,
49/type D, 16/other}
{
\setcounter{a}{\value{b}}
\addtocounter{b}{\p}
\slice{\thea/100*360}
{\theb/100*360}
{\p\%}{\t}
}
\end{tikzpicture}
\end{document}
我怎么能够...
- 为每一片添加自定义颜色(例如
black!30
,black!20
等等)? - 使用小数(23.32)而不是整数(23)?
示例 2
有另一个很好的例子带有漂亮天线的标签:
最小工作示例(MWE):
% 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"}}
\newcount\cyclecount \cyclecount=-1
\newcount\ind \ind=-1
\begin{tikzpicture}[nodes = {font=\sffamily}]
\foreach \percent/\name in {
46.6/Chrome,
24.6/Internet Explorer,
20.4/Firefox,
5.1/Safari,
1.3/Opera,
2.0/Other
} {
\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
\ifnum3<\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={\color}] (0,0) -- (\angle:\radius)
arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
\node at (\angle+0.5*\percent*3.6:0.7*\radius) {\percent\,\%};
\node[pin=\angle+0.5*\percent*3.6:\name]
at (\angle+0.5*\percent*3.6:\radius) {};
\pgfmathparse{\angle+\percent*3.6} % Advance angle
\xdef\angle{\pgfmathresult} % and store in \angle
\fi
};
\end{tikzpicture}
\end{document}
也许这个更容易适应。我怎样才能去除这个丑陋的颜色循环,并用每个切片的自定义颜色( 、 等)black!20
替换它?black!30
非常感谢!
答案1
就这样(在看到你发布的第二个例子之前我就已经这么做了):
\documentclass{article}
\usepackage{calc}
\usepackage{ifthen}
\usepackage{tikz}
\begin{document}
\newcommand{\slice}[5]{
\pgfmathparse{0.5*#1+0.5*#2}
\let\midangle\pgfmathresult
% \show\midangle
% slice
\draw[thick,fill=#5] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;
% outer label
\node[label=\midangle:#4] at (\midangle:1) {};
% inner label
\pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
\let\temp\pgfmathresult
\pgfmathparse{max(\temp,-0.5) + 0.8}
\let\innerpos\pgfmathresult
\node at (\midangle:\innerpos) {#3};
}
\makeatletter
\let\stripatpt\strip@pt
\makeatother
\begin{tikzpicture}[scale=3]
\newlength\lena
\newlength\lenb
\foreach \p/\t in {20.5/type A, 8.5/type B, 10/type C,
45.5/type D, 15.5/other}
{
\global\lena=\lenb
\global\lenb=\dimexpr\lenb+\dimexpr\p pt\relax
\edef\numbera{\stripatpt\lena}
\edef\numberb{\stripatpt\lenb}
\slice{\numbera/100*360}
{\numberb/100*360}
{\p\%}{\t}
{black!\p}
}
\end{tikzpicture}
\end{document}
a
您发布的第一个版本使用计数器来表示和(切片的百分比和累积百分比)的值b
,而 TeX 计数器是整数,因此(我猜您尝试过并得到一个错误)给出一个非整数会触发错误。
因此,我用长度(浮点的 TeX 等效项)替换了计数器,以允许任何实数(但我不知道负数)。
我还添加了第五个参数,\slice
即颜色。在我提供的示例中,它是黑色和切片百分比的混合。但您可以在列表中添加另一个\foreach
具有您想要的特定颜色的项目。
编辑:
至于你的第二个例子,调整起来更简单。首先,文件头上已经有了明确的颜色列表(\def\cyclelist{{"orange","blue","red","green"}}
),只需根据自己的喜好添加即可。唯一的问题是第 29 行有以下内容:
\draw[fill={\color!50},draw={\color}] (0,0) -- (\angle:\radius)
因此,如果您输入的颜色为black!10
,则fill
选项将为\color!50
,这将扩展为black!10!50
,而 不存在并会产生错误。由于您想删除彩色内容,因此可以!50
从上面的行中删除 ,然后代码将接受任何颜色。
除此之外,您可以删除此部分,正如我在第一个例子中所说的那样,在列表中添加第三个项目\forall
,如下所示:
\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\def\angle{0}
\def\radius{3}
\newcount\cyclecount \cyclecount=-1
\newcount\ind \ind=-1
\begin{tikzpicture}[nodes = {font=\sffamily}]
\foreach \percent/\name/\color in {
46.6/Chrome/black!10,
24.6/Internet Explorer/black!20,
20.4/Firefox/black!30,
5.1/Safari/black!40,
1.3/Opera/black!50,
2.0/Other/black!60
} {
\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
\ifnum3<\cyclecount % If cyclecount is larger than list
\global\cyclecount=0 % reset cyclecount and
\global\ind=0 % reset list index
\fi
% Draw angle and set labels
\draw[fill={\color},draw={\color}] (0,0) -- (\angle:\radius)
arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
\node at (\angle+0.5*\percent*3.6:0.7*\radius) {\percent\,\%};
\node[pin=\angle+0.5*\percent*3.6:\name]
at (\angle+0.5*\percent*3.6:\radius) {};
\pgfmathparse{\angle+\percent*3.6} % Advance angle
\xdef\angle{\pgfmathresult} % and store in \angle
\fi
};
\end{tikzpicture}
\end{document}
那么它就会像魔法一样起作用:)
。