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