Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I really like the 'ring chart' or 'wheel chart' given as an example on page 88 of the PGF manual. It is like a pie chart, but with a label in the centre. Unfortunately, the manual doesn't give the source for that chart. Does anyone have an example? Thanks.

The graph

share|improve this question
1  
The PGF/TikZ-manual basically states in that section that pie charts (especially in 3D) and similar charts should be avoided in favor of tables or bar charts because they are clearer. This might be the reason no actual code is provided. However, you might want to check out the sources of the manual. Maybe you are lucky. –  Martin Scharrer May 10 '11 at 16:53
1  
@Martin: Directly below the graphic, the manual also states: [This] graphic has about the same information density as the table (about the same size and the same numbers are shown). In addition, one can directly “see” that there are more good or very good ratings than bad ones. One can also “see” that the number of people who gave no rating at all is not negligible, which is quite common for feedback forms. –  Caramdir May 10 '11 at 16:57
    
@Caramdir: Yes, indeed, it states there that they are good for things which add up to 100%. But AFAIK this types of charts are overused. –  Martin Scharrer May 10 '11 at 17:02
1  
Apropos pie charts: svott.com/mt/archives/pacman_piechart.jpg. –  Martin Scharrer May 10 '11 at 17:02
    
@Martin Or this one: stats.stackexchange.com/questions/423/… –  Sharpie May 10 '11 at 17:06

2 Answers 2

up vote 14 down vote accepted

I am afraid that this is drawn completely manually. The relevant code is in texmf-dist/doc/generic/pgf/text-en/pgfmanual-en-guidelines.tex (in TeX Live 2010). For convenience it is also listed below (with a preamble added). Making a library out of this might be a useful project.

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
  \colorlet{good}{green!75!black}
  \colorlet{bad}{red}
  \colorlet{neutral}{black!60}
  \colorlet{none}{white}

  \node[align=center,text width=3cm]{Ratings given by 50~participants};

  \begin{scope}[line width=4mm,rotate=270]
    \draw[good]          (-123:2cm) arc (-123:-101:2cm);
    \draw[good!60!white] (-36:2cm) arc (-36:-101:2cm);
    \draw[neutral]       (-36:2cm) arc (-36:36:2cm);
    \draw[bad!60!white]  (36:2cm)  arc (36:93:2cm);

    \newcount\mycount
    \foreach \angle in {0,72,...,3599}
    {
      \mycount=\angle\relax
      \divide\mycount by 10\relax
      \draw[black!15,thick] (\the\mycount:18mm) -- (\the\mycount:22mm);
    }

    \draw (0:2.2cm) node[below] {``ok'': 10 (20\%)};
    \draw (165:2.2cm) node[above] {none: 20 (40\%)};
    \draw (-111:2.2cm) node[left] {``very good'': 3 (6\%)};
    \draw (-68:2.2cm) node[left] {``good'': 9 (18\%)};
    \draw (65:2.2cm) node[right] {``bad'': 8 (16\%)};
    \draw (93:2.2cm) node[right] {``very bad'': 0 (0\%)};
  \end{scope}
  \draw[gray] (0,0) circle (2.2cm) circle (1.8cm);
\end{tikzpicture}

\end{document}
share|improve this answer
    
Beat me to it by a couple of seconds :P –  Sharpie May 10 '11 at 16:57
    
@Sharpie: I was just going to post it as well :-) Here the general CTAN link for that file: mirrors.ctan.org/graphics/pgf/base/doc/generic/pgf/text-en/… –  Martin Scharrer May 10 '11 at 16:58
    
@Sharpie Beat me to. I've deleted mine as I didn't notice the "answer added" bar. –  Edd May 10 '11 at 17:01
    
Thanks to all - very useful. Yes, pie charts are over-used, but these are better than traditional ones for when there aren't many choices! –  moabi2000 May 11 '11 at 17:50

Oooh, too tempting! Even though pie/wheel charts are bad, here's a macro to automatically draw the wheel chart from the PGF manual.

This command:

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

Yields this chart:

Automatic wheel chart

Here's the implementation:

\documentclass{article}
\usepackage{tikz}
\begin{document}

% Adjusts the size of the wheel:
\def\innerradius{1.8cm}
\def\outerradius{2.2cm}

% The main macro
\newcommand{\wheelchart}[1]{
    % Calculate total
    \pgfmathsetmacro{\totalnum}{0}
    \foreach \value/\colour/\name in {#1} {
        \pgfmathparse{\value+\totalnum}
        \global\let\totalnum=\pgfmathresult
    }

    \begin{tikzpicture}
    % 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
      \pgfmathsetmacro{\wheelwidth}{\outerradius-\innerradius}
      \pgfmathsetmacro{\midradius}{(\outerradius+\innerradius)/2}

      % 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
      \pgfmathsetmacro{\cumnum}{0}
      \foreach \value/\colour/\name in {#1} {
            \pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}

            % Calculate the percent value
            \pgfmathsetmacro{\percentage}{\value/\totalnum*100}
            % Calculate the mid angle of the colour segments to place the labels
            \pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}

            % This is necessary for the labels to align nicely
            \pgfmathparse{
               (-\midangle<5?"south":
                (-\midangle<85?"south west":
                 (-\midangle<105?"west":
                  (-\midangle<175?"north west":
                   (-\midangle<185?"north":
                    (-\midangle<265?"north east":
                     (-\midangle<275?"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
            \global\let\cumnum=\newcumnum
        }

      \end{scope}
      \draw[gray] (0,0) circle (\outerradius) circle (\innerradius);
    \end{tikzpicture}
}

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

\end{document}
share|improve this answer
    
Could you make a CTAN package out of it? Would easy usage for non-hardcore-IT-users :) –  koppor Apr 21 '12 at 18:35
    
I have issues with babel (pdflatex and lualatex) and polyglossia (xelatex) when using the package: pdflatex.exe> ! Undefined control sequence. pdflatex.exe> \wheelchart ...hsetmacro {\totalnum }{0} \foreach pdflatex.exe> \value /\colour /\name in ... pdflatex.exe> l.80 ...yellow/medium, 9/red/bad, 5/white/neutral} Could you try the code at your side with \usepackage[ngerman]{babel}? –  koppor Apr 21 '12 at 19:43
    
@koppor: This happens because : is made active by babel. It works if you set \catcode:=12\relax`. –  Jake May 19 '12 at 21:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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