6146
|
1 ## Copyright (C) 2001 Laurent Mazet |
|
2 ## Copyright (C) 2006 John W. Eaton |
|
3 ## |
6440
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
|
8 ## the Free Software Foundation; either version 2, or (at your option) |
|
9 ## any later version. |
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
6146
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
6440
|
17 ## along with Octave; see the file COPYING. If not, write to the Free |
|
18 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
19 ## 02110-1301, USA. |
6146
|
20 |
|
21 ## -*- texinfo -*- |
6450
|
22 ## @deftypefn {Function File} {} legend (@var{st1}, @var{st2}, @dots{}) |
6977
|
23 ## @deftypefnx {Function File} {} legend (@var{st1}, @var{st2}, @dots{}, "location", @var{pos}) |
6146
|
24 ## @deftypefnx {Function File} {} legend (@var{matstr}) |
6977
|
25 ## @deftypefnx {Function File} {} legend (@var{matstr}, "location", @var{pos}) |
6146
|
26 ## @deftypefnx {Function File} {} legend (@var{cell}) |
6977
|
27 ## @deftypefnx {Function File} {} legend (@var{cell}, "location", @var{pos}) |
6146
|
28 ## @deftypefnx {Function File} {} legend ('@var{func}') |
|
29 ## |
6895
|
30 ## Display a legend for the current axes using the specified strings |
|
31 ## as labels. Legend entries may be specified as individual character |
|
32 ## string arguments, a character array, or a cell array of character |
|
33 ## strings. Legend works on line graphs, bar graphs, etc. A plot must |
|
34 ## exist before legend is called. |
6146
|
35 ## |
6895
|
36 ## The optional parameter @var{pos} specifies the location of the legend |
|
37 ## as follows: |
6146
|
38 ## |
6977
|
39 ## @multitable @columnfractions 0.06 0.14 0.80 |
|
40 ## @item @tab north @tab |
|
41 ## center top |
|
42 ## @item @tab south @tab |
|
43 ## center bottom |
|
44 ## @item @tab east @tab |
|
45 ## right center |
|
46 ## @item @tab west @tab |
|
47 ## left center |
|
48 ## @item @tab northeast @tab |
|
49 ## right top (default) |
|
50 ## @item @tab northwest @tab |
|
51 ## left top |
|
52 ## @item @tab southeast @tab |
|
53 ## right bottom |
|
54 ## @item @tab southwest @tab |
|
55 ## left bottom |
|
56 ## @item |
|
57 ## @item @tab outside @tab |
|
58 ## can be appended to any location string |
6146
|
59 ## @end multitable |
|
60 ## |
|
61 ## Some specific functions are directely avaliable using @var{func}: |
|
62 ## |
|
63 ## @table @code |
6895
|
64 ## @item "show" |
6146
|
65 ## Show legends from the plot |
6895
|
66 ## @item "hide" |
6146
|
67 ## @itemx off |
|
68 ## Hide legends from the plot |
6895
|
69 ## @item "boxon" |
6146
|
70 ## Draw a box around legends |
6895
|
71 ## @item "boxoff" |
6146
|
72 ## Withdraw the box around legends |
6895
|
73 ## @item "left" |
6146
|
74 ## Text is to the left of the keys |
6895
|
75 ## @item "right" |
6146
|
76 ## Text is to the right of the keys |
|
77 ## @end table |
|
78 ## @end deftypefn |
|
79 |
|
80 ## PKG_ADD mark_as_command legend |
|
81 |
|
82 function legend (varargin) |
|
83 |
6257
|
84 nargs = nargin; |
6146
|
85 |
6257
|
86 ca = gca (); |
6146
|
87 |
|
88 if (nargs > 0) |
|
89 pos = varargin{nargs}; |
|
90 if (isnumeric (pos) && isscalar (pos) && round (pos) == pos) |
6395
|
91 if (pos >= -1 && pos <= 4) |
6257
|
92 set (ca, "keypos", pos); |
6146
|
93 nargs--; |
|
94 else |
|
95 error ("legend: invalid position specified"); |
|
96 endif |
|
97 endif |
|
98 endif |
6977
|
99 |
|
100 if (nargs > 1) |
|
101 pos = varargin{nargs-1}; |
|
102 if (strcmpi (pos, "location") && ischar (str)) |
|
103 set (ca, "keypos", str); |
|
104 nargs -= 2; |
|
105 endif |
|
106 endif |
6146
|
107 |
6257
|
108 kids = get (ca, "children"); |
|
109 nkids = numel (kids); |
|
110 k = 1; |
|
111 turn_on_legend = false; |
|
112 |
6146
|
113 if (nargs == 1) |
|
114 arg = varargin{1}; |
|
115 if (ischar (arg)) |
|
116 if (rows (arg) == 1) |
|
117 str = tolower (deblank (arg)); |
|
118 switch (str) |
|
119 case {"off", "hide"} |
6257
|
120 set (ca, "key", "off"); |
6146
|
121 case "show" |
6257
|
122 set (ca, "key", "on"); |
6146
|
123 case "toggle" |
6257
|
124 val = get (ca, "key"); |
|
125 if (strcmp (val, "on")) |
|
126 set (ca, "key", "off"); |
|
127 else |
|
128 set (ca, "key", "on"); |
|
129 endif |
6146
|
130 case "boxon" |
6257
|
131 set (ca, "key", "on", "keybox", "on"); |
6146
|
132 case "boxoff" |
6257
|
133 set (ca, "keybox", "off"); |
6146
|
134 otherwise |
6257
|
135 while (k <= nkids && ! strcmp (get (kids(k), "type"), "line")) |
|
136 k++; |
|
137 endwhile |
|
138 if (k <= nkids) |
|
139 turn_on_legend = true; |
|
140 set (kids(k), "keylabel", arg); |
|
141 else |
|
142 warning ("legend: ignoring extra labels"); |
|
143 endif |
6146
|
144 endswitch |
|
145 nargs--; |
|
146 else |
|
147 varargin = cellstr (arg); |
6740
|
148 nargs = numel (varargin); |
6146
|
149 endif |
|
150 elseif (iscellstr (arg)) |
|
151 varargin = arg; |
|
152 nargs = numel (varargin); |
|
153 else |
|
154 error ("legend: expecting argument to be a character string"); |
|
155 endif |
|
156 endif |
|
157 |
6272
|
158 if (nargs > 0) |
|
159 have_data = false; |
|
160 for i = 1:nkids |
|
161 if (strcmp (get (kids(k), "type"), "line")) |
|
162 have_data = true; |
|
163 break; |
|
164 endif |
|
165 endfor |
|
166 if (! have_data) |
|
167 warning ("legend: plot data is empty; setting key labels has no effect"); |
|
168 endif |
6147
|
169 endif |
|
170 |
6575
|
171 warned = false; |
6146
|
172 for i = 1:nargs |
|
173 arg = varargin{i}; |
|
174 if (ischar (arg)) |
6257
|
175 while (k <= nkids && ! strcmp (get (kids(k), "type"), "line")) |
|
176 k++; |
|
177 endwhile |
|
178 if (k <= nkids) |
|
179 set (kids(k), "keylabel", arg); |
|
180 turn_on_legend = true; |
6450
|
181 k++; |
6257
|
182 elseif (! warned) |
|
183 warned = true; |
|
184 warning ("legend: ignoring extra labels"); |
|
185 endif |
6146
|
186 else |
|
187 error ("legend: expecting argument to be a character string"); |
|
188 endif |
|
189 endfor |
|
190 |
6257
|
191 if (turn_on_legend) |
|
192 set (ca, "key", "on"); |
6146
|
193 endif |
|
194 |
|
195 endfunction |
|
196 |
|
197 %!demo |
|
198 %! close all; |
|
199 %! plot(1:10, 1:10); |
|
200 %! title("a very long label can sometimes cause problems"); |
6977
|
201 %! legend({"hello world"}, "location", "northeastoutside") |
6146
|
202 |
|
203 %!demo |
|
204 %! close all; |
|
205 %! labels = {}; |
|
206 %! for i = 1:5 |
|
207 %! plot(1:100, i + rand(100,1)); hold on; |
|
208 %! labels = {labels{:}, strcat("Signal ", num2str(i))}; |
|
209 %! endfor; hold off; |
|
210 %! title("Signals with random offset and uniform noise") |
|
211 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]"); |
6977
|
212 %! legend(labels, "location", "southoutside") |
6146
|
213 %! legend("boxon") |