3

I have the following piecharts in two subfigures, however:

  1. One of them is blocking the legend as shown in the picture. Is there any workaround? I tried modifying pgf-pie (thanks Xu Yuan!)but to no avail.
  2. Also, the two subfigures are not aligned horizontally even though I've added an extra command of {b!}
  3. Is it possible to have the smallest percentage visible by having a line drawn out (text=pin) alike since there's no space to label in the pie chart?

    \documentclass{scrbook}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \usepackage{subcaption}
    \usepackage[dvipsnames]{xcolor}
    \usepackage{tikz}
    \usepackage{pgf-pie}
    
    \begin{document}
    \begin{figure*}[t!]
        \centering
        \begin{subfigure}[b!]{0.5\textwidth}
            \begin{tikzpicture}
            \tikzset{lines/.style={draw=white},}
            \pie[color={Orchid, GreenYellow, BurntOrange},sum=auto, after number=\%,every only number node/.style={text=black},text=legend,style={lines}]{60.8/Natural Gas,36.4/Petroleum,2.8/Others}
            \end{tikzpicture}
            \caption{2003}
        \end{subfigure}%
        ~
        \begin{subfigure}[b!]{0.5\textwidth}
            \begin{tikzpicture}
            \tikzset{lines/.style={draw=white},}
            \pie[color={Orchid, GreenYellow, BurntOrange},sum=auto, after number=\%,every only number node/.style={text=black},style={lines}]{95.15/,0.67/,4.18/}
            \end{tikzpicture}
            \caption{2017}
            \end{subfigure}
        \caption{Singapore's fuel mix for electricity generation}
    \end{figure*}
    \end{document}
    

Thank you in advanced! enter image description here

  • 1
    If you add \draw (current bounding box.south west) rectangle (current bounding box.north east); before the first \end{tikzpicture}, you will see that the picture is wider than the space you give it with \begin{subfigure}[b!]{0.5\textwidth}. So either give the picture more space or make it smaller. –  Dec 02 '18 at 23:22
  • Thanks for your reply @marmot, this might sound not too clever but --> do you mind letting me know how can i make the space for picture bigger? – thesilencer Dec 02 '18 at 23:25
  • Some time ago I looked at the internal macros of pgf-pie, and came up with a very rusty method to add pins:https://tex.stackexchange.com/a/451154/121799. I am definitely not claiming it is elegant, but that was the best I could come up with at that time. This does not mean much, but may suggest that having these pins may require more than adding a key somewhere. And you could draw these two pies in one picture, and shift them against each other, and then add the captions in nodes to have the desired vertical and horizontal alignment. –  Dec 02 '18 at 23:29
  • It would be better if you attach the figures you are trying to include, that way people can work on it. – zyy Dec 03 '18 at 02:41
  • @zyy They are in the MWE above. Not quite sure is that what you meant ? – thesilencer Dec 03 '18 at 10:03
  • @thesilencer What I meant is original figures, otherwise people can only imagine working on it. – zyy Dec 03 '18 at 14:54
  • 1
    @zyy The figures are the piecharts included in my MWE. – thesilencer Dec 03 '18 at 15:25
  • @thesilencer Sorry, I did not notice that. – zyy Dec 03 '18 at 15:59

1 Answers1

0

The wheelchart package, which I wrote, can be used.

A command \WCtest is defined of which the output depends on whether the percentage is larger than 4. This command is used in the keys data and wheel data. Here, the percentage is obtained with \WCperc.

By using the key lines={\WCpercentage>4?0:0.5}, the length of the lines is 0 if the percentage is larger than 4 and 0.5 otherwise.

In the key slices style we use \WClistcolors which refers to items in the list given to the key WClistcolors.

The legend is determined by the key legend. This is a tabular which is placed in a \node. The rows are determined by the key legend row. The result of all rows can be obtained with \WClegend.

enter image description here

\documentclass[border=6pt,dvipsnames]{standalone}
\usepackage{wheelchart}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
\def\WCtest#1#2{%
  \pgfmathparse{\WCpercentage>4?"#1":"#2"}%
  \pgfmathresult%
}
\pgfkeys{
  /wheelchart,
  anchor ysep=20,
  counterclockwise,
  data=\WCtest{}{\WCperc},
  lines={\WCpercentage>4?0:0.5},
  perc precision=2,
  pie,
  slices style=\WClistcolors,
  start angle=0,
  WClistcolors={Orchid,GreenYellow,BurntOrange},
  wheel data=\WCtest{\WCperc}{}
}
\wheelchart[
  at={(-5,0)},
  legend row={\tikz\fill[\WClistcolors] (0,0) rectangle (0.3,0.3); & \WCvarB},
  legend={
    \node[anchor=north] at (5,-5) {%
      \begin{tabular}{l@{ }l}%
      \WClegend%
      \end{tabular}%
    };
  }
]{%
  60.8/Natural Gas,
  36.4/Petroleum,
  2.8/Others%
}
\node at (-5,-4) {(a) 2003};
\wheelchart[
  at={(5,0)}
]{%
  95.15,
  0.67,
  4.18%
}
\node at (5,-4) {(b) 2017};
\end{tikzpicture}
\end{document}
matexmatics
  • 4,819