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