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