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