我无法将两个轮盘图放在一起。我已将答案合并到两个并排的身影和Tikz Wheelchart - 防止标签重叠制作并排的两个轮图。我遇到了以下问题:
1. 图未垂直对齐(一个轮子比另一个轮子低,并且一个标签比另一个标签低得多):
2. 我希望右侧图表显示 521.6 MWp,而不是“安装量”
我的 MWE 代码是:
\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
% <---
尝试在您的代码中进行以下更改(在所示的代码中):
\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}