靠近饼图数字的标签

靠近饼图数字的标签

我正在尝试使用 tikz 并排创建两个饼图。我在网上找到了一个代码,正在改编,到目前为止,它似乎有效。但是,我不想在下面使用单独的图例,而是想在图表中的数字 % 附近插入系列标签。有人知道我该如何修改下面的代码来实现它吗?

在此先感谢您提供的任何帮助。

\documentclass[twoside,a4paper,12pt]{report}

\usepackage{tikz}
\usetikzlibrary{chains,positioning,shapes.symbols}

%define each color I want to use for each country/slice
\definecolor{Australia} {RGB} {47,117,181}
\definecolor{Brazil} {RGB} {0,176,80}
\definecolor{Canada} {RGB} {244,176,132}
\definecolor{China} {RGB} {191,143,0}
\definecolor{France} {RGB} {155,194,230}
\definecolor{Germany} {RGB} {198,89,17}
\definecolor{Greece} {RGB} {221,235,247}
\definecolor{Guinea} {RGB} {255,0,255}
\definecolor{Guyana} {RGB} {255,217,102}
\definecolor{Hungary} {RGB} {198,224,180}
\definecolor{India} {RGB} {248,203,173}
\definecolor{Indonesia} {RGB} {146,208,80}
\definecolor{Ireland} {RGB} {255,217,0}
\definecolor{Jamaica} {RGB} {131,60,12}
\definecolor{Japan} {RGB} {31,78,120}
\definecolor{Middle East} {RGB} {84,130,53}
\definecolor{Norway} {RGB} {255,255,0}
\definecolor{Russia} {RGB} {255,0,0}
\definecolor{Suriname} {RGB} {112,48,160}
\definecolor{UK} {RGB} {204,51,0}
\definecolor{USA} {RGB} {0,0,255}
\definecolor{USSR} {RGB} {255,0,0}
\definecolor{Others} {RGB} {166,166,166}



\tikzstyle{chart}=[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}},]

\tikzstyle{pie chart}=[chart,
    slice/.style={line cap=round, line join=round, very thick,draw=white},
    pie title/.style={font={\bf}},
    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{\r}{1}
    \def\c{(0,0)}
    \node[pie title] at (90:1.5) {#2};
    \foreach \v/\s in{#3}{
        \pgfmathsetmacro{\deltaA}{\v/100*360}
        \pgfmathsetmacro{\nextA}{\curA + \deltaA}
        \pgfmathsetmacro{\midA}{(\curA+\nextA)/2}

        \path[slice,\s] \c
            -- +(\curA:\r)
            arc (\curA:\nextA:\r)
            -- cycle;
        \pgfmathsetmacro{\d}{max((\deltaA * -(.5/50) + 1) , .5)}

        \begin{pgfonlayer}{foreground}
        \path \c -- node[pos=\d,pie values,values of \s]{$\v\%$} +(\midA:\r);
        \end{pgfonlayer}

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

\newcommand{\legend}[2][]{
    \begin{scope}[#1]
    \path
        \foreach \n/\s in {#2}
            {
                  ++(0,-10pt) node[\s,legend box] {} +(5pt,0) node[legend label] {\n}
            }
    ;
    \end{scope}
}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
[   pie chart,
    slice type={Australia}{Australia},
    slice type={Brazil}{Brazil},
    slice type={Canada}{Canada},
    slice type={China}{China},
    slice type={France}{France},
    slice type={Germany}{Germany},
    slice type={Greece}{Greece},
    slice type={Guinea}{Guinea},
    slice type={Guyana}{Guyana},
    slice type={Hungary}{Hungary},
    slice type={India}{India},
    slice type={Indonesia}{Indonesia},
    slice type={Ireland}{Ireland},
    slice type={Jamaica}{Jamaica},
    slice type={Japan}{Japan},
    slice type={Middle East}{Middle East},
    slice type={Norway}{Norway},
    slice type={Russia}{Russia},
    slice type={Suriname}{Suriname},
    slice type={UK}{UK},
    slice type={USA}{USA},
    slice type={USSR}{USSR},
    slice type={Others}{Others},
    pie values/.style={font={\small}},
    scale=2
]
    \pie[xshift=0cm,values of Australia/.style={pos=1.2},values of Jamaica/.style={pos=1.2},values of Suriname/.style={pos=1.2},values of USSR/.style={pos=1.2},values of France/.style={pos=1.2},values of Greece/.style={pos=1.2},values of Guinea/.style={pos=1.2},values of Guyana/.style={pos=1.2},values of Hungary/.style={pos=1.2},,values of Others/.style={pos=1.2}]%
    {1972}{20/Australia,18/Jamaica,10/Suriname,11/USSR,5/France,3/Greece,4/Guinea,5/Guyana,3/Hungary,21/Others}

    \pie[xshift=4cm,values of Australia/.style={pos=1.2},values of Brazil/.style={pos=1.2},values of China/.style={pos=1.2},values of Indonesia/.style={pos=1.2},,values of Guinea/.style={pos=1.2},values of India/.style={pos=1.2},values of Jamaica/.style={pos=1.2},values of Russia/.style={pos=1.2},values of Others/.style={pos=1.2}]%
    {2010}{32/Australia,15/Brazil,14/China,11/Indonesia,8/Guinea,6/India,4/Jamaica,3/Russia,7/Others}

    \legend[shift={(-2cm,-1.5cm)}]{{Australia}/Australia,{Brazil}/Brazil,{China}/China}
    \legend[shift={(-0.5cm,-1.5cm)}]{{France}/France,{Greece}/Greece,{Guinea}/Guinea}
    \legend[shift={(1cm,-1.5cm)}]{{Guyana}/Guyana,{Hungary}/Hungary,{India}/India}
    \legend[shift={(2.5cm,-1.5cm)}]{{Indonesia}/Indonesia,{Jamaica}/Jamaica,{Russia}/Russia}
    \legend[shift={(4cm,-1.5cm)}]{{Suriname}/Suriname,{USSR}/USSR,{Others}/Others}
\end{tikzpicture}
\end{figure}

\end{document} 

答案1

这里介绍一种不使用饼图来显示数据的方法,而是使用斜率图

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[y=0.35cm, x=2cm]
\foreach \nameA/\offsetA/\valA/\valB/\nameB/\offsetB in {
Australia/  0/  20/ 32/ Australia/  0,
Jamaica/    0/  18/  4/ Jamaica/    0,
Suriname/   0/  10/ -1/ /           0,
USSR/       0/  11/  3/ Russia/     0,
France/     0/   5/ -1/ /           0,
Greece/     -2ex/    3/ -1/ /       0,
Guinea/     0/   4/  8/ Guinea/     0,
Guyana/     2ex/     5/ -1/         0,
Hungary/    0/   3/ -1/             0,
Others/     0/  21/  7/ Others/     0,
Brazil/     0/  -1/ 15/ Brazil/     0,
China/      0/  -1/ 14/ China/      0,
India/      0/  -1/  6/ India/      0,
Indonesia/  0/  -1/ 11/ Indonesia/  0
}{
\ifdim\valA pt<0pt
    \draw (1,\valB) -- ++(0.1,0) node [anchor=mid west, yshift=\offsetB, yshift=-0.4ex] {\valB\ \ \nameB};
\else
    \ifdim\valB pt<0pt
        \draw (-0.1,\valA) node [anchor=mid east, yshift=\offsetA, yshift=-0.4ex] {\nameA\ \ \valA} -- ++(0.1,0);
    \else
        \draw (-0.1,\valA) node [anchor=mid east, yshift=\offsetA, yshift=-0.4ex] {\nameA\ \ \valA} -- ++(0.1,0) -- 
            (1,\valB) -- ++(0.1,0) node [anchor=mid west, yshift=\offsetB, yshift=-0.4ex] {\valB\ \ \nameB};
    \fi
\fi
}
\node [font=\bfseries\large, anchor=east] at (-0.1,0) {1972};
\node [font=\bfseries\large, anchor=west] at (1.1,0) {2010};
\end{tikzpicture}%
\end{document}

相关内容