view scripts/plot/draw/private/__stem__.m @ 17572:7bb76a22cde1

maint: Split scripts/plot directory into 4 pieces. scripts/gui : user-interface functions scripts/plot/appearance : functions controlling plot appearance scripts/plot/draw : plotting functions which produce graphs scripts/plot/util : low-level plotting functions and utilities. * scripts/gui/guidata.m, scripts/gui/guihandles.m, scripts/gui/module.mk, scripts/gui/private/__file_filter__.m, scripts/gui/private/__fltk_file_filter__.m, scripts/gui/private/__is_function__.m, scripts/gui/private/__uigetdir_fltk__.m, scripts/gui/private/__uigetfile_fltk__.m, scripts/gui/private/__uiobject_split_args__.m, scripts/gui/private/__uiputfile_fltk__.m, scripts/gui/uicontextmenu.m, scripts/gui/uicontrol.m, scripts/gui/uigetdir.m, scripts/gui/uigetfile.m, scripts/gui/uimenu.m, scripts/gui/uipanel.m, scripts/gui/uipushtool.m, scripts/gui/uiputfile.m, scripts/gui/uiresume.m, scripts/gui/uitoggletool.m, scripts/gui/uitoolbar.m, scripts/gui/uiwait.m, scripts/gui/waitbar.m, scripts/gui/waitforbuttonpress.m: Moved from scripts/plot to scripts/gui * scripts/plot/appearance/__clabel__.m, scripts/plot/appearance/__getlegenddata__.m, scripts/plot/appearance/axis.m, scripts/plot/appearance/box.m, scripts/plot/appearance/caxis.m, scripts/plot/appearance/clabel.m, scripts/plot/appearance/daspect.m, scripts/plot/appearance/diffuse.m, scripts/plot/appearance/grid.m, scripts/plot/appearance/gtext.m, scripts/plot/appearance/hidden.m, scripts/plot/appearance/legend.m, scripts/plot/appearance/orient.m, scripts/plot/appearance/pbaspect.m, scripts/plot/appearance/private/__axis_label__.m, scripts/plot/appearance/private/__axis_limits__.m, scripts/plot/appearance/shading.m, scripts/plot/appearance/specular.m, scripts/plot/appearance/text.m, scripts/plot/appearance/title.m, scripts/plot/appearance/view.m, scripts/plot/appearance/whitebg.m, scripts/plot/appearance/xlabel.m, scripts/plot/appearance/xlim.m, scripts/plot/appearance/ylabel.m, scripts/plot/appearance/ylim.m, scripts/plot/appearance/zlabel.m, scripts/plot/appearance/zlim.m: Moved from scripts/plot to subdir appearance. * scripts/plot/draw/area.m, scripts/plot/draw/bar.m, scripts/plot/draw/barh.m, scripts/plot/draw/colorbar.m, scripts/plot/draw/comet.m, scripts/plot/draw/comet3.m, scripts/plot/draw/compass.m, scripts/plot/draw/contour.m, scripts/plot/draw/contour3.m, scripts/plot/draw/contourc.m, scripts/plot/draw/contourf.m, scripts/plot/draw/cylinder.m, scripts/plot/draw/ellipsoid.m, scripts/plot/draw/errorbar.m, scripts/plot/draw/ezcontour.m, scripts/plot/draw/ezcontourf.m, scripts/plot/draw/ezmesh.m, scripts/plot/draw/ezmeshc.m, scripts/plot/draw/ezplot.m, scripts/plot/draw/ezplot3.m, scripts/plot/draw/ezpolar.m, scripts/plot/draw/ezsurf.m, scripts/plot/draw/ezsurfc.m, scripts/plot/draw/feather.m, scripts/plot/draw/fill.m, scripts/plot/draw/fplot.m, scripts/plot/draw/hist.m, scripts/plot/draw/isocolors.m, scripts/plot/draw/isonormals.m, scripts/plot/draw/isosurface.m, scripts/plot/draw/line.m, scripts/plot/draw/loglog.m, scripts/plot/draw/loglogerr.m, scripts/plot/draw/mesh.m, scripts/plot/draw/meshc.m, scripts/plot/draw/meshz.m, scripts/plot/draw/pareto.m, scripts/plot/draw/patch.m, scripts/plot/draw/pcolor.m, scripts/plot/draw/peaks.m, scripts/plot/draw/pie.m, scripts/plot/draw/pie3.m, scripts/plot/draw/plot.m, scripts/plot/draw/plot3.m, scripts/plot/draw/plotmatrix.m, scripts/plot/draw/plotyy.m, scripts/plot/draw/polar.m, scripts/plot/draw/private/__add_datasource__.m, scripts/plot/draw/private/__bar__.m, scripts/plot/draw/private/__contour__.m, scripts/plot/draw/private/__errcomm__.m, scripts/plot/draw/private/__errplot__.m, scripts/plot/draw/private/__ezplot__.m, scripts/plot/draw/private/__interp_cube__.m, scripts/plot/draw/private/__line__.m, scripts/plot/draw/private/__marching_cube__.m, scripts/plot/draw/private/__patch__.m, scripts/plot/draw/private/__pie__.m, scripts/plot/draw/private/__plt__.m, scripts/plot/draw/private/__quiver__.m, scripts/plot/draw/private/__scatter__.m, scripts/plot/draw/private/__stem__.m, scripts/plot/draw/quiver.m, scripts/plot/draw/quiver3.m, scripts/plot/draw/rectangle.m, scripts/plot/draw/ribbon.m, scripts/plot/draw/rose.m, scripts/plot/draw/scatter.m, scripts/plot/draw/scatter3.m, scripts/plot/draw/semilogx.m, scripts/plot/draw/semilogxerr.m, scripts/plot/draw/semilogy.m, scripts/plot/draw/semilogyerr.m, scripts/plot/draw/shrinkfaces.m, scripts/plot/draw/slice.m, scripts/plot/draw/sombrero.m, scripts/plot/draw/sphere.m, scripts/plot/draw/stairs.m, scripts/plot/draw/stem.m, scripts/plot/draw/stem3.m, scripts/plot/draw/stemleaf.m, scripts/plot/draw/surf.m, scripts/plot/draw/surface.m, scripts/plot/draw/surfc.m, scripts/plot/draw/surfl.m, scripts/plot/draw/surfnorm.m, scripts/plot/draw/tetramesh.m, scripts/plot/draw/trimesh.m, scripts/plot/draw/triplot.m, scripts/plot/draw/trisurf.m, scripts/plot/draw/waterfall.m: Moved from plot/ to subdir draw. * scripts/plot/util/__actual_axis_position__.m, scripts/plot/util/__default_plot_options__.m, scripts/plot/util/__gnuplot_drawnow__.m, scripts/plot/util/__next_line_color__.m, scripts/plot/util/__next_line_style__.m, scripts/plot/util/__plt_get_axis_arg__.m, scripts/plot/util/__pltopt__.m, scripts/plot/util/allchild.m, scripts/plot/util/ancestor.m, scripts/plot/util/axes.m, scripts/plot/util/cla.m, scripts/plot/util/clf.m, scripts/plot/util/close.m, scripts/plot/util/closereq.m, scripts/plot/util/colstyle.m, scripts/plot/util/copyobj.m, scripts/plot/util/figure.m, scripts/plot/util/findall.m, scripts/plot/util/findfigs.m, scripts/plot/util/findobj.m, scripts/plot/util/gca.m, scripts/plot/util/gcbf.m, scripts/plot/util/gcbo.m, scripts/plot/util/gcf.m, scripts/plot/util/gco.m, scripts/plot/util/ginput.m, scripts/plot/util/gnuplot_binary.in, scripts/plot/util/graphics_toolkit.m, scripts/plot/util/hdl2struct.m, scripts/plot/util/hggroup.m, scripts/plot/util/hold.m, scripts/plot/util/isaxes.m, scripts/plot/util/isfigure.m, scripts/plot/util/ishghandle.m, scripts/plot/util/ishold.m, scripts/plot/util/isprop.m, scripts/plot/util/linkprop.m, scripts/plot/util/meshgrid.m, scripts/plot/util/ndgrid.m, scripts/plot/util/newplot.m, scripts/plot/util/print.m, scripts/plot/util/printd.m, scripts/plot/util/private/__add_default_menu__.m, scripts/plot/util/private/__fltk_ginput__.m, scripts/plot/util/private/__fltk_print__.m, scripts/plot/util/private/__ghostscript__.m, scripts/plot/util/private/__gnuplot_get_var__.m, scripts/plot/util/private/__gnuplot_ginput__.m, scripts/plot/util/private/__gnuplot_has_feature__.m, scripts/plot/util/private/__gnuplot_has_terminal__.m, scripts/plot/util/private/__gnuplot_open_stream__.m, scripts/plot/util/private/__gnuplot_print__.m, scripts/plot/util/private/__gnuplot_version__.m, scripts/plot/util/private/__go_draw_axes__.m, scripts/plot/util/private/__go_draw_figure__.m, scripts/plot/util/private/__print_parse_opts__.m, scripts/plot/util/private/__tight_eps_bbox__.m, scripts/plot/util/refresh.m, scripts/plot/util/refreshdata.m, scripts/plot/util/saveas.m, scripts/plot/util/shg.m, scripts/plot/util/struct2hdl.m, scripts/plot/util/subplot.m: Moved from plot to subdir util. * etc/HACKING: Updated directory structure info. * scripts/Makefile.am, scripts/plot/appearance/module.mk, scripts/plot/draw/module.mk, scripts/plot/util/module.mk: Added new directories to build system.
author Rik <rik@octave.org>
date Fri, 04 Oct 2013 17:09:08 -0700
parents scripts/plot/private/__stem__.m@76614e624818
children d63878346099
line wrap: on
line source

## Copyright (C) 2006-2012 Michel D. Schmid
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or (at
## your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {@var{h} =} __stem__ (@var{have_z}, @var{varargin})
## Undocumented internal function.
## @end deftypefn

## Author: Michel D. Schmid <michaelschmid@users.sourceforge.net>
## Adapted-by: jwe

function h = __stem__ (have_z, varargin)

  if (have_z)
    caller = "stem3";
  else
    caller = "stem";
  endif

  [hax, varargin, nargin] = __plt_get_axis_arg__ (caller, varargin{:});

  [x, y, z, dofill, llc, ls, mmc, ms, varargin] = ...
                                           check_stem_arg (have_z, varargin{:});

  oldfig = [];
  if (! isempty (hax))
    oldfig = get (0, "currentfigure");
  endif
  unwind_protect
    hax = newplot (hax);
    hold_state = get (hax, "nextplot");
    set (hax, "nextplot", "add");

    h = [];
    nx = rows (x);
    for i = 1 : columns (x)
      if (have_z)
        xt = x(:)';
        xt = [xt; xt; NaN(1, nx)](:);
        yt = y(:)';
        yt = [yt; yt; NaN(1, nx)](:);
        zt = z(:)';
        zt = [zeros(1, nx); zt; NaN(1, nx)](:);
      else
        xt = x(:, i)';
        xt = [xt; xt; NaN(1, nx)](:);
        yt = y(:, i)';
        yt = [zeros(1, nx); yt; NaN(1, nx)](:);
      endif

      if (isempty (llc))
        lc = __next_line_color__ ();
      else
        lc = llc;
      endif

      if (isempty (mmc))
        mc = lc;
      else
        mc = mmc;
      endif

      if (dofill)
        fc = mc;
      else
        fc = "none";
      endif

      ## Must occur after __next_line_color__ in order to work correctly.
      hg = hggroup ();
      h = [h; hg];
      args = __add_datasource__ (caller, hg, {"x", "y", "z"}, varargin{:});

      if (have_z)
        __line__ (hax, xt, yt, zt, "color", lc, "linestyle", ls, "parent", hg);
        __line__ (hax, x, y, z, "color", mc, "linestyle", "none",
                       "marker", ms, "markerfacecolor", fc, "parent", hg);
        h_baseline = [];
      else
        __line__ (hax, xt, yt, "color", lc, "linestyle", ls, "parent", hg);
        __line__ (hax, x(:,i), y(:, i), "color", mc, "linestyle", "none",
                       "marker", ms, "markerfacecolor", fc, "parent", hg);
        x_axis_range = get (hax, "xlim");
        h_baseline = line (hax, x_axis_range, [0, 0], "color", [0, 0, 0]);
        set (h_baseline, "handlevisibility", "off", "xliminclude", "off");
        addlistener (hax, "xlim", @update_xlim);
        addproperty ("basevalue", h_baseline, "data", 0);
        addlistener (h_baseline, "basevalue", {@update_baseline, 0});
        addlistener (h_baseline, "ydata", {@update_baseline, 1});
        addlistener (h_baseline, "visible", {@update_baseline, 2});
      endif

      ## Setup the hggroup and listeners.
      addproperty ("showbaseline", hg, "radio", "{on}|off");
      addproperty ("baseline", hg, "data", h_baseline);
      addproperty ("basevalue", hg, "data", 0);

      if (! have_z)
        addlistener (hg, "showbaseline", @show_baseline);
        addlistener (hg, "basevalue", @move_baseline);
      endif

      addproperty ("color", hg, "linecolor", lc);
      addproperty ("linestyle", hg, "linelinestyle", ls);
      addproperty ("linewidth", hg, "linelinewidth", 0.5);
      addproperty ("marker", hg, "linemarker", ms);
      addproperty ("markeredgecolor", hg, "linemarkerfacecolor", mc);
      addproperty ("markerfacecolor", hg, "linemarkerfacecolor", fc);
      addproperty ("markersize", hg, "linemarkersize", 6);

      addlistener (hg, "color", @update_props);
      addlistener (hg, "linestyle", @update_props);
      addlistener (hg, "linewidth", @update_props);
      addlistener (hg, "marker", @update_props);
      addlistener (hg, "markeredgecolor", @update_props);
      addlistener (hg, "markerfacecolor", @update_props);
      addlistener (hg, "markersize", @update_props);

      addproperty ("xdata", hg, "data", x(:, i));
      addproperty ("ydata", hg, "data", y(:, i));
      if (have_z)
        addproperty ("zdata", hg, "data", z(:, i));
      else
        addproperty ("zdata", hg, "data", []);
      endif

      addlistener (hg, "xdata", @update_data);
      addlistener (hg, "ydata", @update_data);
      addlistener (hg, "zdata", @update_data);

      ## Matlab property, although Octave does not implement it.
      addproperty ("hittestarea", hg, "radio", "on|{off}", "off");

      if (! isempty (args))
        set (hg, args{:});
      endif
      if (i == 1 && ! isempty (h_baseline))
        set (h_baseline, "parent", get (hg, "parent"));
      endif
    endfor

    if (! strcmp (hold_state, "add") && have_z)
      set (hax, "view", [-37.5 30],
                "xgrid", "on", "ygrid", "on", "zgrid", "on");
    endif
    set (hax, "nextplot", hold_state);

  unwind_protect_cleanup
    if (! isempty (oldfig))
      set (0, "currentfigure", oldfig);
    endif
  end_unwind_protect

endfunction

function [x, y, z, dofill, lc, ls, mc, ms, args] = check_stem_arg (have_z, varargin)

  if (have_z)
    caller = "stem3";
  else
    caller = "stem";
  endif
  nargin = nargin - 1;  # account for have_z argument 

  num_numeric = find (cellfun ("isclass", varargin, "char"), 1) - 1;
  if (isempty (num_numeric))
    num_numeric = nargin;     
  endif

  if (num_numeric < 1 || num_numeric > 3)
    print_usage (caller);
  endif

  x = y = z = [];
  if (num_numeric == 1)
    if (have_z)
      z = varargin{1};
    else
      y = varargin{1};
    endif
  elseif (num_numeric == 2)
    if (have_z)
      error ("stem3: must define X, Y, and Z");
    else
      x = varargin{1};
      y = varargin{2};
    endif
  else  # nun_numeric == 3
    if (have_z)
      x = varargin{1};
      y = varargin{2};
      z = varargin{3};
    else
      error ("stem: can not define Z for 2-D stem plot");
    endif
  endif

  ## Validate numeric data
  if (have_z)
    if (isempty (x))
      [nr, nc] = size (z);
      if (nr >= nc)
        x = repmat ([1:nc], nr, 1);
        y = repmat ([1:nr]', 1, nc);
      else
        x = repmat ([1:nc], nr, 1);
        y = repmat ([1:nr]', 1, nc);
      endif
    endif
    if (! (ismatrix (x) && ismatrix (y) && ismatrix (z)))
      error ("stem3: X, Y, and Z must be numeric");
    endif
  else
    if (isempty (x))
      if (isvector (y))
        x = 1:length (y);
      elseif (ismatrix (y))
        x = 1:rows (y);
      endif
    endif
    if (! (ismatrix (x) && ismatrix (y)))
      error ("stem: X and Y must be numeric");
    endif
  endif

  ## Check sizes of x, y and z.
  if (have_z)
    if (! size_equal (x, y, z))
      error ("stem3: inconsistent sizes for X, Y, and Z");
    endif
    x = x(:);
    y = y(:);
    z = z(:);
  else
    if (isvector (x))
      x = x(:);
      if (isvector (y))
        if (length (x) != length (y))
          error ("stem: inconsistent sizes for X and Y");
        endif
        y = y(:);
      else
        if (length (x) == rows (y))
          x = repmat (x(:), 1, columns (y));
        else
          error ("stem: inconsistent sizes for X and Y");
        endif
      endif
    elseif (! size_equal (x, y))
      error ("stem: inconsistent sizes for X and Y");
    endif
  endif

  dofill = false;
  have_line_spec = false;
  ## set specifiers to default values.
  [lc, ls, mc, ms] = set_default_values ();

  args = {};
  ioff = num_numeric + 1;
  while (ioff <= nargin)
    arg = varargin{ioff++};
    if (ischar (arg) && any (strcmpi (arg, {"fill", "filled"})))
      dofill = true;
    elseif ((ischar (arg) || iscellstr (arg)) && ! have_line_spec)
      [linespec, valid] = __pltopt__ (caller, arg, false);
      if (valid)
        have_line_spec = true;
        [lc, ls, mc, ms] = stem_line_spec (linespec);
      else
        args{end+1} = arg;
        if (ioff <= nargin)
          args{end+1} = varargin{ioff++};
        else
          error ('%s: No value specified for property "%s"', caller, arg);
        endif
      endif
    else
      args{end+1} = arg;
      if (ioff <= nargin)
        args{end+1} = varargin{ioff++};
      else
        error ('%s: No value specified for property "%s"', caller, arg);
      endif
    endif
  endwhile

endfunction

function [lc, ls, mc, ms] = stem_line_spec (lspec)

  [lc, ls, mc, ms] = set_default_values ();

  if (! isempty (lspec.color))
    lc = mc = lspec.color;
  endif

  if (! isempty (lspec.linestyle) && ! strcmp (lspec.linestyle, "none"))
    ls = lspec.linestyle;
  endif

  if (! isempty (lspec.marker) && ! strcmp (lspec.marker, "none"))
    ms = lspec.marker;
  endif

endfunction

function [lc, ls, mc, ms] = set_default_values ()
  mc = [];
  lc = [];
  ls = "-";
  ms = "o";
endfunction

function update_xlim (h, ~)
  kids = get (h, "children");
  xlim = get (h, "xlim");

  for i = 1 : length (kids)
    obj = get (kids(i));
    if (strcmp (obj.type, "hggroup") && isfield (obj, "baseline"))
      if (any (get (obj.baseline, "xdata") != xlim))
        set (obj.baseline, "xdata", xlim);
      endif
    endif
  endfor
endfunction

function update_baseline (h, ~, src)
  visible = get (h, "visible");
  if (src == 0)
    basevalue = get (h, "basevalue");
  else
    basevalue = get (h, "ydata")(1);
  endif

  kids = get (get (h, "parent"), "children");
  for i = 1 : length (kids)
    obj = get (kids(i));
    if (strcmp (obj.type, "hggroup") && isfield (obj, "baseline")
        && obj.baseline == h)
      ## Avoid lots of unnecessary listener updates
      if (! strcmp (get (kids(i), "showbaseline"), visible))
        set (kids(i), "showbaseline", visible);
      endif
      if (get (kids(i), "basevalue") != basevalue)
        set (kids(i), "basevalue", basevalue);
      endif
    endif
  endfor
endfunction

function show_baseline (h, ~)
  set (get (h, "baseline"), "visible", get (h, "showbaseline"));
endfunction

function move_baseline (h, ~)
  b0 = get (h, "basevalue");
  bl = get (h, "baseline");

  set (bl, "ydata", [b0, b0]);

  kids = get (h, "children");
  yt = get (h, "ydata")(:)';
  ny = length (yt);
  yt = [b0 * ones(1, ny); yt; NaN(1, ny)](:);
  set (kids(2), "ydata", yt);
endfunction

function update_props (h, ~)
  kids = get (h, "children");
  set (kids(2), "color", get (h, "color"),
                "linestyle", get (h, "linestyle"),
                "linewidth", get (h, "linewidth"));
  set (kids(1), "color", get (h, "markeredgecolor"),
                "marker", get (h, "marker"),
                "markerfacecolor", get (h, "markerfacecolor"),
                "markersize", get (h, "markersize"));
endfunction

function update_data (h, ~)
  x = get (h, "xdata");
  y = get (h, "ydata");
  z = get (h, "zdata");

  if (!isempty (z) && size_equal (x, y, z))
    sz = min ([size(x); size(y); size(z)]);
    x = x(1:sz(1),1:sz(2));
    y = y(1:sz(1),1:sz(2));
    z = z(1:sz(1),1:sz(2));
  elseif (numel (x) != numel (y));
    sz = min ([size(x); size(y)]);
    x = x(1:sz(1),1:sz(2));
    y = y(1:sz(1),1:sz(2));
  endif
  bl = get (h, "basevalue");
  nx = numel (x);
  x = x(:)';
  xt = [x; x; NaN(1, nx)](:);
  if (! isempty (z))
    y = y(:)';
    yt = [y; y; NaN(1, nx)](:);
    z = z(:)';
    zt = [bl * ones(1, nx); z; NaN(1, nx)](:);
  else
    y = y(:)';
    yt = [bl * ones(1, nx); y; NaN(1, nx)](:);
    zt = [];
  endif

  kids = get (h, "children");
  set (kids(2), "xdata", xt, "ydata", yt, "zdata", zt);
  set (kids(1), "xdata", x, "ydata", y, "zdata", z);
endfunction