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