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