]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/private/__pie__.m
update packages
[CreaPhase.git] / octave_packages / m / plot / private / __pie__.m
1 ## Copyright (C) 2007-2012 David Bateman
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))
32     error ("%s: expecting vector argument", caller);
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))
46         error ("%s: mismatch in number of labels and data", caller);
47       endif
48     elseif (isnumeric (arg) || islogical (arg))
49       explode = arg;
50       have_explode = true;
51       if (! size_equal (x, explode))
52         error ("%s: mismatch in number of elements in explode and data",
53                caller);
54       endif
55     else
56       error ("%s: %s is invalid as an optional argument", caller, class (arg));
57     endif
58   endwhile
59
60   if (! have_explode)
61     explode = zeros (size (x));
62   endif
63
64   normalize = true;
65   if (sum (x(:)) < 1)
66     normalize = false;
67   endif
68
69   if (! have_labels)
70     if (normalize)
71       xp = round (100 * x ./ sum (x));
72     else
73       xp = round (100 * x);
74     endif
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;
83   if (normalize)
84     xphi = cumsum (x / sum (x) * 360);
85   else
86     xphi = cumsum (x * 360);
87   endif
88
89   for i = 1:len
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);
110
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;
126         patch(xoff + [0, - sind(xn)], yoff + [0, cosd(xn)], zeros (1, ln + 1), i);
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
142       error ("__pie__: unknown caller `%s'", caller);
143     endif
144   endfor
145
146   addlistener(gca, "view", {@update_text_pos, hlist});
147
148   if (strncmp (caller, "pie3", 4))
149     axis ([-1.25, 1.25, -1.25, 1.25, -0.05, 0.4], "equal", "off");
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))
187           set (tobj(i), "position", [pos(i,1), pos(i,2), -0.05]);
188         else
189           set (tobj(i), "position", [pos(i,1), pos(i,2), 0.40]);
190         endif
191       endif
192
193       if (lr_mask(i))
194         set (tobj(i), "horizontalalignment", "right");
195       else
196         set (tobj(i), "horizontalalignment", "left");
197       endif
198     endfor
199   endif
200 endfunction