changeset 12096:61c4f397499c release-3-2-x

legend.m: fix legend order
author John W. Eaton <jwe@octave.org>
date Fri, 20 Nov 2009 10:43:14 +0100
parents 8d26637b1e19
children 7d396910b6c5
files scripts/ChangeLog scripts/plot/legend.m
diffstat 2 files changed, 71 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog
+++ b/scripts/ChangeLog
@@ -1,3 +1,8 @@
+2009-07-23  John W. Eaton  <jwe@octave.org>
+
+	* plot/legend.m: Process arguments in order, child objects in
+	reverse.  Remove special case for single label.  New demos.
+
 	2009-09-18  Jaroslav Hajek  <highegg@gmail.com>
 
 	Version 3.2.3 released.
--- a/scripts/plot/legend.m
+++ b/scripts/plot/legend.m
@@ -115,8 +115,10 @@
 	switch (str)
 	  case {"off", "hide"}
 	    set (ca, "key", "off");
+	    nargs--;
 	  case "show"
 	    set (ca, "key", "on");
+	    nargs--;
 	  case "toggle"
 	    val = get (ca, "key");
 	    if (strcmpi (val, "on"))
@@ -124,40 +126,21 @@
 	    else
 	      set (ca, "key", "on");
 	    endif
+	    nargs--;
 	  case "boxon"
 	    set (ca, "key", "on", "keybox", "on");
+	    nargs--;
 	  case "boxoff"
 	    set (ca, "keybox", "off");
+	    nargs--;
 	  case "left"
 	    set (ca, "keyreverse", "off")
+	    nargs--;
 	  case "right"
 	    set (ca, "keyreverse", "on")
+	    nargs--;
 	  otherwise
-	    typ = get (kids (k), "type");
-	    while (k <= nkids && ! strcmp (typ, "line") &&
-		   ! strcmp (typ, "hggroup"))
-	      k++;
-	      typ = get (kids (k), "type");
-	    endwhile
-	    if (k <= nkids)
-	      turn_on_legend = true;
-	      if (strcmp (typ, "hggroup"))
-		hgkids = get (kids(k), "children");
-		for j = 1 : length (hgkids)
-		  hgobj = get (hgkids (j));
-		  if (isfield (hgobj, "keylabel"))
-		    set (hgkids(j), "keylabel", arg);
-		    break;
-		  endif
-		endfor
-	      else
-		set (kids(k), "keylabel", arg);
-	      endif
-	    else
-	      warning ("legend: ignoring extra labels");
-	    endif
 	endswitch
-	nargs--;
       else
 	varargin = cellstr (arg);
 	nargs = numel (varargin);
@@ -172,11 +155,10 @@
 
   if (nargs > 0)
     have_data = false;
-    for i = 1:nkids
-      if (strcmp (get (kids(k), "type"), "line")
-	  || strcmp (get (kids(k), "type"), "surface")
-	  || strcmp (get (kids(k), "type"), "patch")
-	  || strcmp (get (kids(k), "type"), "hggroup"))
+    for k = 1:nkids
+      typ = get (kids(k), "type");
+      if (strcmp (typ, "line") || strcmp (typ, "surface")
+	  || strcmp (typ, "patch") || strcmp (typ, "hggroup"))
 	have_data = true;
 	break;
       endif
@@ -187,17 +169,17 @@
   endif
 
   warned = false;
-  for i = nargs:-1:1
+  k = nkids;
+  for i = 1:nargs
     arg = varargin{i};
     if (ischar (arg))
-      while (k <= nkids
-	     && ! (strcmp (get (kids(k), "type"), "line")
-		   || strcmp (get (kids(k), "type"), "surface")
-		   || strcmp (get (kids(k), "type"), "patch")
-		   || strcmp (get (kids(k), "type"), "hggroup")))
-	k++;
+      typ = get (kids(k), "type");
+      while (k > 1
+	     && ! (strcmp (typ, "line") || strcmp (typ, "surface")
+		   || strcmp (typ, "patch") || strcmp (typ, "hggroup")))
+	typ = get (kids(--k), "type");
       endwhile
-      if (k <= nkids)
+      if (k > 0)
 	if (strcmp (get (kids(k), "type"), "hggroup"))
 	  hgkids = get (kids(k), "children");
 	  for j = 1 : length (hgkids)
@@ -211,7 +193,9 @@
 	  set (kids(k), "keylabel", arg);
 	endif
 	turn_on_legend = true;
-	k++;
+	if (--k == 0)
+	  break;
+	endif
       elseif (! warned)
 	warned = true;
 	warning ("legend: ignoring extra labels");
@@ -235,12 +219,24 @@
 
 %!demo
 %! clf
+%! plot(1:10, 1:10, 1:10, fliplr(1:10));
+%! title("incline is blue and decline is green");
+%! legend("I'm blue", "I'm green", "location", "east")
+
+%!demo
+%! clf
 %! plot(1:10, 1:10);
 %! title("a very long label can sometimes cause problems");
 %! legend({"hello world"}, "location", "northeastoutside")
 
 %!demo
 %! clf
+%! plot(1:10, 1:10);
+%! title("a very long label can sometimes cause problems");
+%! legend("hello world", "location", "northeastoutside")
+
+%!demo
+%! clf
 %! labels = {};
 %! for i = 1:5
 %!     h = plot(1:100, i + rand(100,1)); hold on;
@@ -251,3 +247,35 @@
 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]");
 %! legend(labels, "location", "southoutside")
 %! legend("boxon")
+
+%!demo
+%! clf
+%! labels = {};
+%! for i = 1:5
+%!     h = plot(1:100, i + rand(100,1)); hold on;
+%!     set (h, "color", get (gca, "colororder")(i,:))
+%!     labels = {labels{:}, cstrcat("Signal ", num2str(i))};
+%! endfor; hold off;
+%! title("Signals with random offset and uniform noise")
+%! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]");
+%! legend(labels{:}, "location", "southoutside")
+%! legend("boxon")
+
+%!demo
+%! hold ("off");
+%! x = linspace (0, 10);
+%! plot (x, x);
+%! hold ("on");
+%! stem (x, x.^2, 'g')
+%! legend ("linear");
+%! hold ("off");
+
+%!demo
+%! x = linspace (0, 10);
+%! plot (x, x, x, x.^2);
+%! legend ("linear");
+
+%!demo
+%! x = linspace (0, 10);
+%! plot (x, x, x, x.^2);
+%! legend ("linear", "quadratic");