1## Copyright (C) 2007, 2008, 2009 David Bateman
3## This file is part of Octave.
5## Octave is free software; you can redistribute it and/or modify it
6## under the terms of the GNU General Public License as published by
7## the Free Software Foundation; either version 3 of the License, or (at
8## your option) any later version.
10## Octave is distributed in the hope that it will be useful, but
11## WITHOUT ANY WARRANTY; without even the implied warranty of
12## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13## General Public License for more details.
15## You should have received a copy of the GNU General Public License
16## along with Octave; see the file COPYING. If not, see
17## <http://www.gnu.org/licenses/>.
20## @deftypefn {Function File} {} pie (@var{y})
21## @deftypefnx {Function File} {} pie (@var{y}, @var{explode})
22## @deftypefnx {Function File} {} pie (@dots{}, @var{labels})
23## @deftypefnx {Function File} {} pie (@var{h}, @dots{});
24## @deftypefnx {Function File} {@var{h} =} pie (@dots{});
25## Produce a pie chart.
27## Called with a single vector argument, produces a pie chart of the
28## elements in @var{x}, with the size of the slice determined by percentage
29## size of the values of @var{x}.
31## The variable @var{explode} is a vector of the same length as @var{x} that
32## if non zero 'explodes' the slice from the pie chart.
34## If given @var{labels} is a cell array of strings of the same length as
35## @var{x}, giving the labels of each of the slices of the pie chart.
37## The optional return value @var{h} provides a handle to the patch object.
42## Very roughly based on pie.m from octave-forge whose author was
43## Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de>
45function retval = pie (varargin)
47 [h, varargin] = __plt_get_axis_arg__ ("pie", varargin{:});
56 tmp = __pie__ (h, varargin{:});
57 unwind_protect_cleanup
68function hlist = __pie__ (varargin)
71 x = abs (varargin{2});
75 error ("pie: expecting vector argument");
83 while (iarg <= nargin)
84 arg = varargin{iarg++};
88 if (numel (x) != numel (labels))
89 error ("pie: mismatch in number of labels and data");
91 elseif (isnumeric (arg))
94 if (! size_equal (x, explode))
95 error ("pie: mismatch in number of elements in explode and data");
101 explode = zeros (size (x));
105 xp = round (100 * x ./ sum (x));
107 labels{i} = sprintf ("%d%%", xp(i));
113 phi = 0:refinement:360;
114 xphi = cumsum (x / sum (x) * 360);
117 xn = 0 : 360 / refinement : xphi(i);
119 xn = xphi(i-1) : 360 / refinement : xphi(i);
122 if (xn(end) != xphi(i))
126 xn2 = (xn(1) + xn(end)) / 2;
128 xoff = - 0.1 * sind (xn2);
129 yoff = 0.1 * cosd (xn2);
134 xt = - 1.2 * sind (xn2);
135 yt = 1.2 * cosd (xn2);
142 hlist = [hlist; patch(xoff + [0, - sind(xn)], yoff + [0, cosd(xn)], i);
143 text(xt, yt, labels{i}, "horizontalalignment", align)];
147 set (h, "clim", [1, 2]);
149 set (h, "clim", [1, len]);
152 axis ([-1.5, 1.5, -1.5, 1.5], "square");
157%! pie ([3, 2, 1], [0, 0, 1]);
158%! colormap([1,0,0;0,1,0;0,0,1;1,1,0;1,0,1;0,1,1]);
161%! pie ([3, 2, 1], [0, 0, 1], {"Cheddar", "Swiss", "Camembert"});
162%! colormap([1,0,0;0,1,0;0,0,1;1,1,0;1,0,1;0,1,1]);
163%! axis ([-2,2,-2,2]);