1## Copyright (C) 2007, 2008, 2009 Michael Goffioul
2## Copyright (C) 2007, 2008, 2009 David Bateman
4## This file is part of Octave.
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.
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.
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/>.
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.
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.
36## Additional arguments to the @code{area} function are passed to the
37## @code{patch}. The optional return value @var{h} provides a handle to
38## area series object representing the patches of the areas.
39## @seealso{plot, patch}
42function h = area (varargin)
44 [ax, varargin, nargin] = __plt_get_axis_arg__ ("area", varargin{:});
51 ## Check for (X) or (X,Y) arguments and possible base value.
52 if (nargin >= idx && ismatrix (varargin{idx}))
56 if (isscalar (varargin{idx}))
59 elseif (ismatrix (varargin{idx}))
63 if (nargin >= idx && isscalar (varargin{idx}))
72 ## Check for additional args.
74 args = {varargin{idx:end}};
81 x = repmat ([1:size(y, 1)]', 1, size (y, 2));
83 x = repmat (x(:), 1, size (y, 2));
89 tmp = __area__ (ax, x, y, bv, args{:});
90 unwind_protect_cleanup
103function retval = __area__ (ax, x, y, bv, varargin)
105 y0 = bv * ones (1, rows (y));
106 y0 = zeros (1, rows (y));
108 for i = 1: size (y, 2);
110 retval = [retval; hg];
111 args = __add_datasource__ ("area", hg, {"x", "y"}, varargin{:});
115 addproperty ("xdata", hg, "data", x1);
116 addproperty ("ydata", hg, "data", y1);
118 addlistener (hg, "xdata", @update_data);
119 addlistener (hg, "ydata", @update_data);
122 h = patch (ax, [x1(1), x1, fliplr(x1)], [bv, y1, bv*ones(1, length(y1))],
123 __next_line_color__ (), "parent", hg);
126 h = patch (ax, [x1(1), x1, fliplr(x1)], [y0(1), y1, fliplr(y0)],
127 __next_line_color__ (), "parent", hg);
132 addproperty ("basevalue", hg, "data", bv);
133 addlistener (hg, "basevalue", @move_baseline);
135 addproperty ("edgecolor", hg, "patchedgecolor", get (h, "edgecolor"));
136 addproperty ("linewidth", hg, "patchlinewidth", get (h, "linewidth"));
137 addproperty ("linestyle", hg, "patchlinestyle", get (h, "linestyle"));
138 addproperty ("facecolor", hg, "patchfacecolor", get (h, "facecolor"));
140 addlistener (hg, "edgecolor", @update_props);
141 addlistener (hg, "linewidth", @update_props);
142 addlistener (hg, "linestyle", @update_props);
143 addlistener (hg, "facecolor", @update_props);
145 addproperty ("areagroup", hg, "data");
146 set (retval, "areagroup", retval);
148 if (! isempty (args))
155function update_props (h, d)
156 kids = get (h, "children");
157 set (kids, "edgecolor", get (h, "edgecolor"),
158 "linewidth", get (h, "linewidth"),
159 "linestyle", get (h, "linestyle"),
160 "facecolor", get (h, "facecolor"));
163function move_baseline (h, d)
164 persistent recursion = false;
166 ## Don't allow recursion
170 hlist = get (h, "areagroup");
171 b0 = get (h, "basevalue");
175 b1 = get (hh, "basevalue");
177 set (hh, "basevalue", b0);
182 unwind_protect_cleanup
188function update_data (h, d)
189 hlist = get (h, "areagroup");
190 bv = get (h, "basevalue");
191 for i = 1 : length (hlist)
193 x1 = get (hh, "xdata")(:);
194 y1 = get (hh, "ydata")(:);
196 set (get (hh, "children"), "xdata", [x1(1); x1; flipud(x1)]);
198 set (get (hh, "children"), "ydata", [bv; y1; bv*ones(length(y1), 1)]);
201 set (get (hh, "children"), "ydata", [y0(1); y1; flipud(y0)]);