自定义饼图

自定义饼图

我想绘制以下饼图。我想知道是否有人可以告诉我从哪里开始。

(仅饼图和周围的描述圈)

答案1

下一个代码改编自 Jake 的回答在 TeX 中创建环形图这已经改编自他自己的回答我怎样才能制作出像 PGF 手册第 88 页那样的“环形(或轮形)图表”?

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

% Adjusts the size of the wheel:
\def\innerradius{1cm}
\def\outerradius{3.5cm}

% 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,\colour] 
              node [circle, very thick, draw=\colour,
                fill=gray!70, text=white, text width=2cm, align=center, 
                append after command={(\midangle:\midradius pt) -- 
                  (\midangle:\outerradius + 5ex) -- (\tikzlastnode)}] 
              at (\midangle:\outerradius + 1ex) [xshift=\labelshiftdir*0.75cm, 
                inner sep=0pt, outer sep=0pt, ,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{45/blue!70/First Task, 25/orange!70/Second Task, 
      15/yellow!70/Third Task, 15/purple!70/Fourth Task}
\end{document}

在此处输入图片描述

更新 - 描述

我做了一些更改以添加circle descriptions branching from other circles

wheelchart是一个,但它本身macro并不包含一个,但您需要声明它。tikzpicture

\begin{tikzpicture}
\wheelchart{...}
\end{tikzpicture}

每个circle nodeinwheelchart的参数后面都有一个名称\name。您可以使用此节点名称来定位(使用positioning库)descriptions并在它们之间添加一条边。

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

% Adjusts the size of the wheel:
\def\innerradius{1cm}
\def\outerradius{3.5cm}

% 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,\colour] 
             node [circle, very thick, draw=\colour, fill=gray!70, text=white, 
               text width=2cm, align=center, 
               append after command={(\midangle:\midradius pt) -- (\midangle:\outerradius + 5ex) 
               -- (\tikzlastnode)}] (\name) at (\midangle:\outerradius + 1ex) 
               [xshift=\labelshiftdir*0.75cm, inner sep=0pt, outer sep=0pt, 
                 anchor=\textanchor]{\name: \pgfmathprintnumber{\percentage}\%};

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

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

\begin{tikzpicture}

% Usage: \wheelchart{<value1>/<colour1>/<label1>, ...}
\wheelchart{45/blue!70/First Task, 25/orange!70/Second Task, 
     15/yellow!70/Third Task, 15/purple!70/Fourth Task}

\node [above=1cm of First Task, circle, text width=2cm, blue!70, 
      fill=black!70!orange, text=white, align=center] (1TD) 
      {Description for First Task};
\draw[very thick, green!70] (First Task) -- (1TD);

\node [above right=1cm and 0.5cm of Fourth Task, circle, text width=2cm, 
      blue!70, fill=black!70!red, text=white, align=center] (4TD) 
      {Description for Fourth Task};
\draw[very thick, green!70] (Fourth Task) -- (4TD);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

\documentclass[border=12pt,pstricks]{standalone}
\usepackage{pstricks-add}
\def\CPut#1#2{\pscircle*[linecolor=black!15](0,0){5mm}%
      \pscircle[linecolor=#1!70](0,0){5mm}\rput(0,0){\tiny\shortstack{#2}}}
\begin{document}

 \begin{pspicture}(-3.5,-3.5)(4,3.5)
 \psChart[userColor={blue!70,orange!70,yellow!70,purple!70},
    chartSep=13pt,shadow,shadowsize=5pt]{45,25,15,15}{4}{2}
 \pscircle*{0.5}
 \psset{nodesepA=5pt,nodesepB=-10pt}
 \ncline{psChartO1}{psChart1}\nput{0}{psChartO1}{\CPut{blue}{Foo\\ 45\,\%}}
 \ncline{psChartO2}{psChart2}\nput{150}{psChartO2}{\CPut{orange}{Bar\\ 25\%}}
 \ncline{psChartO3}{psChart3}\nput{-90}{psChartO3}{\CPut{yellow}{Baz\\ 15\%}}
 \ncline{psChartO4}{psChart4}\nput{0}{psChartO4}{\CPut{purple}{FBa\\ 15\%}}
 \rput(psChartI1){Taxes}\rput(psChartI2){Rent}\rput(psChartI3){Bills}\rput(psChartI4){Car}
  \rput(0,0){\bfseries\white Rock}
 \end{pspicture}
\end{document}

在此处输入图片描述

答案3

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

使用 键获取线条lines。使用 键将起点放置在外圆lines sep=0

文本用 key 来放置wheel data,这个文本的位置由 key 决定wheel data pos,这个文本被放置在一个节点中,这个节点的样式由 key 决定wheel data style

在此处输入图片描述

\documentclass[border=6pt]{standalone}
\usepackage{wheelchart}
\begin{document}
\begin{tikzpicture}
\wheelchart[
  data=,
  lines,
  lines sep=0,
  wheel data=\WCvarC,
  wheel data pos=2,
  wheel data style={
    circle,
    fill=gray,
    draw=\WCvarB,
    ultra thick
  }
]{%
  1/cyan/text A,
  2/magenta/text B,
  4/orange/text C%
}
\end{tikzpicture}
\end{document}

相关内容