我正在尝试将 CSV 文件连接到tikzpicture
包的轮图。下图看起来不错,唯一的问题是当前数据条目是硬编码的。在下面的代码中,您可以看到我已经生成了一个 CSV 文件。有人知道如何将此 CSV 链接到轮图吗?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
% This CSV should be added to the /wheelchart command below
\begin{filecontents}{testdata.csv}
Name, Quantity
"Blueberries", 16
"Pears", 5
"Bananas", 4
"Grapes", 2
\end{filecontents}
\pgfplotstableread[col sep=comma]{testdata.csv}\datatable
\begin{document}
% Adjusts the size of the wheel:
\def\innerradius{2.8cm}
\def\outerradius{3.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{center}
\begin{tikzpicture}
% 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 \value/\colour/\name in {#1} {
\pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}
% Calculate the percent value
\pgfmathsetmacro{\percentage}{\value}
% 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{1-2*(-\midangle>180)}
% Draw the color segments. Somehow, the \midrow units got lost, so we add 'pt' at the end. Not nice...
\fill[\colour] (-\cumnum:\outerradius) arc (-\cumnum:-(\newcumnum):\outerradius) --
(-\newcumnum:\innerradius) arc (-\newcumnum:-(\cumnum):\innerradius) -- cycle;
% Draw the data labels
\draw [*-,thin] node [append after command={(\midangle:\midradius pt) -- (\midangle:\outerradius + 1ex) -- (\tikzlastnode)}] at (\midangle:\outerradius + 1ex) [xshift=\labelshiftdir*0.5cm,inner sep=0pt, outer sep=0pt, ,anchor=\textanchor]{\name: \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}
}
% This is where the CSV should be added instead of hard-coded data entries.
\wheelchart{16/blue/Blueberries, 5/green/Pears, 4/yellow/Bananas, 2/pink/Grapes}
\end{document}
答案1
这里有一个快速重写,它从表中读取值,因此参数是\wheelchart
。\datatable
我制作了第二个表来保存颜色。您必须确保颜色表中的颜色数量至少与数据表中的颜色数量一样多,但我没有实现任何循环(我的初始尝试失败了)。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{filecontents}
\usepackage{pgfplotstable}
% This CSV should be added to the /wheelchart command below
\begin{filecontents*}{testdata.csv}
Name, Quantity
Blueberries, 16
\end{filecontents*}
\begin{filecontents*}{testdata2.csv}
Name, Quantity
Blueberries, 16
Pears, 5
Bananas, 4
Grapes, 2
\end{filecontents*}
\pgfplotstableread[col sep=comma]{testdata.csv}\datatableA
\pgfplotstableread[col sep=comma]{testdata2.csv}\datatableB
\pgfplotstableread{
clr
blue!70
red
yellow
green
}\MyColors
% Adjusts the size of the wheel:
\def\innerradius{2.8cm}
\def\outerradius{3.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}
% 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
\draw [*-,thin] node [append after command={(\midangle:\midradius pt) -- (\midangle:\outerradius + 1ex) -- (\tikzlastnode)}] at (\midangle:\outerradius + 1ex) [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{document}
\wheelchart{\datatableA}
\wheelchart{\datatableB}
\end{document}