7

I like the ring diagram, so I want to use a kind of ring diagram in my document. Thank Jack for the answer Ring Diagram(https://tex.stackexchange.com/users/2552/jake). However when I use the code in my document, the diagram isn't at a fine position. So i want to xshift it. However the command xshift doesn't work! What hapened?

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}

% Adjusts the size of the wheel: \def\innerradius{1.cm} \def\outerradius{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} \draw[red] (0,0) circle [radius=2.5cm]; % Rotate so we start from the top \begin{scope}[xshift=-1cm,rotate=90]%%here I want to xshift

% 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, ,anchor=\textanchor]{\name: \pgfmathprintnumber{\percentage}\%};


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

\end{scope} \clip (-0cm,-2.5cm) rectangle (5cm,2.5cm);%there should be some problems but the tikz ignores it, why? %\draw[gray] (0,0) circle (\outerradius) circle (\innerradius); \end{tikzpicture} } \wheelchart{26/cyan/Corporate, 28/orange/Plastique, 33.5/yellow/Chimique, 12.5/blue!50!red/Rhodia}

\end{document}

xshift=0cm enter image description here

xshift=-2cm enter image description here

These 2 pictures above are the real outputs after I change the xshift paragram(where? \begin{scope}[xshift=-1cm,rotate=-90]). So why? Thank you for reading and answers. I had installed the texlive 2017.

I want the colored circle to shift along x-axis. The length can be decided by me. Because my document has two columms like this. enter image description here

The colored circle now is at the left column, however I want to move it to the right column like this;

enter image description here

In this way, there are some mistakes which you can see in the picture above.I tried to xshift,but it failed. And last night I took advice and used the \clip to fix my problem. But Why does xshift not work as I want?(move towards left)

BO.LI
  • 173
  • 1
    The shift for scopes has only an effect it you have another object inside your tikzpicture outside the scope. Otherwise - as in your case - this will not have any visible effect. – epR8GaYuh Jul 10 '17 at 14:00
  • You should probably try the \clip-command to reduce/add whitespaces and you might want to think of using a float. Compare https://tex.stackexchange.com/questions/49039/clipping-elements-in-tikz#49040. – CampanIgnis Jul 10 '17 at 14:02
  • @epR8GaYuh thanks a lot for your comment. In fact, my document has two columns. The diagram is at a too left position, so that it only has the left half in the page and the right half disappears. So I want to add some xshift(eg. xshift=-1cm), however it doesn't move at all. – BO.LI Jul 10 '17 at 14:32
  • @CampanIgnis I tried your solution, but it didn't work. I did like this:\end{scope} \clip (-3cm,-2.5cm) rectangle (5cm,2.5cm);... and \end{scope} \clip (-0cm,-2.5cm) rectangle (5cm,2.5cm);... I noticed that the output pictues were the same. – BO.LI Jul 10 '17 at 14:33
  • @JohnKormylo If I want to move the tikzpictures against the layout of a page, How should I implement it? – BO.LI Jul 10 '17 at 14:38
  • @BO.LI AS far as I understand your comment, you have not understand the other post. You need to add the \clip-command before the objects you want to clip (read the fine manual). You could also try \clip[draw] the first times you use this command. – CampanIgnis Jul 10 '17 at 14:45
  • 1
    @CampanIgnis Thanks a lot for advice, the \clip-command does work for my problem. – BO.LI Jul 10 '17 at 15:02
  • positioning of your image in a text had to be set outside of picture. For example ... \begin{center} ... your image ...end{center} ...if you like to have centered. if you like to shift, than use\hspace{ }`. Frankly said, i don't understand well, what is your problem. – Zarko Jul 10 '17 at 15:08
  • @Zarko I want the colored circle xshift towards the left and the red circle doesn't move, however the red moves right and the colored doesn't move. In fact I want to the text and the colored circle are closer.and the red circle doesn't move. – BO.LI Jul 10 '17 at 15:44
  • how you now this? I'm afraid that your expectation are wrong. To see, what is mowing, you should fix one node regarding to page border (not recommend) . otherwise, all is relative (to some starting coordinate). To be honest, I still not understand, what you like to achieve. Any version of shift command works. – Zarko Jul 10 '17 at 17:39
  • @Zarko please look the post which I have edited. I want just to move left the picture to some length. However it doesn't move at all. Oppositely the red(reference) which should not have move at all should move right. I believe it interesting. – BO.LI Jul 11 '17 at 07:08

1 Answers1

5

Your problem is that everything outside the tikzpicture adds space the the left or right of the tikzpicture. I added some code to draw the bounding box, then removed the extra spaces until the bounding box matched the edges of the standalone.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}

% Adjusts the size of the wheel:
\def\innerradius{1.cm}
\def\outerradius{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}
  \draw[red] (0,0) circle [radius=2.5cm];
  % Rotate so we start from the top
  \begin{scope}[rotate=90]%%here I want to xshift

  % 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 [draw,append after command={(\midangle:\midradius pt) -- (\midangle:\outerradius + 1ex) -- (\tikzlastnode)}] at (\midangle:\outerradius + 1ex) [xshift=\labelshiftdir*0.5cm,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[green] (current bounding box.south west) rectangle (current bounding box.north east);
  %\clip (-0cm,-2.5cm) rectangle (5cm,2.5cm);%there should be some problems but the tikz ignores it, why?
  %\draw[gray] (0,0) circle (\outerradius) circle (\innerradius);
\end{tikzpicture}%
}

\begin{document}
\wheelchart{26/cyan/Corporate,  28/orange/Plastique, 33.5/yellow/Chimique, 12.5/blue!50!red/Rhodia}%
\end{document}

demo


If you want to center the origin inside the bounding box, you can use the following (either before, after or instead of drawing the bounding box). It requires the calc tikzlibrary.

\path ($(current bounding box.south west)!2!(0,0)$);
\path ($(current bounding box.north east)!2!(0,0)$);
John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Thanks for your answer. It can fix my problem. And I found it was the extra spaces that disturbed me. I compared your code and my previous code. I found nothing but the line \draw[green] (current bounding box.south west) rectangle (current bounding box.north east); So where is your magical code ? – BO.LI Jul 11 '17 at 07:31
  • Every line that ends with { or } adds a space, whereate {% and }% do not. Also, I moved \begin{document} back as far as possible. – John Kormylo Jul 11 '17 at 15:48