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