0

I wanted to use tickz to draw a pie chart of 3 segments. The problem is that one of percentages is too small and the text comes out clobbered and hard to read.

The 3 percentages are 96.34%,3.22% and 0.155%. When I do the following

\documentclass[border=0.2cm]{standalone}

% Pie chart drawing library \usepackage{pgf-pie}

\begin{document}

\begin{tikzpicture} \pie[ /tikz/every pin/.style={align=center}, color = { yellow!90, green!60, blue!60} ]{96.34/Case 1, 3.22/Case 2, 0.155/Case 3 } \end{tikzpicture}

\end{document}

It comes out as follows

enter image description here

One way this is done using other manual drawing tools, is to make that small segment a little larger than its actual percent indicates (so it shows more clearly inside the pie) but use the actual percentage as a label.

I do not know how to do this in tikz. Another option is to write the percentage outside the pie for that one case, may be with an arrow coming out of that tiny segment.

I tried to modify code in this Unable to set the text and its pin in correct orientation in Tikz pie chart as follows, but having hard time putting the labels in the correct position as I do not understand the code at all. Here is my second attempt

\begin{document}

\begin{figure}[h] \def\angle{0} \def\radius{2} \def\labelradius{3} \def\cyclelist{{"red","blue","cyan"}}

\centering
\newcount\cyclecount \cyclecount=-1
\newcount\ind \ind=-1
\begin{tikzpicture}[nodes = {font=\small}]
  \foreach \percent/\name in {
    96.34/case one,
    3.22/case two,
    0.155/case three
    } {
      \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
        \ifnum6<\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[fill={\color!50},draw={\color}] (0,0) -- (\angle:\radius)
          arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
        \draw[draw=gray, shorten >=2pt] (\angle+0.5*\percent*3.6:\labelradius) node {\name~\percent\%} edge (\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{figure} \end{document}

enter image description here

If I can figure how to push the labels little further away from the pie, the above could work.

What would be the correct way to handle such cases in tikz?

TL 2022 on Linux. I was using this site as reference to learn how to make pie charts.

Update:

I ended up doing this using the IPE latex drawing program. Here is the result

enter image description here

I am sure this can be done in tikz, but was too hard for me.

Nasser
  • 20,220

1 Answers1

1

After taking code from some examples in wheelchart package documentacion, this is a possible result.

\documentclass[tikz, border=2mm]{standalone}
\usepackage{wheelchart}
\usepackage{siunitx}

\begin{document}

\begin{tikzpicture} \newcommand{\WCtest}[2]{% \ifdim \WCpercentage pt > 10 pt% #1% \else #2% \fi% } \wheelchart[ pie, data={\WCtest{}{\qty{\WCvarA}{\percent}}}, data angle shift=\WCvarD, lines={1-max(sign(\WCpercentage-10),0)}, lines style={dotted, thick}, start angle=30, wheel data={\WCtest{\qty{\WCvarA}{\percent}}{}} ]{% 96.34/blue/{Case one}/0, 3.22/green/{Case two}/0, 0.155/red/{Case three}/0 } \end{tikzpicture}

\end{document}

enter image description here

Ignasi
  • 136,588