Mercurial > octave
changeset 28940:914816dd2f1a
fplot.m: Allow setting displayname in function call (bug #59274).
* fplot.m: Separate format string from property value pairs. Set automatically
generated displayname before other property value pairs (to allow overriding
it). Let legend strings be auto-generated from displayname property. Add
BISTs for manual displayname in format string or property value pairs.
author | Markus Mützel <markus.muetzel@gmx.de> |
---|---|
date | Thu, 15 Oct 2020 21:00:08 +0200 |
parents | 72874422e17d |
children | c06fb493b18e |
files | scripts/plot/draw/fplot.m |
diffstat | 1 files changed, 41 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/plot/draw/fplot.m Thu Oct 15 15:16:35 2020 +0200 +++ b/scripts/plot/draw/fplot.m Thu Oct 15 21:00:08 2020 +0200 @@ -128,6 +128,7 @@ n = 5; tol = 2e-3; fmt = {}; + prop_vals = {}; while (i <= numel (varargin)) arg = varargin{i}; if (ischar (arg)) @@ -138,7 +139,7 @@ if (i == numel (varargin)) error ("fplot: bad input in position %d", i); endif - fmt(end+(1:2)) = varargin([i, i+1]); + prop_vals(end+(1:2)) = varargin([i, i+1]); i++; # Skip PROPERTY. endif elseif (isnumeric (arg) && isscalar (arg) && arg > 0) @@ -225,18 +226,24 @@ if (isempty (hax)) hax = gca (); endif - plot (hax, x, y, fmt{:}); + hl = plot (hax, x, y, fmt{:}); + if (isempty (get (hl(1), "displayname"))) + # set displayname for legend if FMT didn't contain a name + if (isvector (y)) + set (hl, "displayname", nam); + else + for i = 1:columns (y) + nams{i} = sprintf ("%s(:,%i)", nam, i); + endfor + set (hl, {"displayname"}, nams(:)); + endif + endif + # properties past as input arguments override other properties + if (! isempty (prop_vals)) + set (hl, prop_vals{:}); + endif axis (hax, limits); - ## FIXME: If hold is on, then this erases the existing legend rather than - ## adding to it. - if (isvector (y)) - legend (hax, nam); - else - for i = 1:columns (y) - nams{i} = sprintf ("%s(:,%i)", nam, i); - endfor - legend (hax, nams{:}); - endif + legend (hax, "show"); endif endfunction @@ -281,6 +288,28 @@ %! assert (rows (x) == rows (y)); %! assert (y, repmat ([0], size (x))); +%!test <*59274> +%! ## Manual displayname overrides automatic legend entry +%! hf = figure ("visible", "off"); +%! unwind_protect +%! fplot (@sin, [0, 3], "displayname", "mysin"); +%! hl = legend (); +%! assert (get (hl, "string"), {"mysin"}); +%! unwind_protect_cleanup +%! close (hf); +%! end_unwind_protect + +%!test <*59274> +%! ## displayname in format string overrides automatic legend entry +%! hf = figure ("visible", "off"); +%! unwind_protect +%! fplot (@sin, [0, 3], "+;mysin;"); +%! hl = legend (); +%! assert (get (hl, "string"), {"mysin"}); +%! unwind_protect_cleanup +%! close (hf); +%! end_unwind_protect + ## Test input validation %!error <Invalid call> fplot () %!error <Invalid call> fplot (1,2,3,4,5,6)