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