1

I'm having trouble placing two wheelcharts next to each other. I have combined answers to Two figures side by side and Tikz Wheelchart - Prevent labels from overlapping to make two wheelcharts side by side. I have the following problems:

1. The plots are not vertically aligned (one wheel is lower than the other and one label is way lower than the other): enter image description here

2. I would like the right graph to read 521.6 MWp instead of 'installations'

My code for the MWE is:

\documentclass{article}
\usepackage{tikz}
\usepackage[inner = 25mm, outer = 25mm,  top = 30mm, bottom = 20mm]{geometry}
\usetikzlibrary{arrows}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{filecontents}{installations.csv}
Name,   Quantity
"Japan", 66
"China", 12
"Korea", 8
"Other", 23 
\end{filecontents}

\begin{filecontents}{installedcapacity.csv}
Name,   Quantity
"Japan", 95.516
"China", 394.258
"Korea", 16.901
"Other", 14.589 
\end{filecontents}

\begin{document}

\pgfplotstableread[ignore chars=",col sep=comma]{installations.csv}\datainstallations
\pgfplotstableread[ignore chars=",col sep=comma]{installedcapacity.csv}\datacapacity

\pgfplotstableread{
    clr
    blue!70!black!70
    red!70!black
    black!70!white
    white!70!black
    pink!30!red!60!
    purple!80
    green!60!black!40!
    black!50
    pink!30!red!60!
    blue!70
    green!10!orange!90!
    yellow!50!orange!50!
    pink!30!red!60!
}\MyColors

% Adjusts the size of the wheel:
\def\innerradius{1.6cm}
\def\outerradius{2cm}

\newcommand\LabelName{}
\newcommand\LabelValue{}

% The main macro
\newcommand{\wheelchart}[1]{
    % Calculate total
    \pgfmathsetmacro{\totalnum}{0}
    % get number of rows in table
    \pgfplotstablegetrowsof{#1}
    % minus 1 because indexing starts as zero
    \pgfmathsetmacro{\RowsInTable}{\pgfplotsretval-1}
    \foreach \i in {0,...,\RowsInTable} {
        \pgfplotstablegetelem{\i}{Quantity}\of{#1}
        \pgfmathparse{\pgfplotsretval+\totalnum}
        \global\let\totalnum=\pgfmathresult
    }
    \begin{center}
        \begin{tikzpicture}
        % The text in the center of the wheel
        \node[align=center,text width=2*\innerradius]{\pgfmathprintnumber{\totalnum}~Installations};
        % 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}[rotate=90]

        % Loop through each value set. \cumnum keeps track of where we are in the wheel
        \pgfmathsetmacro{\cumnum}{0}


        \foreach \i in {0,...,\RowsInTable} {
            % get values from table
            \pgfplotstablegetelem{\i}{Name}\of{#1}\global\let\LabelName=\pgfplotsretval
            \pgfplotstablegetelem{\i}{Quantity}\of{#1}\renewcommand\LabelValue{\pgfplotsretval}

            \pgfmathsetmacro{\newcumnum}{\cumnum + \LabelValue/\totalnum*360}


            % Calculate the percent value
            \pgfmathsetmacro{\percentage}{\LabelValue}
            % 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<180?"west":"east")
            } \edef\textanchor{\pgfmathresult}
            \pgfmathsetmacro\labelshiftdir{ifthenelse(\RowsInTable==0,-1,1)*(1-2*(-\midangle>180))}

            % Draw the color segments. Somehow, the \midrow units got lost, so we add 'pt' at the end. Not nice...
            \pgfplotstablegetelem{\i}{clr}\of{\MyColors}
            \fill[color=\pgfplotsretval] (-\cumnum:\outerradius) arc (-\cumnum:-(\newcumnum):\outerradius) --
            (-\newcumnum:\innerradius) arc (-\newcumnum:-(\cumnum):\innerradius) -- cycle;

            % Draw the data labels
            \pgfmathsetmacro\labelmag{
                1 + .9*(abs(1/sin(\midangle)))^1 - .03*(abs(1/sin(\midangle)))^2}
            \draw  [*-,thin] node [append after command={(\midangle:\midradius pt) -- 
                (\midangle:\outerradius + \labelmag ex) -- (\tikzlastnode)}] at
            (\midangle:\outerradius + \labelmag ex) 
            [xshift=\labelshiftdir*0.5cm,inner sep=0pt, outer sep=0pt, ,anchor=\textanchor]{\LabelName: \pgfmathprintnumber{\percentage}};

            % 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}
    \end{center}
}


\begin{figure}
\centering
\begin{minipage}{0.49\textwidth}
    \centering
    \wheelchart{\datainstallations}
    \caption{first figure}
\end{minipage}\hfill
\begin{minipage}{0.49\textwidth}
    \centering
    \wheelchart{\datacapacity}
    \caption{second figure}
\end{minipage}
\end{figure}


\end{document}
1

try with the following changes (in code indicated by % <--- ) in the your code:

\documentclass{article}
\usepackage{tikz}
\usepackage[inner = 25mm, outer = 25mm,  top = 30mm, bottom = 20mm]{geometry}
\usetikzlibrary{arrows}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{filecontents}{installations.csv}
Name,   Quantity
"Japan", 66
"China", 12
"Korea", 8
"Other", 23
\end{filecontents}

\begin{filecontents}{installedcapacity.csv}
Name,   Quantity
"Japan", 95.516
"China", 394.258
"Korea", 16.901
"Other", 14.589
\end{filecontents}

\begin{document}

\pgfplotstableread[ignore chars=",col sep=comma]{installations.csv}\datainstallations
\pgfplotstableread[ignore chars=",col sep=comma]{installedcapacity.csv}\datacapacity

\pgfplotstableread{
    clr
    blue!70!black!70
    red!70!black
    black!70!white
    white!70!black
    pink!30!red!60!
    purple!80
    green!60!black!40!
    black!50
    pink!30!red!60!
    blue!70
    green!10!orange!90!
    yellow!50!orange!50!
    pink!30!red!60!
}\MyColors

% Adjusts the size of the wheel:
\def\innerradius{1.6cm}
\def\outerradius{2cm}

\newcommand\LabelName{}
\newcommand\LabelValue{}

% The main macro
\newcommand{\wheelchart}[2]{ % <--- (from 1 to 2)
    % Calculate total
    \pgfmathsetmacro{\totalnum}{0}
    % get number of rows in table
    \pgfplotstablegetrowsof{#1}
    % minus 1 because indexing starts as zero
    \pgfmathsetmacro{\RowsInTable}{\pgfplotsretval-1}
    \foreach \i in {0,...,\RowsInTable} {
        \pgfplotstablegetelem{\i}{Quantity}\of{#1}
        \pgfmathparse{\pgfplotsretval+\totalnum}
        \global\let\totalnum=\pgfmathresult
    }
    \begin{center}
        \begin{tikzpicture}
        % The text in the center of the wheel
        \node[align=center,text width=2*\innerradius]{\pgfmathprintnumber{\totalnum}~{#2}}; % <--- (name is now defined by second option of the command \wheelchart
        % 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}[rotate=90]
        % Loop through each value set. \cumnum keeps track of where we are in the wheel
        \pgfmathsetmacro{\cumnum}{0}
        \foreach \i in {0,...,\RowsInTable} {
            % get values from table
            \pgfplotstablegetelem{\i}{Name}\of{#1}\global\let\LabelName=\pgfplotsretval
            \pgfplotstablegetelem{\i}{Quantity}\of{#1}\renewcommand\LabelValue{\pgfplotsretval}

            \pgfmathsetmacro{\newcumnum}{\cumnum + \LabelValue/\totalnum*360}

            % Calculate the percent value
            \pgfmathsetmacro{\percentage}{\LabelValue}
            % 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<180?"west":"east")
            } \edef\textanchor{\pgfmathresult}
            \pgfmathsetmacro\labelshiftdir{ifthenelse(\RowsInTable==0,-1,1)*(1-2*(-\midangle>180))}

            % Draw the color segments. Somehow, the \midrow units got lost, so we add 'pt' at the end. Not nice...
            \pgfplotstablegetelem{\i}{clr}\of{\MyColors}
            \fill[color=\pgfplotsretval] (-\cumnum:\outerradius) arc (-\cumnum:-(\newcumnum):\outerradius) --
            (-\newcumnum:\innerradius) arc (-\newcumnum:-(\cumnum):\innerradius) -- cycle;

            % Draw the data labels
            \pgfmathsetmacro\labelmag{
                1 + .9*(abs(1/sin(\midangle)))^1 - .03*(abs(1/sin(\midangle)))^2}
            \draw  [*-,thin] node [append after command={(\midangle:\midradius pt) --
                (\midangle:\outerradius + \labelmag ex) -- (\tikzlastnode)}] at
            (\midangle:\outerradius + \labelmag ex)
            [xshift=\labelshiftdir*0.5cm,inner sep=0pt, outer sep=0pt, ,anchor=\textanchor]{\LabelName: \pgfmathprintnumber{\percentage}};

            % 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}
    \end{center}
}

\begin{figure}
\centering
\begin{minipage}[b]{0.49\textwidth}
    \centering
    \wheelchart{\datainstallations}{installation}\vskip\baselineskip % <--- added are name in wheel (installation) 
                                                                     % and vertical space for aligning wheels
    \caption{first figure}
\end{minipage}\hfill
\begin{minipage}[b]{0.49\textwidth}
    \centering
    \wheelchart{\datacapacity}{MWE} % <--- added are name in wheel (MWE)
    \caption{second figure}
\end{minipage}
\end{figure}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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