根据论坛上找到的几段代码,我编写了一个简单的代码,用于创建具有可变切片数量的饼图,如下所示。现在,我可以使用第 7 行定义的主色的色调来制作切片的颜色。我正在寻找一种方法来在列表中定义我想要用于每个切片的颜色。类似于:
\def\mycolors{{"red","blue","green","yellow","","",""}}
但我似乎无法找到一种方法来在我的\foreach
循环中正确读取这个列表...有没有办法做到这一点?
我的 MWE:
\documentclass{standalone}
\usepackage{tikz}
% couleurs
\definecolor{main}{RGB}{185,30,50}
%
% paramètres du graphique
\def\theme{main}
\newcommand\shares{{57,40,3,0,0,0,0,0,0,0,0,0,0,0}}%
\def\commentaires{{"très satisfait","satisfait","peu satisfait","","","","","",""}}
\def\titre{niveau de satisfaction}
\def\nbcategories{3}
%
% code
\begin{document}
\begin{tikzpicture}%
\node[black,fill=none] at (0,2.5) {\titre};
%
\edef\startangle{0}%
\draw[\theme!95!black, line width=1pt] (0,0) circle (2.05cm);
\foreach [
remember=\endangle as \startangle,
evaluate=\i as \endangle using {\startangle+(\shares[\i-1]/100*360)},
evaluate=\halfangle using {(\endangle-\startangle)/2+\startangle},]
\i in {1,...,\nbcategories}
{%
\pgfmathsetmacro\intensite{(\nbcategories-\i)/(\nbcategories-1)*100}
\fill[{\theme!\intensite}] (0,0) --++(\startangle:2cm) arc (\startangle:\endangle:2cm);
\draw[white, line width=0.25mm](0,0)--++(\startangle:2cm+0.1pt);
}%
\draw[white,line width=0.25mm](0,0)--++(\startangle:2cm);%
\fill[white](0,0)circle [radius=2cm*0.65];%
%
\foreach [
remember=\endangle as \startangle,
evaluate=\i as \endangle using {\startangle+(\shares[\i-1]/100*360)},
evaluate=\halfangle using {(\endangle-\startangle)/2+\startangle},]
\i in {1,...,\nbcategories} {%
\node[\theme!65!black] at (\halfangle:2cm*0.475) {\textbf{\small\pgfmathparse{\shares[\i-1]}\pgfmathresult\%}};
}%
%
\foreach [] \i in {1,...,\nbcategories} {%
\pgfmathsetmacro\intensite{(\nbcategories-\i)/(\nbcategories-1)*100}
\draw[fill={\theme!\intensite},draw=black!65] (-2,-2.25-\i*0.35) -- (-1.75,-2.25-\i*0.35) -- (-1.75,-2.5-\i*0.35) -- (-2,-2.5-\i*0.35) -- cycle;
\node[right] at (-1.75,-2.375-\i*0.35) {\small \pgfmathparse{\commentaires[\i-1]}\pgfmathresult\phantom{bp}};
}%
\end{tikzpicture}%
\end{document}
答案1
这可能是一个非常低调的解决方案,但对于快速修复来说,您需要的一切都已经存在。只需使用流水号定义颜色,然后使用索引调用,无论如何您都在循环。
以下是可能的颜色定义(完全随机,仅用于原理证明):
\definecolor{category1}{RGB}{125,135,197}
\definecolor{category2}{RGB}{0,135,197}
\definecolor{category3}{RGB}{125,0,197}
\definecolor{category4}{RGB}{125,135,0}
\definecolor{category5}{RGB}{125,0,0}
\definecolor{category6}{RGB}{0,0,197}
\definecolor{category7}{RGB}{0,135,0}
\definecolor{category8}{RGB}{0,0,0}
\definecolor{category9}{RGB}{255,255,255}
然后将其替换\theme!\intensite
为category\i
您的索引\i
。
完整代码如下:
\documentclass{standalone}
\usepackage{tikz}
% couleurs
\definecolor{main}{RGB}{185,30,50}
%
% paramètres du graphique
\def\theme{main}
\newcommand\shares{{11,11,11,11,11,11,11,11,11,11,11,11,11,11}}%
\def\commentaires{{"très satisfait","satisfait","peu satisfait","x","y","z","a","b","c"}}
\def\titre{niveau de satisfaction}
\def\nbcategories{9}
\definecolor{category1}{RGB}{125,135,197}
\definecolor{category2}{RGB}{0,135,197}
\definecolor{category3}{RGB}{125,0,197}
\definecolor{category4}{RGB}{125,135,0}
\definecolor{category5}{RGB}{125,0,0}
\definecolor{category6}{RGB}{0,0,197}
\definecolor{category7}{RGB}{0,135,0}
\definecolor{category8}{RGB}{0,0,0}
\definecolor{category9}{RGB}{255,255,255}
%
% code
\begin{document}
\begin{tikzpicture}%
\node[black,fill=none] at (0,2.5) {\titre};
%
\edef\startangle{0}%
\draw[\theme!95!black, line width=1pt] (0,0) circle (2.05cm);
\foreach [
remember=\endangle as \startangle,
evaluate=\i as \endangle using {\startangle+(\shares[\i-1]/100*360)},
evaluate=\halfangle using {(\endangle-\startangle)/2+\startangle},]
\i in {1,...,\nbcategories}
{%
\pgfmathsetmacro\intensite{(\nbcategories-\i)/(\nbcategories-1)*100}
\fill[{category\i}] (0,0) --++(\startangle:2cm) arc (\startangle:\endangle:2cm);
\draw[white, line width=0.25mm](0,0)--++(\startangle:2cm+0.1pt);
}%
\draw[white,line width=0.25mm](0,0)--++(\startangle:2cm);%
\fill[white](0,0)circle [radius=2cm*0.65];%
%
\foreach [
remember=\endangle as \startangle,
evaluate=\i as \endangle using {\startangle+(\shares[\i-1]/100*360)},
evaluate=\halfangle using {(\endangle-\startangle)/2+\startangle},]
\i in {1,...,\nbcategories} {%
\node[\theme!65!black] at (\halfangle:2cm*0.475) {\textbf{\footnotesize\pgfmathparse{\shares[\i-1]}\pgfmathresult\%}};
}%
%
\foreach [] \i in {1,...,\nbcategories} {%
\pgfmathsetmacro\intensite{(\nbcategories-\i)/(\nbcategories-1)*100}
\draw[fill={category\i},draw=black!65] (-2,-2.25-\i*0.35) -- (-1.75,-2.25-\i*0.35) -- (-1.75,-2.5-\i*0.35) -- (-2,-2.5-\i*0.35) -- cycle;
\node[right] at (-1.75,-2.375-\i*0.35) {\small \pgfmathparse{\commentaires[\i-1]}\pgfmathresult\phantom{bp}};
}%
\end{tikzpicture}%
\end{document}
当然,您可以使用库提供的颜色colorbrewer
,然后尝试循环索引和给定的字母颜色索引,但这需要对代码进行更复杂的更改。
答案2
这轮图我写的包,可以使用。
切片之间的间隙是通过键获得的gap
。
百分比由 给出inner data=\WCperc
。
图例由键决定legend
。这是一个tabular
放置在 中的\node
。行由键决定legend row
。可以使用 获得所有行的结果\WClegend
。
该宏\WClistcolors
引用了给予键的列表中的项目WClistcolors
。
\documentclass[border=6pt]{standalone}
\usepackage{wheelchart}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
\wheelchart[
counterclockwise,
data=,
gap=0.03,
inner data=\WCperc,
inner data sep=0.3,
legend row={\tikz\fill[\WClistcolors,draw=black] (0,0) rectangle (0.3,0.3); & \WCvarB},
legend={
\node[anchor=north] at (0,-3.5) {%
\begin{tabular}{l@{ }l}%
\WClegend%
\end{tabular}%
};
},
slices style=\WClistcolors,
start angle=0,
title=niveau de satisfaction,
WClistcolors={green,orange,red}
]{%
57/très satisfait,
40/satisfait,
3/peu satisfait%
}
\end{tikzpicture}
\end{document}