3

Based on several codes found here and there on this forum, I built a simple code for creating a pie chart with a variable number of slices, see below. Right now, I am able to make the color of slices using shades of a main color defined in line 7. I am looking for a way to define in a list the colors I would like to use for each slice. Something like:

\def\mycolors{{"red","blue","green","yellow","","",""}}

but I cannot seem to find a way to properly read this list within my \foreach loop... Is there a way to do this ?

my automated pie chart

My 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}

2 Answers 2

3

This may be a very low-key solution, but for a quick fix, everything you need is already there. Just define colours with a running number and then call then using the index, you are cycling through anyway.

Here are possible colour definitions (which are totally random, just for proof-of-principle):

\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}

And then replace \theme!\intensite by category\i as your index is \i.

Here is the complete code:

\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}

enter image description here

Of course you could use the colours provided by the colorbrewer library and then try to cycle through your index and the given alphabetical colour indices, but that would require some more elaborate changes of your code.

1
  • Thank you, this works indeed !
    – Alain
    Commented Apr 12, 2021 at 10:44
1

The wheelchart package, which I wrote, can be used.

The gap between the slices is obtained with the key gap.

The percentages are given by inner data=\WCperc.

The legend is determined by the key legend. This is a tabular which is placed in a \node. The rows are determined by the key legend row. The result of all rows can be obtained with \WClegend.

The macro \WClistcolors refers to items in the list given to the key WClistcolors.

enter image description here

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .