Wheel chart

Wheel chart


% Adjusts the size of the wheel:

% The main macro
    % Calculate total
    \foreach \value/\colour/\name in {#1} {

    % The text in the center of the wheel
      \node[align=center,text width=2*\innerradius]{Ratings given by \pgfmathprintnumber{\totalnum}~participants};

      % Calculate the thickness and the middle line of the wheel

      % Rotate so we start from the top
      \begin{scope}[line width=\wheelwidth,rotate=90]

      % Loop through each value set. \cumnum keeps track of where we are in the wheel
      \foreach \value/\colour/\name in {#1} {
            \pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}

            % Calculate the percent value
            % Calculate the mid angle of the colour segments to place the labels

            % This is necessary for the labels to align nicely
                (-\midangle<85?"south west":
                  (-\midangle<175?"north west":
                    (-\midangle<265?"north east":
                      (-\midangle<355?"south east":"south")
            } \edef\textanchor{\pgfmathresult}

            % Draw the color segments. Somehow, the \midrow units got lost, so we add 'pt' at the end. Not nice...
            \draw[\colour] (-\cumnum:\midradius pt) arc (-\cumnum:-(\newcumnum):\midradius pt);

            % Draw the data labels
            \node at (\midangle:\outerradius + 1ex) [inner sep=0pt, outer sep=0pt, ,anchor=\textanchor]{\name: \value\ (\pgfmathprintnumber{\percentage}\%)};

            % The 'spokes'
            \foreach \i in {0,...,\value} {
                \draw [gray,thin] (-\cumnum-\i/\totalnum*360:\innerradius) -- (-\cumnum-\i/\totalnum*360:\outerradius);

            % Set the old cumulated angle to the new value

      \draw[gray] (0,0) circle (\outerradius) circle (\innerradius);

% Usage: \wheelchart{//, ...}
\wheelchart{20/green/good,  10/yellow/medium, 9/red/bad, 5/white/neutral}


Leave a Reply

Your email address will not be published.