]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/area.m
update packages
[CreaPhase.git] / octave_packages / m / plot / area.m
1 ## Copyright (C) 2007-2012 Michael Goffioul
2 ## Copyright (C) 2007-2009 David Bateman
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} {} area (@var{x}, @var{y})
22 ## @deftypefnx {Function File} {} area (@var{x}, @var{y}, @var{lvl})
23 ## @deftypefnx {Function File} {} area (@dots{}, @var{prop}, @var{val}, @dots{})
24 ## @deftypefnx {Function File} {} area (@var{y}, @dots{})
25 ## @deftypefnx {Function File} {} area (@var{h}, @dots{})
26 ## @deftypefnx {Function File} {@var{h} =} area (@dots{})
27 ## Area plot of cumulative sum of the columns of @var{y}.  This shows the
28 ## contributions of a value to a sum, and is functionally similar to
29 ## @code{plot (@var{x}, cumsum (@var{y}, 2))}, except that the area under
30 ## the curve is shaded.
31 ##
32 ## If the @var{x} argument is omitted it is assumed to be given by
33 ## @code{1 : rows (@var{y})}.  A value @var{lvl} can be defined that determines
34 ## where the base level of the shading under the curve should be defined.
35 ##
36 ## Additional arguments to the @code{area} function are passed to
37 ## @code{patch}.
38 ##
39 ## The optional return value @var{h} is a graphics handle to the hggroup
40 ## object representing the area patch objects.
41 ## @seealso{plot, patch}
42 ## @end deftypefn
43
44 function h = area (varargin)
45
46   [ax, varargin, nargin] = __plt_get_axis_arg__ ("area", varargin{:});
47
48   if (nargin > 0)
49     idx = 1;
50     x = y = [];
51     bv = 0;
52     args = {};
53     ## Check for (X) or (X,Y) arguments and possible base value.
54     if (nargin >= idx && ismatrix (varargin{idx}))
55       y = varargin{idx};
56       idx++;
57       if (nargin >= idx)
58         if (isscalar (varargin{idx}))
59           bv = varargin{idx};
60           idx++;
61         elseif (ismatrix (varargin{idx}))
62           x = y;
63           y = varargin{idx};
64           idx++;
65           if (nargin >= idx && isscalar (varargin{idx}))
66             bv = varargin{idx};
67             idx++;
68           endif
69         endif
70       endif
71     else
72       print_usage ();
73     endif
74     ## Check for additional args.
75     if (nargin >= idx)
76       args = {varargin{idx:end}};
77     endif
78     newplot ();
79     if (isvector (y))
80       y = y(:);
81     endif
82     if (isempty (x))
83       x = repmat ([1:size(y, 1)]', 1, size (y, 2));
84     elseif (isvector (x))
85       x = repmat (x(:),  1, size (y, 2));
86     endif
87
88     oldax = gca ();
89     unwind_protect
90       axes (ax);
91       tmp = __area__ (ax, x, y, bv, args{:});
92     unwind_protect_cleanup
93       axes (oldax);
94     end_unwind_protect
95
96     if (nargout > 0)
97       h = tmp;
98     endif
99   else
100     print_usage ();
101   endif
102
103 endfunction
104
105 function retval = __area__ (ax, x, y, bv, varargin)
106
107   y0 = bv * ones (1, rows (y));
108   y0 = zeros (1, rows (y));
109   retval = [];
110   for i = 1: size (y, 2);
111     hg = hggroup ();
112     retval = [retval; hg];
113     args = __add_datasource__ ("area", hg, {"x", "y"}, varargin{:});
114
115     x1 = x(:, 1).';
116     y1 = y (:, i).';
117     addproperty ("xdata", hg, "data", x1);
118     addproperty ("ydata", hg, "data", y1);
119
120     addlistener (hg, "xdata", @update_data);
121     addlistener (hg, "ydata", @update_data);
122
123     if (i == 1)
124       h = patch (ax, [x1(1), x1, fliplr(x1)], [bv, y1, bv*ones(1, length(y1))],
125                  __next_line_color__ (), "parent", hg);
126     else
127       y1 = y0 + y1;
128       h = patch (ax, [x1(1), x1, fliplr(x1)], [y0(1), y1, fliplr(y0)],
129                  __next_line_color__ (), "parent", hg);
130     endif
131
132     y0 = y1;
133
134     addproperty ("basevalue", hg, "data", bv);
135     addlistener (hg, "basevalue", @move_baseline);
136
137     addproperty ("edgecolor", hg, "patchedgecolor", get (h, "edgecolor"));
138     addproperty ("linewidth", hg, "patchlinewidth", get (h, "linewidth"));
139     addproperty ("linestyle", hg, "patchlinestyle", get (h, "linestyle"));
140     addproperty ("facecolor", hg, "patchfacecolor", get (h, "facecolor"));
141
142     addlistener (hg, "edgecolor", @update_props);
143     addlistener (hg, "linewidth", @update_props);
144     addlistener (hg, "linestyle", @update_props);
145     addlistener (hg, "facecolor", @update_props);
146
147     addproperty ("areagroup", hg, "data");
148     set (retval, "areagroup", retval);
149
150     if (! isempty (args))
151       set (hg, args{:});
152     endif
153   endfor
154
155 endfunction
156
157 function update_props (h, d)
158   kids = get (h, "children");
159   set (kids, "edgecolor", get (h, "edgecolor"),
160        "linewidth", get (h, "linewidth"),
161        "linestyle", get (h, "linestyle"),
162        "facecolor", get (h, "facecolor"));
163 endfunction
164
165 function move_baseline (h, d)
166   persistent recursion = false;
167
168   ## Don't allow recursion
169   if (! recursion)
170     unwind_protect
171       recursion = true;
172       hlist = get (h, "areagroup");
173       b0 = get (h, "basevalue");
174
175       for hh = hlist(:)'
176         if (hh != h)
177           b1 = get (hh, "basevalue");
178           if (b1 != b0)
179             set (hh, "basevalue", b0);
180           endif
181         endif
182       endfor
183       update_data (h, d);
184     unwind_protect_cleanup
185       recursion = false;
186     end_unwind_protect
187   endif
188 endfunction
189
190 function update_data (h, d)
191   hlist = get (h, "areagroup");
192   bv = get (h, "basevalue");
193   for i = 1 : length (hlist)
194     hh = hlist(i);
195     x1 = get (hh, "xdata")(:);
196     y1 = get (hh, "ydata")(:);
197
198     set (get (hh, "children"), "xdata", [x1(1); x1; flipud(x1)]);
199     if (i == 1)
200       set (get (hh, "children"), "ydata", [bv; y1; bv*ones(length(y1), 1)]);
201     else
202       y1 = y0 + y1;
203       set (get (hh, "children"), "ydata", [y0(1); y1; flipud(y0)]);
204     endif
205
206     y0 = y1;
207   endfor
208 endfunction