Tortendiagramm Legende statt

Tabellen und Grafiken erstellen und anordnen


PPanny
Forum-Newbie
Forum-Newbie
Beiträge: 8
Registriert: Fr 14. Feb 2020, 10:51

Tortendiagramm Legende statt

Beitrag von PPanny »

Ich habe über http://www.texample.net/ ein Template für ein Tortendiagramm gefunden und schon weitestgehend nach meinen Wünschen angepasst:
\documentclass[tikz,border=10pt]{standalone}
%%%<
\usepackage{verbatim}
%%%>
\begin{document}
\def\angle{0}
\def\radius{3}
\def\cyclelist{{"orange","blue","red","green","cyan"}}
\newcount\cyclecount \cyclecount=-1
\newcount\ind \ind=-1
\begin{tikzpicture}[nodes = {font=\sffamily}]
  \foreach \percent/\name in {
      20/a,
      15/b,
      25/c,
      20/d,
      20/e
    } {
      \ifx\percent\empty\else               % If \percent is empty, do nothing
        \global\advance\cyclecount by 1     % Advance cyclecount
        \global\advance\ind by 1            % Advance list index
        \ifnum4<\cyclecount                 % If cyclecount is larger than list
          \global\cyclecount=0              %   reset cyclecount and
          \global\ind=0                     %   reset list index
        \fi
        \pgfmathparse{\cyclelist[\the\ind]} % Get color from cycle list
        \edef\color{\pgfmathresult}         %   and store as \color
        % Draw angle and set labels
        \draw[fill={\color!50},draw={\color}] (0,0) -- (\angle:\radius)
          arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
        \node at (\angle+0.5*\percent*3.6:0.7*\radius) {\percent\,\%};
        \node[pin=\angle+0.5*\percent*3.6:\name]
          at (\angle+0.5*\percent*3.6:\radius) {};
        \pgfmathparse{\angle+\percent*3.6}  % Advance angle
        \xdef\angle{\pgfmathresult}         %   and store in \angle
      \fi
    };
\end{tikzpicture}
\end{document}
Nun möchte ich statt den Pins (Striche mit "a", "b",..."e") eine Legende.
Also ein kleines farbiges Quadrat seitlich vom Diagramm mit der jeweiligen Beschriftung.
Wenn ich Zeile 32 auskommentiere verschwinden schonmal die Pins. Nur weiß ich jetzt nicht wie ich stattdessen eine Legende erstelle.
%\node[pin=\angle+0.5*\percent*3.6:\name]
Optional würde ich mich auch noch über eine schwarze Kontur des Diagramms freuen.

Vielen Dank!

Bartman
Forum-Meister
Forum-Meister
Beiträge: 2456
Registriert: Do 16. Jul 2009, 21:41
Wohnort: Hessische Provinz

Beitrag von Bartman »

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\def\angle{0}
\def\radius{3}
\def\cyclelist{{"orange","blue","red","green","cyan"}}
\newcount\cyclecount \cyclecount=-1
\newcount\ind \ind=-1
\begin{tikzpicture}[
    nodes = {font=\sffamily},
    legend image/.style={fill=#1!50,draw=#1}
]
  \foreach \percent in {
      20,
      15,
      25,
      20,
      20
    } {
      \ifx\percent\empty\else               % If \percent is empty, do nothing
        \global\advance\cyclecount by 1     % Advance cyclecount
        \global\advance\ind by 1            % Advance list index
        \ifnum4<\cyclecount                 % If cyclecount is larger than list
          \global\cyclecount=0              %   reset cyclecount and
          \global\ind=0                     %   reset list index
        \fi
        \pgfmathparse{\cyclelist[\the\ind]} % Get color from cycle list
        \edef\color{\pgfmathresult}         %   and store as \color
        % Draw angle and set labels
        \draw[fill={\color!50},draw={\color}] (0,0) -- (\angle:\radius)
          arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
        \node at (\angle+0.5*\percent*3.6:0.7*\radius) {\percent\,\%};
%        \node[pin=\angle+0.5*\percent*3.6:\name]
%          at (\angle+0.5*\percent*3.6:\radius) {};
        \pgfmathparse{\angle+\percent*3.6}  % Advance angle
        \xdef\angle{\pgfmathresult}         %   and store in \angle
      \fi
    };
    \matrix [
        matrix of nodes,
        draw,
        column 2/.style={nodes={anchor=west}}
    ] at (\radius+1,0) {
        |[legend image=orange]| & a\\
        |[legend image=blue]|   & b\\
        |[legend image=red]|    & c\\
        |[legend image=green]|  & d\\
        |[legend image=cyan]|   & e\\
    };
\end{tikzpicture}
\end{document}
Wahrscheinlich kann man die Farben noch irgendwie aus der \cyclelist auslesen, damit sie nicht erneut eingegeben werden müssen.

Der Autor des Beispiels zeigt eine andere Art, eine Legende einzufügen.

PPanny
Forum-Newbie
Forum-Newbie
Beiträge: 8
Registriert: Fr 14. Feb 2020, 10:51

Beitrag von PPanny »

Vielen Dank für deine Lösung!
Dein Verweis hat mich zusätzlich noch auf eine andere gebracht:
\documentclass[tikz]{standalone}

\def\angle{0}
\def\radius{3}
\def\cyclelist{{"orange","yellow","red","green","cyan"}}

\newcommand\square[1]{%
  \tikz\filldraw[color=#1] (0,0) rectangle (1.5ex,1.5ex);%
}
\newcount\ind \ind=-1
\begin{document}
\begin{tikzpicture}[nodes = {font=\sffamily}]
  \foreach \percent/\name/\color in {
    20/a/orange,
    5/b/yellow,
    10/c/red,
    15/d/cyan,
    50/e/green%
  } {
    \ifx\percent\empty\else                 % If \percent is empty, do nothing
    \global\advance\ind by 1              % Advance list index
    % Draw angle and set labels
    \draw[fill={\color!100},draw={\color}] (0,0) -- (\angle:\radius) arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
    \node at (\angle+0.5*\percent*3.6:0.7*\radius) {\textbf{\percent\,\%}};
    \node[anchor=base west] at (\radius+0.5,0.5-.5*\ind) {\square{\color} \name};
    %\node[pin=\angle+0.5*\percent*3.6:\percent\%] at (\angle+0.5*\percent*3.6:\radius) {};
    \pgfmathparse{\angle+\percent*3.6}    % Advance angle
    \xdef\angle{\pgfmathresult}           %   and store in \angle
    \fi
  };
\end{tikzpicture}
\end{document}
Werde mir das beste aus beidem zusammenbasteln :)

Antworten