1

I know that there are other solutions for creating diagrams in LaTeX, especially in TikZ, but I've got the following problem. Consider this MWE with the output:

\documentclass[border=5pt,tikz]{standalone}
\newcommand{\dia}[2]{
    \pgfmathsetmacro{\a}{(#1/100)*360}
    \pgfmathsetmacro{\b}{(#2/100)*360}
    \pgfmathsetmacro{\cc}{100-(#1+#2)}
    \pgfmathsetmacro{\c}{(\cc/100)*360}
        \fill[blue] (1,0) arc(0:\a+\a:1) -- (0,0) -- cycle;
        \fill[green] (1,0) arc(0:\b+\c:1) -- (0,0) -- cycle;
        \fill[red] (1,0) arc(0:\c:1) -- (0,0) -- cycle;
            \draw[gray,ultra thick] (0,0) -- (\a+\a:1.01);
            \draw[gray,ultra thick] (0,0) -- (\b+\c:1.01);
            \draw[gray,ultra thick] (0,0) -- (\c:1.01);
    \fill[gray] (0,0) circle(.95);
}
\pagecolor{gray}
\begin{document}
\foreach \n in {0,5,...,45,40,35,...,0}
{
    \begin{tikzpicture}
        \useasboundingbox (-1,-1) rectangle (1,1);
%%      \fill[blue] (1,0) arc(0:120:1) --+ (0,-{sin(120)}) -- cycle;
%       \fill[blue] (1,0) arc(0:120:1) -- (0,0) -- cycle;
%%      \fill[green] (1,0) arc(0:75:1) --+ (0,-{sin(75)}) -- cycle;
%       \fill[green] (1,0) arc(0:75:1) -- (0,0) -- cycle;
            \dia{50}{\n}
    \end{tikzpicture}
}
\end{document}

Screenshot

My question is:

How can I create a command, such that I just have to put \dia{4}{20}{10}{50}, to let TikZ know, that there are four numbers to deal with; the first one represents 20%, the second one 10% and so on and the last one is 100 - [the sum of all previous ones]%?

  • 1
    I guess that a simple way to achieve this is to use dash patterns. (Need to run now.) – user121799 Sep 26 '18 at 17:44
  • 1
    Should this work for an arbitrary number of arguments? – Skillmon likes topanswers.xyz Sep 26 '18 at 17:49
  • @Skillmon: Exactly. – current_user Sep 26 '18 at 19:14
  • 1
    I still don't get what the algorithm should do. If you have $n$ different parameters such that $\sum_p p_n \leq 100%$ should there be $n+1$ differently colored circle segments? And how are the different colors to be chosen? – Skillmon likes topanswers.xyz Sep 26 '18 at 19:53
  • 1
    This seem a ring diagram. Here there is a Jake's answer. – vi pa Sep 27 '18 at 13:34
3

Here I rewrite your macro to become something that takes an arbitrary number of percentages, separated by commas, and colors. The last percentage is unimportant since the last entry defines the color of the last stretch that is needed to complete the circle.

\documentclass[border=5pt,tikz]{standalone}
\newcommand{\dia}[2][]{
    \foreach [count=\Z] \X/\Y in {#2}
    {\xdef\NumArcs{\Z}}
    \xdef\LastX{0}
    \foreach [count=\Z] \X/\Y in {#2}
    {
    \pgfmathsetmacro{\a}{(\LastX/100)*360}
    \ifnum\Z=\NumArcs
    \pgfmathsetmacro{\b}{360}
    \else
    \pgfmathsetmacro{\b}{((\LastX+\X)/100)*360}
    \fi
    \draw[\Y,line width=1mm] (180-\a:1) arc(180-\a:180-\b:1);
    \draw[gray,ultra thick] (0,0) -- (180-\a:1.05);
    \draw[gray,ultra thick] (0,0) -- (180-\b:1.05);
    \pgfmathsetmacro{\LastX}{\LastX+\X}
    \xdef\LastX{\LastX}
    }       
    \fill[gray] (0,0) circle(.95);
}
\pagecolor{gray}
\begin{document}
\foreach \n in {0,5,...,45,40,35,...,0}
{
    \begin{tikzpicture}
        \useasboundingbox (-1,-1) rectangle (1,1);
            \dia{10/red,\n/green,45/blue,0/purple}
    \end{tikzpicture}
}
\end{document}

enter image description here

  • This is really good! But one little thing: How can I make a command such that I can tell how many gaps I want to fill; more precisely: For example I've got the numbers 25%, 50%, 15%, 7% and 3%. That are five parameters. Later than I've got e.g. 50%, 30% and 20%, so that are just three parameters. Did I explained it correctly, but thank you for your answer! – current_user Sep 26 '18 at 19:14
  • Really nice answer! I have just one questions: Why does \dia take an optional argument, I don't see you using it. – Skillmon likes topanswers.xyz Sep 26 '18 at 20:00
  • @Skillmon Its my (perhaps bad) habit. Very often I want to generalize a command, for instance here one could just add #1 to the draw command, \draw[\Y,line width=1mm,#1], such that the user could adjust e.g. the line width or something. My take is that it does not hurt, and if you ever decide to make the command more flexible you would not have to worry about replacing all #1 by #2, all #2 by #3 and so on. – user121799 Sep 26 '18 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.