7118
|
1 ## Copyright (C) 2007 David Bateman |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
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. |
|
9 ## |
|
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. |
|
14 ## |
|
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/>. |
|
18 |
|
19 ## -*- texinfo -*- |
|
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. |
|
26 ## |
|
27 ## Called with a single vector arrgument, 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}. |
|
30 ## |
|
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. |
|
33 ## |
|
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. |
|
36 ## |
|
37 ## The optional return value @var{h} provides a handle to the patch object. |
|
38 ## |
|
39 ## @seealso{bar, stem} |
|
40 ## @end deftypefn |
|
41 |
|
42 ## Very roughly based on pie.m from octave-forge whose author was |
|
43 ## Daniel Heiserer <Daniel.heiserer@physik.tu-muenchen.de> |
|
44 |
|
45 function retval = pie (varargin) |
|
46 |
|
47 if (nargin < 1) |
|
48 print_usage (); |
|
49 elseif (isscalar (varargin{1}) && ishandle (varargin{1})) |
|
50 h = varargin {1}; |
|
51 if (! strcmp (get (h, "type"), "axes")) |
|
52 error ("pie: expecting first argument to be an axes object"); |
|
53 endif |
|
54 oldh = gca (); |
|
55 unwind_protect |
|
56 axes (h); |
|
57 newplot (); |
|
58 tmp = __pie__ (h, varargin{2:end}); |
|
59 unwind_protect_cleanup |
|
60 axes (oldh); |
|
61 end_unwind_protect |
|
62 else |
|
63 newplot (); |
|
64 tmp = __pie__ (gca (), varargin{:}); |
|
65 endif |
|
66 |
|
67 if (nargout > 0) |
|
68 retval = tmp; |
|
69 endif |
|
70 endfunction |
|
71 |
|
72 function hlist = __pie__ (varargin) |
|
73 h = varargin{1}; |
|
74 x = abs (varargin{2}); |
|
75 iarg = 3; |
|
76 |
|
77 if (! isvector (x)) |
|
78 error ("pie: expecting vector argument"); |
|
79 endif |
|
80 |
|
81 len = length (x); |
|
82 |
|
83 while (iarg <= nargin) |
|
84 arg = varargin {iarg++}; |
|
85 if (iscell (arg)) |
|
86 labels = arg; |
|
87 if (! size_equal (x, labels)) |
|
88 error ("pie: mismatch in number of labels and data"); |
|
89 endif |
|
90 elseif (isnumeric (arg)) |
|
91 explode = arg; |
|
92 if (! size_equal (x, explode)) |
|
93 error ("pie: mismatch in number of elements in explode and data"); |
|
94 endif |
|
95 endif |
|
96 endwhile |
|
97 |
|
98 if (! exist ("explode", "var")) |
|
99 explode = zeros (size (x)); |
|
100 endif |
|
101 |
|
102 if (! exist ("labels", "var")) |
|
103 xp = round (100 * x ./ sum (x)); |
|
104 for i = 1 : len |
|
105 labels{i} = sprintf ("%d%%", xp(i)); |
|
106 endfor |
|
107 endif |
|
108 |
|
109 hlist = []; |
|
110 refinement = 90; |
|
111 phi = 0 : refinement : 360; |
|
112 xphi = cumsum (x / sum(x) * 360); |
|
113 for i = 1 : len |
|
114 if (i == 1) |
|
115 xn = 0 : 360 / refinement : xphi(i); |
|
116 else |
|
117 xn = xphi(i-1) : 360 / refinement : xphi(i); |
|
118 endif |
|
119 |
|
120 if (xn (length (xn)) != xphi(i)) |
|
121 xn = [xn, xphi(i)]; |
|
122 endif |
|
123 |
|
124 xn2 = (xn(1) + xn(end)) / 2; |
|
125 if (explode (i)) |
|
126 xoff = - 0.1 * sind (xn2); |
|
127 yoff = 0.1 * cosd (xn2); |
|
128 else |
|
129 xoff = 0; |
|
130 yoff = 0; |
|
131 endif |
|
132 xt = - 1.2 * sind (xn2); |
|
133 yt = 1.2 * cosd (xn2); |
|
134 if (xt > 0) |
|
135 align = "left"; |
|
136 else |
|
137 align = "right"; |
|
138 endif |
|
139 |
|
140 hlist = [hlist; patch(xoff + [0, - sind(xn)], yoff + [0, cosd(xn)], i); |
|
141 text(xt, yt, labels {i}, "HorizontalAlignment", align)]; |
|
142 endfor |
|
143 if (len == 1) |
|
144 set (h, "clim", [1, 2]); |
|
145 else |
|
146 set (h, "clim", [1, len]); |
|
147 endif |
|
148 axis ([-1.5,1.5,-1.5,1.5]); |
|
149 endfunction |
|
150 |