1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
1297
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
240
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
1
|
29 #endif |
|
30 |
1343
|
31 #include <cstring> |
|
32 |
1728
|
33 #include <string> |
|
34 |
1350
|
35 #include <fstream.h> |
|
36 #include <iostream.h> |
|
37 #include <strstream.h> |
|
38 |
|
39 #ifdef HAVE_UNISTD_H |
2442
|
40 #ifdef HAVE_SYS_TYPES_H |
524
|
41 #include <sys/types.h> |
2442
|
42 #endif |
524
|
43 #include <unistd.h> |
|
44 #endif |
1
|
45 |
524
|
46 #include "SLStack.h" |
|
47 #include "procstream.h" |
|
48 |
1802
|
49 #include "file-ops.h" |
2891
|
50 #include "lo-mappers.h" |
1755
|
51 #include "str-vec.h" |
|
52 |
1352
|
53 #include "defun.h" |
|
54 #include "error.h" |
|
55 #include "gripes.h" |
|
56 #include "load-save.h" |
1742
|
57 #include "oct-obj.h" |
|
58 #include "pt-cmd.h" |
|
59 #include "pt-exp.h" |
|
60 #include "pt-plot.h" |
2124
|
61 #include "pt-walk.h" |
2209
|
62 #include "sighandlers.h" |
1750
|
63 #include "sysdep.h" |
1
|
64 #include "utils.h" |
2371
|
65 #include "variables.h" |
1
|
66 |
2176
|
67 // If TRUE, a replot command is issued automatically each time a plot |
|
68 // changes in some way. |
|
69 static bool Vautomatic_replot; |
|
70 |
|
71 // The name of the shell command to execute to start gnuplot. |
|
72 static string Vgnuplot_binary; |
|
73 |
2289
|
74 // TRUE if gnuplot appears to support multiple plot windows with X11. |
|
75 static bool Vgnuplot_has_frames; |
|
76 |
2176
|
77 // TRUE if gnuplot appears to support multiplot. |
|
78 static bool Vgnuplot_has_multiplot; |
|
79 |
1358
|
80 // The number of lines we've plotted so far. |
735
|
81 static int plot_line_count = 0; |
1
|
82 |
|
83 // Is this a parametric plot? Makes a difference for 3D plotting. |
1827
|
84 static bool parametric_plot = false; |
1
|
85 |
943
|
86 // The gnuplot terminal type. |
|
87 static char *gnuplot_terminal_type = 0; |
|
88 |
478
|
89 // Should the graph window be cleared before plotting the next line? |
1827
|
90 static bool clear_before_plotting = true; |
478
|
91 |
524
|
92 // List of files to delete when we exit or crash. |
2614
|
93 // |
|
94 // XXX FIXME XXX -- this should really be static, but that causes |
|
95 // problems on some systems. |
|
96 SLStack <string> tmp_files; |
524
|
97 |
|
98 // Pipe to gnuplot. |
1449
|
99 static oprocstream *plot_stream = 0; |
524
|
100 |
2211
|
101 // ID of the plotter process. |
|
102 static pid_t plot_stream_pid = 0; |
|
103 |
3162
|
104 // Gnuplot command strings that we use. |
|
105 static string Vgnuplot_command_plot; |
|
106 static string Vgnuplot_command_replot; |
|
107 static string Vgnuplot_command_splot; |
|
108 static string Vgnuplot_command_using; |
|
109 static string Vgnuplot_command_with; |
|
110 static string Vgnuplot_command_title; |
|
111 static string Vgnuplot_command_end; |
866
|
112 |
597
|
113 static void |
2209
|
114 plot_stream_death_handler (pid_t pid, int) |
|
115 { |
|
116 close_plot_stream (); |
|
117 |
|
118 warning ("connection to external plotter (pid = %d) lost --", pid); |
|
119 warning ("please try your plot command(s) again"); |
|
120 } |
|
121 |
|
122 static void |
597
|
123 open_plot_stream (void) |
|
124 { |
1827
|
125 static bool initialized = false; |
597
|
126 |
1449
|
127 if (plot_stream && ! *plot_stream) |
|
128 { |
|
129 delete plot_stream; |
|
130 plot_stream = 0; |
|
131 } |
|
132 |
|
133 if (! plot_stream) |
597
|
134 { |
1827
|
135 initialized = false; |
943
|
136 |
597
|
137 plot_line_count = 0; |
|
138 |
2176
|
139 string plot_prog = Vgnuplot_binary; |
1755
|
140 |
2209
|
141 if (plot_prog.empty ()) |
|
142 plot_prog = "gnuplot"; |
|
143 |
|
144 // XXX FIXME XXX -- I'm not sure this is the right thing to do, |
|
145 // but without it, C-c at the octave prompt will kill gnuplot... |
|
146 |
2211
|
147 #if defined (HAVE_POSIX_SIGNALS) |
|
148 sigset_t set, oset; |
|
149 sigemptyset (&set); |
|
150 sigaddset (&set, SIGINT); |
|
151 sigprocmask (SIG_BLOCK, &set, &oset); |
2553
|
152 #else |
2705
|
153 volatile octave_interrupt_handler old_interrupt_handler |
2553
|
154 = octave_ignore_interrupts (); |
2211
|
155 #endif |
2209
|
156 |
|
157 plot_stream = new oprocstream (plot_prog.c_str ()); |
|
158 |
|
159 if (plot_stream) |
597
|
160 { |
2209
|
161 if (! *plot_stream) |
1449
|
162 { |
|
163 delete plot_stream; |
|
164 plot_stream = 0; |
2209
|
165 |
|
166 error ("plot: unable to open pipe to `%s'", plot_prog.c_str ()); |
1449
|
167 } |
2209
|
168 else |
597
|
169 { |
2211
|
170 plot_stream_pid = plot_stream->pid (); |
|
171 octave_child_list::insert (plot_stream_pid, |
|
172 plot_stream_death_handler); |
597
|
173 } |
|
174 } |
|
175 else |
2209
|
176 error ("plot: unable to open pipe to `%s'", plot_prog.c_str ()); |
2211
|
177 |
|
178 #if defined (HAVE_POSIX_SIGNALS) |
|
179 sigprocmask (SIG_SETMASK, &oset, 0); |
2553
|
180 #else |
|
181 octave_set_interrupt_handler (old_interrupt_handler); |
2211
|
182 #endif |
597
|
183 } |
|
184 |
1449
|
185 if (! error_state && plot_stream && *plot_stream && ! initialized) |
597
|
186 { |
1827
|
187 initialized = true; |
1449
|
188 *plot_stream << "set data style lines\n"; |
943
|
189 |
|
190 if (gnuplot_terminal_type) |
3162
|
191 *plot_stream << "set term " << gnuplot_terminal_type |
|
192 << Vgnuplot_command_end; |
597
|
193 } |
|
194 } |
|
195 |
|
196 static int |
|
197 send_to_plot_stream (const char *cmd) |
|
198 { |
1449
|
199 if (! (plot_stream && *plot_stream)) |
597
|
200 { |
|
201 open_plot_stream (); |
|
202 |
|
203 if (error_state) |
|
204 return -1; |
|
205 } |
|
206 |
3162
|
207 int replot_len = Vgnuplot_command_replot.length (); |
|
208 int splot_len = Vgnuplot_command_splot.length (); |
|
209 int plot_len = Vgnuplot_command_plot.length (); |
930
|
210 |
3162
|
211 bool is_replot = (Vgnuplot_command_replot.compare (cmd, 0, replot_len) == 0); |
|
212 bool is_splot = (Vgnuplot_command_splot.compare (cmd, 0, splot_len) == 0); |
|
213 bool is_plot = (Vgnuplot_command_plot.compare (cmd, 0, plot_len) == 0); |
661
|
214 |
|
215 if (plot_line_count == 0 && is_replot) |
597
|
216 error ("replot: no previous plot"); |
|
217 else |
|
218 { |
1449
|
219 *plot_stream << cmd; |
930
|
220 |
661
|
221 if (! (is_replot || is_splot || is_plot) |
|
222 && plot_line_count > 0 |
2176
|
223 && Vautomatic_replot) |
3162
|
224 *plot_stream << Vgnuplot_command_replot << Vgnuplot_command_end; |
930
|
225 |
1449
|
226 plot_stream->flush (); |
597
|
227 } |
|
228 |
|
229 return 0; |
|
230 } |
|
231 |
581
|
232 // Plotting, eh? |
1
|
233 |
|
234 tree_plot_command::~tree_plot_command (void) |
|
235 { |
|
236 delete range; |
|
237 delete plot_list; |
|
238 } |
|
239 |
578
|
240 void |
|
241 tree_plot_command::eval (void) |
1
|
242 { |
143
|
243 if (error_state) |
578
|
244 return; |
143
|
245 |
597
|
246 open_plot_stream (); |
|
247 |
1
|
248 ostrstream plot_buf; |
|
249 |
|
250 switch (ndim) |
|
251 { |
476
|
252 case 1: |
478
|
253 if (plot_line_count == 0) |
|
254 { |
|
255 if (plot_list) |
3162
|
256 plot_buf << Vgnuplot_command_plot; |
478
|
257 else |
|
258 { |
|
259 ::error ("replot: must have something to plot"); |
578
|
260 return; |
478
|
261 } |
|
262 } |
|
263 else |
3162
|
264 plot_buf << Vgnuplot_command_replot; |
476
|
265 break; |
777
|
266 |
1
|
267 case 2: |
478
|
268 if (clear_before_plotting || plot_line_count == 0) |
|
269 { |
|
270 plot_line_count = 0; |
3162
|
271 plot_buf << Vgnuplot_command_plot; |
478
|
272 } |
|
273 else |
3162
|
274 plot_buf << Vgnuplot_command_replot; |
1
|
275 break; |
777
|
276 |
1
|
277 case 3: |
930
|
278 if (clear_before_plotting || plot_line_count == 0) |
|
279 { |
|
280 plot_line_count = 0; |
3162
|
281 plot_buf << Vgnuplot_command_splot; |
930
|
282 } |
|
283 else |
3162
|
284 plot_buf << Vgnuplot_command_replot; |
1
|
285 break; |
777
|
286 |
1
|
287 default: |
771
|
288 gripe_2_or_3_dim_plot (); |
|
289 return; |
1
|
290 } |
|
291 |
524
|
292 if (range) |
478
|
293 { |
|
294 if (plot_line_count == 0) |
|
295 range->print (ndim, plot_buf); |
|
296 else |
|
297 warning ("can't specify new plot ranges with `replot' or while\ |
|
298 hold is on"); |
|
299 } |
1
|
300 |
191
|
301 if (error_state) |
578
|
302 return; |
191
|
303 |
591
|
304 if (plot_list) |
1
|
305 { |
591
|
306 int status = plot_list->print (ndim, plot_buf); |
578
|
307 |
771
|
308 if (error_state || status < 0) |
578
|
309 return; |
1
|
310 } |
|
311 |
3162
|
312 plot_buf << Vgnuplot_command_end << ends; |
1
|
313 |
1358
|
314 // Just testing... |
|
315 // char *message = plot_buf.str (); |
|
316 // cout << "[*]" << message << "[*]\n"; |
1
|
317 |
|
318 if (parametric_plot && ndim == 2) |
|
319 { |
|
320 warning ("can't make 2D parametric plot -- setting noparametric..."); |
|
321 send_to_plot_stream ("set noparametric\n"); |
|
322 char *message = plot_buf.str (); |
|
323 send_to_plot_stream (message); |
|
324 delete [] message; |
|
325 send_to_plot_stream ("set parametric\n"); |
|
326 } |
|
327 else |
|
328 { |
|
329 char *message = plot_buf.str (); |
|
330 send_to_plot_stream (message); |
|
331 delete [] message; |
|
332 } |
|
333 } |
|
334 |
591
|
335 void |
2124
|
336 tree_plot_command::accept (tree_walker& tw) |
1
|
337 { |
2124
|
338 tw.visit_plot_command (*this); |
1
|
339 } |
|
340 |
578
|
341 plot_limits::~plot_limits (void) |
1
|
342 { |
|
343 delete x_range; |
|
344 delete y_range; |
|
345 delete z_range; |
|
346 } |
|
347 |
|
348 void |
578
|
349 plot_limits::print (int ndim, ostrstream& plot_buf) |
1
|
350 { |
|
351 if (ndim == 2 || ndim == 3) |
|
352 { |
524
|
353 if (x_range) |
1
|
354 x_range->print (plot_buf); |
|
355 else |
|
356 return; |
|
357 |
524
|
358 if (y_range) |
1
|
359 y_range->print (plot_buf); |
|
360 else |
|
361 return; |
|
362 } |
|
363 |
524
|
364 if (ndim == 3 && z_range) |
1
|
365 z_range->print (plot_buf); |
|
366 } |
|
367 |
591
|
368 void |
2124
|
369 plot_limits::accept (tree_walker& tw) |
591
|
370 { |
2124
|
371 tw.visit_plot_limits (*this); |
591
|
372 } |
|
373 |
578
|
374 plot_range::~plot_range (void) |
1
|
375 { |
|
376 delete lower; |
|
377 delete upper; |
|
378 } |
|
379 |
|
380 void |
578
|
381 plot_range::print (ostrstream& plot_buf) |
1
|
382 { |
|
383 plot_buf << " ["; |
|
384 |
524
|
385 if (lower) |
1
|
386 { |
2972
|
387 octave_value lower_val = lower->rvalue (); |
|
388 |
191
|
389 if (error_state) |
|
390 { |
240
|
391 ::error ("evaluating lower bound of plot range"); |
191
|
392 return; |
|
393 } |
|
394 else |
|
395 { |
629
|
396 double lo = lower_val.double_value (); |
191
|
397 plot_buf << lo; |
|
398 } |
1
|
399 } |
|
400 |
|
401 plot_buf << ":"; |
|
402 |
524
|
403 if (upper) |
1
|
404 { |
2972
|
405 octave_value upper_val = upper->rvalue (); |
|
406 |
191
|
407 if (error_state) |
|
408 { |
240
|
409 ::error ("evaluating upper bound of plot range"); |
191
|
410 return; |
|
411 } |
|
412 else |
|
413 { |
629
|
414 double hi = upper_val.double_value (); |
191
|
415 plot_buf << hi; |
|
416 } |
1
|
417 } |
|
418 |
|
419 plot_buf << "]"; |
|
420 } |
|
421 |
591
|
422 void |
2124
|
423 plot_range::accept (tree_walker& tw) |
591
|
424 { |
2124
|
425 tw.visit_plot_range (*this); |
591
|
426 } |
|
427 |
578
|
428 subplot_using::~subplot_using (void) |
1
|
429 { |
|
430 delete scanf_fmt; |
|
431 } |
|
432 |
|
433 int |
872
|
434 subplot_using::eval (int ndim, int n_max) |
1
|
435 { |
2124
|
436 if ((ndim == 2 && qual_count > 4) |
|
437 || (ndim == 3 && qual_count > 3)) |
1
|
438 return -1; |
|
439 |
2124
|
440 if (qual_count > 0) |
|
441 val.resize (qual_count); |
872
|
442 |
2124
|
443 for (int i = 0; i < qual_count; i++) |
1
|
444 { |
524
|
445 if (x[i]) |
1
|
446 { |
2972
|
447 octave_value tmp = x[i]->rvalue (); |
|
448 |
191
|
449 if (error_state) |
|
450 { |
240
|
451 ::error ("evaluating plot using command"); |
191
|
452 return -1; |
|
453 } |
|
454 |
872
|
455 double val_tmp; |
1
|
456 if (tmp.is_defined ()) |
|
457 { |
872
|
458 val_tmp = tmp.double_value (); |
1
|
459 |
872
|
460 if (error_state) |
|
461 return -1; |
1
|
462 |
1086
|
463 if (xisnan (val_tmp)) |
|
464 { |
|
465 ::error ("NaN is invalid as a column specifier"); |
|
466 return -1; |
|
467 } |
|
468 |
872
|
469 int n = NINT (val_tmp); |
1086
|
470 |
322
|
471 if (n < 1 || n_max > 0 && n > n_max) |
1
|
472 { |
240
|
473 ::error ("using: column %d out of range", n); |
1
|
474 return -1; |
|
475 } |
|
476 else |
2305
|
477 val (i) = n; |
1
|
478 } |
|
479 else |
|
480 return -1; |
|
481 } |
|
482 else |
|
483 return -1; |
|
484 } |
|
485 |
524
|
486 if (scanf_fmt) |
1
|
487 warning ("ignoring scanf format in plot command"); |
|
488 |
872
|
489 return 0; |
|
490 } |
|
491 |
|
492 ColumnVector |
|
493 subplot_using::values (int ndim, int n_max) |
|
494 { |
|
495 int status = eval (ndim, n_max); |
|
496 |
1203
|
497 if (status < 0) |
872
|
498 return -1; |
|
499 |
|
500 return val; |
|
501 } |
|
502 |
|
503 int |
|
504 subplot_using::print (int ndim, int n_max, ostrstream& plot_buf) |
|
505 { |
|
506 int status = eval (ndim, n_max); |
|
507 |
1203
|
508 if (status < 0) |
872
|
509 return -1; |
|
510 |
2124
|
511 for (int i = 0; i < qual_count; i++) |
872
|
512 { |
|
513 if (i == 0) |
3162
|
514 plot_buf << " " << Vgnuplot_command_using << " "; |
872
|
515 else |
|
516 plot_buf << ":"; |
|
517 |
2305
|
518 plot_buf << val (i); |
872
|
519 } |
|
520 |
1
|
521 return 0; |
|
522 } |
|
523 |
591
|
524 void |
2124
|
525 subplot_using::accept (tree_walker& tw) |
1
|
526 { |
2124
|
527 tw.visit_subplot_using (*this); |
1
|
528 } |
|
529 |
578
|
530 subplot_style::~subplot_style (void) |
1622
|
531 { |
2124
|
532 delete sp_linetype; |
|
533 delete sp_pointtype; |
1
|
534 } |
|
535 |
|
536 int |
578
|
537 subplot_style::print (ostrstream& plot_buf) |
1
|
538 { |
2124
|
539 if (! sp_style.empty ()) |
1
|
540 { |
3162
|
541 plot_buf << " " << Vgnuplot_command_with << " " << sp_style; |
1
|
542 |
2124
|
543 if (sp_linetype) |
1
|
544 { |
2972
|
545 octave_value tmp = sp_linetype->rvalue (); |
|
546 |
191
|
547 if (! error_state && tmp.is_defined ()) |
1
|
548 { |
629
|
549 double val = tmp.double_value (); |
1086
|
550 if (xisnan (val)) |
|
551 { |
|
552 ::error ("NaN is invalid a plotting line style"); |
|
553 return -1; |
|
554 } |
|
555 else |
|
556 plot_buf << " " << NINT (val); |
1
|
557 } |
|
558 else |
191
|
559 { |
240
|
560 ::error ("evaluating plot style command"); |
191
|
561 return -1; |
|
562 } |
1
|
563 } |
|
564 |
2124
|
565 if (sp_pointtype) |
1
|
566 { |
2972
|
567 octave_value tmp = sp_pointtype->rvalue (); |
|
568 |
191
|
569 if (! error_state && tmp.is_defined ()) |
1
|
570 { |
629
|
571 double val = tmp.double_value (); |
1086
|
572 if (xisnan (val)) |
|
573 { |
|
574 ::error ("NaN is invalid a plotting point style"); |
|
575 return -1; |
|
576 } |
|
577 else |
|
578 plot_buf << " " << NINT (val); |
1
|
579 } |
|
580 else |
191
|
581 { |
240
|
582 ::error ("evaluating plot style command"); |
191
|
583 return -1; |
|
584 } |
1
|
585 } |
|
586 } |
|
587 else |
|
588 return -1; |
|
589 |
|
590 return 0; |
|
591 } |
|
592 |
2543
|
593 bool |
|
594 subplot_style::columns_ok (int nc) |
872
|
595 { |
2543
|
596 bool retval = true; |
|
597 |
|
598 if ((almost_match ("boxes", sp_style, 5, 0) && nc != 3) |
|
599 || (almost_match ("boxerrorbars", sp_style, 5, 0) |
|
600 && (! (nc == 3 || nc == 4 || nc == 5))) |
|
601 || ((almost_match ("boxxyerrorbars", sp_style, 4, 0) |
|
602 || almost_match ("xyerrorbars", sp_style, 2, 0)) |
|
603 && (! (nc == 4 || nc == 6 || nc == 7))) |
|
604 || ((almost_match ("candlesticks", sp_style, 1, 0) |
|
605 || almost_match ("financebars", sp_style, 2, 0)) |
|
606 && nc != 5) |
|
607 || ((almost_match ("errorbars", sp_style, 1, 0) |
|
608 || almost_match ("xerrorbars", sp_style, 1, 0) |
|
609 || almost_match ("yerrorbars", sp_style, 1, 0)) |
|
610 && (! (nc == 3 || nc == 4)))) |
|
611 { |
|
612 error |
|
613 ("invalid number of data columns = %d specified for plot style `%s'", |
|
614 nc, sp_style.c_str ()); |
|
615 |
|
616 retval = false; |
|
617 } |
|
618 |
|
619 return retval; |
872
|
620 } |
|
621 |
591
|
622 void |
2124
|
623 subplot_style::accept (tree_walker& tw) |
591
|
624 { |
2124
|
625 tw.visit_subplot_style (*this); |
591
|
626 } |
|
627 |
878
|
628 subplot::~subplot (void) |
|
629 { |
2124
|
630 delete sp_plot_data; |
|
631 delete sp_using_clause; |
|
632 delete sp_title_clause; |
|
633 delete sp_style_clause; |
878
|
634 } |
|
635 |
2086
|
636 octave_value |
|
637 subplot::extract_plot_data (int ndim, octave_value& data) |
872
|
638 { |
2086
|
639 octave_value retval; |
872
|
640 |
2124
|
641 if (sp_using_clause) |
872
|
642 { |
2124
|
643 ColumnVector val = sp_using_clause->values (ndim); |
872
|
644 |
2086
|
645 octave_value_list args; |
2963
|
646 |
872
|
647 args(1) = val; |
2086
|
648 args(0) = octave_value::magic_colon_t; |
872
|
649 |
2963
|
650 retval = data.do_index_op (args); |
872
|
651 |
|
652 if (error_state) |
2086
|
653 return octave_value (); |
872
|
654 } |
|
655 else |
|
656 { |
|
657 retval = data; |
|
658 } |
|
659 |
2543
|
660 int nc = retval.columns (); |
872
|
661 |
2543
|
662 if (ndim == 2 && sp_style_clause && ! sp_style_clause->columns_ok (nc)) |
|
663 return octave_value (); |
872
|
664 |
|
665 return retval; |
|
666 } |
|
667 |
591
|
668 int |
872
|
669 subplot::handle_plot_data (int ndim, ostrstream& plot_buf) |
591
|
670 { |
2124
|
671 if (sp_plot_data) |
591
|
672 { |
2972
|
673 octave_value data = sp_plot_data->rvalue (); |
872
|
674 |
591
|
675 if (! error_state && data.is_defined ()) |
|
676 { |
1750
|
677 string file; |
|
678 |
610
|
679 if (data.is_string ()) |
591
|
680 { |
1358
|
681 // Should really try to look at data file to determine |
|
682 // n_max. Can't do much about other arbitrary gnuplot |
|
683 // commands though... |
872
|
684 |
|
685 int n_max = 0; |
|
686 |
2926
|
687 file = file_ops::tilde_expand (data.string_value ()); |
1728
|
688 |
1750
|
689 ifstream ftmp (file.c_str ()); |
|
690 |
591
|
691 if (ftmp) |
|
692 { |
|
693 plot_buf << " \"" << file << '"'; |
|
694 } |
|
695 else |
|
696 { |
1750
|
697 file = ""; |
591
|
698 |
1358
|
699 // Opening as a file failed. Let's try passing it |
|
700 // along as a plot command. |
|
701 |
591
|
702 plot_buf << " " << data.string_value (); |
872
|
703 } |
|
704 |
2124
|
705 if (sp_using_clause) |
872
|
706 { |
2124
|
707 int status = sp_using_clause->print (ndim, n_max, plot_buf); |
1750
|
708 |
872
|
709 if (status < 0) |
|
710 return -1; |
591
|
711 } |
|
712 } |
872
|
713 else |
591
|
714 { |
1358
|
715 // Eliminate the need for printing a using clause to |
|
716 // plot_buf. |
872
|
717 |
2086
|
718 octave_value tmp_data = extract_plot_data (ndim, data); |
872
|
719 |
|
720 if (tmp_data.is_defined ()) |
|
721 { |
|
722 switch (ndim) |
|
723 { |
|
724 case 2: |
|
725 file = save_in_tmp_file (tmp_data, ndim); |
|
726 break; |
777
|
727 |
872
|
728 case 3: |
|
729 file = save_in_tmp_file (tmp_data, ndim, |
|
730 parametric_plot); |
|
731 break; |
777
|
732 |
872
|
733 default: |
|
734 gripe_2_or_3_dim_plot (); |
|
735 break; |
|
736 } |
591
|
737 |
1750
|
738 if (file.length () > 0) |
872
|
739 { |
|
740 mark_for_deletion (file); |
|
741 plot_buf << " \"" << file << '"'; |
|
742 } |
|
743 } |
591
|
744 } |
|
745 } |
|
746 else |
|
747 return -1; |
|
748 } |
|
749 else |
|
750 return -1; |
|
751 |
872
|
752 return 0; |
|
753 } |
591
|
754 |
872
|
755 int |
|
756 subplot::print (int ndim, ostrstream& plot_buf) |
|
757 { |
|
758 int status = handle_plot_data (ndim, plot_buf); |
|
759 |
|
760 if (status < 0) |
|
761 return -1; |
591
|
762 |
2124
|
763 if (sp_title_clause) |
591
|
764 { |
2972
|
765 octave_value tmp = sp_title_clause->rvalue (); |
|
766 |
610
|
767 if (! error_state && tmp.is_string ()) |
3162
|
768 plot_buf << " " << Vgnuplot_command_title << " " |
866
|
769 << '"' << tmp.string_value () << '"'; |
591
|
770 else |
|
771 { |
|
772 warning ("line title must be a string"); |
3162
|
773 plot_buf << " " << Vgnuplot_command_title << " " |
866
|
774 << '"' << "line " << plot_line_count << '"'; |
591
|
775 } |
|
776 } |
|
777 else |
3162
|
778 plot_buf << " " << Vgnuplot_command_title << " " |
866
|
779 << '"' << "line " << plot_line_count << '"'; |
591
|
780 |
2124
|
781 if (sp_style_clause) |
591
|
782 { |
2124
|
783 int status = sp_style_clause->print (plot_buf); |
591
|
784 if (status < 0) |
|
785 return -1; |
|
786 } |
|
787 |
|
788 return 0; |
|
789 } |
|
790 |
|
791 void |
2124
|
792 subplot::accept (tree_walker& tw) |
591
|
793 { |
2124
|
794 tw.visit_subplot (*this); |
591
|
795 } |
|
796 |
1622
|
797 subplot_list::~subplot_list (void) |
|
798 { |
|
799 while (! empty ()) |
|
800 { |
|
801 subplot *t = remove_front (); |
|
802 delete t; |
|
803 } |
|
804 } |
|
805 |
883
|
806 int |
591
|
807 subplot_list::print (int ndim, ostrstream& plot_buf) |
|
808 { |
|
809 int status = 0; |
|
810 |
|
811 for (Pix p = first (); p != 0; next (p)) |
|
812 { |
|
813 subplot *elt = this->operator () (p); |
|
814 |
|
815 plot_line_count++; |
|
816 |
|
817 if (p != first ()) |
|
818 plot_buf << ",\\\n "; |
|
819 |
|
820 status = elt->print (ndim, plot_buf); |
|
821 |
|
822 if (status < 0) |
|
823 break; |
|
824 } |
|
825 |
|
826 return status; |
|
827 } |
|
828 |
|
829 void |
2124
|
830 subplot_list::accept (tree_walker& tw) |
591
|
831 { |
2124
|
832 tw.visit_subplot_list (*this); |
591
|
833 } |
|
834 |
1755
|
835 string |
2086
|
836 save_in_tmp_file (octave_value& t, int ndim, bool parametric) |
524
|
837 { |
2936
|
838 string name = file_ops::tempnam ("", "oct-"); |
1755
|
839 |
|
840 if (! name.empty ()) |
524
|
841 { |
1755
|
842 ofstream file (name.c_str ()); |
|
843 |
524
|
844 if (file) |
|
845 { |
|
846 switch (ndim) |
|
847 { |
|
848 case 2: |
2799
|
849 save_ascii_data (file, t, name, true); |
524
|
850 break; |
777
|
851 |
524
|
852 case 3: |
607
|
853 save_three_d (file, t, parametric); |
524
|
854 break; |
777
|
855 |
524
|
856 default: |
771
|
857 gripe_2_or_3_dim_plot (); |
524
|
858 break; |
|
859 } |
|
860 } |
|
861 else |
|
862 { |
1755
|
863 error ("couldn't open temporary output file `%s'", name.c_str ()); |
|
864 name.resize (0); |
524
|
865 } |
|
866 } |
1755
|
867 |
524
|
868 return name; |
|
869 } |
|
870 |
|
871 void |
1750
|
872 mark_for_deletion (const string& file) |
524
|
873 { |
1750
|
874 tmp_files.push (file); |
524
|
875 } |
|
876 |
|
877 void |
|
878 cleanup_tmp_files (void) |
|
879 { |
|
880 while (! tmp_files.empty ()) |
|
881 { |
1750
|
882 string filename = tmp_files.pop (); |
|
883 unlink (filename.c_str ()); |
524
|
884 } |
|
885 } |
|
886 |
|
887 void |
|
888 close_plot_stream (void) |
|
889 { |
2211
|
890 octave_child_list::remove (plot_stream_pid); |
|
891 |
1449
|
892 if (plot_stream) |
|
893 { |
|
894 delete plot_stream; |
|
895 plot_stream = 0; |
|
896 } |
524
|
897 |
|
898 plot_line_count = 0; |
|
899 } |
|
900 |
1328
|
901 void |
1755
|
902 do_external_plotter_cd (const string& newdir) |
1328
|
903 { |
1449
|
904 if (plot_stream && *plot_stream) |
1328
|
905 { |
|
906 ostrstream plot_buf; |
3162
|
907 plot_buf << "cd \"" << newdir << "\"" << Vgnuplot_command_end << ends; |
1328
|
908 char *message = plot_buf.str (); |
|
909 send_to_plot_stream (message); |
|
910 delete [] message; |
|
911 } |
|
912 } |
|
913 |
1957
|
914 DEFUN (clearplot, , , |
883
|
915 "clearplot (): clear the plot window") |
|
916 { |
2086
|
917 octave_value_list retval; |
883
|
918 send_to_plot_stream ("clear\n"); |
930
|
919 |
1358
|
920 // XXX FIXME XXX -- instead of just clearing these things, it would |
|
921 // be nice if we could reset things to a user-specified default |
|
922 // state. |
930
|
923 |
|
924 send_to_plot_stream ("set title\n"); |
|
925 send_to_plot_stream ("set xlabel\n"); |
|
926 send_to_plot_stream ("set ylabel\n"); |
940
|
927 send_to_plot_stream ("set nogrid\n"); |
1000
|
928 send_to_plot_stream ("set nolabel\n"); |
940
|
929 |
1358
|
930 // This makes a simple `replot' not work after a `clearplot' command |
|
931 // has been issued. |
940
|
932 |
|
933 plot_line_count = 0; |
930
|
934 |
883
|
935 return retval; |
|
936 } |
|
937 |
884
|
938 DEFALIAS (clg, clearplot); |
|
939 |
1957
|
940 DEFUN (closeplot, , , |
524
|
941 "closeplot (): close the stream to plotter") |
|
942 { |
2086
|
943 octave_value_list retval; |
524
|
944 close_plot_stream (); |
|
945 return retval; |
|
946 } |
|
947 |
1957
|
948 DEFUN_TEXT (hold, args, , |
524
|
949 "hold [on|off]\n\ |
|
950 \n\ |
|
951 determine whether the plot window is cleared before the next line is\n\ |
|
952 drawn. With no argument, toggle the current state.") |
|
953 { |
2086
|
954 octave_value_list retval; |
524
|
955 |
1755
|
956 int argc = args.length () + 1; |
|
957 |
1968
|
958 string_vector argv = args.make_argv ("hold"); |
1755
|
959 |
|
960 if (error_state) |
|
961 return retval; |
524
|
962 |
|
963 switch (argc) |
|
964 { |
|
965 case 1: |
|
966 clear_before_plotting = ! clear_before_plotting; |
|
967 break; |
777
|
968 |
524
|
969 case 2: |
1755
|
970 if (argv[1] == "on") |
1827
|
971 clear_before_plotting = false; |
1755
|
972 else if (argv[1] == "off") |
1827
|
973 clear_before_plotting = true; |
524
|
974 else |
|
975 print_usage ("hold"); |
|
976 break; |
777
|
977 |
524
|
978 default: |
|
979 print_usage ("hold"); |
|
980 break; |
|
981 } |
|
982 |
|
983 return retval; |
|
984 } |
|
985 |
1957
|
986 DEFUN (ishold, , , |
735
|
987 "ishold\n\ |
|
988 \n\ |
|
989 Return 1 if hold is on, otherwise return 0.") |
|
990 { |
2800
|
991 return static_cast<double> (! clear_before_plotting); |
735
|
992 } |
|
993 |
1957
|
994 DEFUN (purge_tmp_files, , , |
524
|
995 "delete temporary data files used for plotting") |
|
996 { |
2086
|
997 octave_value_list retval; |
524
|
998 cleanup_tmp_files (); |
|
999 return retval; |
|
1000 } |
|
1001 |
2520
|
1002 DEFUN_TEXT (gset, args, , |
|
1003 "gset [options]\n\ |
524
|
1004 \n\ |
2520
|
1005 set plotting options for gnuplot") |
524
|
1006 { |
2086
|
1007 octave_value_list retval; |
524
|
1008 |
1755
|
1009 int argc = args.length () + 1; |
|
1010 |
1968
|
1011 string_vector argv = args.make_argv ("set"); |
1755
|
1012 |
|
1013 if (error_state) |
|
1014 return retval; |
524
|
1015 |
|
1016 ostrstream plot_buf; |
|
1017 |
|
1018 if (argc > 1) |
|
1019 { |
|
1020 if (almost_match ("parametric", argv[1], 3)) |
1827
|
1021 parametric_plot = true; |
524
|
1022 else if (almost_match ("noparametric", argv[1], 5)) |
1827
|
1023 parametric_plot = false; |
943
|
1024 else if (almost_match ("term", argv[1], 1)) |
|
1025 { |
|
1026 delete [] gnuplot_terminal_type; |
|
1027 ostrstream buf; |
|
1028 for (int i = 2; i < argc; i++) |
|
1029 buf << argv[i] << " "; |
3162
|
1030 buf << Vgnuplot_command_end << ends; |
943
|
1031 gnuplot_terminal_type = buf.str (); |
|
1032 } |
524
|
1033 } |
|
1034 |
|
1035 for (int i = 0; i < argc; i++) |
|
1036 plot_buf << argv[i] << " "; |
|
1037 |
3162
|
1038 plot_buf << Vgnuplot_command_end << ends; |
524
|
1039 |
|
1040 char *plot_command = plot_buf.str (); |
|
1041 send_to_plot_stream (plot_command); |
|
1042 |
|
1043 delete [] plot_command; |
|
1044 |
|
1045 return retval; |
|
1046 } |
|
1047 |
2520
|
1048 DEFUN_TEXT (set, args, nargout, |
|
1049 "This command is has been replaced by `gset'.") |
|
1050 { |
2521
|
1051 warning ("set is obsolete -- use gset instead"); |
2520
|
1052 return Fgset (args, nargout); |
|
1053 } |
|
1054 |
|
1055 DEFUN_TEXT (gshow, args, , |
|
1056 "gshow [options]\n\ |
524
|
1057 \n\ |
|
1058 show plotting options") |
|
1059 { |
2086
|
1060 octave_value_list retval; |
524
|
1061 |
1755
|
1062 int argc = args.length () + 1; |
|
1063 |
1968
|
1064 string_vector argv = args.make_argv ("show"); |
1755
|
1065 |
|
1066 if (error_state) |
|
1067 return retval; |
524
|
1068 |
|
1069 ostrstream plot_buf; |
|
1070 |
|
1071 for (int i = 0; i < argc; i++) |
|
1072 plot_buf << argv[i] << " "; |
|
1073 |
3162
|
1074 plot_buf << Vgnuplot_command_end << ends; |
524
|
1075 |
|
1076 char *plot_command = plot_buf.str (); |
|
1077 send_to_plot_stream (plot_command); |
|
1078 |
|
1079 delete [] plot_command; |
|
1080 |
|
1081 return retval; |
|
1082 } |
|
1083 |
2520
|
1084 DEFUN_TEXT (show, args, nargout, |
|
1085 "This command is has been replaced by `gshow'.") |
|
1086 { |
2521
|
1087 warning ("show is obsolete -- use gshow instead"); |
2520
|
1088 return Fgshow (args, nargout); |
|
1089 } |
|
1090 |
2176
|
1091 static int |
|
1092 automatic_replot (void) |
|
1093 { |
|
1094 Vautomatic_replot = check_preference ("automatic_replot"); |
|
1095 |
|
1096 return 0; |
|
1097 } |
|
1098 |
3162
|
1099 static int |
|
1100 set_string_var (string& var, const char *nm) |
2176
|
1101 { |
3162
|
1102 int retval = 0; |
2176
|
1103 |
3162
|
1104 string s = builtin_string_variable (nm); |
2176
|
1105 |
|
1106 if (s.empty ()) |
|
1107 { |
3162
|
1108 gripe_invalid_value_specified (nm); |
|
1109 retval = -1; |
2176
|
1110 } |
|
1111 else |
3162
|
1112 var = s; |
|
1113 |
|
1114 return retval; |
|
1115 } |
|
1116 |
|
1117 static int |
|
1118 gnuplot_binary (void) |
|
1119 { |
|
1120 return set_string_var (Vgnuplot_binary, "gnuplot_binary"); |
|
1121 } |
|
1122 |
|
1123 static int |
|
1124 gnuplot_command_plot (void) |
|
1125 { |
|
1126 return set_string_var (Vgnuplot_command_plot, "gnuplot_command_plot"); |
|
1127 } |
|
1128 |
|
1129 static int |
|
1130 gnuplot_command_replot (void) |
|
1131 { |
|
1132 return set_string_var (Vgnuplot_command_replot, "gnuplot_command_replot"); |
|
1133 } |
2176
|
1134 |
3162
|
1135 static int |
|
1136 gnuplot_command_splot (void) |
|
1137 { |
|
1138 return set_string_var (Vgnuplot_command_splot, "gnuplot_command_splot"); |
|
1139 } |
|
1140 |
|
1141 static int |
|
1142 gnuplot_command_using (void) |
|
1143 { |
|
1144 return set_string_var (Vgnuplot_command_using, "gnuplot_command_using"); |
|
1145 } |
|
1146 |
|
1147 static int |
|
1148 gnuplot_command_with (void) |
|
1149 { |
|
1150 return set_string_var (Vgnuplot_command_with, "gnuplot_command_with"); |
|
1151 } |
|
1152 |
|
1153 static int |
|
1154 gnuplot_command_title (void) |
|
1155 { |
|
1156 return set_string_var (Vgnuplot_command_title, "gnuplot_command_title"); |
|
1157 } |
|
1158 |
|
1159 static int |
|
1160 gnuplot_command_end (void) |
|
1161 { |
|
1162 return set_string_var (Vgnuplot_command_end, "gnuplot_command_end"); |
2176
|
1163 } |
|
1164 |
|
1165 static int |
2289
|
1166 gnuplot_has_frames (void) |
|
1167 { |
|
1168 Vgnuplot_has_frames = check_preference ("gnuplot_has_frames"); |
|
1169 |
|
1170 return 0; |
|
1171 } |
|
1172 |
|
1173 static int |
2176
|
1174 gnuplot_has_multiplot (void) |
|
1175 { |
|
1176 Vgnuplot_has_multiplot = check_preference ("gnuplot_has_multiplot"); |
|
1177 |
|
1178 return 0; |
|
1179 } |
|
1180 |
|
1181 void |
|
1182 symbols_of_pt_plot (void) |
|
1183 { |
|
1184 DEFVAR (automatic_replot, 0.0, 0, automatic_replot, |
|
1185 "if true, auto-insert a replot command when a plot changes"); |
|
1186 |
|
1187 DEFVAR (gnuplot_binary, "gnuplot", 0, gnuplot_binary, |
|
1188 "path to gnuplot binary"); |
|
1189 |
3162
|
1190 DEFVAR (gnuplot_command_plot, "pl", 0, gnuplot_command_plot, |
|
1191 ""); |
|
1192 |
|
1193 DEFVAR (gnuplot_command_replot, "rep", 0, gnuplot_command_replot, |
|
1194 ""); |
|
1195 |
|
1196 DEFVAR (gnuplot_command_splot, "sp", 0, gnuplot_command_splot, |
|
1197 ""); |
|
1198 |
|
1199 DEFVAR (gnuplot_command_using, "u", 0, gnuplot_command_using, |
|
1200 ""); |
|
1201 |
|
1202 DEFVAR (gnuplot_command_with, "w", 0, gnuplot_command_with, |
|
1203 ""); |
|
1204 |
|
1205 DEFVAR (gnuplot_command_title, "t", 0, gnuplot_command_title, |
|
1206 ""); |
|
1207 |
|
1208 DEFVAR (gnuplot_command_end, "\n", 0, gnuplot_command_end, |
|
1209 ""); |
|
1210 |
2289
|
1211 #ifdef GNUPLOT_HAS_FRAMES |
|
1212 double with_frames = 1.0; |
|
1213 #else |
|
1214 double with_frames = 0.0; |
|
1215 #endif |
|
1216 |
|
1217 DEFVAR (gnuplot_has_frames, with_frames, 0, gnuplot_has_frames, |
|
1218 "true if gnuplot supports multiple plot windows on X11, false otherwise"); |
|
1219 |
2176
|
1220 #ifdef GNUPLOT_HAS_MULTIPLOT |
|
1221 double with_multiplot = 1.0; |
|
1222 #else |
|
1223 double with_multiplot = 0.0; |
|
1224 #endif |
|
1225 |
|
1226 DEFVAR (gnuplot_has_multiplot, with_multiplot, 0, gnuplot_has_multiplot, |
|
1227 "true if gnuplot supports multiplot, false otherwise"); |
|
1228 } |
|
1229 |
1
|
1230 /* |
|
1231 ;;; Local Variables: *** |
|
1232 ;;; mode: C++ *** |
|
1233 ;;; End: *** |
|
1234 */ |