使用纯 tikz 制作自定义饼图

使用纯 tikz 制作自定义饼图

我使用纯 tikz 绘制了一个自定义饼图,结合了圆弧和三角形。到目前为止一切都很合适。一个主要问题是,三角形与填充圆弧的交点处出现了细白线(例如,参见图 28.8%)。有人知道如何解决这个问题吗?如果有人对如何自动计算以下每个图的角度有什么建议,我也会很感激。

梅威瑟:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
    \begin{tikzpicture}

%Style Definitions
\tikzstyle{piece1} = [fill=black!5] 
\tikzstyle{piece2} = [fill=black!20]
\tikzstyle{piece3} = [fill=black!35]
\tikzstyle{piece4} = [fill=black!50]
\tikzstyle{piece5} = [fill=black!65]
\tikzstyle{line} = [line width=0.4pt,line cap=round,shorten >=0.2pt]
\tikzstyle{line2} = [line width=0.2pt]  
\tikzstyle{percent} = [font=\footnotesize]

%Piece 59,8%
\fill[piece1] (0,0) ++( 90 : 3 )coordinate(l1) arc (90:-90:3 )coordinate(r1)coordinate[pos=0.65](b2);
\fill[piece1] (r1) -- (0,0) -- (l1);

\fill[piece1] (0,0) ++( -90 : 3 )coordinate(l2) arc (-90:-125.28:3 )coordinate(r2) ;
\fill[piece1] (r2) -- (0,0) -- (l2);

%Piece 28,8%
\fill[piece2] (0,0) ++( -125.28 : 3 )coordinate(l3) arc (-125.28:-228.96:3 )coordinate(r3) coordinate[pos=0.5](b3);
\fill[piece2] (r3) -- (0,0) -- (l3);

%Piece 9,2%
\fill[piece3] (0,0) ++( -228.96 : 3 )coordinate(l4) arc (-228.96:-262.08:3 )coordinate(r4) coordinate[pos=0.5](b4);
\fill[piece3] (r4) -- (0,0) -- (l4);

%Piece 1,3%
\fill[piece4] (0,0) ++( -262.08 : 3 )coordinate(l5) arc (-262.08:-266.76:3 )coordinate(r5) coordinate[pos=0.5](b5);
\fill[piece4] (r5) -- (0,0) -- (l5);

%Piece 1%
\fill[piece5] (0,0) ++( -266.76 : 3 )coordinate(l6) arc (-266.76:-270:3 )coordinate(r6) coordinate[pos=0.5](b6);
\fill[piece5] (r6) -- (0,0) -- (l6);

%Pie Lines
\draw[line] (0,0) -- (r2);
\draw[line] (0,0) -- (r3);
\draw[line] (0,0) -- (r4);
\draw[line] (0,0) -- (r5);
\draw[line] (0,0) -- (r6);
\draw[line] (0,0) circle (3);

% Percentages
\path (0,0) -- coordinate[pos=0.65](b2m)(b2);
\node[percent] at (b2m) {59,8\%};

\path (0,0) -- coordinate[pos=0.65](b3m)(b3);
\node[percent] at (b3m) {28,8\%};

\path (0,0) -- coordinate[pos=0.65](b4m)(b4);
\node[percent] at (b4m) {9,2\%};

\draw[line2] (b5) -- ++(0,0.25) -- ++(-0.25,0.25) node[percent,anchor=south east]{1,3\%};

\draw[line2] (b6) -- ++(0,0.25) -- ++(0.25,0.25) node[percent,anchor=south west]{1\%};

%Legend
\node[piece3,draw=black,line width=0.2,rectangle,minimum width=0.25cm,minimum height=0.25cm,label={right:Fachholschulreife}](leg1) at (4,0) {};

\node[piece2,draw=black,line width=0.2,rectangle,minimum width=0.25cm,minimum height=0.25cm,label={right:Fachholschulreife},above=9pt of leg1.north] (leg2){};

\node[piece1,draw=black,line width=0.2,rectangle,minimum width=0.25cm,minimum height=0.25cm,label={right:Fachholschulreife},above=9pt of leg2.north] {};

\node[piece4,draw=black,line width=0.2,rectangle,minimum width=0.25cm,minimum height=0.25cm,label={right:Fachholschulreife},below=9pt of leg1.south] (leg4){};

\node[piece5,draw=black,line width=0.2,rectangle,minimum width=0.25cm,minimum height=0.25cm,label={right:Fachholschulreife},below=9pt of leg4.south] {};
\end{tikzpicture}
\end{document}

答案1

这个问题的答案是,没有必要将这些楔形画成两段。例如,

\fill[piece1] (0,0) ++( 90 : 3 )coordinate(l1) arc (90:-90:3 )coordinate(r1)coordinate[pos=0.65](b2);
\fill[piece1] (r1) -- (0,0) -- (l1);

你可以使用

\fill[piece1] (0,0) -- ++( 90:3)coordinate(l1) arc[start angle=90,end angle=-90,radius=3] 
coordinate(r1)coordinate[pos=0.65](b2)
-- cycle;

where-- cycle不是必需的,但我发现它更简洁。请注意,我为 sarc和切换到了更现代的语法circle,并\tikzstyle用相应的非弃用\tikzset语法(放在 的选项中tikzpicture)替换了弃用的语法。可以进一步简化,也可以pgf-pie在这里使用。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
    \begin{tikzpicture}[%Style Definitions
      piece1/.style={fill=black!5}, 
      piece2/.style={fill=black!20},
      piece3/.style={fill=black!35},
      piece4/.style={fill=black!50},
      piece5/.style={fill=black!65},
      line/.style={line width=0.4pt,line cap=round,shorten >=0.2pt},
      line2/.style={line width=0.2pt},  
      percent/.style={font=\footnotesize},
      box/.style={draw=black,line width=0.2,rectangle,minimum width=0.25cm,minimum height=0.25cm}]

%Piece 59,8%
\fill[piece1] (0,0) -- ++( 90:3)coordinate(l1) arc[start angle=90,end angle=-90,radius=3] 
coordinate(r1)coordinate[pos=0.65](b2)
-- cycle;

\fill[piece1] (0,0) -- ++ (-90:3) coordinate(l2) arc[start angle=-90,end angle=-125.28,radius=3]  
coordinate(r2) -- cycle;

%Piece 28,8%
\fill[piece2] (0,0) -- ++ ( -125.28:3) coordinate(l3) arc[start angle=-125.28,end angle=-228.96,radius=3] 
coordinate(r3) coordinate[pos=0.5](b3)
-- cycle;

%Piece 9,2%
\fill[piece3] (0,0) -- ++ ( -228.96:3) coordinate(l4) arc[start angle=-228.96,end angle=-262.08,radius=3] 
coordinate(r4) coordinate[pos=0.5](b4)-- cycle;

%Piece 1,3%
\fill[piece4] (0,0) -- ++ ( -262.08:3) coordinate(l5) arc[start angle=-262.08,end angle=-266.76,radius=3] 
coordinate(r5) coordinate[pos=0.5](b5)-- cycle;

%Piece 1%
\fill[piece5] (0,0) -- ++ ( -266.76:3) coordinate(l6) arc[start angle=-266.76,end angle=-270,radius=3] 
coordinate(r6) coordinate[pos=0.5](b6)-- cycle;

Pie Lines
\draw[line] (0,0) -- (r2);
\draw[line] (0,0) -- (r3);
\draw[line] (0,0) -- (r4);
\draw[line] (0,0) -- (r5);
\draw[line] (0,0) -- (r6);
\draw[line] (0,0) circle[radius=3];

% Percentages
\path (0,0) -- coordinate[pos=0.65](b2m)(b2);
\node[percent] at (b2m) {59,8\%};

\path (0,0) -- coordinate[pos=0.65](b3m)(b3);
\node[percent] at (b3m) {28,8\%};

\path (0,0) -- coordinate[pos=0.65](b4m)(b4);
\node[percent] at (b4m) {9,2\%};

\draw[line2] (b5) -- ++(0,0.25) -- ++(-0.25,0.25) node[percent,anchor=south east]{1,3\%};

\draw[line2] (b6) -- ++(0,0.25) -- ++(0.25,0.25) node[percent,anchor=south west]{1\%};

%Legend
\node[piece3,box,label={right:Fachholschulreife}](leg1) at (4,0) {};

\node[piece2,box,label={right:Fachholschulreife},above=9pt of leg1.north] (leg2){};

\node[piece1,box,label={right:Fachholschulreife},above=9pt of leg2.north] {};

\node[piece4,box,label={right:Fachholschulreife},below=9pt of leg1.south] (leg4){};

\node[piece5,box,label={right:Fachholschulreife},below=9pt of leg4.south] {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容