自定义饼图中标签和百分比的位置 TikZ

自定义饼图中标签和百分比的位置 TikZ

我正在尝试个性化 TikZ 中的饼图,主要是线条、百分比和标签位置。我有下图和脚本:

在此处输入图片描述

\definecolor{rosso}{RGB}{232,76,61}
\definecolor{giallo}{RGB}{64,64,64}
\definecolor{blu}{RGB}{217,217,217}
\definecolor{verde}{RGB}{242,242,242}
\definecolor{viola}{RGB}{153,0,153}

\tikzset{
  chart/.style={
    legend label/.style={font={\scriptsize},anchor=west,align=left},
    legend box/.style={rectangle, draw, minimum size=5pt},
    axis/.style={black,semithick,->},
    axis label/.style={anchor=east,font={\tiny}},
  },
  pie chart/.style={
    chart,
    slice/.style={line cap=round, line join=round, very thick,draw=white},
    pie title/.style={font={\bfseries}},
    slice type/.style 2 args={
        ##1/.style={fill=##2},
        values of ##1/.style={}
    }
  }
}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}


\newcommand{\pie}[3][]{
    \begin{scope}[#1]
    \pgfmathsetmacro{\curA}{90}
    \pgfmathsetmacro{\radius}{1}
    \def\Centre{(0,0)}
    \node[pie title] at (90:1.3) {#2};
    \foreach \v/\s in{#3}{
        \pgfmathsetmacro{\deltaA}{\v/100*360}
        \pgfmathsetmacro{\nextA}{\curA + \deltaA}
        \pgfmathsetmacro{\midA}{(\curA+\nextA)/2}

        \path[slice,\s] \Centre
            -- +(\curA:\radius)
            arc (\curA:\nextA:\radius)
            -- cycle;
   
   \pgfmathsetmacro{\ysign}{ifthenelse(mod(\midA,360)<=180,1,-1)}
   \pgfmathsetmacro{\xsign}{ifthenelse(mod(\midA-90,360)<=180,-1,1)}

   \begin{pgfonlayer}{foreground}
        \draw[*-,thin] \Centre ++(\midA:\radius/2) -- 
                               ++(\xsign*0.07*\radius,\ysign*0.2*\radius) -- 
                               ++(\xsign*\radius,0) 
                      node[above,near end,pie values,values of \s]{\s};
                      
        \draw[*-,thin] \Centre ++(\midA:\radius/2) -- 
                               ++(\xsign*0.07*\radius,\ysign*0.2*\radius) -- 
                               ++(\xsign*\radius,0) 
                      node[above,near start,pie values,values of \s]{\v\%};
                      
   \end{pgfonlayer}


        \global\let\curA\nextA
    }
    \end{scope}
}

\begin{tikzpicture}
[
    pie chart,
    slice type={A}{blu},
    slice type={B}{rosso},
    slice type={C}{giallo},
    slice type={D}{viola},
    slice type={E}{verde},
    pie values/.style={font={\small}},
    scale=2
]

\pie{Percentages}{20/A,30/B,20/C,15/D,15/E}
\end{tikzpicture}

我想将上一个图修改为下一个图:

在此处输入图片描述

我该如何修改它?

答案1

轮图我写的包,可以使用。

切片之间的间隙是通过键获得的gap

使用 键可得到倾斜的线条lines。使用 键可得到线条的水平延伸lines ext

百分比是自动计算的,并且可以与一起使用\WCperc

在此处输入图片描述

\documentclass[border=6pt]{standalone}
\usepackage{wheelchart}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
\definecolor{rosso}{RGB}{232,76,61}
\definecolor{giallo}{RGB}{64,64,64}
\definecolor{blu}{RGB}{217,217,217}
\definecolor{verde}{RGB}{242,242,242}
\definecolor{viola}{RGB}{153,0,153}
\wheelchart[
  counterclockwise,
  gap,
  lines,
  lines ext,
  lines pos=0.9,
  lines sep=0,
  pie,
  title=\textbf{Percentages},
  wheel data={\Large\WCperc}\\\WCvarD,
  wheel data style={align=center}
]{%
  20/blu/A/a,
  30/rosso/B/b,
  20/giallo/C/c,
  15/viola/D/d,
  15/verde/E/e%
}
\end{tikzpicture}
\end{document}

相关内容