由于值较小而导致的 PGF-PIE 问题

由于值较小而导致的 PGF-PIE 问题

我必须在饼图中包括相当小的值。不幸的是,这些值很难辨认。有没有一种简单的方法可以在饼图外设置小值(或所有值),并用字符串连接这些值和切片?

\documentclass[
a4paper,
captions=tableheading
]
{scrbook}
%%%
\usepackage[
backend=biber %%%% !!! BIBER !!!
]{biblatex}
\usepackage{caption}
\usepackage{tikz}   
\usepackage{pgf-pie}        
\usepackage{pgfplots}
\usepackage{booktabs}                   
\usepackage{pgfplotstable}
\usepackage{siunitx}                            
\usetikzlibrary{patterns}
\usepgfplotslibrary{statistics} 
\sisetup{output-decimal-marker={,},group-separator={\,},}
\def\ScanPercentage#1\afternumber{\SI{#1}{\percent}}
%%%
\begin{document}
\begin{figure}
\textcolor{black!50}{\rule{\linewidth}{0.25pt}}
\begin{center}
\begin{tikzpicture}
\pie[
before number=\ScanPercentage,
after number={},
radius=3.00,  
text=legend, 
rotate=0,
pie values/.style={font={\scriptsize}},
pie legend/.style={font={\scriptsize}}
]
{
38.3/car,
25.0/aircraft,
13.3/boat,
6.7/horse,
1.7/goat,
0.8/ostrich,
14.2/na
}
\end{tikzpicture}
\end{center}
\textcolor{black!50}{\rule{\linewidth}{0.25pt}}
\vspace*{-10pt}
\caption[Pie Chart]
{\textbf{Pie Chart}}
\end{figure}
\end{document}  

除此之外:是否存在预定义的“模式”,其中 tikz 自动定义灰度“颜色”来填充不同的切片?

答案1

我只能回答问题的第二部分。Torbjørn T. 好心地告诉我有一本手册。我认为如果不重写宏,就无法轻松地将数字移出。问题的第二部分很简单:只需在后面添加一个颜色列表color=。接下来,会临时尝试重新定义切片的宏。结果远非完美。我当然不是 TiZ 专家,所以我现在说的话必须谨慎对待。然而,我不得不指出,该代码中完成的许多事情可以以更优雅、更灵活的方式完成。我只是在这里做了最小的损坏修复。有一个键Move Out,如果它不等于 1,则会将数字移出。结果远非最佳,也是因为我继续对事物进行硬编码。我在这个网站上看到了更好的饼图,例如这个,所以对我来说,最有希望的方法似乎是采用其中之一。

\documentclass[
a4paper,
captions=tableheading
]
{scrbook}
\usepackage{caption}
\usepackage{tikz}   
\usepackage{pgf-pie}        
\usepackage{siunitx}                            
\sisetup{output-decimal-marker={,},group-separator={\,},}
\def\ScanPercentage#1\afternumber{\SI{#1}{\percent}}
%%%
\newif\ifOut
%\Debugtrue
\Outfalse
\pgfkeys{/pgf/.cd,
Move Out/.initial=1,
Move Out=1,
Minimum Angle/.initial=0}
\newcounter{myslice}
\makeatletter
\renewcommand{\pgfpie@slice}[8]{\stepcounter{myslice}
  \pgfmathparse{0.5*(#1)+0.5*(#2)}
  \let\midangle\pgfmathresult

  \path (#8) -- ++(\midangle:#5) coordinate(O);

  \pgfmathparse{#7+#5}
  \let\radius\pgfmathresult

  \pgfmathsetmacro{\SwitchOff}{ifthenelse(#2-#1<\pgfkeysvalueof{/pgf/Minimum
  Angle},1,0)}

  % slice
  \draw[line join=round, fill=#6, \style] (O) -- ++(#1:#7) arc (#1:#2:#7) -- cycle;

  \pgfmathparse{min(((#2)-(#1)-10)/110*(-0.3),0)}
  \let\temp\pgfmathresult
  \pgfmathtruncatemacro{\itest}{ifthenelse(\pgfkeysvalueof{/pgf/Move Out}==1,1,0)}
  \ifnum\itest=1
  \pgfmathsetmacro\innerpos{((max(\temp,-0.5)+0.8)*#7)}
  \else
  \pgfmathsetmacro\innerpos{\pgfkeysvalueof{/pgf/Move Out}*#7*(1+0.2*sin(90*\themyslice))}
  \fi
  \ifthenelse{\equal{\pgfpie@text}{inside}}
  {
    % label and number together
    \path (O) -- ++(\midangle:\innerpos) node
    {\scalefont{#3}\shortstack{#4\\\beforenumber#3\afternumber}};
  }
  {
    % label
    \iflegend
    \else
    \path (O) -- ++ (\midangle:\radius)
    node[inner sep=0, \pgfpie@text=\midangle:#4]{};
    \fi

    % number
    \ifnum\SwitchOff=1
    \else
     \ifnum\itest=1
      \path (O) -- ++(\midangle:\innerpos) node
      {\scalefont{#3}\beforenumber#3\afternumber};
     \else
      \path (O) -- ++(\midangle:\innerpos)  node (pie-N-\themyslice)
      {\scalefont{#3}\beforenumber#3\afternumber};
      \draw[latex-] (\midangle:{0.85*#7}) -- (pie-N-\themyslice);
     \fi
    \fi
  }
}
\makeatother
\begin{document}
\begin{figure}
\textcolor{black!50}{\rule{\linewidth}{0.25pt}}
\begin{center}
\begin{tikzpicture}
\pie[
before number=\ScanPercentage,
after number={},
radius=3.00,  
text=legend, 
rotate=0,
/pgf/Move Out=1.5,
/pgf/Minimum Angle=8,
color={gray!10,gray!20,gray!30,gray!40,gray!50,gray!60,gray!70},
pie values/.style={font={\scriptsize}},
pie legend/.style={font={\scriptsize}}
]
{
38.3/car,
25.0/aircraft,
13.3/boat,
6.7/horse,
1.7/goat,
0.8/ostrich,
14.2/na
}
\path (0,6);
\end{tikzpicture}
\end{center}
\textcolor{black!50}{\rule{\linewidth}{0.25pt}}
\vspace*{-10pt}
\caption[Pie Chart]
{\textbf{Pie Chart}}
\end{figure}
\end{document}  

在此处输入图片描述

至于评论中提出的问题:

  1. 我添加了一个键Minimum Angle。每当线段的角度小于此角度时,标签将被抑制。对于Minimum Angle=8,这会杀死0,8% and 1,7%
  2. 我添加了\path (0,6);,这会使饼图稍微向下移动。如果您使用\path (0,7);,饼图将进一步向下移动。

相关内容