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