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