我在这里得到了这个很棒的饼图代码: 如何绘制条形图和饼图 代码来自博尔代戈尔。
我正在尝试编辑代码,以便图表中没有 % 作为标签,而是类别名称,如下所示:
负责该操作的代码部分如下:
\begin{pgfonlayer}{foreground}
\path \c -- node[pos=\d,pie values,values of \s]{$\v\%$} +(\midA:\r);
\end{pgfonlayer}
为了便于理解,我将发布基于以下代码的 MWE:博尔代戈尔这里。请注意,我不是设计此代码的人,必须归功于博尔代戈尔。
\documentclass[border=10pt,multi,tikz]{standalone}
\definecolor{rosso}{RGB}{220,57,18}
\definecolor{giallo}{RGB}{255,153,0}
\definecolor{blu}{RGB}{102,140,217}
\definecolor{verde}{RGB}{16,150,24}
\definecolor{viola}{RGB}{153,0,153}
\makeatletter
\tikzstyle{chart}=[
legend label/.style={font={\scriptsize},anchor=west,align=left},
legend box/.style={rectangle, draw, minimum size=5pt},
axis/.style={black,semithick,->},
axis label/.style={anchor=east,font={\tiny}},
]
\tikzstyle{bar chart}=[
chart,
bar width/.code={
\pgfmathparse{##1/2}
\global\let\bar@w\pgfmathresult
},
bar/.style={very thick, draw=white},
bar label/.style={font={\bf\small},anchor=north},
bar value/.style={font={\footnotesize}},
bar width=.75,
]
\tikzstyle{pie chart}=[
chart,
slice/.style={line cap=round, line join=round, very thick,draw=white},
pie title/.style={font={\bf}},
slice type/.style 2 args={
##1/.style={fill=##2},
values of ##1/.style={}
}
]
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\newcommand{\pie}[3][]{
\begin{scope}[#1]
\pgfmathsetmacro{\curA}{90}
\pgfmathsetmacro{\r}{1}
\def\c{(0,0)}
\node[pie title] at (90:1.3) {#2};
\foreach \v/\s in{#3}{
\pgfmathsetmacro{\deltaA}{\v/100*360}
\pgfmathsetmacro{\nextA}{\curA + \deltaA}
\pgfmathsetmacro{\midA}{(\curA+\nextA)/2}
\path[slice,\s] \c
-- +(\curA:\r)
arc (\curA:\nextA:\r)
-- cycle;
\pgfmathsetmacro{\d}{max((\deltaA * -(.5/50) + 1) , .5)}
\begin{pgfonlayer}{foreground}
\path \c -- node[pos=\d,pie values,values of \s]{$\v\%$} +(\midA:\r);
\end{pgfonlayer}
\global\let\curA\nextA
}
\end{scope}
}
\newcommand{\legend}[2][]{
\begin{scope}[#1]
\path
\foreach \n/\s in {#2}
{
++(0,-10pt) node[\s,legend box] {} +(5pt,0) node[legend label] {\n}
}
;
\end{scope}
}
\begin{document}
\begin{tikzpicture}
[
pie chart,
slice type={comet}{blu},
slice type={legno}{rosso},
slice type={coltello}{giallo},
slice type={sedia}{viola},
slice type={caffe}{verde},
pie values/.style={font={\small}},
scale=2
]
\pie{2008}{73/comet,13/legno,7/sedia,7/coltello}
\pie[xshift=2.2cm,values of coltello/.style={pos=1.1}]%
{2009}{52/comet,23/legno,17/sedia,3/coltello,5/caffe}
\pie[xshift=4.4cm,values of caffe/.style={pos=1.1}]%
{2010}{56/comet,26/legno,9/sedia,7/coltello,2/caffe}
\legend[shift={(0cm,-1cm)}]{{Comet (Pordenone)}/comet, {Wood and furniture (Livenza)}/legno, {Knife (Maniago)}/coltello}
\legend[shift={(3cm,-1cm)}]{{Chair (Manzano)}/sedia, {Coffee (Trieste)}/caffe}
\end{tikzpicture}
\end{document}
答案1
类别是宏\s
:
\path \c -- node[pos=\d,pie values,values of \s]{\s} +(\midA:\r);
完整示例(基于 Bordaigorl 的回答):
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\definecolor{rosso}{RGB}{220,57,18}
\definecolor{giallo}{RGB}{255,153,0}
\definecolor{blu}{RGB}{102,140,217}
\definecolor{verde}{RGB}{16,150,24}
\definecolor{viola}{RGB}{153,0,153}
\makeatletter
\tikzstyle{chart}=[
legend label/.style={font={\scriptsize},anchor=west,align=left},
legend box/.style={rectangle, draw, minimum size=5pt},
axis/.style={black,semithick,->},
axis label/.style={anchor=east,font={\tiny}},
]
\tikzstyle{bar chart}=[
chart,
bar width/.code={
\pgfmathparse{##1/2}
\global\let\bar@w\pgfmathresult
},
bar/.style={very thick, draw=white},
bar label/.style={font={\bf\small},anchor=north},
bar value/.style={font={\footnotesize}},
bar width=.75,
]
\tikzstyle{pie chart}=[
chart,
slice/.style={line cap=round, line join=round, very thick,draw=white},
pie title/.style={font={\bf}},
slice type/.style 2 args={
##1/.style={fill=##2},
values of ##1/.style={}
}
]
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\newcommand{\pie}[3][]{
\begin{scope}[#1]
\pgfmathsetmacro{\curA}{90}
\pgfmathsetmacro{\r}{1}
\def\c{(0,0)}
\node[pie title] at (90:1.3) {#2};
\foreach \v/\s in{#3}{
\pgfmathsetmacro{\deltaA}{\v/100*360}
\pgfmathsetmacro{\nextA}{\curA + \deltaA}
\pgfmathsetmacro{\midA}{(\curA+\nextA)/2}
\path[slice,\s] \c
-- +(\curA:\r)
arc (\curA:\nextA:\r)
-- cycle;
\pgfmathsetmacro{\d}{max((\deltaA * -(.5/50) + 1) , .5)}
\begin{pgfonlayer}{foreground}
\path \c -- node[pos=\d,pie values,values of \s]{\s} +(\midA:\r);
\end{pgfonlayer}
\global\let\curA\nextA
}
\end{scope}
}
\newcommand{\legend}[2][]{
\begin{scope}[#1]
\path
\foreach \n/\s in {#2}
{
++(0,-10pt) node[\s,legend box] {} +(5pt,0) node[legend label] {\n}
}
;
\end{scope}
}
\begin{document}
\begin{tikzpicture}
[
pie chart,
slice type={A}{blu},
slice type={B}{rosso},
slice type={C}{gray},
pie values/.style={font={\small}},
scale=2
]
\pie{Title}{50/A, 25/B, 25/C}
\legend[shift={(0cm, -1cm)}]{
{A (50\,\%)}/A,
{B (25\,\%)}/B,
{C (25\,\%)}/C%
}
\end{tikzpicture}
\end{document}
答案2
这与原始示例不太兼容,因为没有空间放置标签。但是,它应该提供基本的想法。
我对代码进行了一些现代化修改。
\tikzstyle
已弃用,因此我将其替换为\tikzset
。\bf
等。对于 LaTeX 来说已经过时 20 多年了,不应该在 LaTeX 中使用。请改用\bfseries
等。backgrounds
是 Ti钾Z 库提供了一种访问背景层的便捷方法。我复制并修改了相关代码,以便对前景层执行相同操作。
与问题相关的实质性变化是
\begin{scope}[on foreground layer]
\path \c -- node[pos=\d,pie values,values of \s]{\expandafter\MakeUppercase\s} +(\midA:\r);
\end{scope}
也就是说,我使用\expandafter\MakeUppercase\s
而不是$\v\%$
作为标签。如果您不想将其大写,只需使用\s
即可。
这是由于被压缩而变得有些混乱的结果:
完整代码改编自 Bordaigorl 的回答:
\documentclass[border=10pt,multi,tikz]{standalone}
\definecolor{rosso}{RGB}{220,57,18}
\definecolor{giallo}{RGB}{255,153,0}
\definecolor{blu}{RGB}{102,140,217}
\definecolor{verde}{RGB}{16,150,24}
\definecolor{viola}{RGB}{153,0,153}
\usetikzlibrary{backgrounds}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\makeatletter
\tikzset{% \tikzstyle is deprecated
chart/.style={%
legend label/.style={font=\scriptsize,anchor=west,align=left},
legend box/.style={rectangle, draw, minimum size=5pt},
axis/.style={black,semithick,->},
axis label/.style={anchor=east,font=\tiny},
},
bar chart/.style={%
chart,
bar width/.code={
\pgfmathparse{##1/2}
\global\let\bar@w\pgfmathresult
},
bar/.style={very thick, draw=white},
bar label/.style={font=\bfseries\small,anchor=north},% \bfseries is 20+ years obsolete in LaTeX!
bar value/.style={font=\footnotesize},
bar width=.75,
},
pie chart/.style={%
chart,
slice/.style={line cap=round, line join=round, very thick,draw=white},
pie title/.style={font=\bfseries},
slice type/.style 2 args={%
##1/.style={fill=##2},
values of ##1/.style={}
},
},
on foreground layer/.style={% adapted from tex/generic/pgf/frontendlayer/tikz/libraries/tikzlibrarybackgrounds.code.tex
execute at begin scope={%
\pgfonlayer{foreground}%
\let\tikz@options=\pgfutil@empty%
\tikzset{every on foreground layer/.try,#1}%
\tikz@options%
},
execute at end scope={\endpgfonlayer}
},
}
\newcommand{\pie}[3][]{%
\begin{scope}[#1]
\pgfmathsetmacro{\curA}{90}
\pgfmathsetmacro{\r}{1}
\def\c{(0,0)}
\node[pie title] at (90:1.3) {#2};
\foreach \v/\s in {#3} {
\pgfmathsetmacro{\deltaA}{\v/100*360}
\pgfmathsetmacro{\nextA}{\curA + \deltaA}
\pgfmathsetmacro{\midA}{(\curA+\nextA)/2}
\path[slice,\s] \c
-- +(\curA:\r)
arc (\curA:\nextA:\r)
-- cycle;
\pgfmathsetmacro{\d}{max((\deltaA * -(.5/50) + 1) , .5)}
\begin{scope}[on foreground layer]
\path \c -- node[pos=\d,pie values,values of \s]{\expandafter\MakeUppercase\s} +(\midA:\r);
\end{scope}
\global\let\curA\nextA
}
\end{scope}
}
\begin{document}
\begin{tikzpicture}
[
pie chart,
slice type={comet}{blu},
slice type={legno}{rosso},
slice type={coltello}{giallo},
slice type={sedia}{viola},
slice type={caffe}{verde},
pie values/.style={font={\small}},
scale=2
]
\pie{2008}{73/comet,13/legno,7/sedia,7/coltello}
\pie[xshift=2.2cm,values of coltello/.style={pos=1.1}]%
{2009}{52/comet,23/legno,17/sedia,3/coltello,5/caffe}
\pie[xshift=4.4cm,values of caffe/.style={pos=1.1}]%
{2010}{56/comet,26/legno,9/sedia,7/coltello,2/caffe}
\end{tikzpicture}
\end{document}