类似 3D 的饼图

类似 3D 的饼图

我怎样才能获得这种饼图样式:

在此处输入图片描述

平均能量损失

\documentclass[tikz,border=10pt]{standalone}

\usepackage{verbatim}

\begin{comment}
\end{comment}
\begin{document}
\def\angle{0}
\def\radius{3}
\def\cyclelist{{"orange","blue","red","green"}}
\newcount\cyclecount \cyclecount=-1
\newcount\ind \ind=-1
\begin{tikzpicture}[nodes = {font=\sffamily}]
  \foreach \percent/\name in {
      46.6/Chrome,
      24.6/Internet Explorer,
      20.4/Firefox,
      5.1/Safari,
      1.3/Opera,
      2.0/Other
    } {
      \ifx\percent\empty\else               % If \percent is empty, do nothing
        \global\advance\cyclecount by 1     % Advance cyclecount
        \global\advance\ind by 1            % Advance list index
        \ifnum3<\cyclecount                 % If cyclecount is larger than list
          \global\cyclecount=0              %   reset cyclecount and
          \global\ind=0                     %   reset list index
        \fi
        \pgfmathparse{\cyclelist[\the\ind]} % Get color from cycle list
        \edef\color{\pgfmathresult}         %   and store as \color
        % Draw angle and set labels
        \draw[fill={\color!50},draw={\color}] (0,0) -- (\angle:\radius)
          arc (\angle:\angle+\percent*3.6:\radius) -- cycle;
        \node at (\angle+0.5*\percent*3.6:0.7*\radius) {\percent\,\%};
        \node[pin=\angle+0.5*\percent*3.6:\name]
          at (\angle+0.5*\percent*3.6:\radius) {};
        \pgfmathparse{\angle+\percent*3.6}  % Advance angle
        \xdef\angle{\pgfmathresult}         %   and store in \angle
      \fi
    };
\end{tikzpicture}
\end{document}

答案1

这个答案很大程度上受到启发并依赖于这个答案。它有几个附加功能。绘制饼图变得非常简单,只需说

\path[3d pie chart/.cd,radius=4cm,h=1.5cm,colors={"yellow","strangegreen"}] pic{3d pie chart={36/Nuts,64/Honey}};

以下是代码和结果:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{fadings}
\definecolor{strangegreen}{RGB}{15,111,83}
\tikzset{pics/wedge/.style={code={%
  \tikzset{wedge/.cd,#1}
  \def\kvw##1{\pgfkeysvalueof{/tikz/wedge/##1}}
  \pgfmathtruncatemacro{\itest}{3*(1+sign(sin(\kvw{alpha})))+1+sign(sin(\kvw{beta}))}
  \ifcase\itest
    %0: alpha>180,beta>180
       \draw[fill=\kvw{color},very thin]
                       (\kvw{alpha}:\kvw{radius}) 
                       -- ++(0,-\kvw{h}) arc(\kvw{alpha}:\kvw{beta}:\kvw{radius}) 
                       -- ++(0,\kvw{h})  arc(\kvw{beta}:\kvw{alpha}:\kvw{radius});
   \or
    %1: alpha>180,beta=0,180                       
       \draw[fill=\kvw{color},very thin]
                       (\kvw{alpha}:\kvw{radius}) 
                       -- ++(0,-\kvw{h}) arc(\kvw{alpha}:\kvw{beta}:\kvw{radius}) 
                       -- ++(0,\kvw{h})  arc(\kvw{beta}:\kvw{alpha}:\kvw{radius});
   \or
    %2: alpha>180,beta<180                     
       \draw[fill=\kvw{color},very thin]
                       (\kvw{alpha}:\kvw{radius}) 
                       -- ++(0,-\kvw{h}) arc(\kvw{alpha}:360:\kvw{radius}) 
                       -- ++(0,\kvw{h})  arc(360:\kvw{alpha}:\kvw{radius});
   \or
    %3: alpha=0,180,beta>180                       
       \draw[fill=\kvw{color},very thin]
                       (180:\kvw{radius}) 
                       -- ++(0,-\kvw{h}) arc(180:\kvw{beta}:\kvw{radius}) 
                       -- ++(0,\kvw{h})  arc(\kvw{beta}:180:\kvw{radius});
   \or
    %4: alpha=0,180,beta=0,180                     
       \draw[fill=\kvw{color},very thin]
                       (180:\kvw{radius}) 
                       -- ++(0,-\kvw{h}) arc(180:0:\kvw{radius}) 
                       -- ++(0,\kvw{h})  arc(0:180:\kvw{radius});
   \or
    %5: alpha=0,180,beta=<180                      
   \or
    %6: alpha<180,beta=>180                    
       \draw[fill=\kvw{color},very thin]
                       (180:\kvw{radius}) 
                       -- ++(0,-\kvw{h}) arc(180:\kvw{beta}:\kvw{radius}) 
                       -- ++(0,\kvw{h})  arc(\kvw{beta}:180:\kvw{radius});
   \or
    %7: alpha<180,beta=0,180
    \pgfmathtruncatemacro{\ibeta}{sign(cos(\kvw{beta}))}
    \ifnum\ibeta=1
        \draw[fill=\kvw{color},very thin] 
                        (180:\kvw{radius}) 
                        -- ++(0,-\kvw{h}) arc(180:360:\kvw{radius}) 
                        -- ++(0,\kvw{h})  arc(360:180:\kvw{radius});
    \fi
   \or
    %8: alpha<180,beta<180
    \pgfmathtruncatemacro{\ibeta}{sign(sin(\kvw{alpha})-sin(\kvw{beta}))}
    \ifnum\ibeta=1
       \draw[fill=\kvw{color},very thin]
                        (180:\kvw{radius}) 
                        -- ++(0,-\kvw{h}) arc(180:360:\kvw{radius}) 
                        -- ++(0,\kvw{h})  arc(360:180:\kvw{radius});
    \fi
  \fi
  \path[fill=\kvw{color},draw=black] (0,0)--
  (\kvw{alpha}:\kvw{radius})  arc(\kvw{alpha}:\kvw{beta}:\kvw{radius})
                                     --cycle;
}},
wedge/.cd,alpha/.initial=0,beta/.initial=0,%beta > alpha!
color/.initial=blue,
mix color/.initial=gray,radius/.initial=3cm,h/.initial=1cm,
/tikz/.cd,
pics/3d pie chart/.style={code={
  \def\kvw##1{\pgfkeysvalueof{/tikz/3d pie chart/##1}}
  \begin{scope}[yscale=\kvw{aspect},transform shape]
    \path[preaction={fill=black,opacity=.8,
           path fading=circle with fuzzy edge 20 percent}] 
           (0,-\kvw{h}-\kvw{radius}/4.5) 
           circle[radius=1.05*\kvw{radius}];
    \pgfmathsetmacro{\mysum}{0}      
    \foreach \XX/\ZZ  in {#1}  
    {\pgfmathsetmacro{\mysum}{\mysum+\XX}
     \xdef\mysum{\mysum}}
    \pgfmathsetmacro{\myangle}{\kvw{alpha0}}
    \foreach \XX/\ZZ [count=\YY starting from 0,remember=\myangle as \myangle] in {#1} 
    {\pgfmathsetmacro{\myangleB}{\myangle+\XX*(360/\mysum)}
     \pgfmathsetmacro{\mycolor}{{\kvw{colors}}[\YY]}
     \pic{wedge={alpha=\myangle,beta=\myangleB,color=\mycolor,
        radius/.expanded=\kvw{radius},
        h/.expanded=\kvw{h}
        }};
     \fill (\myangle/2+\myangleB/2:\kvw{radius}*\kvw{eccentricity})
      coordinate (\kvw{cname}-\YY) circle[radius=2pt];
     \pgfmathtruncatemacro{\mysign}{sign(cos(\myangle/2+\myangleB/2))} 
     \draw[thick] (\kvw{cname}-\YY)  -- 
      ++(\myangle/2+\myangleB/2:\kvw{armA}) -- ++ 
      (\mysign*3,0)
      \ifnum\mysign<0
        node[above right,transform shape=false]{\ZZ}
        node[below right,transform shape=false]{\XX\%}
      \else
        node[above left,transform shape=false]{\ZZ}
        node[below left,transform shape=false]{\XX\%}
      \fi;    
     \pgfmathsetmacro{\myangle}{\myangleB}
    }
    \shade[left color=black,middle color=white,right color=gray,opacity=0.4]
                          (180:\kvw{radius}) 
                          -- ++(0,-\kvw{h}) arc(180:360:\kvw{radius}) 
                          -- ++(0,\kvw{h})  arc(360:180:\kvw{radius});
  \end{scope}                       
}},
3d pie chart/.cd,
colors/.initial={"blue","red","orange","green","yellow"},
radius/.initial=3cm,h/.initial=1cm,alpha0/.initial=0,
aspect/.initial=0.6,eccentricity/.initial=0.7,cname/.initial=c,
armA/.initial=2cm,armB/.initial=3cm
}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
 \path[3d pie chart/.cd,radius=4cm,h=1.5cm,colors={"yellow","strangegreen"}] pic{3d pie chart={36/Nuts,64/Honey}};
 \path[xshift=12cm,3d pie chart/.cd,radius=4cm,h=1.5cm,alpha0=45,
 colors={"green","yellow","strangegreen"}] 
 pic{3d pie chart={13/Nuts,5/Honey,82/Beer}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

先前的答案:这主要取自这个答案,进而用于这个答案

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{fadings}
\definecolor{strangegreen}{RGB}{15,111,83}
\pgfkeys{%
/piechartthreed/.cd,
scale/.code                =  {\def\piechartthreedscale{#1}},
mix color/.code            =  {\def\piechartthreedmixcolor{#1}},
background color/.code     =  {\def\piechartthreedbackcolor{#1}},
name/.code                 =  {\def\piechartthreedname{#1}}}

\newcommand\piechartthreed[2][]{% 
   \pgfkeys{/piechartthreed/.cd,
     scale            = 1,
     mix color        = gray,
     background color = white,
     name             = pc} 
  \pgfqkeys{/piechartthreed}{#1}
  \begin{scope}[scale=\piechartthreedscale] 
  \begin{scope}[xscale=5,yscale=3] 
     \path[preaction={fill=black,opacity=.8,
         path fading=circle with fuzzy edge 20 percent,
         transform canvas={yshift=-15mm*\piechartthreedscale}}] (0,0) circle (1cm);
     \pgfmathsetmacro\totan{0} 
     \global\let\totan\totan 
     \pgfmathsetmacro\bottoman{180} \global\let\bottoman\bottoman 
     \pgfmathsetmacro\toptoman{0}   \global\let\toptoman\toptoman 
     \begin{scope}[draw=black,thin]
     \foreach \an/\col [count=\xi] in {#2}{%
     \def\space{ } 
        \coordinate (\piechartthreedname\space\xi) at (\totan+\an/2:0.75cm); 
        \ifdim 180pt>\totan pt 
         \ifdim 0pt=\toptoman pt
            \pgfmathsetmacro\toptoman{180} 
            \global\let\toptoman\toptoman         
            \else
          \fi
        \fi   
        \fill[\col!80!gray,draw=black] (0,0)--(\totan:1cm)  arc(\totan:\totan+\an:1cm)
                                     --cycle;     
       \pgfmathsetmacro\finan{\totan+\an}
       \ifdim 180pt<\finan pt 
         \ifdim 180pt=\bottoman pt
            \shadedraw[left color=\col!20!\piechartthreedmixcolor,
                       right color=\col!5!\piechartthreedmixcolor,
                       draw=black,very thin] (180:1cm) -- ++(0,-3mm) arc (180:\totan+\an:1cm) 
                                                       -- ++(0,3mm)  arc (\totan+\an:180:1cm);
            \pgfmathsetmacro\bottoman{0}
            \global\let\bottoman\bottoman
            \else
            \shadedraw[left color=\col!20!\piechartthreedmixcolor,
                       right color=\col!5!\piechartthreedmixcolor,
                       draw=black,very thin](\totan:1cm)-- ++(0,-3mm) arc(\totan:\totan+\an:1cm)
                                                        -- ++(0,3mm)  arc(\totan+\an:\totan:1cm); 
          \fi
        \fi
        \pgfmathsetmacro\totan{\totan+\an}  \global\let\totan\totan 
       } 
    \end{scope}
   \end{scope}  
\end{scope}
}
\begin{document} 
 \begin{tikzpicture}
   \piechartthreed[scale=0.8,
    %                 background color=orange!50,
                   mix color= darkgray]
                   {135/yellow,225/strangegreen}
   \foreach \i in {1,...,2} { \fill (pc \i) circle (.5mm);}
   \draw[darkgray] (pc 1)  -- ++(4,0) coordinate (s1) node[anchor=south east] {Sector 1}
                                                      node[anchor=north east]
                                                      {37.5\%};
   \draw[darkgray] (pc 2)  -- ++(-1,-1) coordinate (s2) -- 
   ++(-4,0) node[anchor=south west] {Sector 2}
                                                      node[anchor=north west]
                                                      {62.5\%}; 
 \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容