8

Using the following code I am trying to visualize nested circles:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,fpu}
\usetikzlibrary{decorations.text}

\begin{document}
\def\sizeA{1.248800}
\def\sizeB{.801900}
\def\sizeC{.450300}
\def\sizeD{.255400}
\def\baseRad{5}


\begin{tikzpicture}[rotate=90,%
  textDiscLabel/.style={%
    postaction={%
      decorate,decoration={%
        text along path,%
        reverse path=true,%
        raise=-2ex,%
        text={#1}}
    }
  }
  ]

  \pgfmathsetmacro{\dRatio}{(\sizeD)/(\sizeA)}
  \pgfmathsetmacro{\cRatio}{(\sizeC)/(\sizeA)}
  \pgfmathsetmacro{\bRatio}{(\sizeB)/(\sizeA)}

  \draw[fill=black!30] (0,0) circle (\baseRad);
  \path [textDiscLabel={Size A - 1,248,800}] (0,0) circle (\baseRad);

  \pgfmathparse{\baseRad*(\bRatio-1)}
  \draw[fill=red!40] (\pgfmathresult,0) circle (\bRatio*\baseRad);
  \path [textDiscLabel={Size B - 801,900}] (\pgfmathresult,0) circle (\bRatio*\baseRad);

  \pgfmathparse{\baseRad*(\cRatio-1)}
  \draw[fill=green!50] (\pgfmathresult,0) circle (\cRatio*\baseRad);
  \path [textDiscLabel={Size C - 450,300}] (\pgfmathresult,0) circle (\cRatio*\baseRad);

  \pgfmathparse{\baseRad*(\dRatio-1)}
  \draw[fill=blue!40] (\pgfmathresult,0) circle (\dRatio*\baseRad);
  \path [textDiscLabel={Size D - 255,400}] (\pgfmathresult,0) circle (\dRatio*\baseRad);
\end{tikzpicture}

\end{document}

This yields the following picture:

enter image description here

I fail to adjust the positioning of the text along the perimeter of the discs. In particular, I want it to be centered with respect to the vertical line passing through the centers of the circles. How can I do it? I am looking for a programmed solution because the values that I use may (and will) vary (and thus the radii of the discs).

Bonus question: as you see, the values that I have are between 10^6 to 10^5. PGF fails to do the computations for the ratios (sizeA is too big). How can I overcome this and automate the computation and the labeling?

3 Answers 3

9

The following should help simply things somewhat. The text align key centers the decoration text on the path, but I would use arcs to draw the circle, as it is easier to start the circle borders from the same point and also to know in advance where the center of the the path is located.

Also a partial and not particularly robust (or accurate) solution to the scaling problem: use sp units in the ratio calculations. Note the formatting in the numbers from the OPs post is lost.

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations.text}
\begin{document}
\def\sizeA{1248800}
\def\sizeB{801900}
\def\sizeC{450300}
\def\sizeD{255400}
\begin{tikzpicture}[x=5cm, y=5cm,
  textDiscLabel/.style={%
    decorate,
    decoration={%
        text along path,%
        text align=center,
        reverse path=true,%
        raise=-2ex,%
        text={#1}}
  }]

  \pgfmathsetmacro{\dRatio}{(\sizeD sp)/(\sizeA sp)}
  \pgfmathsetmacro{\cRatio}{(\sizeC sp)/(\sizeA sp)}
  \pgfmathsetmacro{\bRatio}{(\sizeB sp)/(\sizeA sp)}

  \draw [fill=black!30, postaction={textDiscLabel={Size A - \sizeA}}] 
    (0,0) arc (-90:270:1) -- cycle;

  \draw [fill=red!40,   postaction={textDiscLabel={Size B - \sizeB}}] 
    (0,0) arc (-90:270:\bRatio) -- cycle;

  \draw [fill=green!40, postaction={textDiscLabel={Size C - \sizeC}}] 
    (0,0) arc (-90:270:\cRatio) -- cycle;

  \draw [fill=blue!40,  postaction={textDiscLabel={Size D - \sizeD}}] 
    (0,0) arc (-90:270:\dRatio) -- cycle;

\end{tikzpicture}
\end{document}

enter image description here

2
  • There looks to be a missing ] to close the options to tikzpicture...
    – sgmoye
    Aug 11, 2014 at 18:56
  • @sgmoye well spotted. That will teach me to edit code after I've pasted it into an answer. Aug 11, 2014 at 20:17
2

Here is a solution derived form Mark Wibrow's answer with formated numbers. I use \pgfmathprintnumberto to print each number in a macro then I append {} to this macro (text along path seems not happy with math at the end of text).

enter image description here

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations.text}
\begin{document}
\def\sizeA{1248800}
\def\sizeB{801900}
\def\sizeC{450300}
\def\sizeD{255400}
\begin{tikzpicture}[x=5cm, y=5cm,
  textDiscLabel/.style={%
    decorate,
    decoration={%
        text along path,%
        text align=center,
        reverse path=true,%
        raise=-2ex,%
        text={#1}}
  }]

  \pgfmathsetmacro{\dRatio}{(\sizeD sp)/(\sizeA sp)}
  \pgfmathsetmacro{\cRatio}{(\sizeC sp)/(\sizeA sp)}
  \pgfmathsetmacro{\bRatio}{(\sizeB sp)/(\sizeA sp)}

  \pgfkeys{/pgf/number format/.cd,fixed}
  \pgfmathprintnumberto{\sizeA}{\numA}\edef\numA{\numA{}}
  \pgfmathprintnumberto{\sizeB}{\numB}\edef\numB{\numB{}}
  \pgfmathprintnumberto{\sizeC}{\numC}\edef\numC{\numC{}}
  \pgfmathprintnumberto{\sizeD}{\numD}\edef\numD{\numD{}}


  \draw [fill=black!30, postaction={textDiscLabel={Size A -  \numA}}] 
    (0,0) arc (-90:270:1) -- cycle;

  \draw [fill=red!40,   postaction={textDiscLabel={Size B - \numB}}] 
    (0,0) arc (-90:270:\bRatio) -- cycle;

  \draw [fill=green!40, postaction={textDiscLabel={Size C - \numC}}] 
    (0,0) arc (-90:270:\cRatio) -- cycle;

  \draw [fill=blue!40,  postaction={textDiscLabel={Size D - \numD}}] 
    (0,0) arc (-90:270:\dRatio) -- cycle;

\end{tikzpicture}
\end{document}
0

I got the OP's setup to work, though Mark Wibrow's answer is far better. I don't understand the technicalities of \pgfmathparse so will have to look into that further. Still, for the record:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,fpu}
\usetikzlibrary{decorations.text}

\begin{document}
\def\sizeA{1.248800}
\def\sizeB{.801900}
\def\sizeC{.450300}
\def\sizeD{.255400}
\def\baseRad{5}


\begin{tikzpicture}[rotate=90,%
  textDiscLabel/.style={%
    postaction={%
      decorate,decoration={%
        text along path,%
        reverse path=true,%
        text align = {align=center},
        raise=-2ex,%
        text={#1}}
    }
  }
  ]

  \pgfmathsetmacro{\dRatio}{(\sizeD)/(\sizeA)}
  \pgfmathsetmacro{\cRatio}{(\sizeC)/(\sizeA)}
  \pgfmathsetmacro{\bRatio}{(\sizeB)/(\sizeA)}

  \pgfmathsetmacro{\ddRatio}{\baseRad*(1-(\sizeD)/(\sizeA))}
  \pgfmathsetmacro{\ccRatio}{\baseRad*(1-(\sizeC)/(\sizeA))}
  \pgfmathsetmacro{\bbRatio}{\baseRad*(1-(\sizeB)/(\sizeA))}

  \draw[fill=black!30] (0,0) circle (\baseRad);
  \path [textDiscLabel={Size A - 1,248,800},rotate=180] (0,0) circle (\baseRad);

  \pgfmathparse{\baseRad*(\bRatio-1)}
  \draw[fill=red!40] (\pgfmathresult,0) circle (\bRatio*\baseRad);
  \path [textDiscLabel={Size B - 801,900},rotate=180] (\bbRatio,0) circle (\bRatio*\baseRad);

  \pgfmathparse{\baseRad*(\cRatio-1)}
  \draw[fill=green!50] (\pgfmathresult,0) circle (\cRatio*\baseRad);
  \path [textDiscLabel={Size C - 450,300},rotate=180] (\ccRatio,0) circle (\cRatio*\baseRad);

  \pgfmathparse{\baseRad*(\dRatio-1)}
  \draw[fill=blue!40] (\pgfmathresult,0) circle (\dRatio*\baseRad);
  \path [textDiscLabel={Size D - 255,400},rotate=180] (\ddRatio,0) circle (\dRatio*\baseRad);
\end{tikzpicture}

\end{document}

Centered text on circle.

1
  • Now I'm puzzled (not a novel state of affairs): Not once have I gotten any kind of arithmetic overflow error from TikZ/PGF. What would cause that in this case?
    – sgmoye
    Aug 11, 2014 at 22:29

You must log in to answer this question.

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