Codebase list libgraphics-gnuplotif-perl / 696f089
[svn-inject] Installing original source of libgraphics-gnuplotif-perl Carlo U. Segre 14 years ago
11 changed file(s) with 1780 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 use strict;
1 use warnings;
2 use Module::Build;
3
4 my $builder = Module::Build->new(
5 module_name => 'Graphics::GnuplotIF',
6 license => 'perl',
7 dist_author => 'Dr.-Ing. Fritz Mehner <mehner@fh-swf.de>',
8 dist_version_from => 'lib/Graphics/GnuplotIF.pm',
9 requires => {
10 'Test::More' => 0,
11 'version' => 0,
12 },
13 add_to_cleanup => [ 'Graphics-GnuplotIF-*' ],
14 );
15
16 $builder->create_build_script();
0 Revision history for Perl extension GnuplotIF.
1
2 1.0 Thu Jul 28 11:00:14 2005
3 - original version; created by h2xs 1.23 with options
4 -XA -n GnuplotIF
5
6 1.1 Aug 27 2006
7 - method gnuplot_set_plot_titles added (thanks to Stephen Marshall)
8
9 1.2 Jun 06 2007
10 - tests improved
11
12 1.3 Oct 13 2007
13 - Module no longer dies if no display is available (allows batch mode).
14 - Method gnuplot_plot_xy_style (thanks to Georg Bauhaus)
15
16 1.3.1 Nov 10 2007
17 - All prints to pipes will be checked.
18 - New test: 8 pipes open in parallel.
19
20 1.4 Nov 28 2007
21 - Plot window can survive after gnuplot exits.
22 - Plot can be stored to a script file instead of beeing executed immediately.
23 - New method gnuplot_plot_3d makes 3-D-plots from arrays.
24 - New method gnuplot_hardcopy writes a plot into a file for printing.
25 - New method gnuplot_restore_terminal restores terminal settings before the last hardcopy.
26
27 1.5 June 11 2008
28 - Method gnuplot_plot_many() and gnuplot_plot_many_style for x-y-plots not sharing an x-axis.
29 - Most method calls can now be chained (thanks to Bruce Ravel for both).
30
0 Build.PL
1 Changes
2 MANIFEST
3 META.yml # Will be created by "make dist"
4 Makefile.PL
5 README
6 lib/Graphics/GnuplotIF.pm
7 t/00.load.t
8 t/pod-coverage.t
9 t/pod.t
10 t/GnuplotIF.t
0 --- #YAML:1.0
1 name: Graphics-GnuplotIF
2 version: 1.5
3 abstract: A dynamic Perl interface to gnuplot
4 license: ~
5 author:
6 - Dr.-Ing. Fritz Mehner <mehner@fh-swf.de>
7 generated_by: ExtUtils::MakeMaker version 6.44
8 distribution_type: module
9 requires:
10 Test::More: 0
11 version: 0
12 meta-spec:
13 url: http://module-build.sourceforge.net/META-spec-v1.3.html
14 version: 1.3
0 use strict;
1 use warnings;
2 use ExtUtils::MakeMaker;
3
4 WriteMakefile(
5 NAME => 'Graphics::GnuplotIF',
6 AUTHOR => 'Dr.-Ing. Fritz Mehner <mehner@fh-swf.de>',
7 VERSION_FROM => 'lib/Graphics/GnuplotIF.pm',
8 ABSTRACT_FROM => 'lib/Graphics/GnuplotIF.pm',
9 PL_FILES => {},
10 PREREQ_PM => {
11 'Test::More' => 0,
12 'version' => 0,
13 },
14 dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
15 clean => { FILES => 'Graphics-GnuplotIF-*' },
16 );
0 GnuplotIF version 1.5
1 =====================
2
3 GnuplotIF is a simple and easy to use Perl interface to gnuplot. gnuplot is a
4 freely available, command-driven graphical display tool for Unix. It compiles
5 and works quite well on a number of Unix flavours as well as other operating
6 systems. The following module enables sending display requests to gnuplot
7 through simple Perl subroutine calls.
8
9 GnuplotIF starts gnuplot as a separate process. The plot commands are send
10 through a pipe. The graphical output from gnuplot will be displayed
11 immediately.
12
13 Several independent plots can be started from one script. Each plot has its
14 own pipe. All pipes will be closed automatically when the script terminates.
15 The gnuplot processes terminate when the corresponding pipes are closed. Their
16 graphical output will now disappear.
17
18 GnuplotIF is similar to gnuplot_i , a C interface to gnuplot
19 ( http://ndevilla.free.fr/gnuplot/ ), and to gnuplot_i++ , a C++ interface
20 to gnuplot ( http://jijo.cjb.net/code/cc++ ).
21
22
23 INSTALLATION
24
25 To install this module type the following:
26
27 perl Makefile.PL
28 make
29 make test
30 make install
31
32 DEPENDENCIES
33
34 This module requires these other modules and libraries:
35
36 gnuplot ( http://sourceforge.net/projects/gnuplot ) must be installed.
37
38 The module IO::Handle is used to handle output pipes.
39 Your operating system must support pipes, of course.
40
41 The environment variable DISPLAY is checked for the display.
42
43 COPYRIGHT AND LICENCE
44
45 Copyright (C) 2005-2008 by Fritz Mehner
46
47 This module is free software; you can redistribute it and/or modify it under
48 the same terms as Perl itself. See perlartistic. This program is distributed
49 in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
50 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
51
52
0 #
1 #===============================================================================
2 #
3 # FILE: GnuplotIF.pm
4 #
5 # DESCRIPTION: A simple and easy to use Perl interface to gnuplot.
6 # (see POD below)
7 # FILES: ---
8 # BUGS: ---
9 # NOTES: ---
10 # AUTHOR: Dr.-Ing. Fritz Mehner (Mn), <mehner@fh-swf.de>
11 # COMPANY: FH Suedwestfalen, Iserlohn
12 # VERSION: see $VERSION below
13 # CREATED: 16.07.2005 13:43:11 CEST
14 # REVISION: $Id: GnuplotIF.pm,v 1.13 2008/06/11 08:07:08 mehner Exp $
15 #===============================================================================
16
17 package Graphics::GnuplotIF;
18
19 use strict;
20 use warnings;
21 use Carp;
22 use IO::Handle;
23
24 our $VERSION = '1.5'; # version number
25
26 use base qw(Exporter);
27 use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);
28
29 # Symbols to be exported by default
30 @EXPORT_OK = ( 'GnuplotIF' );
31
32 #---------------------------------------------------------------------------
33 # Code common to gnuplot_plot_xy and gnuplot_plot_y to allow user-specified
34 # titles set by gnuplot_set_plot_titles. This code assumes the plot titles
35 # were all set in the command to the literal text "<PLOT_TITLE>", without any
36 # surrounding quotes. This function replaces that text.
37 #---------------------------------------------------------------------------
38 my $private_apply_plot_titles = sub {
39 my ($self, $cmd_ref) = @_;
40 my $default_plot_title = q{-}; # Title if user did not specify one
41 if (defined $self->{plot_titles} ) {
42 # Substitute each plot title sequentially with the user-supplied value
43 for my $plot_title (@{$self->{plot_titles}}) {
44 if ( !defined $plot_title ) {
45 $plot_title = $default_plot_title;
46 }
47 ${$cmd_ref} =~ s/title <PLOT_TITLE>/title "$plot_title"/;
48 }
49 }
50
51 # Substitute any plot titles we did not already catch globally
52 ${$cmd_ref} =~ s/title <PLOT_TITLE>/title "$default_plot_title"/g;
53 };
54
55 #---------------------------------------------------------------------------
56 # Code generates a file comment for a plot script.
57 #---------------------------------------------------------------------------
58 my $private_plot_script_header = sub {
59 my ( $self ) = @_;
60 my $localtime = scalar localtime;
61
62 my $comment;
63 ($comment = <<"END") =~ s/^\s+//gm;
64 #
65 # This file is a GNUPLOT plot script.
66 # It was generated automatically by '${0}'
67 # using the Graphics::GnuplotIF extension to perl.
68 # Creation time : ${localtime}
69 #
70 END
71
72 $self->gnuplot_cmd( $comment );
73 return;
74 };
75
76 #---------------------------------------------------------------------------
77 # warn if there is no graphic display
78 #---------------------------------------------------------------------------
79 if ( ! $ENV{'DISPLAY'} ) {
80 warn "Graphics::GnuplotIF : cannot find environment variable DISPLAY \n"
81 }
82
83 #=== FUNCTION ================================================================
84 # NAME: new
85 # DESCRIPTION: constructor
86 # PARAMETER 1: anonymous hash containing some plot parameter (defaults shown):
87 # style => "lines"
88 # title => "",
89 # xlabel => "",
90 # ylabel => "",
91 # xrange => [],
92 # yrange => [],
93 # scriptfile => "",
94 # persist => 0,
95 # objectname => "",
96 # RETURNS: object reference
97 #===============================================================================
98 {
99 my $object_number = 0; # number of objects created
100
101 sub new {
102
103 my ( $class, %args ) = @_;
104 my $IOHANDLE;
105
106 my $self = {
107 style => 'lines',
108 title => q{},
109 xlabel => q{},
110 ylabel => q{},
111 xrange => [],
112 yrange => [],
113 plot_titles => [],
114 scriptfile => q{},
115 persist => 0,
116 objectname => q{},
117 silent_pause => 1,
118 %args,
119 __pausetime => -1,
120 __pausemess => q{},
121 __output_type => q{},
122 __objectnumber=> ++$object_number,
123 __plotnumber => 1,
124 __error_log => q{},
125 };
126
127 bless $self, $class ;
128
129 # let plot windows survive after gnuplot exits
130 my $persist = q{};
131 if ( $self->{persist} == 1 ) {
132 $persist = '-persist';
133 }
134
135 $self->{__error_log} = ".gnuplot.${$}.${object_number}.stderr.log";
136
137 if ( $self->{scriptfile} eq q{} ) {
138 $self->{__output_type} = 'pipe';
139 open $IOHANDLE, '|- ', "gnuplot ${persist} 2> $self->{__error_log}"
140 or die "\n$0 : failed to open pipe to \"gnuplot\" : $!\n";
141 $IOHANDLE->autoflush(1);
142 }
143 else {
144 $self->{__output_type} = 'file';
145 open $IOHANDLE, '>', $self->{scriptfile}
146 or die "\n$0 : failed to open file \"$self->{scriptfile}\" : $!\n";
147 }
148 $self->{__iohandle} = $IOHANDLE;
149
150 $self->$private_plot_script_header();
151 $self->gnuplot_set_style ( $self->{style } );
152 $self->gnuplot_set_title ( $self->{title } );
153 $self->gnuplot_set_xlabel( $self->{xlabel} );
154 $self->gnuplot_set_ylabel( $self->{ylabel} );
155 $self->gnuplot_set_xrange( @{$self->{xrange}} );
156 $self->gnuplot_set_yrange( @{$self->{yrange}} );
157
158 return $self;
159 } # ---------- end of subroutine new ----------
160 }
161
162 #=== CLASS METHOD ============================================================
163 # NAME: GnuplotIF
164 # PURPOSE: constructor - short form
165 # PARAMETERS: see new
166 #===============================================================================
167 sub GnuplotIF {
168 my @args = @_;
169 return __PACKAGE__->new(@args);
170 } # ---------- end of subroutine GnuplotIF ----------
171
172 #=== CLASS METHOD ============================================================
173 # NAME: DESTROY
174 # PURPOSE: destructor - close pipe or file
175 # PARAMETERS: ---
176 #===============================================================================
177 sub DESTROY {
178 my $self = shift;
179 #---------------------------------------------------------------------------
180 # close pipe to gnuplot / close the script file
181 #---------------------------------------------------------------------------
182 if ( !close $self->{__iohandle} ) {
183 if ( $self->{__output_type} eq 'pipe' ) {
184 print { *STDERR } "Graphics::GnuplotIF (object $self->{__objectnumber}): "
185 ."problem closing communication to gnuplot\n";
186 }
187 if ( $self->{__output_type} eq 'file' ) {
188 print { *STDERR } "Graphics::GnuplotIF (object $self->{__objectnumber}): "
189 ."problem closing file $self->{scriptfile}\n";
190 }
191 }
192 #---------------------------------------------------------------------------
193 # remove empty error logfiles, if any
194 #---------------------------------------------------------------------------
195 my @stat = stat $self->{__error_log};
196
197 if ( defined $stat[7] && $stat[7]==0 ) {
198 unlink $self->{__error_log}
199 or croak "Couldn't unlink $self->{__error_log}: $!"
200 }
201 return;
202 } # ---------- end of subroutine DESTROY ----------
203
204 #=== CLASS METHOD ============================================================
205 # NAME: gnuplot_set_style
206 # PURPOSE: Sets one of the allowed line styles in a plot command.
207 # PARAMETERS: plotstyle (string)
208 # RETURNS: ---
209 # SEE ALSO: new()
210 #===============================================================================
211 {
212 my %linestyles = # allowed line styles
213 (
214 boxes => q{},
215 dots => q{},
216 filledcurves=> q{},
217 fsteps => q{},
218 histeps => q{},
219 impulses => q{},
220 lines => q{},
221 linespoints => q{},
222 points => q{},
223 steps => q{},
224 );
225
226 sub gnuplot_set_style {
227 my $self = shift;
228 my $style = shift;
229 if ( defined $style && exists $linestyles{$style} ) {
230 $self->{style} = $style;
231 }
232 return;
233 } # ---------- end of subroutine gnuplot_set_style ----------
234 }
235
236 #=== CLASS METHOD ============================================================
237 # NAME: gnuplot_plot_y
238 # PURPOSE: Plot one or more arrays over 0, 1, 2, 3, ...
239 # PARAMETERS: array reference(s)
240 # RETURNS: ---
241 #===============================================================================
242 sub gnuplot_plot_y {
243 my ( $self, @yref ) = @_;
244 my $parnr = 0;
245 my $cmd = " '-' with $self->{style} title <PLOT_TITLE>,\\\n" x (scalar @yref);
246 $cmd =~ s/,\\$//s;
247 $self->$private_apply_plot_titles(\$cmd); # Honor gnuplot_set_plot_titles
248 return $self if $cmd eq q{};
249
250 $self->gnuplot_cmd( "plot \\\n$cmd\n" );
251
252 foreach my $item ( @yref ) {
253 $parnr++;
254 die "Graphics::GnuplotIF (object $self->{__objectnumber}): gnuplot_plot_y : $parnr. "
255 ."argument not an array reference\n"
256 if ref($item) ne 'ARRAY';
257 $self->gnuplot_cmd( join( "\n", @{$item}), 'e' );
258 } # ----- end foreach -----
259 $self->{__plotnumber}++;
260 return $self;
261 } # ---------- end of subroutine gnuplot_plot_y ----------
262
263 #=== CLASS METHOD ============================================================
264 # NAME: gnuplot_plot_xy
265 # PURPOSE: x-y-plot(s)
266 # PARAMETERS: 1. array reference : x-values
267 # 2. array reference : y-values
268 # ...
269 # RETURNS: ---
270 # DESCRIPTION: Takes two or more array references. The first array is assumed
271 # to contain the x-values for the following function values.
272 #===============================================================================
273 sub gnuplot_plot_xy {
274 my ( $self, $xref, @yref ) = @_;
275 my $parnr = 1;
276 my $cmd = " '-' using 1:2 with $self->{style} title <PLOT_TITLE>,\\\n" x (scalar @yref);
277 $cmd =~ s/,\\\n$//s;
278 $self->$private_apply_plot_titles(\$cmd); # Honor gnuplot_set_plot_titles
279 return $self if $cmd eq q{};
280
281 $self->gnuplot_cmd( "plot \\\n$cmd\n" );
282
283 die "Graphics::GnuplotIF (object $self->{__objectnumber}): gnuplot_plot_xy : $parnr. "
284 ."argument not an array reference\n"
285 if ref($xref) ne 'ARRAY';
286
287 foreach my $j ( 0..$#yref ) {
288 $parnr++;
289 die "Graphics::GnuplotIF (object $self->{__objectnumber}): gnuplot_plot_xy - "
290 ."$parnr. argument not an array reference\n"
291 if ref($yref[$j]) ne 'ARRAY';
292
293 # there may be lesser y-values than x-values
294
295 my $min = $#{$xref} < $#{$yref[$j]} ? $#{$xref} : $#{$yref[$j]};
296 foreach my $i ( 0..$min ) {
297 $self->gnuplot_cmd( "$$xref[$i] $yref[$j]->[$i]" );
298 }
299 $self->gnuplot_cmd( 'e' );
300 }
301 $self->{__plotnumber}++;
302 return $self;
303 } # ---------- end of subroutine gnuplot_plot_xy ----------
304
305 #=== CLASS METHOD ============================================================
306 # NAME: gnuplot_plot_many
307 # PURPOSE: x-y-plot(s) not sharing an x-axis
308 # PARAMETERS: 1. array reference1 : x-values
309 # 2. array reference1 : y-values
310 # (3. array reference2 : x-values)
311 # (4. array reference2 : y-values)
312 # ...
313 # RETURNS: ---
314 # DESCRIPTION: Takes pairs of array references. The first array in each pair
315 # is assumed to contain the x-values and the second pair is
316 # assumed to contain y-values
317 #===============================================================================
318 sub gnuplot_plot_many {
319 my ( $self, @array_refs ) = @_;
320 my $parnr = 0;
321 my $cmd = " '-' using 1:2 with $self->{style} title <PLOT_TITLE>,\\\n" x ((scalar @array_refs)/2);
322 $cmd =~ s/,\\\n$//s;
323 $self->$private_apply_plot_titles(\$cmd); # Honor gnuplot_set_plot_titles
324 return $self if $cmd eq q{};
325
326 $self->gnuplot_cmd( "plot \\\n$cmd\n" );
327
328 while (@array_refs) {
329 my $xxr = shift @array_refs;
330 $parnr++;
331 die "Graphics::GnuplotIF (object $self->{__objectnumber}): gnuplot_plot_many - "
332 ."$parnr. argument not an array reference\n"
333 if ref($xxr) ne 'ARRAY';
334 my $yyr = shift @array_refs ;
335 $parnr++;
336 die "Graphics::GnuplotIF (object $self->{__objectnumber}): gnuplot_plot_many - "
337 ."$parnr. argument not an array reference\n"
338 if ref($yyr) ne 'ARRAY';
339
340 # there may be fewer y-values than x-values
341
342 my $min = $#{$xxr} < $#{$yyr} ? $#{$xxr} : $#{$yyr};
343 foreach my $i ( 0..$min ) {
344 $self->gnuplot_cmd( "$$xxr[$i] $$yyr[$i]" );
345 }
346 $self->gnuplot_cmd( 'e' );
347 }
348 $self->{__plotnumber}++;
349 return $self;
350 } # ---------- end of subroutine gnuplot_plot_many ----------
351
352 #=== CLASS METHOD ============================================================
353 # NAME: gnuplot_plot_xy_style
354 # PURPOSE: x-y-plot(s) with each graph using individual settings
355 # PARAMETERS: 1. array reference : x-values
356 # 2. hash reference : (y-values, y-style)
357 # ...
358 # RETURNS: ---
359 # DESCRIPTION: Takes one array reference and one or more hash references.
360 # The first array is assumed to contain the x-values for the
361 # following function values. The following hashes are assumed
362 # to contain pairs of values and settings.
363 #===============================================================================
364 sub gnuplot_plot_xy_style {
365 my ( $self, $xref, @yref ) = @_;
366 my $parnr = 1;
367 my ( $cmd, @cmd );
368
369 foreach my $j ( 0..$#yref ) {
370 die "Graphics::GnuplotIF (object $self->{__objectnumber}): gnuplot_plot_xy_style - "
371 .($parnr + $j + 1).". argument not a suitable hash reference\n"
372 if ! (ref($yref[$j]) eq 'HASH'
373 && exists $yref[$j]->{'style_spec'} && exists $yref[$j]->{'y_values'});
374
375 push @cmd, " '-' using 1:2 with $yref[$j]->{'style_spec'} title <PLOT_TITLE>";
376 }
377 $cmd = join ", \\\n", @cmd;
378 $self->$private_apply_plot_titles(\$cmd); # Honor gnuplot_set_plot_titles
379 return $self if $cmd eq q{};
380
381 $self->gnuplot_cmd( "plot \\\n$cmd" );
382
383 die "Graphics::GnuplotIF (object $self->{__objectnumber}): gnuplot_plot_xy_style : $parnr. "
384 ."argument not an array reference\n"
385 if ref($xref) ne 'ARRAY';
386
387 foreach my $j ( 0..$#yref ) {
388 $parnr++;
389 die "Graphics::GnuplotIF (object $self->{__objectnumber}): gnuplot_plot_xy_style - "
390 ."$parnr. argument is missing an array reference\n"
391 if ref($yref[$j]->{'y_values'}) ne 'ARRAY';
392
393 # there may be lesser y-values than x-values
394
395 my $min = $#{$xref} < $#{$yref[$j]->{'y_values'}}
396 ? $#{$xref}
397 : $#{$yref[$j]->{'y_values'}};
398 foreach my $i ( 0..$min ) {
399 $self->gnuplot_cmd( "$$xref[$i] $yref[$j]->{'y_values'}->[$i]" );
400 }
401 $self->gnuplot_cmd( 'e' );
402 }
403 $self->{__plotnumber}++;
404 return $self;
405 } # ---------- end of subroutine gnuplot_plot_xy_style ----------
406
407 #=== CLASS METHOD ============================================================
408 # NAME: gnuplot_plot_many_style
409 # PURPOSE: x-y-plot(s) not sharing an x-axis using individual settings
410 # PARAMETERS: 1. hash reference1 : (x-values, y-values, y-style)
411 # 2. hash reference2 : (x-values, y-values, y-style)
412 # ...
413 # RETURNS: ---
414 # DESCRIPTION: Takes array of hash references. The hashes are assumed
415 # to contain x- and y-values and settings.
416 #===============================================================================
417 sub gnuplot_plot_many_style {
418 my ( $self, @hash_refs ) = @_;
419 my $parnr = 0;
420 my ( $cmd, @cmd );
421
422 foreach my $rh (@hash_refs) {
423 $parnr++;
424 die "Graphics::GnuplotIF (object $self->{__objectnumber}): gnuplot_plot_many_style - "
425 .($parnr).". argument not a suitable hash reference\n"
426 if ! ( ref($rh) eq 'HASH'
427 && exists $rh->{'style_spec'}
428 && exists $rh->{'x_values'}
429 && exists $rh->{'y_values'}
430 );
431 my $style = $rh->{'style_spec'};
432 push @cmd, " '-' using 1:2 with $style title <PLOT_TITLE>";
433 };
434 $cmd = join ", \\\n", @cmd;
435 $self->$private_apply_plot_titles(\$cmd); # Honor gnuplot_set_plot_titles
436 return $self if $cmd eq q{};
437 $self->gnuplot_cmd( "plot \\\n$cmd\n" );
438
439 $parnr = 0;
440 foreach my $rh (@hash_refs) {
441 my $xref = $rh->{'x_values'};
442 my $yref = $rh->{'y_values'};
443 $parnr++;
444 die "Graphics::GnuplotIF (object $self->{__objectnumber}): gnuplot_plot_many_style - "
445 ."$parnr. 'x_values' argument not an array reference\n"
446 if ref($xref) ne 'ARRAY';
447 die "Graphics::GnuplotIF (object $self->{__objectnumber}): gnuplot_plot_many_style - "
448 ."$parnr. 'y_values' argument not an array reference\n"
449 if ref($yref) ne 'ARRAY';
450
451 # there may be fewer y-values than x-values
452 my $min = $#{$xref} < $#{$yref} ? $#{$xref} : $#{$yref};
453 foreach my $i ( 0..$min ) {
454 $self->gnuplot_cmd( "$$xref[$i] $$yref[$i]" );
455 }
456 $self->gnuplot_cmd( 'e' );
457 }
458 $self->{__plotnumber}++;
459 return $self;
460 } # ---------- end of subroutine gnuplot_plot_many_style ----------
461
462 #=== CLASS METHOD ============================================================
463 # NAME: gnuplot_plot_equation
464 # PURPOSE: Plot one or more functions described by strings.
465 # PARAMETERS: strings describing functions
466 # RETURNS: ---
467 #===============================================================================
468 sub gnuplot_plot_equation {
469 my ( $self, @equations ) = @_;
470 my $leftside;
471 my @leftside;
472
473 foreach my $equ ( @equations ) {
474 $self->gnuplot_cmd( "$equ" );
475 ( $leftside ) = split /=/, $equ;
476 push @leftside, $leftside;
477 } # ----- end foreach -----
478 @leftside = map {$_." with $self->{style}"} @leftside;
479 $leftside = join ', ', @leftside;
480 return $self if $leftside eq q{};
481 $self->gnuplot_cmd( "plot $leftside" );
482 $self->{__plotnumber}++;
483 return $self;
484 } # ---------- end of subroutine gnuplot_plot_equation ----------
485
486 #=== CLASS METHOD ============================================================
487 # NAME: gnuplot_plot_3d
488 # PURPOSE: Draw 3-d plots.
489 # PARAMETERS: Reference to a 2-D-matrix containing the z-values.
490 # RETURNS: ---
491 #===============================================================================
492 sub gnuplot_plot_3d {
493 my ( $self, $arrayref ) = @_;
494 my $parnr = 0;
495 my $cmd = " '-' matrix with $self->{style} title <PLOT_TITLE>," ;
496 $cmd =~ s/,$//;
497 $self->$private_apply_plot_titles(\$cmd); # Honor gnuplot_set_plot_titles
498 return $self if $cmd eq q{};
499
500 $self->gnuplot_cmd( "splot $cmd" );
501
502 die "Graphics::GnuplotIF (object $self->{__objectnumber}): gnuplot_plot_3d : "
503 ."argument not an array reference\n"
504 if ref($arrayref) ne 'ARRAY';
505
506 foreach my $i ( @{$arrayref} ) {
507 $self->gnuplot_cmd( join q{ }, @{$i} ) ;
508 }
509 $self->gnuplot_cmd( "\ne" );
510
511 $self->{__plotnumber}++;
512 return $self;
513 } # ---------- end of subroutine gnuplot_plot_3d ----------
514
515 #=== CLASS METHOD ============================================================
516 # NAME: gnuplot_pause
517 # PURPOSE: Wait a specified amount of time.
518 # PARAMETERS: 1. parameter (optional): time value (seconds):
519 # -1 do not wait
520 # 0 wait for a carriage return
521 # >0 wait the specified number of seconds
522 # 2. parameter (optional): text
523 # message to display
524 # RETURNS: ---
525 #===============================================================================
526 sub gnuplot_pause {
527 my ( $self, $pause, $message ) = @_;
528 if ( defined $pause && $pause =~ m/[+-]?(\d+|\d+\.\d*|\d*\.\d+)/x ) {
529 $self->{__pausetime} = $pause;
530 }
531 if ( defined $message && $message ne q{} ) {
532 $self->{__pausemess} = "\"$message\"";
533 }
534 my $msg0 = "Graphics::GnuplotIF (object $self->{__objectnumber}): $self->{__pausemess} -- ";
535 my $msg1 = "hit RETURN to continue \n";
536 my $msg2 = "wait $self->{__pausetime} second(s) \n";
537 if ( $self->{__pausetime} == 0 ) {
538 print "$msg0$msg1";
539 my $dummy = <>; # hit RETURN to go on
540 }
541 elsif ( $self->{__pausetime} < 0 ) {
542 $self->gnuplot_cmd("\n");
543 }
544 else {
545 if ( $self->{silent_pause} == 1 ) {
546 print "$msg0$msg2";
547 }
548 $self->gnuplot_cmd( "pause $self->{__pausetime} \n" );
549 }
550 return $self;
551 } # ---------- end of subroutine gnuplot_pause ----------
552
553 #=== CLASS METHOD ============================================================
554 # NAME: gnuplot_cmd
555 # PURPOSE: Pass on one or more Gnuplot commands.
556 # PARAMETERS: string(s)
557 # RETURNS: ---
558 #===============================================================================
559 sub gnuplot_cmd {
560 my ($self, @commands) = @_;
561 @commands = map {$_."\n"} @commands;
562 print { $self->{__iohandle}} @commands
563 or croak "Couldn't write to pipe: $!";
564 return $self;
565 } # ---------- end of subroutine gnuplot_cmd ----------
566
567 #=== CLASS METHOD ============================================================
568 # NAME: gnuplot_hardcopy
569 # PURPOSE: Write a plot into a file.
570 # PARAMETERS: 1. file name
571 # 2. gnuplot terminal type
572 # 3. terminal settings (optional)
573 # RETURNS: ---
574 #===============================================================================
575 sub gnuplot_hardcopy {
576 my ($self, $filename, $terminal, @keywords) = @_;
577
578 # remember the current terminal including its settings
579 $self->gnuplot_cmd( 'set terminal push' );
580
581 my $set_terminal = "set terminal $terminal @keywords\n";
582 my $set_output = "set output \"$filename\"\n";
583 $self->gnuplot_cmd( $set_terminal, $set_output );
584 return $self;
585 } # ---------- end of subroutine gnuplot_hardcopy ----------
586
587 #=== CLASS METHOD ============================================================
588 # NAME: gnuplot_restore_terminal
589 # PURPOSE: Restore the terminal settings before the last hardcopy.
590 # PARAMETERS: ---
591 # RETURNS: ---
592 #===============================================================================
593 sub gnuplot_restore_terminal {
594 my ($self) = @_;
595 $self->gnuplot_cmd( 'set output', 'set terminal pop' );
596 return $self;
597 } # ---------- end of subroutine gnuplot_restore_terminal ----------
598
599 #=== CLASS METHOD ============================================================
600 # NAME: gnuplot_set_plot_titles
601 # PURPOSE: Sets the list of titles used in the key.
602 # PARAMETERS: array of titles
603 # RETURNS: ---
604 #===============================================================================
605 sub gnuplot_set_plot_titles {
606 my ( $self, @user_plot_titles ) = @_;
607 my @plot_titles = @user_plot_titles;
608 $self->{plot_titles} = \@plot_titles;
609 return $self;
610 }
611
612 #=== CLASS METHOD ============================================================
613 # NAME: gnuplot_reset
614 # PURPOSE: Set all options set with the set command to their
615 # gnuplot default values.
616 # PARAMETERS: ---
617 # RETURNS: ---
618 #===============================================================================
619 sub gnuplot_reset {
620 my ($self) = @_;
621 $self->{plot_titles} = undef;
622 $self->gnuplot_cmd( 'reset' );
623 return $self;
624 } # ---------- end of subroutine gnuplot_reset ----------
625
626 #=== CLASS METHOD ============================================================
627 # NAME: gnuplot_set_title
628 # PURPOSE: Sets the plot title.
629 # PARAMETERS: title (string)
630 # RETURNS: ---
631 #===============================================================================
632 sub gnuplot_set_title {
633 my ( $self, $title ) = @_;
634 if ( defined $title ) {
635 $self->gnuplot_cmd( "set title \"$title\"" );
636 };
637 return $self;
638 } # ---------- end of subroutine gnuplot_set_title ----------
639
640 #=== CLASS METHOD ============================================================
641 # NAME: gnuplot_set_xlabel
642 # PURPOSE: Sets the x axis label.
643 # PARAMETERS: string
644 # RETURNS: ---
645 #===============================================================================
646 sub gnuplot_set_xlabel {
647 my ( $self, $xlabel ) = @_;
648 if ( defined $xlabel ) {
649 $self->gnuplot_cmd( "set xlabel \"$xlabel\"" );
650 };
651 return $self;
652 } # ---------- end of subroutine gnuplot_set_xlabel ----------
653
654 #=== CLASS METHOD ============================================================
655 # NAME: gnuplot_set_ylabel
656 # PURPOSE: Sets the y axis label.
657 # PARAMETERS: string
658 # RETURNS: ---
659 #===============================================================================
660 sub gnuplot_set_ylabel {
661 my ( $self, $ylabel ) = @_;
662 if ( defined $ylabel ) {
663 $self->gnuplot_cmd( "set ylabel \"$ylabel\"" );
664 };
665 return $self;
666 } # ---------- end of subroutine gnuplot_set_ylabel ----------
667
668 #=== CLASS METHOD ============================================================
669 # NAME: gnuplot_set_xrange
670 # PURPOSE: Sets the horizontal range that will be displayed.
671 # PARAMETERS: 1. parameter: range, left value
672 # 2. parameter: range, right value
673 # RETURNS: ---
674 #===============================================================================
675 sub gnuplot_set_xrange {
676 my ( $self, $xleft, $xright ) = @_;
677 if ( defined $xleft && defined $xright ) {
678 $self->gnuplot_cmd( "set xrange [ $xleft : $xright ]" );
679 }
680 return $self;
681 } # ---------- end of subroutine gnuplot_set_xrange ----------
682
683 #=== CLASS METHOD ============================================================
684 # NAME: gnuplot_set_yrange
685 # PURPOSE: Sets the vertical range that will be displayed.
686 # PARAMETERS: 1. parameter: range, low value
687 # 2. parameter: range, high value
688 # RETURNS: ---
689 #===============================================================================
690 sub gnuplot_set_yrange {
691 my ( $self, $yleft, $yright ) = @_;
692 if ( defined $yleft && defined $yright ) {
693 $self->gnuplot_cmd( "set yrange [ $yleft : $yright ]" );
694 }
695 return $self;
696 } # ---------- end of subroutine gnuplot_set_yrange ----------
697
698 #=== CLASS METHOD ============================================================
699 # NAME: gnuplot_get_plotnumber
700 # PURPOSE: Get the (internal) plot number
701 # PARAMETERS: ---
702 # RETURNS: object number
703 #===============================================================================
704 sub gnuplot_get_plotnumber {
705 my ( $self ) = @_;
706 return $self->{__plotnumber};
707 } # ---------- end of subroutine gnuplot_get_plotnumber ----------
708
709 #=== CLASS METHOD ============================================================
710 # NAME: gnuplot_get_object_id
711 # PURPOSE: Get the (internal) object number
712 # PARAMETERS: ---
713 # RETURNS: object number
714 #===============================================================================
715 sub gnuplot_get_object_id {
716 my ( $self ) = @_;
717 if ( wantarray ) {
718 return ( $self->{__objectnumber}, $self->{objectname} );
719 }
720 else {
721 return $self->{__objectnumber};
722 }
723 } # ---------- end of subroutine gnuplot_get_object_id ----------
724
725 END { } # module clean-up code
726
727 1;
728
729 #__END__
730 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731 # Module Documentation
732 #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
733
734 =head1 NAME
735
736 Graphics::GnuplotIF - A dynamic Perl interface to gnuplot
737
738 =head1 VERSION
739
740 This documentation refers to Graphics::GnuplotIF version 1.4
741
742 =head1 SYNOPSIS
743
744 use Graphics::GnuplotIF qw(GnuplotIF);
745
746 my @x = ( -2, -1.50, -1, -0.50, 0, 0.50, 1, 1.50, 2 ); # x values
747 my @y1 = ( 4, 2.25, 1, 0.25, 0, 0.25, 1, 2.25, 4 ); # function 1
748 my @y2 = ( 2, 0.25, -1, -1.75, -2, -1.75, -1, 0.25, 2 ); # function 2
749
750 my $plot1 = Graphics::GnuplotIF->new(title => "line", style => "points");
751
752 $plot1->gnuplot_plot_y( \@x ); # plot 9 points over 0..8
753
754 $plot1->gnuplot_pause( ); # hit RETURN to continue
755
756 $plot1->gnuplot_set_title( "parabola" ); # new title
757 $plot1->gnuplot_set_style( "lines" ); # new line style
758 $plot1->gnuplot_plot_xy( \@x, \@y1, \@y2 ); # rewrite plot1: y1, y2 over x
759 $plot1->gnuplot_plot_many( \@x1, \@y1, \@x2, \@y2 ); # rewrite plot1: y1 over x1, y2 over x2
760
761 my $plot2 = Graphics::GnuplotIF->new; # new plot object
762
763 $plot2->gnuplot_set_xrange( 0, 4 ); # set x range
764 $plot2->gnuplot_set_yrange( -2, 2 ); # set y range
765 $plot2->gnuplot_cmd( "set grid" ); # send a gnuplot command
766 $plot2->gnuplot_plot_equation( # 3 equations in one plot
767 "y1(x) = sin(x)",
768 "y2(x) = cos(x)",
769 "y3(x) = sin(x)/x" );
770
771 $plot2->gnuplot_pause( ); # hit RETURN to continue
772
773 $plot2->gnuplot_plot_equation( # rewrite plot 2
774 "y4(x) = 2*exp(-x)*sin(4*x)" );
775
776 $plot2->gnuplot_pause( ); # hit RETURN to continue
777
778 my $plot3 = GnuplotIF; # new plot object
779
780 my @xyz = ( # 2-D-matrix, z-values
781 [0, 1, 4, 9],
782 [1, 2, 6, 15],
783 [4, 6, 12, 27],
784 [9, 15, 27, 54],
785 );
786
787 $plot3->gnuplot_cmd( "set grid" ); # send a gnuplot command
788 $plot3->gnuplot_set_plot_titles("surface"); # set legend
789 $plot3->gnuplot_plot_3d( \@xyz ); # start 3-D-plot
790 $plot3->gnuplot_pause( ); # hit RETURN to continue
791
792 =head1 DESCRIPTION
793
794 Graphics::GnuplotIF is a simple and easy to use dynamic Perl interface to
795 B<gnuplot>. B<gnuplot> is a freely available, command-driven graphical display
796 tool for Unix. It compiles and works quite well on a number of Unix flavours
797 as well as other operating systems.
798
799 This module enables sending display requests asynchronously to B<gnuplot>
800 through simple Perl subroutine calls.
801
802 A gnuplot session is an instance of class Graphics::GnuplotIF. The constructor
803 starts B<gnuplot> as a separate process for each session. The plot commands are
804 send through a I<pipe>. The graphical output from B<gnuplot> will be displayed
805 immediately.
806
807 Several independent plots can be started from one script. Each plot has its
808 own pipe. All pipes will be closed automatically by the destructor when the
809 script terminates. The B<gnuplot> processes terminate when the corresponding
810 pipes are closed. Their graphical output will now disappear (but see parameter
811 L<persist|new>).
812
813 Graphics::GnuplotIF is similar to C< gnuplot_i >, a C interface to B<gnuplot>
814 ( http://ndevilla.free.fr/gnuplot/ ), and to C< gnuplot_i++ >, a C++ interface
815 to B<gnuplot> ( http://jijo.cjb.net/code/cc++ ).
816
817 =head1 SUBROUTINES/METHODS
818
819 An object of this class represents an interface to a running B<gnuplot>
820 process. During the creation of an object such an process will be started for
821 each such object. Communication is done through an unidirectional pipe; the
822 resulting stream is write-only.
823
824 Most methods return a reference to the Graphics::GnuplotIF object, allowing
825 method calls to be chained like so:
826
827 $plot1 -> gnuplot_plot_xy(\@x, \@y)
828 -> gnuplot_reset;
829
830 The exception to this are L</gnuplot_get_plotnumber> and
831 L</gnuplot_get_object_id>, which are used to obtain specific scalar
832 values.
833
834 =head2 new
835
836 The constructor creates a new B<gnuplot> session object, referenced by a
837 handle:
838
839 $plot1 = Graphics::GnuplotIF->new( );
840
841 A few named arguments can be passed as key - value pairs (here shown with
842 their default values):
843
844
845 style => 'lines', # one of the gnuplot line styles (see below)
846 title => '', # string
847 xlabel => 'x', # string
848 ylabel => 'y', # string
849 xrange => [], # array reference; autoscaling, if empty
850 xrange => [], # array reference; autoscaling, if empty
851 plot_titles => [], # array of strings; titles used in the legend
852 scriptfile => '', # write all plot commands to the specified file
853 persist => 0, # let plot windows survive after gnuplot exits
854 # 0 : close / 1 : survive
855 objectname => '', # an optional name for the object
856 silent_pause => 1, # 0 suppress message from gnuplot_pause()
857
858 These attributes are stored in each object.
859
860 Allowed line styles are
861
862 boxes dots filledcurves fsteps histeps
863 impulses lines linespoints points steps
864
865 The generated B<gnuplot> commands can be stored to a file instead of beeing
866 executed immediately. This file can be used as input to B<gnuplot>, e.g.
867
868 gnuplot < function_set_1.gnuplot
869
870 A script file can also be used for checking the commands send to B<gnuplot>.
871
872 The objects are automatically deleted by a destructor. The destructor closes
873 the pipe to the B<gnuplot> process belonging to that object. The B<gnuplot>
874 process will also terminate and remove the graphic output. The termination can
875 be controlled by the method L<C<gnuplot_pause> | gnuplot_pause> .
876
877 =head2 GnuplotIF
878
879 The short form of the constructor above (L<C<new>|new>):
880
881 use Graphics::GnuplotIF qw(GnuplotIF);
882
883 $plot1 = GnuplotIF;
884
885 This subroutine is exported only on request.
886
887 =head2 gnuplot_plot_y
888
889 $plot1->gnuplot_plot_y( \@y1, \@y2 );
890
891 C<gnuplot_plot_y> takes one or more array references and plots the values over
892 the x-values 0, 1, 2, 3, ...
893
894 =head2 gnuplot_plot_xy
895
896 $plot1->gnuplot_plot_xy( \@x, \@y1, \@y2 );
897
898 C<gnuplot_plot_xy> takes two or more array references. The first array is
899 assumed to contain the x-values for the following function values.
900
901 =head2 gnuplot_plot_xy_style
902
903 %y1 = ( 'y_values' => \@y1, 'style_spec' => "lines lw 3" );
904 %y2 = ( 'y_values' => \@y2,
905 'style_spec' => "points pointtype 4 pointsize 5" );
906
907 $plot1->gnuplot_plot_xy_style( \@x, \%y1, \%y2 );
908
909 C<gnuplot_plot_xy_style> takes one array reference and one or more hash
910 references. The first array is assumed to contain the x-values for the
911 following function values. The following hashes are assumed to contain pairs of
912 y-values and individual style specifications for use in the plot command. The
913 'style_spec' settings are placed between C<with> and C<title> of B<gnuplot>'s
914 C<plot> command.
915
916 =head2 gnuplot_plot_many
917
918 $plot1->gnuplot_plot_xy( \@x1, \@y1, \@x2, \@y2 );
919
920 C<gnuplot_plot_many> takes pairs of array references. Each pair represents a
921 function and is a reference to the arrays of x- and y-values for that function.
922
923 =head2 gnuplot_plot_many_style
924
925 %f1 = ( 'x_values' => \@x1, 'y_values' => \@y1, 'style_spec' => "lines lw 3" );
926 %f2 = ( 'x_values' => \@x2, 'y_values' => \@y2,
927 'style_spec' => "points pointtype 4 pointsize 5" );
928
929 $plot1->gnuplot_plot_many_style( \%f1, \%f2 );
930
931 C<gnuplot_plot_many_style> takes one or more hash references. The hashes are
932 assumed to contain array referenses to x-values and y-values and individual
933 style specifications for use in the plot command. The 'style_spec' settings are
934 placed between C<with> and C<title> of B<gnuplot>'s C<plot> command.
935
936 =head2 gnuplot_plot_equation
937
938 $plot2->gnuplot_plot_equation( # 3 equations in one plot
939 "y1(x) = sin(x)",
940 "y2(x) = cos(x)",
941 "y3(x) = sin(x)/x" );
942
943 C<gnuplot_plot_equation> takes one or more B<gnuplot> function descriptions as
944 strings. The plot ranges can be controlled by
945 L<C<gnuplot_set_xrange>|gnuplot_set_xrange> and
946 L<C<gnuplot_set_yrange>|gnuplot_set_yrange> .
947
948 =head2 gnuplot_plot_3d
949
950 $plot2->gnuplot_plot_3d( \@array ); # 3-D-plot
951
952 C<gnuplot_plot_3d> takes one reference to an 2-D-array of z-values.
953
954 =head2 gnuplot_pause
955
956 $plot1->gnuplot_pause( [time] [,text] );
957
958 This is an emulation of the B<gnuplot> C<pause> command. It displays any text
959 associated with the command and waits a specified amount of time or until the
960 carriage return is pressed. The message can be suppressed by
961
962 silent_pause => 0
963
964 given to the constructor (see L<new | new>).
965
966 C<time> may be any constant or expression. Choosing 0 (default) will
967 wait until a carriage return is hit, a negative value won't pause at
968 all, and a positive number will wait the specified number of seconds.
969
970 The time value and the text are stored in the object and reused. A sequence
971 like
972
973 $plot1->gnuplot_pause( 5.5 ); # delay is 5.5 seconds
974 $plot1->gnuplot_plot_y( \@y1 );
975
976 $plot1->gnuplot_pause( );
977 $plot1->gnuplot_plot_y( \@y2 );
978
979 $plot1->gnuplot_pause( );
980 $plot1->gnuplot_plot_y( \@y3 );
981
982 $plot1->gnuplot_pause( -1 );
983
984 will display 3 plots with 5 seconds delay and wait for a final carriage return.
985
986
987 =head2 gnuplot_cmd
988
989 $plot2->gnuplot_cmd( 'set grid',
990 'set timestamp "%d/%m/%y %H:%M" 0,0 "Helvetica"'
991 );
992
993 C<gnuplot_cmd> can be used to send one or more B<gnuplot> commands, especially
994 those not wrapped by a Graphics::GnuplotIF method.
995
996 =head2 gnuplot_reset
997
998 $plot1->gnuplot_reset();
999
1000 Set all options set with the C<set> command to their B<gnuplot> default values.
1001
1002 =head2 gnuplot_set_style
1003
1004 $plot1->gnuplot_set_style( "steps" ); # new line style
1005
1006 Sets one of the allowed line styles (see L<new | new>)
1007 in a plot command.
1008
1009 =head2 gnuplot_set_title
1010
1011 $plot1->gnuplot_set_title("parabola"); # new title
1012
1013 Sets the plot title.
1014 Equivalent to the B<gnuplot> command C<set title "parabola">.
1015
1016 =head2 gnuplot_set_xlabel
1017
1018 $plot1->gnuplot_set_xlabel("time (days)");
1019
1020 Sets the x axis label.
1021 Equivalent to the B<gnuplot> command C<set xlabel "time (days)">.
1022
1023 =head2 gnuplot_set_ylabel
1024
1025 $plot1->gnuplot_set_ylabel("bugs fixed");
1026
1027 Sets the y axis label.
1028 Equivalent to the B<gnuplot> command C<set ylabel "bugs fixed">.
1029
1030 =head2 gnuplot_set_xrange
1031
1032 $plot1->gnuplot_set_xrange( left, right );
1033
1034 Sets the horizontal range that will be displayed.
1035 Equivalent to the B<gnuplot> command C<set xrange [left:right]>.
1036
1037 =head2 gnuplot_set_yrange
1038
1039 $plot1->gnuplot_set_yrange( low, high );
1040
1041 Sets the vertical range that will be displayed.
1042 Equivalent to the B<gnuplot> command C<set yrange [low:high]>.
1043
1044 =head2 gnuplot_set_plot_titles
1045
1046 $plot1->gnuplot_set_plot_titles( @ytitles );
1047
1048 Sets the list of titles used in the key for each of the y-coordinate data sets
1049 specified in subsequent calls to gnuplot_plot_xy or gnuplot_plot_y commands.
1050 This is not equivalent to a complete B<gnuplot> command; rather it adds a
1051 C<title> clause to each data set specified in a B<gnuplot> C<plot> command.
1052
1053 =head2 gnuplot_hardcopy
1054
1055 C<gnuplot_cmd> can be used to write a plot into a file or make a printable file
1056 by setting/resetting the terminal and the output file:
1057
1058 $plot1->gnuplot_hardcopy( 'function1.gnuplot.ps',
1059 'postscript',
1060 'color lw 3' );
1061
1062 $plot1->gnuplot_plot_xy( \@x, \@y1, \@y2 );
1063
1064 $plot1->gnuplot_restore_terminal();
1065
1066 The 1. parameter is a file name, the 2. parameter is a B<gnuplot> terminal type,
1067 the 3. parameter is a string with additional terminal parameters (optional).
1068 The current terminal settings will be saved.
1069
1070 =head2 gnuplot_restore_terminal
1071
1072 Restores the saved terminal settings after a call to C<gnuplot_hardcopy()>.
1073 Output will go to C<STDOUT> again.
1074
1075 =head3 Print a plot directly
1076
1077 A hardcopy can be made with an appropriate output format and a pipe to a
1078 printer:
1079
1080 $plot1->gnuplot_cmd( 'set terminal postscript',
1081 'set output " | lpr " ' );
1082
1083 $plot1->gnuplot_plot_xy( \@x, \@y1, \@y2 );
1084
1085 $plot1->gnuplot_cmd( 'set output',
1086 'set terminal x11' );
1087
1088 =head2 gnuplot_get_object_id
1089
1090 Get the (internal) object number (and the object name):
1091
1092 $obj_number = $plot1->gnuplot_get_object_id();
1093 ($obj_number, $obj_name) = $plot1->gnuplot_get_object_id();
1094
1095 The object number is set automatically by the constructor. The object name can
1096 be set by the constructor (objectname => 'MyName').
1097
1098 =head2 gnuplot_get_plotnumber
1099
1100 Get the (internal) plot number of the B<next> plot:
1101
1102 $plot_number = $plot1->gnuplot_get_plotnumber()
1103
1104 The plot number is set automatically by the constructor starting with 1. Each
1105 call to
1106
1107 gnuplot_plot_y
1108 gnuplot_plot_xy
1109 gnuplot_plot_xy_style
1110 gnuplot_plot_many
1111 gnuplot_plot_many_style
1112 gnuplot_plot_equation
1113 gnuplot_plot_3d
1114
1115 increments this number by 1. This can be used to identify single plots, e.g.
1116 with
1117
1118 $plot->gnuplot_cmd( "set timestamp \"plot number ${plot_number} / %c\"" );
1119
1120 =head1 EXPORTS
1121
1122 B<GnuplotIF> constructor, short form (see L<C<GnuplotIF> | GnuplotIF>).
1123
1124 =head1 DIAGNOSTICS
1125
1126 Dialog messages and diagnostic messages start with
1127 C< Graphics::GnuplotIF (object NR): ... > .
1128
1129 C<NR> is the number of the corresponding Graphics::GnuplotIF object and output
1130 stream. NR counts the objects in the order of their generation.
1131
1132 The gnuplot messages going to STDERR will be redirected to the file
1133 C<.gnuplot.PPP.OOO.stderr.log>. PPP is the process number, OOO is the number of
1134 the plot object (see L<C<gnuplot_get_object_id>|gnuplot_get_object_id>).
1135
1136 =head1 CONFIGURATION AND ENVIRONMENT
1137
1138 The environment variable DISPLAY is checked for the display.
1139
1140 =head1 DEPENDENCIES
1141
1142 =over 2
1143
1144 =item *
1145
1146 C<gnuplot> ( http://sourceforge.net/projects/gnuplot ) must be installed.
1147
1148 =item *
1149
1150 The module C<Carp> is used for error handling.
1151
1152 =item *
1153
1154 The module C<IO::Handle> is used to handle output pipes. Your operating system
1155 must support pipes, of course.
1156
1157 =back
1158
1159 =head1 INCOMPATIBILITIES
1160
1161 There are no known incompatibilities.
1162
1163 =head1 BUGS AND LIMITATIONS
1164
1165 $plot1->gnuplot_cmd("pause -1"); # send the gnuplot pause command
1166
1167 does not work. Use
1168
1169 $plot1->gnuplot_pause( );
1170
1171 There are no known bugs in this module. Please report problems to author.
1172 Patches are welcome.
1173
1174 =head1 AUTHOR
1175
1176 Dr.-Ing. Fritz Mehner (mehner@fh-swf.de)
1177
1178 =head1 CREDITS
1179
1180 Stephen Marshall (smarshall at wsi dot com) contributed C<gnuplot_set_plot_titles>.
1181
1182 Georg Bauhaus (bauhaus at futureapps dot de) contributed C<gnuplot_plot_xy_style>.
1183
1184 Bruce Ravel (bravel at bnl dot gov) contributed C<gnuplot_plot_many> and
1185 C<gnuplot_plot_many_style> and made method calls chainable.
1186
1187 =head1 LICENSE AND COPYRIGHT
1188
1189 Copyright (C) 2005-2008 by Fritz Mehner
1190
1191 This module is free software; you can redistribute it and/or modify it under
1192 the same terms as Perl itself. See perldoc perlartistic. This program is
1193 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
1194 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
1195 PARTICULAR PURPOSE.
1196
1197 =head1 SEE ALSO
1198
1199 C<gnuplot(1)>.
1200
1201 =cut
1202
0 #
1 #===============================================================================
2 #
3 # FILE: 00.load.t
4 #
5 # DESCRIPTION: module load test
6 #
7 # FILES: ---
8 # BUGS: ---
9 # NOTES: ---
10 # AUTHOR: Dr.-Ing. Fritz Mehner (Mn), <mehner@fh-swf.de>
11 # COMPANY: Fachhochschule Südwestfalen, Iserlohn
12 # VERSION: 1.0
13 # CREATED: 06.06.2007 19:51:15 CEST
14 # REVISION: $Id: 00.load.t,v 1.1.1.1 2007/06/07 07:14:27 mehner Exp $
15 #===============================================================================
16
17 use strict;
18 use warnings;
19
20 use Test::More tests => 1; # last test to print
21
22 BEGIN {
23 use_ok( 'Graphics::GnuplotIF' );
24 }
25
26 diag( "Testing GnuplotIF $Graphics::GnuplotIF::VERSION" );
0 #
1 #===============================================================================
2 #
3 # FILE: GnuplotIF.t
4 #
5 # DESCRIPTION: Test cases for GnuplotIF.pm
6 # Before `make install' is performed this script should be
7 # run with `make test'. After `make install' it should work
8 # as `perl GnuplotIF.t'
9 #
10 # FILES: ---
11 # BUGS: ---
12 # NOTES: ---
13 # AUTHOR: Dr.-Ing. Fritz Mehner (Mn), <mehner@fh-swf.de>
14 # COMPANY: Fachhochschule Südwestfalen, Iserlohn
15 # CREATED: 27.08.2006 13:39:57 CEST
16 # REVISION: $Id: GnuplotIF.t,v 1.11 2008/06/11 08:04:21 mehner Exp $
17 #===============================================================================
18
19 use strict;
20 use warnings;
21 use Math::Trig;
22
23 use Test::More tests => 7; # last test to print
24
25 use Graphics::GnuplotIF qw(GnuplotIF);
26
27 #---------------------------------------------------------------------------
28 # tests
29 #---------------------------------------------------------------------------
30 ok ( test1() , 'run Graphics::GnuplotIF demo 1' );
31 ok ( test2() , 'run Graphics::GnuplotIF demo 2' );
32 ok ( test3() , 'run Graphics::GnuplotIF demo 3' );
33 ok ( test4() , 'run Graphics::GnuplotIF demo 4' );
34 ok ( test5() , 'run Graphics::GnuplotIF demo 5' );
35 ok ( test6() , 'run Graphics::GnuplotIF demo 6' );
36 ok ( test7() , 'run Graphics::GnuplotIF demo 7' );
37
38 #---------------------------------------------------------------------------
39 # test function 1
40 #---------------------------------------------------------------------------
41 sub test1 {
42
43 if ( !$ENV{'DISPLAY'} ) { return 1; } # no display; skip this test
44
45 my @x = ( -2, -1.50, -1, -0.50, 0, 0.50, 1, 1.50, 2 ); # x values
46 my @y1 = ( 4, 2.25, 1, 0.25, 0, 0.25, 1, 2.25, 4 ); # function 1
47 my @y2 = ( 2, 0.25, -1, -1.75, -2, -1.75, -1, 0.25, 2 ); # function 2
48 my $wait = 3;
49 my $plotnumber = 0;
50
51 my $timeformat = '%d-%m-%y %H:%M:%S';
52
53 #---------------------------------------------------------------------------
54 # plot object 1
55 #---------------------------------------------------------------------------
56 my $plot1 = Graphics::GnuplotIF->new(
57 title => 'line',
58 style => 'points',
59 plot_titles => ['function 1.1', 'function 1.2'],
60 );
61
62 $plotnumber++;
63 $plot1->gnuplot_cmd("set timestamp \"plot number ${plotnumber} / ${timeformat}\" bottom");
64 $plot1->gnuplot_plot_y( \@x ); # plot 9 points over 0..8
65
66 $plot1->gnuplot_pause($wait); # wait
67
68 $plot1->gnuplot_set_title('parabola'); # new title
69 $plot1->gnuplot_set_style('lines'); # new line style
70
71 $plotnumber++;
72 $plot1->gnuplot_cmd("set timestamp \"plot number ${plotnumber} / ${timeformat}\" bottom");
73 $plot1->gnuplot_plot_xy( \@x, \@y1, \@y2 ); # rewrite plot1 : y1, y2 over x
74 $plot1->gnuplot_pause($wait);
75
76 # Plot same data again, this time with all default settings.
77 $plot1->gnuplot_reset();
78 $plotnumber++;
79 $plot1->gnuplot_cmd("set timestamp \"plot number ${plotnumber} / ${timeformat}\" bottom");
80 $plot1->gnuplot_plot_xy( \@x, \@y1, \@y2 ); # rewrite plot1 : y1, y2 over x
81 $plot1->gnuplot_pause($wait);
82
83 # Plot same data again, again starting from default settings,
84 # but with individual plotting styles applied per function
85 my %y1 = ( 'y_values' => \@y1, 'style_spec' => 'lines lw 3' );
86 my %y2 = ( 'y_values' => \@y2, 'style_spec' => 'points pointtype 4 pointsize 5' );
87
88 $plotnumber++;
89 $plot1->gnuplot_cmd("set timestamp \"plot number ${plotnumber} / ${timeformat}\" bottom");
90 $plot1->gnuplot_plot_xy_style( \@x, \%y1, \%y2 );
91 $plot1->gnuplot_pause($wait);
92
93 #---------------------------------------------------------------------------
94 # plot object 2
95 #---------------------------------------------------------------------------
96 my $plot2 = Graphics::GnuplotIF->new;
97
98 $plot2->gnuplot_set_xrange( 0, '2*pi' ); # set x range
99 $plot2->gnuplot_set_yrange( -2, 2 ); # set y range
100 $plot2->gnuplot_cmd('set grid'); # send a gnuplot command
101 $plot2->gnuplot_plot_equation( # 3 equations in one plot
102 'y1(x) = sin(x)',
103 'y2(x) = cos(x)',
104 'y3(x) = sin(x)/x'
105 );
106
107 $plot2->gnuplot_pause($wait); # wait
108
109 $plot2->gnuplot_plot_equation( # rewrite plot 2
110 'y4(x) = 2*exp(-x)*sin(4*x)'
111 );
112
113 $plot2->gnuplot_pause($wait); # wait
114
115 return 1;
116 } # ---------- end of subroutine test1 ----------
117
118 #---------------------------------------------------------------------------
119 # test function 2 : write a script file
120 #---------------------------------------------------------------------------
121 sub test2 {
122
123 if ( !$ENV{'DISPLAY'} ) { return 1; } # no display; skip this test
124
125 my $wait = 4;
126 my $scriptfilename = 't/test2.gnuplot';
127 my @x = ( 0, +1, +0.8, 0, -1, -0.8, 0, +1, +0.8, 0, -1, -0.8, 0 );
128
129 if ( -e $scriptfilename ) {
130 unlink $scriptfilename;
131 }
132 my $plot = Graphics::GnuplotIF->new(
133 title => 'function #2',
134 style => 'lines',
135 plot_titles => ['function 2'],
136 scriptfile => $scriptfilename,
137 );
138 $plot->gnuplot_set_yrange( -1.5, +1.5 ); # set y range
139 $plot->gnuplot_cmd('set xzeroaxis');
140
141 $plot->gnuplot_plot_y( \@x )->gnuplot_pause($wait); # plot and wait
142
143 $plot->gnuplot_plot_y( \@x );
144
145 foreach my $count ( 1 ... 30 ) { # wait for the plot file
146 if ( -e $scriptfilename ) {
147 my @args = ( 'gnuplot', $scriptfilename );
148 system(@args) == 0
149 or die "system @args failed: $!";
150 $plot->gnuplot_pause(4);
151 return 1;
152 }
153 sleep 1;
154 }
155
156 return 0;
157 } # ---------- end of subroutine test2 ----------
158
159 #---------------------------------------------------------------------------
160 # test function 3 : animation
161 #---------------------------------------------------------------------------
162 sub test3 {
163
164 if ( !$ENV{'DISPLAY'} ) { return 1; } # no display; skip this test
165
166 my $steps = 10;
167 my $intervall = 2 * pi;
168 my $n;
169
170 my $plot1 = Graphics::GnuplotIF->new(
171 title => 'Fourier Series -- Square Wave',
172 silent_pause => 0,
173 );
174
175 $plot1->gnuplot_cmd('set grid'); # send a gnuplot command
176 $plot1->gnuplot_cmd('set key off'); # send a gnuplot command
177 $plot1->gnuplot_cmd('set timestamp bottom'); # send a gnuplot command
178 $plot1->gnuplot_set_xrange( 0, $intervall ); # set x range
179
180 my $fourier = q{};
181 foreach my $i ( 1 .. $steps ) {
182 $n = 2 * $i - 1;
183 $fourier .= " +sin($n*x)/$n";
184 $plot1->gnuplot_cmd("plot (4/pi)*($fourier)")->gnuplot_pause(.4);
185 }
186
187 return 1;
188 } # ---------- end of subroutine test3 ----------
189
190 #---------------------------------------------------------------------------
191 # test function 4 : hardcopy
192 #---------------------------------------------------------------------------
193 sub test4 {
194
195 if ( !$ENV{'DISPLAY'} ) { return 1; } # no display; skip this test
196 my @x = ( 0, +1, +1, 0, -1, -1, 0, +1, +1, 0, -1, -1, 0 ); # x values
197 my @y1 = ( 4, 2.25, 1, 0.25, 0, 0.25, 1, 2.25, 4 ); # function 1
198
199 my $plot = Graphics::GnuplotIF->new(
200 title => 'function #4',
201 style => 'lines',
202 );
203 $plot->gnuplot_set_yrange( -1.5, +1.5 ); # set y range
204 $plot->gnuplot_cmd('set xzeroaxis');
205 $plot->gnuplot_plot_y( \@x );
206
207 $plot->gnuplot_hardcopy( 't/function4.gnuplot.ps', 'postscript', 'color' );
208 $plot->gnuplot_plot_y( \@x );
209 $plot->gnuplot_restore_terminal();
210 $plot->gnuplot_pause(4);
211
212 return 1;
213 } # ---------- end of subroutine test4 ----------
214
215 #---------------------------------------------------------------------------
216 # test function 5 : 3-D-plot
217 #---------------------------------------------------------------------------
218 sub test5 {
219
220 if ( !$ENV{'DISPLAY'} ) { return 1; } # no display; skip this test
221
222 my $plot = Graphics::GnuplotIF->new(
223 title => 'function #5',
224 style => 'lines',
225 );
226 $plot->gnuplot_set_title('x*y*sin(x)*sin(y)'); # new title
227 $plot->gnuplot_set_plot_titles('surface 1');
228
229 my @array;
230 foreach my $i ( 0 .. 64 ) {
231 foreach my $j ( 0 .. 64 ) {
232 $array[$i][$j] = $i * $j * sin( 0.2 * $i ) * sin( 0.2 * $j );
233 }
234 }
235
236 $plot->gnuplot_plot_3d( \@array )->gnuplot_pause(6);
237
238 return 1;
239 } # ---------- end of subroutine test5 ----------
240
241 #---------------------------------------------------------------------------
242 # test function 6 :
243 #---------------------------------------------------------------------------
244 sub test6 {
245
246 if ( !$ENV{'DISPLAY'} ) { return 1; } # no display; skip this test
247
248 my $plot = GnuplotIF( # constructor; short form
249 title => 'test 6',
250 style => 'lines',
251 objectname => '=polynomials=',
252 );
253 $plot->gnuplot_cmd('set grid'); # send a gnuplot command
254 $plot->gnuplot_cmd('set xzeroaxis linestyle 1'); # send a gnuplot command
255
256 my $timeformat = '%d-%m-%y %H:%M:%S';
257 my @x;
258 my @y;
259
260 foreach my $n ( 1 ... 4 ) {
261 my ( $id, $name ) = $plot->gnuplot_get_object_id();
262 my $plotnumber = $plot->gnuplot_get_plotnumber();
263 $plot->gnuplot_set_plot_titles("(x-1)**$n");
264 foreach my $i ( 0 ... 40 ) {
265 $x[$i] = 0.1 * $i - 2.0;
266 $y[$i] = $x[$i]**$n;
267 }
268
269 $plot->gnuplot_cmd(
270 "set timestamp \"plot object ${id} / plot number ${plotnumber} / ${timeformat}\" bottom"
271 );
272 $plot->gnuplot_cmd(
273 "set timestamp \"plot number ${plotnumber} / %d/%m/%y %H:%M\""
274 );
275 $plot->gnuplot_plot_xy( \@x, \@y )->gnuplot_pause(.4);
276 }
277
278 return 1;
279 } # ---------- end of subroutine test6 ----------
280
281
282 #---------------------------------------------------------------------------
283 # test function 7
284 #---------------------------------------------------------------------------
285 sub test7 {
286
287 if ( !$ENV{'DISPLAY'} ) { return 1; } # no display; skip this test
288
289 my @x1 = ( -2, -1.50, -1, -0.50, 0, 0.50, 1, 1.50, 2 ); # x values (1.set)
290 my @y1 = ( 4, 2.25, 1, 0.25, 0, 0.25, 1, 2.25, 4 ); # y-values (1.set)
291
292 my @x2 = ( -4, -3.50, -3, -2.50, -2, -1.50, -1, -0.50, 0 );# x values (2.set)
293 my @y2 = ( 5, 3.25, 2, 1.25, 1, 1.25, 2, 3.25, 5 );# y-values (2.set)
294
295 my @x3 = ( 0, 0.50, 1, 1.50, 2, 2.50, 3, 3.50, 4 ); # x values (3.set)
296 my @y3 = ( 3, 1.25, 0, -0.75, -1, -0.75, 0, 1.25, 3 ); # y-values (3.set)
297
298 my $wait = 3;
299 my $plotnumber = 0;
300
301 my $timeformat = '%d-%m-%y %H:%M:%S';
302
303 #---------------------------------------------------------------------------
304 # plot object 1
305 #---------------------------------------------------------------------------
306 my $plot1 = Graphics::GnuplotIF->new(
307 title => 'x-y-plot(s) not sharing an x-axis',
308 style => 'lines',
309 plot_titles => ['function 1.1', 'function 1.2', 'function 1.3'],
310 );
311
312 $plotnumber++;
313 $plot1->gnuplot_cmd("set timestamp \"plot number ${plotnumber} / ${timeformat}\" bottom");
314 $plot1->gnuplot_plot_many( \@x1, \@y1 )->gnuplot_pause($wait);
315
316 $plotnumber++;
317 $plot1->gnuplot_cmd("set timestamp \"plot number ${plotnumber} / ${timeformat}\" bottom");
318 $plot1->gnuplot_plot_many( \@x1, \@y1, \@x2, \@y2 )->gnuplot_pause($wait);
319
320 $plotnumber++;
321 $plot1->gnuplot_cmd("set timestamp \"plot number ${plotnumber} / ${timeformat}\" bottom");
322 $plot1->gnuplot_plot_many( \@x1, \@y1, \@x2, \@y2, \@x3, \@y3 )->gnuplot_pause($wait);
323
324 my %f1 = ( 'x_values' => \@x1, 'y_values' => \@y1, 'style_spec' => "lines lw 3" );
325 my %f2 = ( 'x_values' => \@x2, 'y_values' => \@y2,
326 'style_spec' => "points pointtype 4 pointsize 5" );
327
328 $plot1->gnuplot_plot_many_style( \%f1, \%f2 )->gnuplot_pause($wait);
329
330 #---------------------------------------------------------------------------
331 # plot object 2
332 #---------------------------------------------------------------------------
333 my @x21 = ( -2, -1.50, -1, -0.50, 0, 0.50, 1, 1.50, 2 ); # 9 points
334 my @y21 = ( 4, 2.25, 1, 0.25, 0, 0.25, 1, 2.25, 4 ); # function 1
335
336 my @x22 = ( -1.1, -0.1, 0.9, 1.9, 2.9); # 5 points
337 my @y22 = ( 0.1, 0.2, 0.3, 0.4, 0.5); # function 2
338
339 my $plot2 = Graphics::GnuplotIF->new(
340 title => "parabola",
341 style => "lines"
342 );
343
344 $plot2->gnuplot_plot_many( \@x21, \@y21, \@x22, \@y22 )->gnuplot_pause($wait);
345
346 return 1;
347 } # ---------- end of subroutine test7 ----------
0 #!perl -T
1 #===============================================================================
2 #
3 # FILE: pod-coverage.t
4 #
5 # DESCRIPTION: testing POD coverage
6 #
7 # FILES: ---
8 # BUGS: ---
9 # NOTES: ---
10 # AUTHOR: Dr.-Ing. Fritz Mehner (Mn), <mehner@fh-swf.de>
11 # COMPANY: Fachhochschule Südwestfalen, Iserlohn
12 # VERSION: 1.0
13 # CREATED: 06.06.2007 19:51:15 CEST
14 # REVISION: $Id: pod-coverage.t,v 1.1.1.1 2007/06/07 07:14:27 mehner Exp $
15 #===============================================================================
16
17 use strict;
18 use warnings;
19
20 use Test::More;
21 eval "use Test::Pod::Coverage 1.04";
22 plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@;
23
24 my $login;
25 eval { $login = $ENV{USER} };
26 plan skip_all => "Only the author needs to check the POD documentation." if ($@ || $login ne "mehner" );
27
28 all_pod_coverage_ok();
0 #!perl -T
1 #===============================================================================
2 #
3 # FILE: pod.t
4 #
5 # DESCRIPTION: Testing POD
6 #
7 # FILES: ---
8 # BUGS: ---
9 # NOTES: ---
10 # AUTHOR: Dr.-Ing. Fritz Mehner (Mn), <mehner@fh-swf.de>
11 # COMPANY: Fachhochschule Südwestfalen, Iserlohn
12 # VERSION: 1.0
13 # CREATED: 06.06.2007 19:51:15 CEST
14 # REVISION: $Id: pod.t,v 1.2 2007/10/13 15:55:50 mehner Exp $
15 #===============================================================================
16
17 use strict;
18 use warnings;
19
20 use Test::More;
21 eval "use Test::Pod 1.0";
22 plan skip_all => "Test::Pod 1.0 required for testing POD" if $@;
23
24 my $login;
25 eval { $login = $ENV{USER} };
26 plan skip_all => "Only the author needs to check the POD documentation." if ($@ || $login ne "mehner" );
27
28 all_pod_files_ok();