Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong location of the legend when the pie is not alone in the tikzpicture. #18

Open
jofab-hub opened this issue Apr 29, 2024 · 0 comments

Comments

@jofab-hub
Copy link

jofab-hub commented Apr 29, 2024

Dear all,

With pgf-pie, you can only draw, most of the time, one pie with
legends, alone in a tikzpicture. Most of the time, if you draw two
pies with legends, the location of the legend is wrong for the second
pie (unless you draw them aligned horizontally, and from left to
right).

With one pie the location of the legend is correct:

  \pie[text=legend, pos={-5,0}]{10/First, 20/Second, 30/Third, 40/Fourth};

but with two (or more) pies:

  \pie[text=legend, pos={5,0}]{10/First, 20/Second, 30/Third, 40/Fourth};
  \pie[text=legend, pos={-5,0}]{10/First, 20/Second, 30/Third, 40/Fourth};

or

  \pie[text=legend, pos={-5,-5}]{10/First, 20/Second, 30/Third, 40/Fourth};
  \pie[text=legend, pos={5,5}]{10/First, 20/Second, 30/Third, 40/Fourth};

the location is wrong unless you draw them aligned horizontally, and from left to right:

  \pie[text=legend, pos={-5,0}]{10/First, 20/Second, 30/Third, 40/Fourth};
  \pie[text=legend, pos={5,0}]{10/First, 20/Second, 30/Third, 40/Fourth};

Equally, if you draw other elements before drawing your pie, the location of the legend is also wrong:

  \draw (5,2) node{test};
  \pie[text=legend, pos={0,5}]{10/First, 20/Second, 30/Third, 40/Fourth};

The reason is the last lines of pfg-pie.sty:

  % legend
  \iflegend
  \coordinate[xshift=0.8cm,
  yshift=(\value{pgfpie@sliceLength}*0.5+1)*0.5cm] (legendpos) at
  (current bounding box.east);

  \begin{scope}[node distance=0.5cm]
    \foreach \p/\t  [count=\i from 0] in {#2}
    {
      \pgfpie@findColor{\i}
      \node[draw, fill=\thecolor, \pgfpie@style, below of=legendpos, label=0:\t] (legendpos) {};
    }
  \end{scope}
  \fi

The location of the legend is provided by (current bounding box.east) so if any other
drawing moves the right boundary of the current bounding box, the location of the legend
is then wrong.

A solution would be to scope the drawing of the pie and save the boundary of the bounding
box of the scope.

\begin{scope}[local bounding box=scope bounding box]
% ... draw the pie ...
\end{scope}

Now when you want to deduce the location of the legend, you can rely
on (scope bounding box): instead of

yshift=(\value{pgfpie@sliceLength}*0.5+1)*0.5cm] (legendpos) at
  (current bounding box.east);

you can use:

yshift=(\value{pgfpie@sliceLength}*0.5+1)*0.5cm] (legendpos) at
  (scope bounding box.east);

So the location of the legend will be always be attached to the
location of the pie.

Thank you,
J.F.

Example:

\documentclass{article}
\usepackage[a4paper,margin=1cm]{geometry}
\usepackage{tikz}
\usepackage{pgf-pie}
\title{PGF-PIE}
\author{}
\date{}

\begin{document}

\begin{tikzpicture}
  \pie[text=legend, pos={-5,0}]{10/First, 20/Second, 30/Third, 40/Fourth};
  \pie[text=legend, pos={5,0}]{10/First, 20/Second, 30/Third, 40/Fourth};
\end{tikzpicture}\\
\noindent\rule{\textwidth}{0.5pt}
\begin{tikzpicture}
  \pie[text=legend, pos={5,0}]{10/First, 20/Second, 30/Third, 40/Fourth};
  \pie[text=legend, pos={-5,0}]{10/First, 20/Second, 30/Third, 40/Fourth};
\end{tikzpicture}\\
\noindent\rule{\textwidth}{0.5pt}
\begin{tikzpicture}
  \pie[text=legend, pos={-5,-5}]{10/First, 20/Second, 30/Third, 40/Fourth};
  \pie[text=legend, pos={5,5}]{10/First, 20/Second, 30/Third, 40/Fourth};
\end{tikzpicture}\\
\noindent\rule{\textwidth}{0.5pt}
\begin{tikzpicture}
  \draw (5,2) node{test};
  \pie[text=legend, pos={0,5}]{10/First, 20/Second, 30/Third, 40/Fourth};
\end{tikzpicture}


\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant