0
|
1 ## Copyright (C) 2001 Laurent Mazet |
|
2 ## |
|
3 ## This program is free software; it is distributed in the hope that it |
|
4 ## will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
5 ## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
|
6 ## the GNU General Public License for more details. |
|
7 ## |
|
8 ## You should have received a copy of the GNU General Public License |
|
9 ## along with this file; see the file COPYING. If not, write to the |
|
10 ## Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
11 ## 02111-1307, USA. |
|
12 |
|
13 ## usage: legend (string1, string2, string3, ..., [pos]) |
|
14 ## legend ([string1; string2; string3; ...], [pos]) |
|
15 ## legend ("off") |
|
16 ## |
|
17 ## Legend puts a legend on the current plot using the specified strings |
|
18 ## as labels. Legend works on line graphs, bar graphs, etc... |
|
19 ## |
|
20 ## pos: places the legend in the specified location: |
|
21 ## 0 = Don't move the legend box (default) |
|
22 ## 1 = Upper right-hand corner |
|
23 ## 2 = Upper left-hand corner |
|
24 ## 3 = Lower left-hand corner |
|
25 ## 4 = Lower right-hand corner |
|
26 ## -1 = To the right of the plot |
|
27 ## |
|
28 ## off will switch off legends from the plot |
|
29 ## |
|
30 ## REQUIRES: unix piping functionality, grep, sed and awk |
|
31 |
|
32 ## 2001-03-31 Paul Kienzle |
|
33 ## * use tmpnam for temporary file name; unlink to remove |
|
34 |
|
35 function legend (...) |
|
36 |
|
37 gset key; |
|
38 |
|
39 ## Data type |
|
40 |
|
41 data_type = 0; |
|
42 va_start(); |
|
43 str = ""; |
|
44 if (nargin > 0) |
|
45 str = va_arg(); |
|
46 endif; |
|
47 |
|
48 ## Test for off |
|
49 |
|
50 if ((isstr(str)) && (strcmp(tolower(deblank(str)),"off")) && (nargin == 1)) |
|
51 gset nokey; |
|
52 replot; |
|
53 return; |
|
54 endif; |
|
55 |
|
56 ## Test for data type (0 -> list of string, 1 -> array of string) |
|
57 |
|
58 if (length(str) != 0) && (isstr(str(1,:))) && (rows(str) != 1) |
|
59 data_type = 1; |
|
60 va_start(); |
|
61 data = va_arg(); |
|
62 nb_data = rows(data); |
|
63 nargin--; |
|
64 endif; |
|
65 |
|
66 pos_leg = 0; |
|
67 |
|
68 ## Get the original plotting command |
|
69 |
|
70 tmpfilename=tmpnam; |
|
71 command=["save \"",tmpfilename,"\"\n"]; |
|
72 graw(command); |
|
73 |
|
74 awk_prog= \ |
|
75 "BEGIN { \ |
|
76 dq = 0; \ |
|
77 format = \"%s\\n\"; \ |
|
78 } \ |
|
79 NF != 0 { \ |
|
80 for (i=1;i<=NF;i++) { \ |
|
81 if ($(i) == \"\\\"\") { \ |
|
82 if (dp == 0) { \ |
|
83 dp = 1; \ |
|
84 if ($(i+1) != \"\\\"\") { \ |
|
85 i++; \ |
|
86 printf (\"%s\", $(i)); \ |
|
87 } \ |
|
88 format = \" %s\"; \ |
|
89 } else { \ |
|
90 dp = 0; \ |
|
91 format = \"%s\\n\"; \ |
|
92 printf (\"\\n\"); \ |
|
93 } \ |
|
94 } else { \ |
|
95 printf (format, $(i)); \ |
|
96 } \ |
|
97 } \ |
|
98 }"; |
|
99 |
|
100 shell_cmd=["grep \"^pl \" " tmpfilename " | " \ |
|
101 "sed -e 's/,/ , /g' -e 's/\"/ \" /g'" " | " \ |
|
102 "awk '" awk_prog "'"]; |
|
103 plot_cmd = split(system(shell_cmd),"\n"); |
|
104 if (~length(deblank(plot_cmd(rows(plot_cmd), :)))) |
|
105 plot_cmd = plot_cmd ([1:rows(plot_cmd)-1],:); |
|
106 endif; |
|
107 unlink(tmpfilename); |
|
108 |
|
109 ## Look for the number of graph |
|
110 |
|
111 nb_graph = 0; |
|
112 i = 0; |
|
113 while (i++ < rows(plot_cmd)) |
|
114 line = deblank(plot_cmd(i,:)); |
|
115 if ((strcmp(line, "pl")) || (strcmp(line, ","))) |
|
116 nb_graph++; |
|
117 endif; |
|
118 endwhile; |
|
119 |
|
120 ## Change the legend of each graph |
|
121 |
|
122 new_plot = []; |
|
123 if (data_type == 0) |
|
124 va_start(); |
|
125 endif; |
|
126 fig = 0; |
|
127 i = 1; |
|
128 while (fig < nb_graph) |
|
129 |
|
130 ## Get the legend string |
|
131 |
|
132 if (((data_type == 0) && (nargin <= 0)) || \ |
|
133 ((data_type == 1) && (fig >= nb_data))) |
|
134 leg = "\"\""; |
|
135 else |
|
136 if (data_type == 0) |
|
137 leg = va_arg () ; |
|
138 nargin--; |
|
139 else |
|
140 leg = data(fig+1,:); |
|
141 endif; |
|
142 if (!isstr(leg)) |
|
143 pos_leg = leg; |
|
144 leg = "\"\""; |
|
145 elseif (length(deblank(leg)) == 0) |
|
146 leg = "\"\""; |
|
147 else |
|
148 leg=["\"" leg "\""]; |
|
149 endif; |
|
150 endif; |
|
151 |
|
152 ## look for the end of the graph command i.e. "," |
|
153 |
|
154 new_line = [deblank(plot_cmd(i++,:)) " \"" deblank(plot_cmd(i++,:)) "\""]; |
|
155 while ((i <= rows(plot_cmd)) && (!strcmp(deblank(plot_cmd(i,:)), ","))) |
|
156 if (strcmp(deblank(plot_cmd(i,:)), "t")) |
|
157 new_line = [new_line " t " leg]; |
|
158 i++; |
|
159 else |
|
160 new_line = [new_line " " deblank(plot_cmd(i,:))]; |
|
161 endif; |
|
162 i++; |
|
163 endwhile; |
|
164 |
|
165 if (length(new_plot)) |
|
166 new_plot = [ new_plot new_line]; |
|
167 else |
|
168 new_plot = new_line; |
|
169 endif; |
|
170 |
|
171 fig++; |
|
172 endwhile; |
|
173 |
|
174 ## Create a new ploting command |
|
175 |
|
176 new_plot = [new_plot "\n"]; |
|
177 graw(new_plot); |
|
178 |
|
179 ## Check for the last argument if we don't already get it |
|
180 |
|
181 while (nargin-- > 0) |
|
182 pos_leg = va_arg () ; |
|
183 if (isstr(pos_leg)) |
|
184 pos_leg = 0; |
|
185 endif; |
|
186 endwhile; |
|
187 |
|
188 ## Change the legend position |
|
189 |
|
190 if ((is_scalar (pos_leg)) && (isreal(pos_leg))) |
|
191 switch (pos_leg) |
|
192 case 1 |
|
193 gset key right top; |
|
194 case 2 |
|
195 gset key left top; |
|
196 case 3 |
|
197 gset key left bottom; |
|
198 case 4 |
|
199 gset key right bottom; |
|
200 case -1 |
|
201 gset key right top outside; |
|
202 endswitch; |
|
203 else |
|
204 warning ("pos must be a scalar"); |
|
205 endif; |
|
206 |
|
207 ## Regenerate the plot |
|
208 |
|
209 replot; |
|
210 |
|
211 endfunction; |