Mercurial > hg > octave-lyh
annotate scripts/plot/private/__pie__.m @ 14535:8150ccfffa22
Apply broadcasting to inputs of line().
* __line__.m: Use broadcasting the match the sizes of inputs.
* line.m: Add demo.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Fri, 06 Apr 2012 19:21:16 -0400 |
parents | 72c96de7a403 |
children | 5d3a684236b0 d174210ce1ec |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11589
diff
changeset
|
1 ## Copyright (C) 2007-2012 David Bateman |
11330 | 2 ## Copyright (C) 2010 Kai Habel |
3 ## | |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
8 ## the Free Software Foundation; either version 3 of the License, or (at | |
9 ## your option) any later version. | |
10 ## | |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
17 ## along with Octave; see the file COPYING. If not, see | |
18 ## <http://www.gnu.org/licenses/>. | |
19 | |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {@var{hlist} =} __pie__ (caller, @dots{}) | |
22 ## Undocumented internal function. | |
23 ## @end deftypefn | |
24 | |
25 function hlist = __pie__ (caller, varargin) | |
26 | |
27 h = varargin{1}; | |
28 x = abs (varargin{2}); | |
29 iarg = 3; | |
30 | |
31 if (! isvector (x)) | |
11338
892ef3688ccf
__pie__.m: improve error messages
John W. Eaton <jwe@octave.org>
parents:
11332
diff
changeset
|
32 error ("%s: expecting vector argument", caller); |
11330 | 33 endif |
34 | |
35 len = length (x); | |
36 | |
37 have_explode = false; | |
38 have_labels = false; | |
39 | |
40 while (iarg <= nargin - 1) | |
41 arg = varargin{iarg++}; | |
42 if (iscell (arg)) | |
43 labels = arg; | |
44 have_labels = true; | |
45 if (numel (x) != numel (labels)) | |
11338
892ef3688ccf
__pie__.m: improve error messages
John W. Eaton <jwe@octave.org>
parents:
11332
diff
changeset
|
46 error ("%s: mismatch in number of labels and data", caller); |
11330 | 47 endif |
11338
892ef3688ccf
__pie__.m: improve error messages
John W. Eaton <jwe@octave.org>
parents:
11332
diff
changeset
|
48 elseif (isnumeric (arg) || islogical (arg)) |
11330 | 49 explode = arg; |
50 have_explode = true; | |
51 if (! size_equal (x, explode)) | |
11338
892ef3688ccf
__pie__.m: improve error messages
John W. Eaton <jwe@octave.org>
parents:
11332
diff
changeset
|
52 error ("%s: mismatch in number of elements in explode and data", |
892ef3688ccf
__pie__.m: improve error messages
John W. Eaton <jwe@octave.org>
parents:
11332
diff
changeset
|
53 caller); |
11330 | 54 endif |
11338
892ef3688ccf
__pie__.m: improve error messages
John W. Eaton <jwe@octave.org>
parents:
11332
diff
changeset
|
55 else |
892ef3688ccf
__pie__.m: improve error messages
John W. Eaton <jwe@octave.org>
parents:
11332
diff
changeset
|
56 error ("%s: %s is invalid as an optional argument", caller, class (arg)); |
11330 | 57 endif |
58 endwhile | |
59 | |
60 if (! have_explode) | |
61 explode = zeros (size (x)); | |
62 endif | |
63 | |
11341
3c7ba1e3dc21
Add missing option slice for pie and pie3
Kai Habel <kai.habel@gmx.de>
parents:
11338
diff
changeset
|
64 normalize = true; |
3c7ba1e3dc21
Add missing option slice for pie and pie3
Kai Habel <kai.habel@gmx.de>
parents:
11338
diff
changeset
|
65 if (sum (x(:)) < 1) |
3c7ba1e3dc21
Add missing option slice for pie and pie3
Kai Habel <kai.habel@gmx.de>
parents:
11338
diff
changeset
|
66 normalize = false; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
67 endif |
11341
3c7ba1e3dc21
Add missing option slice for pie and pie3
Kai Habel <kai.habel@gmx.de>
parents:
11338
diff
changeset
|
68 |
11330 | 69 if (! have_labels) |
11341
3c7ba1e3dc21
Add missing option slice for pie and pie3
Kai Habel <kai.habel@gmx.de>
parents:
11338
diff
changeset
|
70 if (normalize) |
3c7ba1e3dc21
Add missing option slice for pie and pie3
Kai Habel <kai.habel@gmx.de>
parents:
11338
diff
changeset
|
71 xp = round (100 * x ./ sum (x)); |
3c7ba1e3dc21
Add missing option slice for pie and pie3
Kai Habel <kai.habel@gmx.de>
parents:
11338
diff
changeset
|
72 else |
3c7ba1e3dc21
Add missing option slice for pie and pie3
Kai Habel <kai.habel@gmx.de>
parents:
11338
diff
changeset
|
73 xp = round (100 * x); |
3c7ba1e3dc21
Add missing option slice for pie and pie3
Kai Habel <kai.habel@gmx.de>
parents:
11338
diff
changeset
|
74 endif |
11330 | 75 for i = 1:len |
76 labels{i} = sprintf ("%d%%", xp(i)); | |
77 endfor | |
78 endif | |
79 | |
80 hlist = []; | |
81 refinement = 90; | |
82 phi = 0:refinement:360; | |
11341
3c7ba1e3dc21
Add missing option slice for pie and pie3
Kai Habel <kai.habel@gmx.de>
parents:
11338
diff
changeset
|
83 if (normalize) |
3c7ba1e3dc21
Add missing option slice for pie and pie3
Kai Habel <kai.habel@gmx.de>
parents:
11338
diff
changeset
|
84 xphi = cumsum (x / sum (x) * 360); |
3c7ba1e3dc21
Add missing option slice for pie and pie3
Kai Habel <kai.habel@gmx.de>
parents:
11338
diff
changeset
|
85 else |
3c7ba1e3dc21
Add missing option slice for pie and pie3
Kai Habel <kai.habel@gmx.de>
parents:
11338
diff
changeset
|
86 xphi = cumsum (x * 360); |
3c7ba1e3dc21
Add missing option slice for pie and pie3
Kai Habel <kai.habel@gmx.de>
parents:
11338
diff
changeset
|
87 endif |
3c7ba1e3dc21
Add missing option slice for pie and pie3
Kai Habel <kai.habel@gmx.de>
parents:
11338
diff
changeset
|
88 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
89 for i = 1:len |
11330 | 90 if (i == 1) |
91 xn = 0 : 360 / refinement : xphi(i); | |
92 else | |
93 xn = xphi(i-1) : 360 / refinement : xphi(i); | |
94 endif | |
95 | |
96 if (xn(end) != xphi(i)) | |
97 xn = [xn, xphi(i)]; | |
98 endif | |
99 | |
100 xn2 = (xn(1) + xn(end)) / 2; | |
101 if (explode (i)) | |
102 xoff = - 0.1 * sind (xn2); | |
103 yoff = 0.1 * cosd (xn2); | |
104 else | |
105 xoff = 0; | |
106 yoff = 0; | |
107 endif | |
108 xt = - 1.2 * sind (xn2); | |
109 yt = 1.2 * cosd (xn2); | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
110 |
11330 | 111 if (len == 1) |
112 set (h, "clim", [1, 2]); | |
113 else | |
114 set (h, "clim", [1, len]); | |
115 endif | |
116 | |
117 if (strncmp (caller, "pie3", 4)) | |
118 ln = length (xn); | |
119 zlvl = 0.35; | |
120 sx = repmat (xoff + [0, - sind(xn), 0], [2 1]); | |
121 sy = repmat (yoff + [0, cosd(xn), 0], [2 1]); | |
122 sz = [zeros(1, ln + 2); zlvl * ones(1, ln + 2)]; | |
123 sc = i * ones (size (sz)); | |
124 | |
125 hlist = [hlist; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
126 patch(xoff + [0, - sind(xn)], yoff + [0, cosd(xn)], zeros (1, ln + 1), i); |
11330 | 127 surface(sx, sy, sz, sc); |
128 patch(xoff + [0, - sind(xn)], yoff + [0, cosd(xn)], zlvl * ones (1, ln + 1), i); | |
129 text(xt, yt, zlvl, labels{i})]; | |
130 | |
131 elseif (strncmp (caller, "pie", 3)) | |
132 if (xt > 0) | |
133 align = "left"; | |
134 else | |
135 align = "right"; | |
136 endif | |
137 | |
138 hlist = [hlist; patch(xoff + [0, - sind(xn)], yoff + [0, cosd(xn)], i); | |
139 text(xt, yt, labels{i}, "horizontalalignment", align)]; | |
140 | |
141 else | |
11338
892ef3688ccf
__pie__.m: improve error messages
John W. Eaton <jwe@octave.org>
parents:
11332
diff
changeset
|
142 error ("__pie__: unknown caller `%s'", caller); |
11330 | 143 endif |
144 endfor | |
145 | |
146 addlistener(gca, "view", {@update_text_pos, hlist}); | |
147 | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
148 if (strncmp (caller, "pie3", 4)) |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
149 axis ([-1.25, 1.25, -1.25, 1.25, -0.05, 0.4], "equal", "off"); |
11330 | 150 view (-37.5, 30); |
151 elseif (strncmp (caller, "pie", 3)) | |
152 axis ([-1.5, 1.5, -1.5, 1.5], "square", "off"); | |
153 endif | |
154 endfunction | |
155 | |
156 function update_text_pos (all_handles) | |
157 ## Text objects in the foreground should be at the base level. | |
158 ## Text objects in the background should be at the top level. | |
159 ## Text objects on the right side should be aligned to the right | |
160 ## and on the left side to the left. | |
161 tobj = findobj (all_handles, "type", "text"); | |
162 | |
163 ## check if we are called from pie3 | |
164 s = findobj (all_handles, "type", "surface"); | |
165 is_pie3 = false; | |
166 if (length (s) > 0) | |
167 is_pie3 = true; | |
168 endif | |
169 | |
170 if (length (tobj) > 0) | |
171 ax = get (tobj(1), "parent"); | |
172 azel = get (ax, "view"); | |
173 pos = get (tobj, "position"); | |
174 if (iscell (pos)) | |
175 pos = cell2mat (pos); | |
176 endif | |
177 phi = atand (pos(:,1) ./ pos(:,2)); | |
178 [theta, r] = cart2pol (pos(:,1), pos(:,2)); | |
179 theta *= 180/pi; | |
180 theta -= azel(1); | |
181 theta = mod (theta, 360); | |
182 ud_mask = (theta > 180); | |
183 lr_mask = (theta > 90) & (theta < 270); | |
184 for i = 1 : length (tobj) | |
185 if (is_pie3) | |
186 if (ud_mask(i)) | |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
187 set (tobj(i), "position", [pos(i,1), pos(i,2), -0.05]); |
11330 | 188 else |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
189 set (tobj(i), "position", [pos(i,1), pos(i,2), 0.40]); |
11330 | 190 endif |
191 endif | |
192 | |
193 if (lr_mask(i)) | |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
194 set (tobj(i), "horizontalalignment", "right"); |
11330 | 195 else |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
196 set (tobj(i), "horizontalalignment", "left"); |
11330 | 197 endif |
198 endfor | |
199 endif | |
200 endfunction |