如何为每个饼图赋予不同的颜色?

如何为每个饼图赋予不同的颜色?

我已经看过答案了饼图包

您能否建议如何为每个饼图赋予不同的颜色?

答案1

\documentclass{article}

\usepackage{calc}
\usepackage{ifthen}
\usepackage{tikz}
\begin{document}
\newcommand{\slice}[5]{ %% use 5 arguments here
  \pgfmathparse{0.5*#1+0.5*#2}
  \let\midangle\pgfmathresult

  % slice %% I changed !10 to !30 to get darker color
  % use the fifth argument #5 to pass the color  
  \draw[thick,fill=#5!30] (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}
%% -------------- use the new variable \c to pass the color
\foreach \p/\t/\c in {20/type A/red, 4/type B/blue, 11/type C/red,
                   49/type D/green, 16/other/yellow}
  {
    \setcounter{a}{\value{b}}
    \addtocounter{b}{\p}
    \slice{\thea/100*360}
          {\theb/100*360}
          {\p\%}{\t}{\c}  %% here we use the fifth variable
  }

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

只是为了和 PSTricks 一起玩。

\documentclass[pstricks,border=12pt,10pt]{standalone}
\SpecialCoor

\newcounter{sum}
\addtocounter{sum}{0}

\psset{fillstyle=solid,opacity=.5,linejoin=1}
\degrees[100]

\newcommand\Item[3][red]{%
    \pstVerb{/IA \thesum\space def /FA IA #2 add def}%
    \pswedge[fillcolor=#1]{3}{!IA}{!FA}%
    \uput{1.5}[!IA FA add 2 div](0,0){\scriptsize #2\%}%
    \uput{3.2}[!IA FA add 2 div](0,0){#3}%
    \addtocounter{sum}{#2}%
    \ignorespaces
}

\begin{document}
\begin{pspicture}(-4,-4)(4,4)
\Item{20}{type A}
\Item[blue]{4}{type B}
\Item{11}{type C}
\Item[green]{49}{type D}
\Item[yellow]{\the\numexpr100-\thesum\relax}{other}
\end{pspicture}
\end{document}

在此处输入图片描述

答案3

chartColor=color将获得预定义的颜色而不是用户定义的颜色。

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pstricks-add}

\begin{document}
 \psset{unit=1.5}
 \begin{pspicture}(-3,-3)(3,3)
 \psChart[userColor={red!40,green!50,blue!40,gray,cyan!50,
 magenta!80,cyan},chartSep=30pt,shadow=true,shadowsize=5pt
]{20,4,11,49,16}{3}{2}
 \psset{nodesepA=5pt,nodesepB=-10pt}
 \ncline{psChartO1}{psChart1}\nput{0}{psChartO1}{20\%}
 \ncline{psChartO2}{psChart2}\nput{150}{psChartO2}{4\%}
 \ncline{psChartO3}{psChart3}\nput{90}{psChartO3}{11\%}
 \ncline{psChartO4}{psChart4}\nput{-90}{psChartO4}{49\%}
 \ncline{psChartO5}{psChart5}\nput{0}{psChartO5}{16\%}
 \bfseries%
 \rput(psChartI1){type A}\rput[b](psChartI2){type B}
 \rput[t](psChartI3){type C}
 \rput(psChartI4){type D}\rput(psChartI5){other}
 \end{pspicture}
\end{document}

在此处输入图片描述

相关内容