comparison scripts/plot/appearance/legend.m @ 18647:ce36233e4849

legend.m: Correctly handle axis handle as first argument (bug #42035). * legend.m: Save and restore figure and axis handle as is done for other plotting routines. Add %!test to check behavior. * legend.m (updateline): When string changes and legend must be re-built, call routine with axis handle as first argument.
author Rik <rik@octave.org>
date Sat, 12 Apr 2014 22:13:38 -0700
parents 8c0646dd9e5a
children adb948d7fae4
comparison
equal deleted inserted replaced
18646:2deed6538c72 18647:ce36233e4849
563 ypad = 2; 563 ypad = 2;
564 564
565 linelength = 15; 565 linelength = 15;
566 566
567 ## Create the axis first 567 ## Create the axis first
568 oldfig = get (0, "currentfigure");
569 if (oldfig != fig)
570 set (0, "currentfigure", fig);
571 else
572 oldfig = [];
573 endif
568 curaxes = get (fig, "currentaxes"); 574 curaxes = get (fig, "currentaxes");
569 unwind_protect 575 unwind_protect
570 ud = ancestor (hplots, "axes"); 576 ud = ancestor (hplots, "axes");
571 if (! isscalar (ud)) 577 if (! isscalar (ud))
572 ud = unique ([ud{:}]); 578 ud = unique ([ud{:}]);
1044 addlistener (hlegend, "string", @updatelegend); 1050 addlistener (hlegend, "string", @updatelegend);
1045 addlistener (hlegend, "textposition", @updatelegend); 1051 addlistener (hlegend, "textposition", @updatelegend);
1046 endif 1052 endif
1047 unwind_protect_cleanup 1053 unwind_protect_cleanup
1048 set (fig, "currentaxes", curaxes); 1054 set (fig, "currentaxes", curaxes);
1055 if (! isempty (oldfig))
1056 set (0, "currentfigure", oldfig);
1057 endif
1049 end_unwind_protect 1058 end_unwind_protect
1050 endif 1059 endif
1051 endif 1060 endif
1052 1061
1053 if (nargout > 0) 1062 if (nargout > 0)
1178 function updateline (h, ~, hlegend, linelength, update_name) 1187 function updateline (h, ~, hlegend, linelength, update_name)
1179 1188
1180 if (update_name) 1189 if (update_name)
1181 ## When string changes, have to rebuild legend completely 1190 ## When string changes, have to rebuild legend completely
1182 [hplots, text_strings] = __getlegenddata__ (hlegend); 1191 [hplots, text_strings] = __getlegenddata__ (hlegend);
1183 legend (hplots, text_strings); 1192 legend (get (hplots(1), "parent"), hplots, text_strings);
1184 else 1193 else
1185 kids = get (hlegend, "children"); 1194 kids = get (hlegend, "children");
1186 ll = lm = []; 1195 ll = lm = [];
1187 for i = 1 : numel (kids) 1196 for i = 1 : numel (kids)
1188 if (get (kids(i), "userdata") == h 1197 if (get (kids(i), "userdata") == h
1624 %! unwind_protect_cleanup 1633 %! unwind_protect_cleanup
1625 %! close (h); 1634 %! close (h);
1626 %! graphics_toolkit (toolkit); 1635 %! graphics_toolkit (toolkit);
1627 %! end_unwind_protect 1636 %! end_unwind_protect
1628 1637
1638 %!test
1639 %! ## bug #42035
1640 %! h = figure ("visible", "off");
1641 %! unwind_protect
1642 %! hax1 = subplot (1,2,1);
1643 %! plot (1:10);
1644 %! hax2 = subplot (1,2,2);
1645 %! plot (1:10);
1646 %! hleg1 = legend (hax1, "foo");
1647 %! assert (get (hleg1, "userdata").handle, hax1)
1648 %! assert (gca (), hax2);
1649 %! hleg2 = legend ("bar");
1650 %! assert (get (hleg2, "userdata").handle, gca ())
1651 %! unwind_protect_cleanup
1652 %! close (h);
1653 %! end_unwind_protect
1654