1

I drawed a custom pie chart with plain tikz using a combination of arcs and triangles. All fits well so far. One major remaining problem is that there are thin white lines appearing at the intersection of the triangles with the filled out arcs (see for example in piece 28.8%). Does anyone knows how to fix this? And if anyone has some suggestions as to how to automate the calculation of the angles in each of the following pieces I would appretiate some advice to that too.

MWE:

\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}
2
  • I don't see any thin white line??? btw, have look here tex.stackexchange.com/questions/525376/…
    – Black Mild
    Mar 10, 2020 at 18:13
  • 1
    @BlackMild There is a white line on some of the viewers (and at some magnifications). For instance, under Preview on a Mac.
    – user194703
    Mar 10, 2020 at 19:04

1 Answer 1

2

The answer to the question is that there is no need to draw these wedges in two stretches. For instance, instead of

\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);

you can just use

\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 is not necessary but I find it cleaner. Please note that I switched to a more modern syntax for the arcs and circle, and replaced the deprecated \tikzstyle by the corresponding, nondeprecated \tikzset syntax (which are put in the options of the tikzpicture). Further simplifications are possible, and one may also use pgf-pie here.

\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}

enter image description here

1
  • Thank you very much! :) I droped use of pgf-pie since I saw no way to put percentages out of the pieces like in the custom made example above.
    – kilsen
    Mar 11, 2020 at 7:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .