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 |
7215
|
47 [h, varargin] = __plt_get_axis_arg__ ("pie", varargin{:}); |
|
48 |
7118
|
49 if (nargin < 1) |
|
50 print_usage (); |
7215
|
51 else |
7118
|
52 oldh = gca (); |
|
53 unwind_protect |
|
54 axes (h); |
|
55 newplot (); |
7215
|
56 tmp = __pie__ (h, varargin{:}); |
7118
|
57 unwind_protect_cleanup |
|
58 axes (oldh); |
|
59 end_unwind_protect |
|
60 endif |
|
61 |
|
62 if (nargout > 0) |
|
63 retval = tmp; |
|
64 endif |
7119
|
65 |
7118
|
66 endfunction |
|
67 |
|
68 function hlist = __pie__ (varargin) |
7119
|
69 |
7118
|
70 h = varargin{1}; |
|
71 x = abs (varargin{2}); |
|
72 iarg = 3; |
|
73 |
|
74 if (! isvector (x)) |
|
75 error ("pie: expecting vector argument"); |
|
76 endif |
|
77 |
|
78 len = length (x); |
|
79 |
7313
|
80 have_explode = false; |
|
81 have_labels = false; |
|
82 |
7118
|
83 while (iarg <= nargin) |
7119
|
84 arg = varargin{iarg++}; |
7118
|
85 if (iscell (arg)) |
|
86 labels = arg; |
7313
|
87 have_labels = true; |
7118
|
88 if (! size_equal (x, labels)) |
|
89 error ("pie: mismatch in number of labels and data"); |
|
90 endif |
|
91 elseif (isnumeric (arg)) |
|
92 explode = arg; |
7313
|
93 have_explode = true; |
7118
|
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 |
7313
|
100 if (! have_explode) |
7118
|
101 explode = zeros (size (x)); |
|
102 endif |
|
103 |
7313
|
104 if (! have_labels) |
7118
|
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); |
7208
|
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]); |