如何在环形图的两行中添加标签?

如何在环形图的两行中添加标签?

在带有 pgf-plot 的二维环状饼图的标签中,如果标签很长,如何将其移到第二行?

基本上,我指的是2D 环图并想添加两行标签(我有大标签)

我对 LaTeX 还很陌生,无法更改它。有人能帮我做到这一点吗?我将不胜感激。

答案1

我会添加到文本节点,然后在文本中align=...添加换行符( )。\\

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
    \usetikzlibrary{arrows}
% Adjusts the size of the wheel:
\def\innerradius{1.8cm}
\def\outerradius{2.2cm}

% The main macro
\newcommand{\wheelchart}[1]{
    % Calculate total
    \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 wheel
        \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 wheel
            \pgfmathsetmacro{\cumnum}{0}
            \foreach \value/\colour/\name in {#1} {
                \pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}

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

                % This is necessary for the labels to align nicely
                \pgfmathparse{
                    (-\midangle<180?"west":"east")
                } \edef\textanchor{\pgfmathresult}
                \pgfmathsetmacro\labelshiftdir{1-2*(-\midangle>180)}

                % 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 data labels
                \draw  [*-,thin] node [
                    append after command={
                        (\midangle:\midradius pt)
                        -- (\midangle:\outerradius + 1ex)
                        -- (\tikzlastnode)
                    }
                ] at (\midangle:\outerradius + 1ex) [
                    xshift=\labelshiftdir*0.5cm,
%                    inner sep=0pt,          % <-- commented
                    outer sep=0pt,
                    anchor=\textanchor,
                    % ---------------------------------------------------------
                    % add `align=...' and add linebreaks (`\\') to the text
                    align=left,
                    % ---------------------------------------------------------
                ] {\name: \pgfmathprintnumber{\percentage}\%};

                % Set the old cumulated angle to the new value
                \global\let\cumnum=\newcumnum
            }
        \end{scope}
%      \draw[gray] (0,0) circle (\outerradius) circle (\innerradius);
    \end{tikzpicture}
}
\begin{document}
    % Usage: \wheelchart{<value1>/<colour1>/<label1>, ...}
    \wheelchart{
        26/cyan/Corporate,
        28/orange/This is a very long label \\ that is wrapped over more \\ than one line,
        33.5/yellow/Chimique,
        12.5/blue!50!red/Rhodia%
    }
\end{document}

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

答案2

也许最简单的选择是添加text width到相应节点的选项中。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}

% Adjusts the size of the wheel:
\def\innerradius{1.8cm}
\def\outerradius{2.2cm}

% The main macro
\newcommand{\wheelchart}[1]{
    % Calculate total
    \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 wheel
      \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 wheel
      \pgfmathsetmacro{\cumnum}{0}
      \foreach \value/\colour/\name in {#1} {
            \pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}

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

            % This is necessary for the labels to align nicely
            \pgfmathparse{
               (-\midangle<180?"west":"east")
            } \edef\textanchor{\pgfmathresult}
            \pgfmathsetmacro\labelshiftdir{1-2*(-\midangle>180)}

            % 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 data labels
            \draw  [*-,thin] node [append after command={(\midangle:\midradius
            pt) -- (\midangle:\outerradius + 1ex) --
            (\tikzlastnode)}] at (\midangle:\outerradius + 1ex)
            [xshift=\labelshiftdir*0.5cm,inner sep=0pt, outer sep=0pt, 
            text width=3cm, %<-added
            anchor=\textanchor]{\name: \pgfmathprintnumber{\percentage}\%};


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

      \end{scope}
%      \draw[gray] (0,0) circle (\outerradius) circle (\innerradius);
    \end{tikzpicture}
}

% Usage: \wheelchart{<value1>/<colour1>/<label1>, ...}
\wheelchart{26/cyan/Corporate bla bla bla,  28/orange/Plastique, 33.5/yellow/Chimique, 12.5/blue!50!red/Rhodia}

\end{document}

在此处输入图片描述

答案3

轮图我写的包,可以使用。

\\可以使用换行符来实现数据中的多行。

百分比是自动计算的,并且可以与一起使用\WCperc

在此处输入图片描述

\documentclass[border=6pt]{standalone}
\usepackage{wheelchart}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}
\wheelchart[
  data=\WCvarC: \WCperc,
  lines=0.4,
  lines ext=0.5,
  lines ext left anchor=east,
  lines ext right anchor=west,
  lines sep=-0.2,
  lines style={
    postaction=decorate,
    decoration={
      markings,
      mark=at position 0 with {
        \fill (0,0) circle[radius=0.1];
      }
    }
  },
  perc precision=1,
  radius={1.8}{2.2},
]{%
  26/cyan/Corporate\\and a\\long text,
  28/orange/Plastique\\and another\\long text,
  33.5/yellow/Chimique\\and a\\short text,
  12.5/blue!50!red/Rhodia%
}
\end{tikzpicture}
\end{document}

相关内容