关于在环形图中将标签放置在扇区中心的指导

关于在环形图中将标签放置在扇区中心的指导

我从互联网上获取了此甜甜圈/饼图的 Latex 代码。我根据自己的要求进行了修改。不知何故,我无法将标签放入甜甜圈图中扇区的中心。我尝试修改\draw节点命令,但它没有将标签放入扇区的中心。这是因为我的 Tikz 技能和 Latex 技能不够高级。如果您能指导我将另一个标签(例如扇区 5)放入甜甜圈图孔中并为其提供颜色,我将不胜感激。提前谢谢。

以下是我的 MWE:

\documentclass[border=2mm]{standalone}
\usepackage{xcolor}
\usepackage{tikz}

\def\innerradius{1.0cm}
\def\outerradius{2.2cm}

%%%%% Donut chart macro %%%%%%
\newcommand{\donutchart}[1]{
\pgfmathsetmacro{\totalnum}{0} \foreach \value/\colour/\name in {#1} {
    \pgfmathparse{\value+\totalnum}
    \global\let\totalnum=\pgfmathresult
}

\begin{tikzpicture}

  % Calculate the thickness and the middle line of the chart
  \pgfmathsetmacro{\wheelwidth}{\outerradius-\innerradius}
  \pgfmathsetmacro{\midradius}{(\outerradius+\innerradius)/2}

  % Rotate so we start from the top
  \begin{scope}[rotate=90]

  % Loop through each value set.cumnum keeps track of where we are in the chart
  \pgfmathsetmacro{\cumnum}{0}
  \foreach \value/\colour/\name in {#1} {
        \pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}

        % Calculate the mid angle of the colour segments to place the labels
        \pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}

        % Draw the color segments. Somehow, the \midrow units got lost, so we add 'pt' at the end. Not nice...
        \fill[\colour] (-\cumnum:\outerradius) arc (-\cumnum:-(\newcumnum):\outerradius) --
        (-\newcumnum:\innerradius) arc (-\newcumnum:-(\cumnum):\innerradius) -- cycle;

        % Draw the labels for sector
        \draw  node [text=black, font=\bfseries] at (\midangle:\innerradius+ 1ex) {\name};

        % Set the old cumulated angle to the new value
        \global\let\cumnum=\newcumnum
    }

  \end{scope}
\end{tikzpicture}}

\begin{document}

 \donutchart{26/cyan/Sector1,  28/orange/Sector2, 33.5/yellow/Sector3, 12.5/red/Sector4}

\end{document}

这是我的代码的结果:

在此处输入图片描述

答案1

如果您只计算半径的中心,这很容易。但是如果您不旋转文本,它仍然看起来很奇怪,尽管它确实居中。

要了解更多详细信息,请查看代码中添加的注释。

\documentclass[border=2mm]{standalone}
\usepackage{xcolor}
\usepackage{tikz}

    \def\innerradius{1.0cm}
    \def\outerradius{2.2cm}
    % add a command to compute the center of the radii
    \pgfmathsetlengthmacro{\centerradius}{(\outerradius + \innerradius)/2}

    %%%%% Donut chart macro %%%%%%
    \newcommand{\donutchart}[1]{%
        \pgfmathsetmacro{\totalnum}{0} \foreach \value/\colour/\name in {#1} {
            \pgfmathparse{\value+\totalnum}
            \global\let\totalnum=\pgfmathresult
        }
        \begin{tikzpicture}

            % Calculate the thickness and the middle line of the chart
            \pgfmathsetmacro{\wheelwidth}{\outerradius-\innerradius}
            \pgfmathsetmacro{\midradius}{(\outerradius+\innerradius)/2}

            % Rotate so we start from the top
            \begin{scope}[rotate=90]

                % Loop through each value set.cumnum keeps track of where we are in the chart
                \pgfmathsetmacro{\cumnum}{0}
                \foreach \value/\colour/\name in {#1} {
                    \pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}

                    % Calculate the mid angle of the colour segments to place the labels
                    \pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}

                    % Draw the color segments. Somehow, the \midrow units got lost, so we add 'pt' at the end. Not nice...
                    \fill[\colour] (-\cumnum:\outerradius) arc (-\cumnum:-(\newcumnum):\outerradius) --
                    (-\newcumnum:\innerradius) arc (-\newcumnum:-(\cumnum):\innerradius) -- cycle;

                    % Draw the labels for sector
                    \node [
                        text=black,
                        font=\bfseries,
                        % rotate the text so it can be better seen that it
                        % is really in the center
                        rotate=\midangle,
%                        draw=green,
                    ] at (\midangle:\centerradius) {\name};
                    % for debugging purposes only
                    \draw  node [circle,fill=green,inner sep=0pt,minimum size=5pt] at (\midangle:\centerradius) {};

                    % Set the old cumulated angle to the new value
                    \global\let\cumnum=\newcumnum
                }

            \end{scope}
        \end{tikzpicture}%
    }

\begin{document}

 \donutchart{26/cyan/Sector1,  28/orange/Sector2, 33.5/yellow/Sector3, 12.5/red/Sector4}

\end{document}

该图显示了上述代码的结果

相关内容