# HG changeset patch
# User Stefan Mahr <dac922@gmx.de>
# Date 1404307374 -7200
#      Wed Jul 02 15:22:54 2014 +0200
# Node ID e674875e94207a27e1a034e45f53cfe908ec3af4
# Parent  cc631b24468150885b1213e393d65aeb0d5f0c51
fix scaling of polar plot (bug #39495)

* scripts/plot/draw/polar.m (resetaxis): New function
  (__calc_rtick__): New function
  (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.

diff -r cc631b244681 -r e674875e9420 scripts/plot/draw/polar.m
--- a/scripts/plot/draw/polar.m	Sun Mar 16 13:25:57 2014 +0100
+++ b/scripts/plot/draw/polar.m	Wed Jul 02 15:22:54 2014 +0200
@@ -96,46 +96,52 @@
     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");
 
-    if (! isprop (hax, "rtick"))
-      addproperty ("rtick", hax, "data");
+      set (hax, "visible", "off", "plotboxaspectratio", [1, 1, 1],
+                "zlim", [-1 1]);
+
+      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);
+
+      set (hg, "deletefcn", {@resetaxis, hax});
+
+      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
 
-    ## 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);
-
-    ## add t(heta)tick
-    if (! isprop (hax, "ttick"))
-      addproperty ("ttick", hax, "data");
-    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 +154,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 +367,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;
