comparison scripts/plot/legend.m @ 11587:c792872f8942

all script files: untabify and strip trailing whitespace
author John W. Eaton <jwe@octave.org>
date Thu, 20 Jan 2011 17:35:29 -0500
parents 3c6e8aaa9555
children eda5eabd5d0b
comparison
equal deleted inserted replaced
11586:12df7854fa7c 11587:c792872f8942
26 ## @deftypefnx {Function File} {} legend (@var{hobjs}, @dots{}) 26 ## @deftypefnx {Function File} {} legend (@var{hobjs}, @dots{})
27 ## @deftypefnx {Function File} {} legend (@var{hax}, @var{hobjs}, @dots{}) 27 ## @deftypefnx {Function File} {} legend (@var{hax}, @var{hobjs}, @dots{})
28 ## @deftypefnx {Function File} {} legend ("@var{option}") 28 ## @deftypefnx {Function File} {} legend ("@var{option}")
29 ## 29 ##
30 ## Display a legend for the axes with handle @var{hax}, or the current axes, 30 ## Display a legend for the axes with handle @var{hax}, or the current axes,
31 ## using the specified strings as labels. Legend entries may be specified 31 ## using the specified strings as labels. Legend entries may be specified
32 ## as individual character string arguments, a character array, or a cell 32 ## as individual character string arguments, a character array, or a cell
33 ## array of character strings. If the handles, @var{hobjs}, are not specified 33 ## array of character strings. If the handles, @var{hobjs}, are not specified
34 ## then the legend's strings will be associated with the axes' descendants. 34 ## then the legend's strings will be associated with the axes' descendants.
35 ## Legend works on line graphs, bar graphs, etc. 35 ## Legend works on line graphs, bar graphs, etc.
36 ## A plot must exist before legend is called. 36 ## A plot must exist before legend is called.
62 ## right bottom 62 ## right bottom
63 ## 63 ##
64 ## @item @tab southwest @tab 64 ## @item @tab southwest @tab
65 ## left bottom 65 ## left bottom
66 ## 66 ##
67 ## @item 67 ## @item
68 ## 68 ##
69 ## @item @tab outside @tab 69 ## @item @tab outside @tab
70 ## can be appended to any location string 70 ## can be appended to any location string
71 ## @end multitable 71 ## @end multitable
72 ## 72 ##
116 116
117 if (strcmp (get (ca, "tag"), "plotyy")) 117 if (strcmp (get (ca, "tag"), "plotyy"))
118 plty = get(ca (strcmp (get (ca, "tag"), "plotyy")), "userdata"); 118 plty = get(ca (strcmp (get (ca, "tag"), "plotyy")), "userdata");
119 if (isscalar (plty)) 119 if (isscalar (plty))
120 ca = [ca, plty]; 120 ca = [ca, plty];
121 else 121 else
122 ca = [ca, plty{:}]; 122 ca = [ca, plty{:}];
123 endif 123 endif
124 endif 124 endif
125 125
126 if (nargin > 0 && all (ishandle (varargin{1}))) 126 if (nargin > 0 && all (ishandle (varargin{1})))
154 else 154 else
155 error ("legend: invalid position specified"); 155 error ("legend: invalid position specified");
156 endif 156 endif
157 endif 157 endif
158 endif 158 endif
159 159
160 while (nargs > 1) 160 while (nargs > 1)
161 pos = varargin{nargs-1}; 161 pos = varargin{nargs-1};
162 str = varargin{nargs}; 162 str = varargin{nargs};
163 if (strcmpi (pos, "location") && ischar (str)) 163 if (strcmpi (pos, "location") && ischar (str))
164 position = lower (str); 164 position = lower (str);
169 else 169 else
170 break; 170 break;
171 endif 171 endif
172 endwhile 172 endwhile
173 173
174 ## Validate the orientation 174 ## Validate the orientation
175 switch (orientation) 175 switch (orientation)
176 case {"vertical", "horizontal","default"} 176 case {"vertical", "horizontal","default"}
177 otherwise 177 otherwise
178 error ("legend: unrecognized legend orientation"); 178 error ("legend: unrecognized legend orientation");
179 endswitch 179 endswitch
199 endswitch 199 endswitch
200 200
201 hlegend = []; 201 hlegend = [];
202 fkids = get (fig, "children"); 202 fkids = get (fig, "children");
203 for i = 1 : numel(fkids) 203 for i = 1 : numel(fkids)
204 if (ishandle (fkids (i)) && strcmp (get (fkids (i), "type"), "axes") 204 if (ishandle (fkids (i)) && strcmp (get (fkids (i), "type"), "axes")
205 && (strcmp (get (fkids (i), "tag"), "legend"))) 205 && (strcmp (get (fkids (i), "tag"), "legend")))
206 udata = get (fkids (i), "userdata"); 206 udata = get (fkids (i), "userdata");
207 if (! isempty (intersect (udata.handle, ca))) 207 if (! isempty (intersect (udata.handle, ca)))
208 hlegend = fkids (i); 208 hlegend = fkids (i);
209 break; 209 break;
358 if (k > 0) 358 if (k > 0)
359 if (strcmp (get (kids(k), "type"), "hggroup")) 359 if (strcmp (get (kids(k), "type"), "hggroup"))
360 hgkids = get (kids(k), "children"); 360 hgkids = get (kids(k), "children");
361 for j = 1 : length (hgkids) 361 for j = 1 : length (hgkids)
362 hgobj = get (hgkids (j)); 362 hgobj = get (hgkids (j));
363 if (isfield (hgobj, "displayname") 363 if (isfield (hgobj, "displayname")
364 && ! isempty (hgobj.displayname)) 364 && ! isempty (hgobj.displayname))
365 hplots = [hplots, hgkids(j)]; 365 hplots = [hplots, hgkids(j)];
366 text_strings = {text_strings{:}, hbobj.displayname}; 366 text_strings = {text_strings{:}, hbobj.displayname};
367 break; 367 break;
368 endif 368 endif
420 if (strcmp (orientation, "default")) 420 if (strcmp (orientation, "default"))
421 orientation = "vertical"; 421 orientation = "vertical";
422 endif 422 endif
423 box = "off"; 423 box = "off";
424 endif 424 endif
425 425
426 ## Get axis size and fontsize in points. 426 ## Get axis size and fontsize in points.
427 ## Rely on listener to handle coversion. 427 ## Rely on listener to handle coversion.
428 units = get (ca(1), "units"); 428 units = get (ca(1), "units");
429 fontunits = get (ca(1), "fontunits"); 429 fontunits = get (ca(1), "fontunits");
430 unwind_protect 430 unwind_protect
431 set (ca(1), "units", "points"); 431 set (ca(1), "units", "points");
456 if (isempty (hlegend)) 456 if (isempty (hlegend))
457 addprops = true; 457 addprops = true;
458 hlegend = axes ("tag", "legend", "userdata", struct ("handle", ud), 458 hlegend = axes ("tag", "legend", "userdata", struct ("handle", ud),
459 "box", box, "outerposition", [0, 0, 0, 0], 459 "box", box, "outerposition", [0, 0, 0, 0],
460 "xtick", [], "ytick", [], "xticklabel", "", 460 "xtick", [], "ytick", [], "xticklabel", "",
461 "yticklabel", "", "zticklabel", "", 461 "yticklabel", "", "zticklabel", "",
462 "xlim", [0, 1], "ylim", [0, 1], "visible", "off", 462 "xlim", [0, 1], "ylim", [0, 1], "visible", "off",
463 "activepositionproperty", "position"); 463 "activepositionproperty", "position");
464 else 464 else
465 addprops = false; 465 addprops = false;
466 axes (hlegend); 466 axes (hlegend);
472 texthandle = []; 472 texthandle = [];
473 maxwidth = 0; 473 maxwidth = 0;
474 maxheight = 0; 474 maxheight = 0;
475 for k = 1 : nentries 475 for k = 1 : nentries
476 if (strcmp (textpos, "right")) 476 if (strcmp (textpos, "right"))
477 texthandle = [texthandle, text(0, 0, text_strings {k}, 477 texthandle = [texthandle, text(0, 0, text_strings {k},
478 "horizontalalignment", "left", 478 "horizontalalignment", "left",
479 "userdata", hplots(k))]; 479 "userdata", hplots(k))];
480 else 480 else
481 texthandle = [texthandle, text(0, 0, text_strings {k}, 481 texthandle = [texthandle, text(0, 0, text_strings {k},
482 "horizontalalignment", "right", 482 "horizontalalignment", "right",
610 lpos = [ca_pos(1) + ypad, ... 610 lpos = [ca_pos(1) + ypad, ...
611 ca_pos(2) + ca_pos(4) - lpos(4) - ypad, lpos(3), lpos(4)]; 611 ca_pos(2) + ca_pos(4) - lpos(4) - ypad, lpos(3), lpos(4)];
612 endif 612 endif
613 case "southeast" 613 case "southeast"
614 if (outside) 614 if (outside)
615 lpos = [ca_outpos(1) + ca_outpos(3) - lpos(3), ca_outpos(2), 615 lpos = [ca_outpos(1) + ca_outpos(3) - lpos(3), ca_outpos(2),
616 lpos(3), lpos(4)]; 616 lpos(3), lpos(4)];
617 new_pos = [ca_pos(1), ca_pos(2) + lpos(4), ... 617 new_pos = [ca_pos(1), ca_pos(2) + lpos(4), ...
618 ca_pos(3) - lpos(3), ca_pos(4) - lpos(4)]; 618 ca_pos(3) - lpos(3), ca_pos(4) - lpos(4)];
619 new_outpos = [ca_outpos(1), ca_outpos(2) + lpos(4), ... 619 new_outpos = [ca_outpos(1), ca_outpos(2) + lpos(4), ...
620 ca_outpos(3) - lpos(3), ca_outpos(4) - lpos(4)]; 620 ca_outpos(3) - lpos(3), ca_outpos(4) - lpos(4)];
651 case "line" 651 case "line"
652 color = get (hplots(k), "color"); 652 color = get (hplots(k), "color");
653 style = get (hplots(k), "linestyle"); 653 style = get (hplots(k), "linestyle");
654 if (! strcmp (style, "none")) 654 if (! strcmp (style, "none"))
655 l1 = line ("xdata", ([xoffset, xoffset + linelength] + xk * xstep) / lpos(3), 655 l1 = line ("xdata", ([xoffset, xoffset + linelength] + xk * xstep) / lpos(3),
656 "ydata", [1, 1] .* (lpos(4) - yoffset - yk * ystep) / lpos(4), 656 "ydata", [1, 1] .* (lpos(4) - yoffset - yk * ystep) / lpos(4),
657 "color", color, "linestyle", style, "marker", "none", 657 "color", color, "linestyle", style, "marker", "none",
658 "userdata", hplots (k)); 658 "userdata", hplots (k));
659 hobjects = [hobjects, l1]; 659 hobjects = [hobjects, l1];
660 endif 660 endif
661 marker = get (hplots(k), "marker"); 661 marker = get (hplots(k), "marker");
662 if (! strcmp (marker, "none")) 662 if (! strcmp (marker, "none"))
663 l1 = line ("xdata", (xoffset + 0.5 * linelength + xk * xstep) / lpos(3), 663 l1 = line ("xdata", (xoffset + 0.5 * linelength + xk * xstep) / lpos(3),
664 "ydata", (lpos(4) - yoffset - yk * ystep) / lpos(4), 664 "ydata", (lpos(4) - yoffset - yk * ystep) / lpos(4),
665 "color", color, "linestyle", "none", "marker", marker, 665 "color", color, "linestyle", "none", "marker", marker,
666 "markeredgecolor", get (hplots (k), "markeredgecolor"), 666 "markeredgecolor", get (hplots (k), "markeredgecolor"),
667 "markerfacecolor", get (hplots (k), "markerfacecolor"), 667 "markerfacecolor", get (hplots (k), "markerfacecolor"),
668 "markersize", get (hplots (k), "markersize"), 668 "markersize", get (hplots (k), "markersize"),
669 "userdata", hplots (k)); 669 "userdata", hplots (k));
697 endif 697 endif
698 endfor 698 endfor
699 699
700 ## Add an invisible text object to original axis 700 ## Add an invisible text object to original axis
701 ## that when it is destroyed will remove the legend 701 ## that when it is destroyed will remove the legend
702 t1 = text (0, 0, "", "parent", ca(1), "tag", "legend", 702 t1 = text (0, 0, "", "parent", ca(1), "tag", "legend",
703 "handlevisibility", "off", "visible", "off", 703 "handlevisibility", "off", "visible", "off",
704 "xliminclude", "off", "yliminclude", "off"); 704 "xliminclude", "off", "yliminclude", "off");
705 set (t1, "deletefcn", {@deletelegend1, hlegend}); 705 set (t1, "deletefcn", {@deletelegend1, hlegend});
706 706
707 ## Resize the axis the legend is attached to if the 707 ## Resize the axis the legend is attached to if the
708 ## legend is "outside" the plot and create listener to 708 ## legend is "outside" the plot and create listener to
709 ## resize axis to original size if the legend is deleted, 709 ## resize axis to original size if the legend is deleted,
710 ## hidden or shown 710 ## hidden or shown
711 if (outside) 711 if (outside)
712 for i = 1 : numel (ca) 712 for i = 1 : numel (ca)
713 units = get (ca(i), "units"); 713 units = get (ca(i), "units");
714 unwind_protect 714 unwind_protect
730 730
731 if (addprops) 731 if (addprops)
732 addproperty ("edgecolor", hlegend, "color", [0, 0, 0]); 732 addproperty ("edgecolor", hlegend, "color", [0, 0, 0]);
733 addproperty ("textcolor", hlegend, "color", [0, 0, 0]); 733 addproperty ("textcolor", hlegend, "color", [0, 0, 0]);
734 addproperty ("location", hlegend, "radio", "north|south|east|west|{northeast}|southeast|northwest|southwest|northoutside|southoutside|eastoutside|westoutside|northeastoutside|southeastoutside|northwestoutside|southwestoutside"); 734 addproperty ("location", hlegend, "radio", "north|south|east|west|{northeast}|southeast|northwest|southwest|northoutside|southoutside|eastoutside|westoutside|northeastoutside|southeastoutside|northwestoutside|southwestoutside");
735 addproperty ("orientation", hlegend, "radio", 735 addproperty ("orientation", hlegend, "radio",
736 "{vertical}|horizontal"); 736 "{vertical}|horizontal");
737 addproperty ("string", hlegend, "any", text_strings); 737 addproperty ("string", hlegend, "any", text_strings);
738 addproperty ("textposition", hlegend, "radio", "{left}|right"); 738 addproperty ("textposition", hlegend, "radio", "{left}|right");
739 else 739 else
740 set (hlegend, "string", text_strings); 740 set (hlegend, "string", text_strings);
741 endif 741 endif
742 742
743 if (outside) 743 if (outside)
744 set (hlegend, "location", strcat (position, "outside"), 744 set (hlegend, "location", strcat (position, "outside"),
745 "orientation", orientation, "textposition", textpos); 745 "orientation", orientation, "textposition", textpos);
746 else 746 else
747 set (hlegend, "location", position, "orientation", orientation, 747 set (hlegend, "location", position, "orientation", orientation,
748 "textposition", textpos); 748 "textposition", textpos);
749 endif 749 endif
874 function updateline (h, d, hlegend, linelength) 874 function updateline (h, d, hlegend, linelength)
875 lm = []; 875 lm = [];
876 ll = []; 876 ll = [];
877 kids = get (hlegend, "children"); 877 kids = get (hlegend, "children");
878 for i = 1 : numel (kids) 878 for i = 1 : numel (kids)
879 if (get (kids (i), "userdata") == h 879 if (get (kids (i), "userdata") == h
880 && strcmp (get (kids(i), "type"), "line")) 880 && strcmp (get (kids(i), "type"), "line"))
881 if (strcmp (get (kids (i), "marker"), "none")) 881 if (strcmp (get (kids (i), "marker"), "none"))
882 ll = kids (i); 882 ll = kids (i);
883 else 883 else
884 lm = kids (i); 884 lm = kids (i);
888 888
889 linestyle = get (h, "linestyle"); 889 linestyle = get (h, "linestyle");
890 marker = get (h, "marker"); 890 marker = get (h, "marker");
891 displayname = get (h, "displayname"); 891 displayname = get (h, "displayname");
892 892
893 if ((isempty (displayname) 893 if ((isempty (displayname)
894 || (strcmp (marker, "none") && strcmp (linestyle, "none"))) 894 || (strcmp (marker, "none") && strcmp (linestyle, "none")))
895 && (! isempty (lm) || isempty (ll))) 895 && (! isempty (lm) || isempty (ll)))
896 ## An element was removed from the legend. Need to recall the 896 ## An element was removed from the legend. Need to recall the
897 ## legend function to recreate a new legend 897 ## legend function to recreate a new legend
898 [hplots, text_strings] = getlegenddata (hlegend); 898 [hplots, text_strings] = getlegenddata (hlegend);
902 text_strings(i) = []; 902 text_strings(i) = [];
903 break; 903 break;
904 endif 904 endif
905 endfor 905 endfor
906 legend (hplots, text_strings); 906 legend (hplots, text_strings);
907 elseif ((!isempty (displayname) 907 elseif ((!isempty (displayname)
908 && (! strcmp (marker, "none") || ! strcmp (linestyle, "none"))) 908 && (! strcmp (marker, "none") || ! strcmp (linestyle, "none")))
909 && isempty (lm) && isempty (ll)) 909 && isempty (lm) && isempty (ll))
910 ## An element was added to the legend. Need to recall the 910 ## An element was added to the legend. Need to recall the
911 ## legend function to recreate a new legend 911 ## legend function to recreate a new legend
912 [hplots, text_strings] = getlegenddata (hlegend); 912 [hplots, text_strings] = getlegenddata (hlegend);
929 ypos1 = [ypos2, ypos2]; 929 ypos1 = [ypos2, ypos2];
930 xpos1 = xpos2 + [-0.5, 0.5] * linelength; 930 xpos1 = xpos2 + [-0.5, 0.5] * linelength;
931 delete (lm); 931 delete (lm);
932 endif 932 endif
933 if (! strcmp (linestyle, "none")) 933 if (! strcmp (linestyle, "none"))
934 line ("xdata", xpos1, "ydata", ypos1, "color", get (h, "color"), 934 line ("xdata", xpos1, "ydata", ypos1, "color", get (h, "color"),
935 "linestyle", get (h, "linestyle"), "marker", "none", 935 "linestyle", get (h, "linestyle"), "marker", "none",
936 "userdata", h, "parent", hlegend); 936 "userdata", h, "parent", hlegend);
937 endif 937 endif
938 if (! strcmp (marker, "none")) 938 if (! strcmp (marker, "none"))
939 line ("xdata", xpos2, "ydata", ypos2, "color", get (h, "color"), 939 line ("xdata", xpos2, "ydata", ypos2, "color", get (h, "color"),
940 "marker", marker, "markeredgecolor", get (h, "markeredgecolor"), 940 "marker", marker, "markeredgecolor", get (h, "markeredgecolor"),
941 "markerfacecolor", get (h, "markerfacecolor"), 941 "markerfacecolor", get (h, "markerfacecolor"),
942 "markersize", get (h, "markersize"), "linestyle", "none", 942 "markersize", get (h, "markersize"), "linestyle", "none",
943 "userdata", h, "parent", hlegend); 943 "userdata", h, "parent", hlegend);
944 endif 944 endif
964 if (k > 0) 964 if (k > 0)
965 if (strcmp (get (kids(k), "type"), "hggroup")) 965 if (strcmp (get (kids(k), "type"), "hggroup"))
966 hgkids = get (kids(k), "children"); 966 hgkids = get (kids(k), "children");
967 for j = 1 : length (hgkids) 967 for j = 1 : length (hgkids)
968 hgobj = get (hgkids (j)); 968 hgobj = get (hgkids (j));
969 if (isfield (hgobj, "displayname") 969 if (isfield (hgobj, "displayname")
970 && ! isempty (hgobj.displayname)) 970 && ! isempty (hgobj.displayname))
971 hplots = [hplots, hgkids(j)]; 971 hplots = [hplots, hgkids(j)];
972 text_strings = {text_strings{:}, hbobj.displayname}; 972 text_strings = {text_strings{:}, hbobj.displayname};
973 break; 973 break;
974 endif 974 endif