PGF-Pie - 如何与 \pgfplotstableread 结合

PGF-Pie - 如何与 \pgfplotstableread 结合

我想知道如何将 pgfplotstableread 添加到此饼图中。我已经为您定义了一个,但它尚未链接到表格。 在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgf-pie}

\pgfplotstableread{
    Value, Name
    9, Blueberries
    7, Strawberries
    10, Pineapples
    16, Bananas
}\dataTable

\begin{document}

\begin{tikzpicture}
\pie [rotate = 90,
text = legend,
sum=auto,
every only number node/.style={text=white},
color={blue,green,pink,yellow},] {
    9/Blueberries,
    7/Strawberries,
    10/Pineapples,
    16/Bananas}

\end{tikzpicture}

\end{document}

答案1

我只是想快速找点\expandafter乐子。无论如何,请不要使用饼图。

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{pgf-pie}

\pgfplotstableread[col sep=comma]{%
Value, Name
9, \textbf{Blueberries}
7, Strawberries
10, $\displaystyle\int{\tan\theta d\theta}$
16, \emph{\textsf{Bananas}}
}\dataTable


\def\convertpgfplotstabletopie#1#2#3{%
  \begingroup%
  \def\mypie{}%
  \pgfplotstablegetrowsof{#1}\edef\mytemprow{\pgfplotsretval}%
  \foreach\x in {0,...,\numexpr\mytemprow-1\relax}{%
    \pgfplotstablegetelem{\x}{[index]#2}\of{#1}%
    \expandafter\def\expandafter\mytemp\expandafter{\pgfplotsretval}%
    \pgfplotstablegetelem{\x}{[index]#3}\of{#1}%
    \expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\mytemp\expandafter\expandafter\expandafter{\expandafter\mytemp\expandafter/\pgfplotsretval}%
    \ifnum\x<\numexpr\mytemprow-1\relax\expandafter\def\expandafter\mytemp\expandafter{\mytemp,}\fi%
    \expandafter\expandafter\expandafter\gdef\expandafter\expandafter\expandafter\mypie\expandafter\expandafter\expandafter{\expandafter\mypie\mytemp}%
  }%
  \endgroup%
}
\def\pietable[#1]#2{%
\pgfkeys{#1}\expandafter\pie\expandafter{#2}%
}
\begin{document}


\begin{tikzpicture}

\convertpgfplotstabletopie{\dataTable}{0}{1}
\pietable[rotate = 90,text = legend,every only number node/.style={text=white},color={blue,green,pink,yellow}]{\mypie}

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容