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 |
7119
|
70 |
7118
|
71 endfunction |
|
72 |
|
73 function hlist = __pie__ (varargin) |
7119
|
74 |
7118
|
75 h = varargin{1}; |
|
76 x = abs (varargin{2}); |
|
77 iarg = 3; |
|
78 |
|
79 if (! isvector (x)) |
|
80 error ("pie: expecting vector argument"); |
|
81 endif |
|
82 |
|
83 len = length (x); |
|
84 |
|
85 while (iarg <= nargin) |
7119
|
86 arg = varargin{iarg++}; |
7118
|
87 if (iscell (arg)) |
|
88 labels = arg; |
|
89 if (! size_equal (x, labels)) |
|
90 error ("pie: mismatch in number of labels and data"); |
|
91 endif |
|
92 elseif (isnumeric (arg)) |
|
93 explode = arg; |
|
94 if (! size_equal (x, explode)) |
|
95 error ("pie: mismatch in number of elements in explode and data"); |
|
96 endif |
|
97 endif |
|
98 endwhile |
|
99 |
|
100 if (! exist ("explode", "var")) |
|
101 explode = zeros (size (x)); |
|
102 endif |
|
103 |
|
104 if (! exist ("labels", "var")) |
|
105 xp = round (100 * x ./ sum (x)); |
7119
|
106 for i = 1:len |
7118
|
107 labels{i} = sprintf ("%d%%", xp(i)); |
|
108 endfor |
|
109 endif |
|
110 |
|
111 hlist = []; |
|
112 refinement = 90; |
7119
|
113 phi = 0:refinement:360; |
|
114 xphi = cumsum (x / sum (x) * 360); |
|
115 for i = 1:len |
7118
|
116 if (i == 1) |
|
117 xn = 0 : 360 / refinement : xphi(i); |
|
118 else |
|
119 xn = xphi(i-1) : 360 / refinement : xphi(i); |
|
120 endif |
|
121 |
7119
|
122 if (xn(end) != xphi(i)) |
7118
|
123 xn = [xn, xphi(i)]; |
|
124 endif |
|
125 |
|
126 xn2 = (xn(1) + xn(end)) / 2; |
|
127 if (explode (i)) |
|
128 xoff = - 0.1 * sind (xn2); |
|
129 yoff = 0.1 * cosd (xn2); |
|
130 else |
|
131 xoff = 0; |
|
132 yoff = 0; |
|
133 endif |
|
134 xt = - 1.2 * sind (xn2); |
|
135 yt = 1.2 * cosd (xn2); |
|
136 if (xt > 0) |
|
137 align = "left"; |
|
138 else |
|
139 align = "right"; |
|
140 endif |
|
141 |
|
142 hlist = [hlist; patch(xoff + [0, - sind(xn)], yoff + [0, cosd(xn)], i); |
7119
|
143 text(xt, yt, labels {i}, "horizontalalignment", align)]; |
7118
|
144 endfor |
7119
|
145 |
7118
|
146 if (len == 1) |
|
147 set (h, "clim", [1, 2]); |
|
148 else |
|
149 set (h, "clim", [1, len]); |
|
150 endif |
7119
|
151 |
7120
|
152 axis ([-1.5, 1.5, -1.5, 1.5], "square"); |
7119
|
153 |
7118
|
154 endfunction |
7120
|
155 |
|
156 %!demo |
|
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]); |
|
159 |
|
160 %!demo |
|
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]); |