1 ## Copyright (C) 1996-2012 John W. Eaton
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} __bar__ (@var{vertical}, @var{func}, @dots{})
21 ## Undocumented internal function.
26 function varargout = __bar__ (vertical, func, varargin)
28 [h, varargin] = __plt_get_axis_arg__ ((nargout > 1), func, varargin{:});
30 ## Slightly smaller than 0.8 to avoid clipping issue in gnuplot 4.0
31 width = 0.8 - 10 * eps;
39 if (nargin > 3 && isnumeric (varargin{2}))
48 if (size (x, 1) != size (y, 1))
57 error ("%s: x must be a vector", func);
71 have_line_spec = false;
72 while (idx <= nargin - 2)
73 if (ischar (varargin{idx}) && strcmpi (varargin{idx}, "grouped"))
76 elseif (ischar (varargin{idx}) && strcmpi (varargin{idx}, "stacked"))
80 if ((ischar (varargin{idx}) || iscell (varargin{idx}))
82 [linespec, valid] = __pltopt__ (func, varargin{idx}, false);
84 have_line_spec = true;
85 newargs = [{"facecolor", linespec.color}, newargs]
90 if (isscalar(varargin{idx}))
91 width = varargin{idx++};
92 elseif (idx == nargin - 2)
93 newargs = [newargs,varargin(idx++)];
94 elseif (ischar (varargin{idx})
95 && strcmpi (varargin{idx}, "basevalue")
96 && isscalar (varargin{idx+1}))
100 newargs = [newargs,varargin(idx:idx+1)];
110 error ("%s: length of x and y must be equal", func);
112 if (any (x(2:end) < x(1:end-1)))
113 error ("%s: x vector values must be in ascending order", func);
118 cutoff = min (diff (double(x))) / 2;
123 delta_p = delta_m = repmat (cutoff * width / ycols, size (x));
125 delta_p = delta_m = repmat (cutoff * width, size (x));
127 x1 = (x - delta_m)(:)';
128 x2 = (x + delta_p)(:)';
129 xb = repmat ([x1; x1; x2; x2](:), 1, ycols);
132 offset = ((delta_p + delta_m) * [-(ycols - 1) / 2 : (ycols - 1) / 2]);
133 xb(1:4:4*ylen,:) += offset;
134 xb(2:4:4*ylen,:) += offset;
135 xb(3:4:4*ylen,:) += offset;
136 xb(4:4:4*ylen,:) += offset;
137 y0 = zeros (size (y)) + bv;
141 y0 = [zeros(ylen,1)+bv, y1(:,1:end-1)];
144 yb = zeros (4*ylen, ycols);
145 yb(1:4:4*ylen,:) = y0;
146 yb(2:4:4*ylen,:) = y1;
147 yb(3:4:4*ylen,:) = y1;
148 yb(4:4:4*ylen,:) = y0;
150 xb = reshape (xb, [4, numel(xb) / 4 / ycols, ycols]);
151 yb = reshape (yb, [4, numel(yb) / 4 / ycols, ycols]);
159 tmp = bars (h, vertical, x, y, xb, yb, width, group,
160 have_line_spec, bv, newargs{:});
164 unwind_protect_cleanup
179 function tmp = bars (ax, vertical, x, y, xb, yb, width, group, have_color_spec, base_value, varargin)
182 clim = get (ax, "clim");
188 args = __add_datasource__ ("bar", hg, {"x", "y"}, varargin{:});
191 if (! have_color_spec)
195 lev = (i - 1) * (clim(2) - clim(1)) / (ycols - 1) - clim(1);
197 h = patch(xb(:,:,i), yb(:,:,i), "FaceColor", "flat",
198 "cdata", lev, "parent", hg);
200 h = patch(xb(:,:,i), yb(:,:,i), "parent", hg);
203 if (! have_color_spec)
207 lev = (i - 1) * (clim(2) - clim(1)) / (ycols - 1) - clim(1);
209 h = patch(yb(:,:,i), xb(:,:,i), "FaceColor", "flat",
210 "cdata", lev, "parent", hg);
212 h = patch(yb(:,:,i), xb(:,:,i), "parent", hg);
217 x_axis_range = get (ax, "xlim");
218 h_baseline = line (x_axis_range, [0, 0], "color", [0, 0, 0]);
219 set (h_baseline, "handlevisibility", "off");
220 set (h_baseline, "xliminclude", "off");
221 addlistener (ax, "xlim", @update_xlim);
222 addlistener (h_baseline, "ydata", @update_baseline);
223 addlistener (h_baseline, "visible", @update_baseline);
226 ## Setup the hggroup and listeners
227 addproperty ("showbaseline", hg, "radio", "{on}|off");
228 addproperty ("basevalue", hg, "data", base_value);
229 addproperty ("baseline", hg, "data", h_baseline);
231 addlistener (hg, "showbaseline", @show_baseline);
232 addlistener (hg, "basevalue", @move_baseline);
234 addproperty ("barwidth", hg, "data", width);
236 addproperty ("barlayout", hg, "radio", "stacked|{grouped}", "grouped");
238 addproperty ("barlayout", hg, "radio", "{stacked}|grouped", "stacked");
241 addproperty ("horizontal", hg, "radio", "on|{off}", "off");
243 addproperty ("horizontal", hg, "radio", "{on}|off", "on");
246 addlistener (hg, "barwidth", @update_group);
247 addlistener (hg, "barlayout", @update_group);
248 addlistener (hg, "horizontal", @update_group);
250 addproperty ("edgecolor", hg, "patchedgecolor", get (h, "edgecolor"));
251 addproperty ("linewidth", hg, "patchlinewidth", get (h, "linewidth"));
252 addproperty ("linestyle", hg, "patchlinestyle", get (h, "linestyle"));
253 addproperty ("facecolor", hg, "patchfacecolor", get (h, "facecolor"));
255 addlistener (hg, "edgecolor", @update_props);
256 addlistener (hg, "linewidth", @update_props);
257 addlistener (hg, "linestyle", @update_props);
258 addlistener (hg, "facecolor", @update_props);
261 addproperty ("xdata", hg, "data", x);
263 addproperty ("xdata", hg, "data", x(:, i));
265 addproperty ("ydata", hg, "data", y(:, i));
267 addlistener (hg, "xdata", @update_data);
268 addlistener (hg, "ydata", @update_data);
270 addproperty ("bargroup", hg, "data");
271 set (tmp, "bargroup", tmp);
272 if (! isempty (args))
276 set (h_baseline, "parent", get (hg, "parent"));
280 update_xlim (ax, []);
283 function update_xlim (h, d)
284 kids = get (h, "children");
285 xlim = get (h, "xlim");
287 for i = 1 : length (kids)
288 obj = get (kids (i));
289 if (strcmp (obj.type, "hggroup") && isfield (obj, "baseline"))
290 if (any (get (obj.baseline, "xdata") != xlim))
291 set (obj.baseline, "xdata", xlim);
297 function update_baseline (h, d)
298 visible = get (h, "visible");
299 ydata = get (h, "ydata")(1);
301 kids = get (get (h, "parent"), "children");
302 for i = 1 : length (kids)
303 obj = get (kids (i));
304 if (strcmp (obj.type, "hggroup") && isfield (obj, "baseline")
305 && obj.baseline == h)
306 ## Only alter if changed to avoid recursion of the listener functions
307 if (! strcmpi (get (kids(i), "showbaseline"), visible))
308 set (kids (i), "showbaseline", visible);
310 if (! strcmpi (get (kids(i), "basevalue"), visible))
311 set (kids (i), "basevalue", ydata);
317 function show_baseline (h, d)
318 persistent recursion = false;
320 ## Don't allow recursion
324 hlist = get (h, "bargroup");
325 showbaseline = get (h, "showbaseline");
328 set (hh, "showbaseline", showbaseline);
331 set (get (h, "baseline"), "visible", showbaseline);
332 unwind_protect_cleanup
338 function move_baseline (h, d)
339 b0 = get (h, "basevalue");
340 bl = get (h, "baseline");
342 if (get (bl, "ydata") != [b0, b0])
343 set (bl, "ydata", [b0, b0]);
346 if (strcmpi (get (h, "barlayout"), "grouped"))
351 function update_props (h, d)
352 kids = get (h, "children");
353 set (kids, "edgecolor", get (h, "edgecolor"),
354 "linewidth", get (h, "linewidth"),
355 "linestyle", get (h, "linestyle"),
356 "facecolor", get (h, "facecolor"));
359 function update_data (h, d)
360 persistent recursion = false;
362 ## Don't allow recursion
366 hlist = get (h, "bargroup");
367 x = get (h, "xdata");
373 ytmp = get (hh, "ydata");
377 [xb, yb] = bar (x, y, get (h, "barwidth"), get (h, "barlayout"),
378 "basevalue", get (h, "basevalue"));
380 vert = strcmpi (get (h, "horizontal"), "off");
383 hp = get (hlist(i), "children");
385 set (hp, "xdata", xb(:,:,i), "ydata", yb(:,:,i));
387 set (hp, "xdata", yb(:,:,i), "ydata", xb(:,:,i));
390 unwind_protect_cleanup
396 function update_group (h, d)
397 persistent recursion = false;
399 ## Don't allow recursion
403 hlist = get (h, "bargroup");
404 barwidth = get(h, "barwidth");
405 barlayout = get (h, "barlayout");
406 horizontal = get (h, "horizontal");
408 ## To prevent recursion, only change if modified
411 if (get (hh, "barwidth") != barwidth)
412 set (hh, "barwidth", barwidth);
414 if (! strcmpi (get (hh, "barlayout"), barlayout))
415 set (hh, "barlayout", barlayout);
417 if (! strcmpi (get (hh, "horizontal"), horizontal))
418 set (hh, "horizontal", horizontal);
423 unwind_protect_cleanup