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