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