1########################################################################
2##
3## Copyright (C) 2007-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn  {} {} pie (@var{x})
28## @deftypefnx {} {} pie (@dots{}, @var{explode})
29## @deftypefnx {} {} pie (@dots{}, @var{labels})
30## @deftypefnx {} {} pie (@var{hax}, @dots{})
31## @deftypefnx {} {@var{h} =} pie (@dots{})
32## Plot a 2-D pie chart.
33##
34## When called with a single vector argument, produce a pie chart of the
35## elements in @var{x}.  The size of the ith slice is the percentage that the
36## element @var{x}i represents of the total sum of @var{x}:
37## @code{pct = @var{x}(i) / sum (@var{x})}.
38##
39## The optional input @var{explode} is a vector of the same length as @var{x}
40## that, if nonzero, "explodes" the slice from the pie chart.
41##
42## The optional input @var{labels} is a cell array of strings of the same
43## length as @var{x} specifying the label for each slice.
44##
45## If the first argument @var{hax} is an axes handle, then plot into this axes,
46## rather than the current axes returned by @code{gca}.
47##
48## The optional return value @var{h} is a list of handles to the patch
49## and text objects generating the plot.
50##
51## Note: If @code{sum (@var{x}) @leq{} 1} then the elements of @var{x} are
52## interpreted as percentages directly and are not normalized by @code{sum
53## (x)}.  Furthermore, if the sum is less than 1 then there will be a missing
54## slice in the pie plot to represent the missing, unspecified percentage.
55##
56## @seealso{pie3, bar, hist, rose}
57## @end deftypefn
58
59## Very roughly based on pie.m from Octave Forge whose author was
60## Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de>
61
62function h = pie (varargin)
63
64  [hax, varargin, nargin] = __plt_get_axis_arg__ ("pie", varargin{:});
65
66  if (nargin < 1)
67    print_usage ();
68  endif
69
70  if (! all (isfinite (varargin{1})))
71    error ("pie: all data in X must be finite");
72  endif
73
74  oldfig = [];
75  if (! isempty (hax))
76    oldfig = get (0, "currentfigure");
77  endif
78  unwind_protect
79    hax = newplot (hax);
80    htmp = __pie__ ("pie", hax, varargin{:});
81  unwind_protect_cleanup
82    if (! isempty (oldfig))
83      set (0, "currentfigure", oldfig);
84    endif
85  end_unwind_protect
86
87  if (nargout > 0)
88    h = htmp;
89  endif
90
91endfunction
92
93
94%!demo
95%! clf;
96%! pie ([3, 2, 1], [0, 0, 1]);
97%! colormap ([1,0,0;0,1,0;0,0,1;1,1,0;1,0,1;0,1,1]);
98%! title ("pie() with exploded wedge");
99
100%!demo
101%! clf;
102%! pie ([3, 2, 1], [0, 0, 1], {"Cheddar", "Swiss", "Camembert"});
103%! colormap ([1,0,0;0,1,0;0,0,1;1,1,0;1,0,1;0,1,1]);
104%! axis ([-2,2,-2,2]);
105%! title ("pie() with labels");
106
107
108%!demo
109%! clf;
110%! pie ([0.17, 0.34, 0.41], {"Cheddar", "Swiss", "Camembert"});
111%! colormap ([1,0,0;0,1,0;0,0,1;1,1,0;1,0,1;0,1,1]);
112%! axis ([-2,2,-2,2]);
113%! title ("pie() with missing slice");
114
115## Test input validation
116%!error pie ()
117%!error <all data in X must be finite> pie ([1 2 Inf])
118%!error <all data in X must be finite> pie ([1 2 NaN])
119