1
|
1 // tree-plot.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
1297
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
240
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
1
|
30 #endif |
|
31 |
1343
|
32 #include <cstring> |
|
33 |
1350
|
34 #include <fstream.h> |
|
35 #include <iostream.h> |
|
36 #include <strstream.h> |
|
37 |
|
38 #ifdef HAVE_UNISTD_H |
524
|
39 #include <sys/types.h> |
|
40 #include <unistd.h> |
|
41 #endif |
1
|
42 |
524
|
43 #include "SLStack.h" |
|
44 #include "procstream.h" |
|
45 |
1352
|
46 #include "defun.h" |
|
47 #include "error.h" |
|
48 #include "gripes.h" |
|
49 #include "help.h" |
|
50 #include "load-save.h" |
584
|
51 #include "tree-base.h" |
|
52 #include "tree-cmd.h" |
524
|
53 #include "tree-const.h" |
1352
|
54 #include "tree-expr.h" |
524
|
55 #include "tree-plot.h" |
1352
|
56 #include "user-prefs.h" |
1
|
57 #include "utils.h" |
|
58 |
87
|
59 extern "C" |
|
60 { |
581
|
61 #include <readline/tilde.h> |
87
|
62 } |
|
63 |
1358
|
64 // The number of lines we've plotted so far. |
735
|
65 static int plot_line_count = 0; |
1
|
66 |
|
67 // Is this a parametric plot? Makes a difference for 3D plotting. |
735
|
68 static int parametric_plot = 0; |
1
|
69 |
943
|
70 // The gnuplot terminal type. |
|
71 static char *gnuplot_terminal_type = 0; |
|
72 |
478
|
73 // Should the graph window be cleared before plotting the next line? |
735
|
74 static int clear_before_plotting = 1; |
478
|
75 |
524
|
76 // List of files to delete when we exit or crash. |
|
77 static SLStack <char *> tmp_files; |
|
78 |
|
79 // Pipe to gnuplot. |
|
80 static oprocstream plot_stream; |
|
81 |
866
|
82 // Use shortest possible abbreviations to minimize trouble caused by |
|
83 // gnuplot's fixed-length command line buffer. |
|
84 |
|
85 #ifndef GNUPLOT_COMMAND_PLOT |
|
86 #define GNUPLOT_COMMAND_PLOT "pl" |
|
87 #endif |
|
88 |
|
89 #ifndef GNUPLOT_COMMAND_REPLOT |
|
90 #define GNUPLOT_COMMAND_REPLOT "rep" |
|
91 #endif |
|
92 |
|
93 #ifndef GNUPLOT_COMMAND_SPLOT |
|
94 #define GNUPLOT_COMMAND_SPLOT "sp" |
|
95 #endif |
|
96 |
|
97 #ifndef GNUPLOT_COMMAND_USING |
|
98 #define GNUPLOT_COMMAND_USING "u" |
|
99 #endif |
|
100 |
|
101 #ifndef GNUPLOT_COMMAND_WITH |
|
102 #define GNUPLOT_COMMAND_WITH "w" |
|
103 #endif |
|
104 |
|
105 #ifndef GNUPLOT_COMMAND_TITLE |
|
106 #define GNUPLOT_COMMAND_TITLE "t" |
|
107 #endif |
|
108 |
597
|
109 static void |
|
110 open_plot_stream (void) |
|
111 { |
|
112 static int initialized = 0; |
|
113 |
|
114 if (! plot_stream.is_open ()) |
|
115 { |
943
|
116 initialized = 0; |
|
117 |
597
|
118 plot_line_count = 0; |
|
119 |
|
120 char *plot_prog = user_pref.gnuplot_binary; |
|
121 if (plot_prog) |
|
122 { |
|
123 plot_stream.open (plot_prog); |
|
124 if (! plot_stream.is_open ()) |
|
125 { |
|
126 warning ("plot: unable to open pipe to `%s'", |
|
127 plot_prog); |
|
128 |
|
129 if (strcmp (plot_prog, "gnuplot") != 0) |
|
130 { |
|
131 warning ("having trouble finding plotting program."); |
|
132 warning ("trying again with `gnuplot'"); |
|
133 goto last_chance; |
|
134 } |
|
135 } |
|
136 } |
|
137 else |
|
138 { |
|
139 last_chance: |
|
140 |
|
141 plot_stream.open ("gnuplot"); |
|
142 |
|
143 if (! plot_stream.is_open ()) |
|
144 error ("plot: unable to open pipe to `%s'", plot_prog); |
|
145 } |
|
146 } |
|
147 |
|
148 if (! initialized) |
|
149 { |
|
150 initialized = 1; |
|
151 plot_stream << "set data style lines\n"; |
943
|
152 |
|
153 if (gnuplot_terminal_type) |
|
154 plot_stream << "set term " << gnuplot_terminal_type << "\n"; |
597
|
155 } |
|
156 } |
|
157 |
|
158 static int |
|
159 send_to_plot_stream (const char *cmd) |
|
160 { |
1358
|
161 // From sighandlers.cc: |
|
162 |
597
|
163 extern int pipe_handler_error_count; |
|
164 |
|
165 if (! plot_stream.is_open ()) |
|
166 { |
|
167 open_plot_stream (); |
|
168 |
|
169 if (error_state) |
|
170 return -1; |
|
171 } |
|
172 |
930
|
173 int replot_len = strlen (GNUPLOT_COMMAND_REPLOT); |
|
174 int splot_len = strlen (GNUPLOT_COMMAND_SPLOT); |
|
175 int plot_len = strlen (GNUPLOT_COMMAND_PLOT); |
|
176 |
|
177 int is_replot = (strncmp (cmd, GNUPLOT_COMMAND_REPLOT, replot_len) == 0); |
|
178 int is_splot = (strncmp (cmd, GNUPLOT_COMMAND_SPLOT, splot_len) == 0); |
|
179 int is_plot = (strncmp (cmd, GNUPLOT_COMMAND_PLOT, plot_len) == 0); |
661
|
180 |
|
181 if (plot_line_count == 0 && is_replot) |
597
|
182 error ("replot: no previous plot"); |
|
183 else |
|
184 { |
|
185 plot_stream << cmd; |
930
|
186 |
661
|
187 if (! (is_replot || is_splot || is_plot) |
|
188 && plot_line_count > 0 |
|
189 && user_pref.automatic_replot) |
866
|
190 plot_stream << GNUPLOT_COMMAND_REPLOT << "\n"; |
930
|
191 |
597
|
192 plot_stream.flush (); |
|
193 pipe_handler_error_count = 0; |
|
194 } |
|
195 |
|
196 return 0; |
|
197 } |
|
198 |
581
|
199 // Plotting, eh? |
1
|
200 |
581
|
201 tree_plot_command::tree_plot_command (void) : tree_command () |
1
|
202 { |
524
|
203 range = 0; |
|
204 plot_list = 0; |
1
|
205 ndim = 0; |
|
206 } |
|
207 |
578
|
208 tree_plot_command::tree_plot_command (subplot_list *plt, int nd) |
581
|
209 : tree_command () |
1
|
210 { |
524
|
211 range = 0; |
1
|
212 plot_list = plt; |
|
213 ndim = nd; |
|
214 } |
|
215 |
578
|
216 tree_plot_command::tree_plot_command (subplot_list *plt, |
|
217 plot_limits *rng, int nd) |
581
|
218 : tree_command () |
1
|
219 { |
|
220 range = rng; |
|
221 plot_list = plt; |
|
222 ndim = nd; |
|
223 } |
|
224 |
|
225 tree_plot_command::~tree_plot_command (void) |
|
226 { |
|
227 delete range; |
|
228 delete plot_list; |
|
229 } |
|
230 |
578
|
231 void |
|
232 tree_plot_command::eval (void) |
1
|
233 { |
143
|
234 if (error_state) |
578
|
235 return; |
143
|
236 |
597
|
237 open_plot_stream (); |
|
238 |
1
|
239 ostrstream plot_buf; |
|
240 |
|
241 switch (ndim) |
|
242 { |
476
|
243 case 1: |
478
|
244 if (plot_line_count == 0) |
|
245 { |
|
246 if (plot_list) |
866
|
247 plot_buf << GNUPLOT_COMMAND_PLOT; |
478
|
248 else |
|
249 { |
|
250 ::error ("replot: must have something to plot"); |
578
|
251 return; |
478
|
252 } |
|
253 } |
|
254 else |
866
|
255 plot_buf << GNUPLOT_COMMAND_REPLOT; |
476
|
256 break; |
777
|
257 |
1
|
258 case 2: |
478
|
259 if (clear_before_plotting || plot_line_count == 0) |
|
260 { |
|
261 plot_line_count = 0; |
866
|
262 plot_buf << GNUPLOT_COMMAND_PLOT; |
478
|
263 } |
|
264 else |
930
|
265 plot_buf << GNUPLOT_COMMAND_REPLOT; |
1
|
266 break; |
777
|
267 |
1
|
268 case 3: |
930
|
269 if (clear_before_plotting || plot_line_count == 0) |
|
270 { |
|
271 plot_line_count = 0; |
|
272 plot_buf << GNUPLOT_COMMAND_SPLOT; |
|
273 } |
|
274 else |
|
275 plot_buf << GNUPLOT_COMMAND_REPLOT; |
1
|
276 break; |
777
|
277 |
1
|
278 default: |
771
|
279 gripe_2_or_3_dim_plot (); |
|
280 return; |
1
|
281 } |
|
282 |
524
|
283 if (range) |
478
|
284 { |
|
285 if (plot_line_count == 0) |
|
286 range->print (ndim, plot_buf); |
|
287 else |
|
288 warning ("can't specify new plot ranges with `replot' or while\ |
|
289 hold is on"); |
|
290 } |
1
|
291 |
191
|
292 if (error_state) |
578
|
293 return; |
191
|
294 |
591
|
295 if (plot_list) |
1
|
296 { |
591
|
297 int status = plot_list->print (ndim, plot_buf); |
578
|
298 |
771
|
299 if (error_state || status < 0) |
578
|
300 return; |
1
|
301 } |
|
302 |
|
303 plot_buf << "\n" << ends; |
|
304 |
1358
|
305 // Just testing... |
|
306 // char *message = plot_buf.str (); |
|
307 // cout << "[*]" << message << "[*]\n"; |
1
|
308 |
|
309 if (parametric_plot && ndim == 2) |
|
310 { |
|
311 warning ("can't make 2D parametric plot -- setting noparametric..."); |
|
312 send_to_plot_stream ("set noparametric\n"); |
|
313 char *message = plot_buf.str (); |
|
314 send_to_plot_stream (message); |
|
315 delete [] message; |
|
316 send_to_plot_stream ("set parametric\n"); |
|
317 } |
|
318 else |
|
319 { |
|
320 char *message = plot_buf.str (); |
|
321 send_to_plot_stream (message); |
|
322 delete [] message; |
|
323 } |
|
324 } |
|
325 |
591
|
326 void |
|
327 tree_plot_command::print_code (ostream& os) |
1
|
328 { |
591
|
329 print_code_indent (os); |
86
|
330 |
591
|
331 switch (ndim) |
1
|
332 { |
591
|
333 case 1: |
|
334 os << "replot"; |
|
335 break; |
777
|
336 |
591
|
337 case 2: |
|
338 os << "gplot"; |
|
339 break; |
777
|
340 |
591
|
341 case 3: |
|
342 os << "gsplot"; |
|
343 break; |
777
|
344 |
591
|
345 default: |
771
|
346 os << "<unkown plot command>"; |
591
|
347 break; |
1
|
348 } |
|
349 |
591
|
350 if (range) |
|
351 range->print_code (os); |
1
|
352 |
591
|
353 if (plot_list) |
|
354 plot_list->print_code (os); |
1
|
355 } |
|
356 |
578
|
357 plot_limits::plot_limits (void) |
1
|
358 { |
524
|
359 x_range = 0; |
|
360 y_range = 0; |
|
361 z_range = 0; |
1
|
362 } |
|
363 |
578
|
364 plot_limits::plot_limits (plot_range *xlim) |
1
|
365 { |
|
366 x_range = xlim; |
524
|
367 y_range = 0; |
|
368 z_range = 0; |
1
|
369 } |
|
370 |
578
|
371 plot_limits::plot_limits (plot_range *xlim, |
|
372 plot_range *ylim) |
1
|
373 { |
|
374 x_range = xlim; |
|
375 y_range = ylim; |
524
|
376 z_range = 0; |
1
|
377 } |
|
378 |
578
|
379 plot_limits::plot_limits (plot_range *xlim, |
|
380 plot_range *ylim, |
|
381 plot_range *zlim) |
1
|
382 { |
|
383 x_range = xlim; |
|
384 y_range = ylim; |
|
385 z_range = zlim; |
|
386 } |
|
387 |
578
|
388 plot_limits::~plot_limits (void) |
1
|
389 { |
|
390 delete x_range; |
|
391 delete y_range; |
|
392 delete z_range; |
|
393 } |
|
394 |
|
395 void |
578
|
396 plot_limits::print (int ndim, ostrstream& plot_buf) |
1
|
397 { |
|
398 if (ndim == 2 || ndim == 3) |
|
399 { |
524
|
400 if (x_range) |
1
|
401 x_range->print (plot_buf); |
|
402 else |
|
403 return; |
|
404 |
524
|
405 if (y_range) |
1
|
406 y_range->print (plot_buf); |
|
407 else |
|
408 return; |
|
409 } |
|
410 |
524
|
411 if (ndim == 3 && z_range) |
1
|
412 z_range->print (plot_buf); |
|
413 } |
|
414 |
591
|
415 void |
|
416 plot_limits::print_code (ostream& os) |
|
417 { |
|
418 if (x_range) |
|
419 x_range->print_code (os); |
|
420 |
|
421 if (y_range) |
|
422 y_range->print_code (os); |
|
423 |
|
424 if (z_range) |
|
425 z_range->print_code (os); |
|
426 } |
|
427 |
578
|
428 plot_range::plot_range (void) |
1
|
429 { |
524
|
430 lower = 0; |
|
431 upper = 0; |
1
|
432 } |
|
433 |
578
|
434 plot_range::plot_range (tree_expression *l, tree_expression *u) |
1
|
435 { |
|
436 lower = l; |
|
437 upper = u; |
|
438 } |
|
439 |
578
|
440 plot_range::~plot_range (void) |
1
|
441 { |
|
442 delete lower; |
|
443 delete upper; |
|
444 } |
|
445 |
|
446 void |
578
|
447 plot_range::print (ostrstream& plot_buf) |
1
|
448 { |
|
449 plot_buf << " ["; |
|
450 |
524
|
451 if (lower) |
1
|
452 { |
|
453 tree_constant lower_val = lower->eval (0); |
191
|
454 if (error_state) |
|
455 { |
240
|
456 ::error ("evaluating lower bound of plot range"); |
191
|
457 return; |
|
458 } |
|
459 else |
|
460 { |
629
|
461 double lo = lower_val.double_value (); |
191
|
462 plot_buf << lo; |
|
463 } |
1
|
464 } |
|
465 |
|
466 plot_buf << ":"; |
|
467 |
524
|
468 if (upper) |
1
|
469 { |
|
470 tree_constant upper_val = upper->eval (0); |
191
|
471 if (error_state) |
|
472 { |
240
|
473 ::error ("evaluating upper bound of plot range"); |
191
|
474 return; |
|
475 } |
|
476 else |
|
477 { |
629
|
478 double hi = upper_val.double_value (); |
191
|
479 plot_buf << hi; |
|
480 } |
1
|
481 } |
|
482 |
|
483 plot_buf << "]"; |
|
484 } |
|
485 |
591
|
486 void |
|
487 plot_range::print_code (ostream& os) |
|
488 { |
|
489 os << " ["; |
|
490 |
|
491 if (lower) |
|
492 lower->print_code (os); |
|
493 |
|
494 os << ":"; |
|
495 |
|
496 if (upper) |
|
497 upper->print_code (os); |
|
498 |
|
499 os << "]"; |
|
500 } |
|
501 |
578
|
502 subplot_using::subplot_using (void) |
1
|
503 { |
|
504 qualifier_count = 0; |
872
|
505 x[0] = x[1] = x[2] = x[3] = 0; |
524
|
506 scanf_fmt = 0; |
1
|
507 } |
|
508 |
872
|
509 subplot_using::subplot_using (tree_expression *fmt) : val (4, -1) |
1
|
510 { |
|
511 qualifier_count = 0; |
872
|
512 x[0] = x[1] = x[2] = x[3] = 0; |
1
|
513 scanf_fmt = fmt; |
|
514 } |
|
515 |
578
|
516 subplot_using::~subplot_using (void) |
1
|
517 { |
|
518 delete scanf_fmt; |
|
519 } |
|
520 |
578
|
521 subplot_using * |
|
522 subplot_using::set_format (tree_expression *fmt) |
1
|
523 { |
|
524 scanf_fmt = fmt; |
|
525 return this; |
|
526 } |
|
527 |
578
|
528 subplot_using * |
|
529 subplot_using::add_qualifier (tree_expression *t) |
1
|
530 { |
|
531 if (qualifier_count < 4) |
|
532 x[qualifier_count] = t; |
|
533 |
|
534 qualifier_count++; |
|
535 |
|
536 return this; |
|
537 } |
|
538 |
|
539 int |
872
|
540 subplot_using::eval (int ndim, int n_max) |
1
|
541 { |
|
542 if ((ndim == 2 && qualifier_count > 4) |
|
543 || (ndim == 3 && qualifier_count > 3)) |
|
544 return -1; |
|
545 |
872
|
546 if (qualifier_count > 0) |
|
547 val.resize (qualifier_count); |
|
548 |
1
|
549 for (int i = 0; i < qualifier_count; i++) |
|
550 { |
524
|
551 if (x[i]) |
1
|
552 { |
|
553 tree_constant tmp = x[i]->eval (0); |
191
|
554 if (error_state) |
|
555 { |
240
|
556 ::error ("evaluating plot using command"); |
191
|
557 return -1; |
|
558 } |
|
559 |
872
|
560 double val_tmp; |
1
|
561 if (tmp.is_defined ()) |
|
562 { |
872
|
563 val_tmp = tmp.double_value (); |
1
|
564 |
872
|
565 if (error_state) |
|
566 return -1; |
1
|
567 |
1086
|
568 if (xisnan (val_tmp)) |
|
569 { |
|
570 ::error ("NaN is invalid as a column specifier"); |
|
571 return -1; |
|
572 } |
|
573 |
872
|
574 int n = NINT (val_tmp); |
1086
|
575 |
322
|
576 if (n < 1 || n_max > 0 && n > n_max) |
1
|
577 { |
240
|
578 ::error ("using: column %d out of range", n); |
1
|
579 return -1; |
|
580 } |
|
581 else |
872
|
582 val.elem (i) = n; |
1
|
583 } |
|
584 else |
|
585 return -1; |
|
586 } |
|
587 else |
|
588 return -1; |
|
589 } |
|
590 |
524
|
591 if (scanf_fmt) |
1
|
592 warning ("ignoring scanf format in plot command"); |
|
593 |
872
|
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 |
1203
|
602 if (status < 0) |
872
|
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 |
1203
|
613 if (status < 0) |
872
|
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 { |
1358
|
849 // Should really try to look at data file to determine |
|
850 // n_max. Can't do much about other arbitrary gnuplot |
|
851 // commands though... |
872
|
852 |
|
853 int n_max = 0; |
|
854 |
591
|
855 file = tilde_expand (data.string_value ()); |
|
856 ifstream ftmp (file); |
|
857 if (ftmp) |
|
858 { |
|
859 plot_buf << " \"" << file << '"'; |
|
860 free (file); |
|
861 } |
|
862 else |
|
863 { |
|
864 free (file); |
|
865 file = 0; |
|
866 |
1358
|
867 // Opening as a file failed. Let's try passing it |
|
868 // along as a plot command. |
|
869 |
591
|
870 plot_buf << " " << data.string_value (); |
872
|
871 } |
|
872 |
999
|
873 if (using_clause) |
872
|
874 { |
999
|
875 int status = using_clause->print (ndim, n_max, plot_buf); |
872
|
876 if (status < 0) |
|
877 return -1; |
591
|
878 } |
|
879 } |
872
|
880 else |
591
|
881 { |
1358
|
882 // Eliminate the need for printing a using clause to |
|
883 // plot_buf. |
872
|
884 |
|
885 tree_constant tmp_data = extract_plot_data (ndim, data); |
|
886 |
|
887 if (tmp_data.is_defined ()) |
|
888 { |
|
889 switch (ndim) |
|
890 { |
|
891 case 2: |
|
892 file = save_in_tmp_file (tmp_data, ndim); |
|
893 break; |
777
|
894 |
872
|
895 case 3: |
|
896 file = save_in_tmp_file (tmp_data, ndim, |
|
897 parametric_plot); |
|
898 break; |
777
|
899 |
872
|
900 default: |
|
901 gripe_2_or_3_dim_plot (); |
|
902 break; |
|
903 } |
591
|
904 |
872
|
905 if (file) |
|
906 { |
|
907 mark_for_deletion (file); |
|
908 plot_buf << " \"" << file << '"'; |
|
909 } |
|
910 } |
591
|
911 } |
|
912 } |
|
913 else |
|
914 return -1; |
|
915 } |
|
916 else |
|
917 return -1; |
|
918 |
872
|
919 return 0; |
|
920 } |
591
|
921 |
872
|
922 int |
|
923 subplot::print (int ndim, ostrstream& plot_buf) |
|
924 { |
|
925 int status = handle_plot_data (ndim, plot_buf); |
|
926 |
|
927 if (status < 0) |
|
928 return -1; |
591
|
929 |
999
|
930 if (title_clause) |
591
|
931 { |
999
|
932 tree_constant tmp = title_clause->eval (0); |
610
|
933 if (! error_state && tmp.is_string ()) |
866
|
934 plot_buf << " " << GNUPLOT_COMMAND_TITLE << " " |
|
935 << '"' << tmp.string_value () << '"'; |
591
|
936 else |
|
937 { |
|
938 warning ("line title must be a string"); |
866
|
939 plot_buf << " " << GNUPLOT_COMMAND_TITLE << " " |
|
940 << '"' << "line " << plot_line_count << '"'; |
591
|
941 } |
|
942 } |
|
943 else |
866
|
944 plot_buf << " " << GNUPLOT_COMMAND_TITLE << " " |
|
945 << '"' << "line " << plot_line_count << '"'; |
591
|
946 |
999
|
947 if (style_clause) |
591
|
948 { |
999
|
949 int status = style_clause->print (plot_buf); |
591
|
950 if (status < 0) |
|
951 return -1; |
|
952 } |
|
953 |
|
954 return 0; |
|
955 } |
|
956 |
|
957 void |
|
958 subplot::print_code (ostream& os) |
|
959 { |
|
960 if (plot_data) |
592
|
961 { |
|
962 os << " "; |
|
963 plot_data->print_code (os); |
|
964 } |
591
|
965 |
999
|
966 if (using_clause) |
|
967 using_clause->print_code (os); |
591
|
968 |
999
|
969 if (title_clause) |
|
970 title_clause->print_code (os); |
591
|
971 |
999
|
972 if (style_clause) |
|
973 style_clause->print_code (os); |
591
|
974 } |
|
975 |
883
|
976 int |
591
|
977 subplot_list::print (int ndim, ostrstream& plot_buf) |
|
978 { |
|
979 int status = 0; |
|
980 |
|
981 for (Pix p = first (); p != 0; next (p)) |
|
982 { |
|
983 subplot *elt = this->operator () (p); |
|
984 |
|
985 plot_line_count++; |
|
986 |
|
987 if (p != first ()) |
|
988 plot_buf << ",\\\n "; |
|
989 |
|
990 status = elt->print (ndim, plot_buf); |
|
991 |
|
992 if (status < 0) |
|
993 break; |
|
994 } |
|
995 |
|
996 return status; |
|
997 } |
|
998 |
|
999 void |
|
1000 subplot_list::print_code (ostream& os) |
|
1001 { |
|
1002 Pix p = first (); |
|
1003 |
|
1004 while (p) |
|
1005 { |
|
1006 subplot *elt = this->operator () (p); |
|
1007 |
|
1008 next (p); |
|
1009 |
|
1010 if (elt) |
|
1011 { |
|
1012 elt->print_code (os); |
|
1013 |
|
1014 if (p) |
592
|
1015 os << ","; |
591
|
1016 } |
|
1017 } |
|
1018 } |
|
1019 |
524
|
1020 char * |
|
1021 save_in_tmp_file (tree_constant& t, int ndim, int parametric) |
|
1022 { |
662
|
1023 char *name = octave_tmp_file_name (); |
524
|
1024 if (name) |
|
1025 { |
|
1026 ofstream file (name); |
|
1027 if (file) |
|
1028 { |
|
1029 switch (ndim) |
|
1030 { |
|
1031 case 2: |
871
|
1032 save_ascii_data (file, t, 0, 1); |
524
|
1033 break; |
777
|
1034 |
524
|
1035 case 3: |
607
|
1036 save_three_d (file, t, parametric); |
524
|
1037 break; |
777
|
1038 |
524
|
1039 default: |
771
|
1040 gripe_2_or_3_dim_plot (); |
524
|
1041 break; |
|
1042 } |
|
1043 } |
|
1044 else |
|
1045 { |
|
1046 error ("couldn't open temporary output file `%s'", name); |
|
1047 name = 0; |
|
1048 } |
|
1049 } |
|
1050 return name; |
|
1051 } |
|
1052 |
|
1053 void |
|
1054 mark_for_deletion (const char *filename) |
|
1055 { |
|
1056 char *tmp = strsave (filename); |
|
1057 tmp_files.push (tmp); |
|
1058 } |
|
1059 |
|
1060 void |
|
1061 cleanup_tmp_files (void) |
|
1062 { |
|
1063 while (! tmp_files.empty ()) |
|
1064 { |
|
1065 char *filename = tmp_files.pop (); |
|
1066 unlink (filename); |
|
1067 delete [] filename; |
|
1068 } |
|
1069 } |
|
1070 |
|
1071 void |
|
1072 close_plot_stream (void) |
|
1073 { |
|
1074 if (plot_stream.is_open ()) |
|
1075 plot_stream.close (); |
|
1076 |
|
1077 plot_line_count = 0; |
|
1078 } |
|
1079 |
1328
|
1080 void |
|
1081 do_external_plotter_cd (const char *newdir) |
|
1082 { |
|
1083 if (plot_stream.is_open ()) |
|
1084 { |
|
1085 ostrstream plot_buf; |
|
1086 plot_buf << "cd \"" << newdir << "\"\n" << ends; |
|
1087 char *message = plot_buf.str (); |
|
1088 send_to_plot_stream (message); |
|
1089 delete [] message; |
|
1090 } |
|
1091 } |
|
1092 |
883
|
1093 DEFUN ("clearplot", Fclearplot, Sclearplot, 0, 0, |
|
1094 "clearplot (): clear the plot window") |
|
1095 { |
|
1096 Octave_object retval; |
|
1097 send_to_plot_stream ("clear\n"); |
930
|
1098 |
1358
|
1099 // XXX FIXME XXX -- instead of just clearing these things, it would |
|
1100 // be nice if we could reset things to a user-specified default |
|
1101 // state. |
930
|
1102 |
|
1103 send_to_plot_stream ("set title\n"); |
|
1104 send_to_plot_stream ("set xlabel\n"); |
|
1105 send_to_plot_stream ("set ylabel\n"); |
940
|
1106 send_to_plot_stream ("set nogrid\n"); |
1000
|
1107 send_to_plot_stream ("set nolabel\n"); |
940
|
1108 |
1358
|
1109 // This makes a simple `replot' not work after a `clearplot' command |
|
1110 // has been issued. |
940
|
1111 |
|
1112 plot_line_count = 0; |
930
|
1113 |
883
|
1114 return retval; |
|
1115 } |
|
1116 |
884
|
1117 DEFALIAS (clg, clearplot); |
|
1118 |
883
|
1119 DEFUN ("closeplot", Fcloseplot, Scloseplot, 0, 0, |
524
|
1120 "closeplot (): close the stream to plotter") |
|
1121 { |
|
1122 Octave_object retval; |
|
1123 close_plot_stream (); |
|
1124 return retval; |
|
1125 } |
|
1126 |
883
|
1127 DEFUN_TEXT ("hold", Fhold, Shold, 1, 0, |
524
|
1128 "hold [on|off]\n\ |
|
1129 \n\ |
|
1130 determine whether the plot window is cleared before the next line is\n\ |
|
1131 drawn. With no argument, toggle the current state.") |
|
1132 { |
|
1133 Octave_object retval; |
|
1134 |
|
1135 DEFINE_ARGV("hold"); |
|
1136 |
|
1137 switch (argc) |
|
1138 { |
|
1139 case 1: |
|
1140 clear_before_plotting = ! clear_before_plotting; |
|
1141 break; |
777
|
1142 |
524
|
1143 case 2: |
|
1144 if (strcasecmp (argv[1], "on") == 0) |
|
1145 clear_before_plotting = 0; |
|
1146 else if (strcasecmp (argv[1], "off") == 0) |
|
1147 clear_before_plotting = 1; |
|
1148 else |
|
1149 print_usage ("hold"); |
|
1150 break; |
777
|
1151 |
524
|
1152 default: |
|
1153 print_usage ("hold"); |
|
1154 break; |
|
1155 } |
|
1156 |
|
1157 DELETE_ARGV; |
|
1158 |
|
1159 return retval; |
|
1160 } |
|
1161 |
735
|
1162 DEFUN ("ishold", Fishold, Sishold, 0, 1, |
|
1163 "ishold\n\ |
|
1164 \n\ |
|
1165 Return 1 if hold is on, otherwise return 0.") |
|
1166 { |
|
1167 return (double) (! clear_before_plotting); |
|
1168 } |
|
1169 |
883
|
1170 DEFUN ("purge_tmp_files", Fpurge_tmp_files, Spurge_tmp_files, 0, 0, |
524
|
1171 "delete temporary data files used for plotting") |
|
1172 { |
|
1173 Octave_object retval; |
|
1174 cleanup_tmp_files (); |
|
1175 return retval; |
|
1176 } |
|
1177 |
883
|
1178 DEFUN_TEXT ("set", Fset, Sset, -1, 0, |
524
|
1179 "set [options]\n\ |
|
1180 \n\ |
|
1181 set plotting options") |
|
1182 { |
|
1183 Octave_object retval; |
|
1184 |
|
1185 DEFINE_ARGV("set"); |
|
1186 |
|
1187 ostrstream plot_buf; |
|
1188 |
|
1189 if (argc > 1) |
|
1190 { |
|
1191 if (almost_match ("parametric", argv[1], 3)) |
|
1192 parametric_plot = 1; |
|
1193 else if (almost_match ("noparametric", argv[1], 5)) |
|
1194 parametric_plot = 0; |
943
|
1195 else if (almost_match ("term", argv[1], 1)) |
|
1196 { |
|
1197 delete [] gnuplot_terminal_type; |
|
1198 ostrstream buf; |
|
1199 for (int i = 2; i < argc; i++) |
|
1200 buf << argv[i] << " "; |
|
1201 buf << "\n" << ends; |
|
1202 gnuplot_terminal_type = buf.str (); |
|
1203 } |
524
|
1204 } |
|
1205 |
|
1206 for (int i = 0; i < argc; i++) |
|
1207 plot_buf << argv[i] << " "; |
|
1208 |
|
1209 plot_buf << "\n" << ends; |
|
1210 |
|
1211 char *plot_command = plot_buf.str (); |
|
1212 send_to_plot_stream (plot_command); |
|
1213 |
|
1214 delete [] plot_command; |
|
1215 |
|
1216 DELETE_ARGV; |
|
1217 |
|
1218 return retval; |
|
1219 } |
|
1220 |
883
|
1221 DEFUN_TEXT ("show", Fshow, Sshow, -1, 0, |
524
|
1222 "show [options]\n\ |
|
1223 \n\ |
|
1224 show plotting options") |
|
1225 { |
|
1226 Octave_object retval; |
|
1227 |
|
1228 DEFINE_ARGV("show"); |
|
1229 |
|
1230 ostrstream plot_buf; |
|
1231 |
|
1232 for (int i = 0; i < argc; i++) |
|
1233 plot_buf << argv[i] << " "; |
|
1234 |
|
1235 plot_buf << "\n" << ends; |
|
1236 |
|
1237 char *plot_command = plot_buf.str (); |
|
1238 send_to_plot_stream (plot_command); |
|
1239 |
|
1240 delete [] plot_command; |
|
1241 |
|
1242 DELETE_ARGV; |
|
1243 |
|
1244 return retval; |
|
1245 } |
|
1246 |
1
|
1247 /* |
|
1248 ;;; Local Variables: *** |
|
1249 ;;; mode: C++ *** |
|
1250 ;;; page-delimiter: "^/\\*" *** |
|
1251 ;;; End: *** |
|
1252 */ |