changeset 18887:cff49c82f722

polar.m: Fix scaling of polar plot (bug #39495). * scripts/plot/draw/polar.m (resetaxis): New function * scripts/plot/draw/polar.m (__calc_rtick__): New function * scripts/plot/draw/polar.m (polar): Draw grid only, if not ishold. Set deletefcn for clearing the listeners if axis is cleared. If ishold true, update rtick only and let listeners do the grid update.
author Stefan Mahr <dac922@gmx.de>
date Wed, 02 Jul 2014 15:22:54 +0200
parents a8cacb2e98da
children 962db83e36c3
files scripts/plot/draw/polar.m
diffstat 1 files changed, 71 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/plot/draw/polar.m	Tue Jul 01 09:41:50 2014 -0700
+++ b/scripts/plot/draw/polar.m	Wed Jul 02 15:22:54 2014 +0200
@@ -95,47 +95,52 @@
       print_usage ();
     endif
 
-    ## FIXME: Should more gracefully handle "hold on" and not override props.
-    set (hax, "visible", "off", "plotboxaspectratio", [1, 1, 1],
-              "zlim", [-1 1]);
+    if (! ishold (hax))
+      hg = hggroup (hax, "tag", "polar_grid", "handlevisibility", "off");
+
+      set (hax, "visible", "off", "plotboxaspectratio", [1, 1, 1],
+                "zlim", [-1 1]);
+
+      if (! isprop (hax, "rtick"))
+        addproperty ("rtick", hax, "data");
+      endif
 
-    if (! isprop (hax, "rtick"))
-      addproperty ("rtick", hax, "data");
-    endif
+      set (hax, "rtick", __calc_rtick__ (hax, maxr));
+
+      ## add t(heta)tick
+      if (! isprop (hax, "ttick"))
+        addproperty ("ttick", hax, "data");
+      endif
+
+      ## theta(angular) ticks in degrees
+      set (hax, "ttick", 0:30:330);
+
+      __update_polar_grid__ (hax, [], hg);
 
-    ## calculate r(ho)tick from xtick
-    xtick = get (hax, "xtick");
-    rtick = xtick(find (xtick > 0, 1):find (xtick >= maxr, 1));
-    if (isempty (rtick))
-      rtick = [0.5 1];
-    endif
-    set (hax, "rtick", rtick);
+      set (hg, "deletefcn", {@resetaxis, hax});
 
-    ## add t(heta)tick
-    if (! isprop (hax, "ttick"))
-      addproperty ("ttick", hax, "data");
+      addlistener (hax, "rtick", {@__update_polar_grid__, hg});
+      addlistener (hax, "ttick", {@__update_polar_grid__, hg});
+      addlistener (hax, "color", {@__update_patch__, hg});
+      addlistener (hax, "fontangle", {@__update_text__, hg, "fontangle"});
+      addlistener (hax, "fontname", {@__update_text__, hg, "fontname"});
+      addlistener (hax, "fontsize", {@__update_text__, hg, "fontsize"});
+      addlistener (hax, "fontunits", {@__update_text__, hg, "fontunits"});
+      addlistener (hax, "fontweight", {@__update_text__, hg, "fontweight"});
+      addlistener (hax, "interpreter", {@__update_text__, hg, "interpreter"});
+      addlistener (hax, "layer", {@__update_layer__, hg});
+      addlistener (hax, "gridlinestyle",{@__update_lines__,hg,"gridlinestyle"});
+      addlistener (hax, "linewidth", {@__update_lines__, hg, "linewidth"});
+    else
+      hg = findall (hax, "tag", "polar_grid");
+      if (! isempty (hg))
+        oldrtick = max (get (hax, "rtick"));
+        if (maxr > oldrtick)
+          set (hax, "rtick", __calc_rtick__ (hax, maxr));
+        endif
+      endif
     endif
 
-    ## theta(angular) ticks in degrees
-    set (hax, "ttick", 0:30:330);
-
-    ## Create hggroup to hold text/line objects and attach listeners
-    hg = hggroup (hax, "tag", "polar_grid", "handlevisibility", "off");
-    __update_polar_grid__(hax, [], hg);
-
-    addlistener (hax, "rtick", {@__update_polar_grid__, hg});
-    addlistener (hax, "ttick", {@__update_polar_grid__, hg});
-    addlistener (hax, "color", {@__update_patch__, hg});
-    addlistener (hax, "fontangle", {@__update_text__, hg, "fontangle"});
-    addlistener (hax, "fontname", {@__update_text__, hg, "fontname"});
-    addlistener (hax, "fontsize", {@__update_text__, hg, "fontsize"});
-    addlistener (hax, "fontunits", {@__update_text__, hg, "fontunits"});
-    addlistener (hax, "fontweight", {@__update_text__, hg, "fontweight"});
-    addlistener (hax, "interpreter", {@__update_text__, hg, "interpreter"});
-    addlistener (hax, "layer", {@__update_layer__, hg});
-    addlistener (hax, "gridlinestyle", {@__update_lines__, hg,"gridlinestyle"});
-    addlistener (hax, "linewidth", {@__update_lines__, hg, "linewidth"});
-
   unwind_protect_cleanup
     if (! isempty (oldfig))
       set (0, "currentfigure", oldfig);
@@ -148,6 +153,19 @@
 
 endfunction
 
+function rtick = __calc_rtick__ (hax, maxr)
+  ## FIXME: workaround: calculate r(ho)tick from xtick
+  savexlim = get (hax, "xlim");
+  saveylim = get (hax, "ylim");
+  set (hax, "xlim", [-maxr maxr], "ylim", [-maxr maxr]);
+  xtick = get (hax, "xtick");
+  rtick = xtick(find (xtick > 0, 1):find (xtick >= maxr, 1));
+  if (isempty (rtick))
+    rtick = [0.5 1];
+  endif
+  set (hax, "xlim", savexlim, "ylim", saveylim);
+endfunction
+
 function retval = __plr1__ (h, theta, fmt)
 
   theta = theta(:);
@@ -348,6 +366,23 @@
 
 endfunction
 
+function resetaxis (~, ~, hax)
+  if (isaxes (hax))
+    dellistener (hax, "rtick");
+    dellistener (hax, "ttick");
+    dellistener (hax, "color");
+    dellistener (hax, "fontangle");
+    dellistener (hax, "fontname");
+    dellistener (hax, "fontsize");
+    dellistener (hax, "fontunits");
+    dellistener (hax, "fontweight");
+    dellistener (hax, "interpreter");
+    dellistener (hax, "layer");
+    dellistener (hax, "gridlinestyle");
+    dellistener (hax, "linewidth");
+  endif
+endfunction
+
 
 %!demo
 %! clf;