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