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