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