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