changelog shortlog tags changeset files revisions annotate raw

scripts/plot/area.m

changeset 10289: 4b124317dc38
parent:16f53d29049f
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (54 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2007, 2008, 2009 Michael Goffioul
2## Copyright (C) 2007, 2008, 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 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}
40## @end deftypefn
41
42function h = area (varargin)
43
44 [ax, varargin, nargin] = __plt_get_axis_arg__ ("area", varargin{:});
45
46 if (nargin > 0)
47 idx = 1;
48 x = y = [];
49 bv = 0;
50 args = {};
51 ## Check for (X) or (X,Y) arguments and possible base value.
52 if (nargin >= idx && ismatrix (varargin{idx}))
53 y = varargin{idx};
54 idx++;
55 if (nargin >= idx)
56 if (isscalar (varargin{idx}))
57 bv = varargin{idx};
58 idx++;
59 elseif (ismatrix (varargin{idx}))
60 x = y;
61 y = varargin{idx};
62 idx++;
63 if (nargin >= idx && isscalar (varargin{idx}))
64 bv = varargin{idx};
65 idx++;
66 endif
67 endif
68 endif
69 else
70 print_usage ();
71 endif
72 ## Check for additional args.
73 if (nargin >= idx)
74 args = {varargin{idx:end}};
75 endif
76 newplot ();
77 if (isvector (y))
78 y = y(:);
79 endif
80 if (isempty (x))
81 x = repmat ([1:size(y, 1)]', 1, size (y, 2));
82 elseif (isvector (x))
83 x = repmat (x(:), 1, size (y, 2));
84 endif
85
86 oldax = gca ();
87 unwind_protect
88 axes (ax);
89 tmp = __area__ (ax, x, y, bv, args{:});
90 unwind_protect_cleanup
91 axes (oldax);
92 end_unwind_protect
93
94 if (nargout > 0)
95 h = tmp;
96 endif
97 else
98 print_usage ();
99 endif
100
101endfunction
102
103function retval = __area__ (ax, x, y, bv, varargin)
104
105 y0 = bv * ones (1, rows (y));
106 y0 = zeros (1, rows (y));
107 retval = [];
108 for i = 1: size (y, 2);
109 hg = hggroup ();
110 retval = [retval; hg];
111 args = __add_datasource__ ("area", hg, {"x", "y"}, varargin{:});
112
113 x1 = x(:, 1).';
114 y1 = y (:, i).';
115 addproperty ("xdata", hg, "data", x1);
116 addproperty ("ydata", hg, "data", y1);
117
118 addlistener (hg, "xdata", @update_data);
119 addlistener (hg, "ydata", @update_data);
120
121 if (i == 1)
122 h = patch (ax, [x1(1), x1, fliplr(x1)], [bv, y1, bv*ones(1, length(y1))],
123 __next_line_color__ (), "parent", hg);
124 else
125 y1 = y0 + y1;
126 h = patch (ax, [x1(1), x1, fliplr(x1)], [y0(1), y1, fliplr(y0)],
127 __next_line_color__ (), "parent", hg);
128 endif
129
130 y0 = y1;
131
132 addproperty ("basevalue", hg, "data", bv);
133 addlistener (hg, "basevalue", @move_baseline);
134
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"));
139
140 addlistener (hg, "edgecolor", @update_props);
141 addlistener (hg, "linewidth", @update_props);
142 addlistener (hg, "linestyle", @update_props);
143 addlistener (hg, "facecolor", @update_props);
144
145 addproperty ("areagroup", hg, "data");
146 set (retval, "areagroup", retval);
147
148 if (! isempty (args))
149 set (hg, args{:});
150 endif
151 endfor
152
153endfunction
154
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"));
161endfunction
162
163function move_baseline (h, d)
164 persistent recursion = false;
165
166 ## Don't allow recursion
167 if (! recursion)
168 unwind_protect
169 recursion = true;
170 hlist = get (h, "areagroup");
171 b0 = get (h, "basevalue");
172
173 for hh = hlist(:)'
174 if (hh != h)
175 b1 = get (hh, "basevalue");
176 if (b1 != b0)
177 set (hh, "basevalue", b0);
178 endif
179 endif
180 endfor
181 update_data (h, d);
182 unwind_protect_cleanup
183 recursion = false;
184 end_unwind_protect
185 endif
186endfunction
187
188function update_data (h, d)
189 hlist = get (h, "areagroup");
190 bv = get (h, "basevalue");
191 for i = 1 : length (hlist)
192 hh = hlist(i);
193 x1 = get (hh, "xdata")(:);
194 y1 = get (hh, "ydata")(:);
195
196 set (get (hh, "children"), "xdata", [x1(1); x1; flipud(x1)]);
197 if (i == 1)
198 set (get (hh, "children"), "ydata", [bv; y1; bv*ones(length(y1), 1)]);
199 else
200 y1 = y0 + y1;
201 set (get (hh, "children"), "ydata", [y0(1); y1; flipud(y0)]);
202 endif
203
204 y0 = y1;
205 endfor
206endfunction