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