1 ## Copyright (C) 2006-2012 Michel D. Schmid
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} {@var{h} =} __stem__ (@var{have_z}, @var{varargin})
21 ## Undocumented internal function.
24 ## Author: Michel D. Schmid <michaelschmid@users.sourceforge.net>
27 function h = __stem__ (have_z, varargin)
35 [ax, varargin, nargin] = __plt_get_axis_arg__ (caller, varargin{:});
37 [x, y, z, dofill, llc, ls, mmc, ms, varargin] = ...
38 check_stem_arg (have_z, varargin{:});
43 hold_state = get (ax, "nextplot");
48 for i = 1: columns (x)
51 xt = [xt; xt; NaN(1, nx)](:);
53 yt = [yt; yt; NaN(1, nx)](:);
55 zt = [zeros(1, nx); zt; NaN(1, nx)](:);
58 xt = [xt; xt; NaN(1, nx)](:);
60 yt = [zeros(1, nx); yt; NaN(1, nx)](:);
65 args = __add_datasource__ (caller, hg, {"x", "y", "z"}, varargin{:});
68 set (ax, "nextplot", "add");
72 lc = __next_line_color__ ();
90 h_stems = plot3 (xt, yt, zt, "color", lc, "linestyle", ls,
91 "parent", hg, x, y, z, "color", mc,
92 "marker", ms, "linestyle", "none",
93 "markerfacecolor", fc, "parent", hg);
97 h_stems = plot (xt, yt, "color", lc, "linestyle", ls,
98 "parent", hg, x(:,i), y(:, i), "color", mc, "marker",
99 ms, "linestyle", "none", "markerfacecolor",
102 x_axis_range = get (ax, "xlim");
103 h_baseline = line (x_axis_range, [0, 0], "color", [0, 0, 0]);
104 set (h_baseline, "handlevisibility", "off");
105 set (h_baseline, "xliminclude", "off");
106 addlistener (ax, "xlim", @update_xlim);
107 addlistener (h_baseline, "ydata", @update_baseline);
108 addlistener (h_baseline, "visible", @update_baseline);
111 ## Setup the hggroup and listeners.
112 addproperty ("showbaseline", hg, "radio", "{on}|off");
113 addproperty ("basevalue", hg, "data", 0);
114 addproperty ("baseline", hg, "data", h_baseline);
117 addlistener (hg, "showbaseline", @show_baseline);
118 addlistener (hg, "basevalue", @move_baseline);
121 addproperty ("color", hg, "linecolor", lc);
122 addproperty ("linewidth", hg, "linelinewidth", 0.5);
123 addproperty ("linestyle", hg, "linelinestyle", ls);
124 addproperty ("marker", hg, "linemarker", ms);
125 addproperty ("markerfacecolor", hg, "linemarkerfacecolor", fc);
126 addproperty ("markersize", hg, "linemarkersize", 6);
128 addlistener (hg, "color", @update_props);
129 addlistener (hg, "linewidth", @update_props);
130 addlistener (hg, "linestyle", @update_props);
131 addlistener (hg, "marker", @update_props);
132 addlistener (hg, "markerfacecolor", @update_props);
133 addlistener (hg, "markersize", @update_props);
135 addproperty ("xdata", hg, "data", x(:, i));
136 addproperty ("ydata", hg, "data", y(:, i));
138 addproperty ("zdata", hg, "data", z(:, i));
140 addproperty ("zdata", hg, "data", []);
143 addlistener (hg, "xdata", @update_data);
144 addlistener (hg, "ydata", @update_data);
145 addlistener (hg, "zdata", @update_data);
147 if (! isempty (args))
150 if (i == 1 && !isempty(h_baseline))
151 set (h_baseline, "parent", get (hg, "parent"));
155 unwind_protect_cleanup
156 set (ax, "nextplot", hold_state);
161 function [x, y, z, dofill, lc, ls, mc, ms, newargs] = check_stem_arg (have_z, varargin)
163 ## FIXME -- there seems to be a lot of duplicated code in this
164 ## function. It seems like it should be possible to simplify things
165 ## by combining some of the nearly identical code sections into
166 ## additional subfunctions.
174 ## Remove prop/val pairs from data to consider.
177 while (i < length (varargin))
178 if (ischar (varargin{i}) && !(strcmpi ("fill", varargin{i})
179 || strcmpi ("filled", varargin{i})))
180 newargs{end + 1} = varargin{i};
181 newargs{end + 1} = varargin{i + 1};
183 varargin(i:i+1) = [];
189 ## set specifiers to default values.
190 [lc, ls, mc, ms] = set_default_values ();
196 ## Check input arguments.
206 elseif (ismatrix (y))
209 error ("stem: Y must be a matrix");
210 endif # in each case, x & y will be defined
213 ## Several possibilities
215 ## 1. the real y data
218 if (ischar (varargin{2}))
219 ## Only 2. or 3. possible.
220 if (strcmpi ("fill", varargin{2}) || strcmpi ("filled", varargin{2}))
223 ## Parse the linespec.
224 [lc, ls, mc, ms] = stem_line_spec (caller, varargin{2});
234 elseif (ismatrix (y))
237 error ("stem: Y must be a matrix");
238 endif # in each case, x & y will be defined
242 error ("stem3: must define X, Y and Z");
244 ## Must be the real y data.
247 if (! (ismatrix (x) && ismatrix (y)))
248 error ("stem: X and Y must be matrices");
253 ## Again, several possibilities:
256 ## arg2 2. 'filled' or linespec
258 ## arg3 2. 'filled' or linespec
259 if (ischar (varargin{2}))
260 ## Only arg2 2. / arg3 1. & arg3 3. are possible.
261 if (strcmpi ("fill", varargin{2}) || strcmpi ("filled", varargin{2}))
263 fill_2 = 1; # Be sure, no second "fill" is in the arguments.
265 ## Must be a linespec.
266 [lc, ls, mc, ms] = stem_line_spec (caller, varargin{2});
277 elseif (ismatrix (y))
280 error ("stem: Y must be a matrix");
281 endif # in each case, x & y will be defined
288 if (! (ismatrix (x) && ismatrix (y) && ismatrix (z)))
289 error ("stem3: X, Y and Z must be matrices");
292 ## must be the real y data.
295 if (! (ismatrix (x) && ismatrix (y)))
296 error ("stem: X and Y must be matrices");
299 endif # if ischar(varargin{2})
301 ## varargin{3} must be char.
303 if ((strcmpi (varargin{3}, "fill") || strcmpi (varargin{3}, "filled"))
305 error ("stem: duplicate fill argument");
306 elseif (strcmpi ("fill", varargin{3}) && linespec_2)
310 elseif ((strcmpi (varargin{3}, "fill") || strcmpi (varargin{3}, "filled"))
315 elseif (! linespec_2)
317 [lc, ls, mc, ms] = stem_line_spec (caller, varargin{3});
326 if (! (ismatrix (x) && ismatrix (y) && ismatrix (z)))
327 error ("stem3: X, Y and Z must be matrices");
332 if (! (ismatrix (x) && ismatrix (y)))
333 error ("stem: X and Y must be matrices");
338 if (strcmpi (varargin{3}, "fill") || strcmpi (varargin{3}, "filled"))
340 fill_2 = 1; # Be sure, no second "fill" is in the arguments.
342 ## Must be a linespec.
343 [lc, ls, mc, ms] = stem_line_spec (caller, varargin{3});
349 if ((strcmpi (varargin{4}, "fill") || strcmpi (varargin{4}, "filled"))
351 error ("%s: duplicate fill argument", caller);
352 elseif ((strcmpi (varargin{4}, "fill") || strcmpi (varargin{4}, "filled"))
357 elseif (!strcmpi (varargin{4}, "fill") && !strcmpi (varargin{4}, "filled")
360 [lc, ls, mc, ms] = stem_line_spec (caller, varargin{4});
363 elseif (nargin == 6 && have_z)
367 if (! (ismatrix (x) && ismatrix (y) && ismatrix (z)))
368 error ("stem3: X, Y and Z must be matrices");
371 if (strcmpi (varargin{4}, "fill") || strcmpi (varargin{4}, "filled"))
373 fill_2 = 1; # be sure, no second "fill" is in the arguments
375 ## Must be a linespec.
376 [lc, ls, mc, ms] = stem_line_spec (caller, varargin{4});
380 ## check for "fill" ..
381 if ((strcmpi (varargin{5}, "fill") || strcmpi (varargin{5}, "filled"))
383 error ("stem3: duplicate fill argument");
384 elseif ((strcmpi (varargin{5}, "fill") || strcmpi (varargin{5}, "filled"))
389 elseif (!strcmpi (varargin{5}, "fill") && !strcmpi (varargin{5}, "filled")
392 [lc, ls, mc, ms] = stem_line_spec (caller, varargin{5});
396 error ("%s: incorrect number of arguments", caller);
399 ## Check sizes of x, y and z.
401 if (!size_equal (x, y, z))
402 error ("stem3: inconsistent size of x, y and z");
412 if (length (x) != length (y))
413 error ("stem: inconsistent size of x and y");
418 if (length (x) == rows (y))
419 x = repmat (x(:), 1, columns (y));
421 error ("stem: inconsistent size of x and y");
424 elseif (!size_equal (x, y))
425 error ("stem: inconsistent size of x and y");
431 function [lc, ls, mc, ms] = stem_line_spec (caller, str)
433 error ("%s: expecting argument to be \"fill\" or a string of specifiers",
436 [lc, ls, mc, ms] = set_default_values ();
437 ## Parse the line specifier string.
438 cur_props = __pltopt__ ("stem", str, false);
439 for i = 1:length(cur_props)
440 if (isfield (cur_props(i), "color") && ! isempty (cur_props(i).color)); # means line color
441 mc = lc = cur_props(i).color;
442 elseif (isfield (cur_props(i), "linestyle"))
443 ls = cur_props(i).linestyle;
445 ls = __next_line_style__ ();
447 elseif (isfield (cur_props(i), "marker") && ! strcmpi (cur_props(i).marker, "none"))
448 ms = cur_props(i).marker;
450 [dummy, ms] = __next_line_style__ ();
456 function [lc, ls, mc, ms] = set_default_values ()
457 ## set default values
464 function update_xlim (h, d)
465 kids = get (h, "children");
466 xlim = get (h, "xlim");
468 for i = 1 : length (kids)
469 obj = get (kids (i));
470 if (strcmp (obj.type, "hggroup") && isfield (obj, "baseline"))
471 if (any (get (obj.baseline, "xdata") != xlim))
472 set (obj.baseline, "xdata", xlim);
478 function update_baseline (h, d)
479 visible = get (h, "visible");
480 ydata = get (h, "ydata")(1);
482 kids = get (get (h, "parent"), "children");
483 for i = 1 : length (kids)
484 obj = get (kids (i));
485 if (strcmp (obj.type, "hggroup") && isfield (obj, "baseline")
486 && obj.baseline == h)
487 ## Only alter if changed to avoid recursion of the listener functions
488 if (! strcmpi (get (kids(i), "showbaseline"), visible))
489 set (kids (i), "showbaseline", visible);
491 if (! strcmpi (get (kids(i), "basevalue"), visible))
492 set (kids (i), "basevalue", ydata);
498 function show_baseline (h, d)
499 set (get (h, "baseline"), "visible", get (h, "showbaseline"));
502 function move_baseline (h, d)
503 b0 = get (h, "basevalue");
504 bl = get (h, "baseline");
506 if (get (bl, "ydata") != [b0, b0])
507 set (bl, "ydata", [b0, b0]);
510 kids = get (h, "children");
511 yt = get(h, "ydata")(:)';
513 yt = [b0 * ones(1, ny); yt; NaN(1, ny)](:);
514 set (kids(2), "ydata", yt);
517 function update_props (h, d)
518 kids = get (h, "children");
519 set (kids(2), "color", get (h, "color"),
520 "linewidth", get (h, "linewidth"),
521 "linestyle", get (h, "linestyle"));
522 set (kids(1), "color", get (h, "color"),
523 "marker", get (h, "marker"),
524 "markerfacecolor", get (h, "markerfacecolor"),
525 "markersize", get (h, "markersize"));
528 function update_data (h, d)
529 x = get (h, "xdata");
530 y = get (h, "ydata");
531 z = get (h, "zdata");
533 if (!isempty (z) && size_equal (x, y, z))
534 error ("stem3: inconsistent size of x, y and z");
535 elseif (numel(x) != numel (y))
536 error ("stem: inconsistent size of x and y");
538 bl = get (h, "basevalue");
541 xt = [x; x; NaN(1, nx)](:);
544 yt = [y; y; NaN(1, nx)](:);
546 zt = [bl * ones(1, nx); z; NaN(1, nx)](:);
549 yt = [bl * ones(1, nx); y; NaN(1, nx)](:);
553 kids = get (h, "children");
554 set (kids(2), "xdata", xt, "ydata", yt, "zdata", zt);
555 set (kids(1), "xdata", x, "ydata", y, "zdata", z);