Chart-Gnuplot

 view release on metacpan or  search on metacpan

lib/Chart/Gnuplot.pm  view on Meta::CPAN

    return($out);
}


# Set title and layout of the multiplot
sub _setMultiplot
{
    my ($self, $nrows, $ncols) = @_;

    open(PLT, ">>$self->{_script}") || confess("Can't write $self->{_script}");
    print PLT "set multiplot";
    print PLT " title \"$self->{title}\"" if (defined $self->{title});
    print PLT " layout $nrows, $ncols" if (defined $nrows);
    print PLT "\n";
    close(PLT);
}


# Usage example:
# timestamp => {
#    fmt    => '%d/%m/%y %H:%M',
#    offset => "10,-3"
#    font   => "Helvetica",
# },
# # OR
# timestamp => 'on';
sub _setTimestamp
{
    my ($ts) = @_;

    my $out = '';
    if (ref($ts) eq 'HASH')
    {
        $out .= " \"$$ts{fmt}\"" if (defined $$ts{fmt});
        $out .= " offset $$ts{offset}" if (defined $$ts{offset});
        $out .= " font \"$$ts{font}\"" if (defined $$ts{font});
    }
    elsif ($ts ne 'on')
    {
        return($ts);
    }
    return($out);
}


# Call Gnuplot to generate the image file
sub execute
{
    my ($self) = @_;

    # Try to find the executable of Gnuplot
    my $gnuplot = 'gnuplot';
    if (defined $self->{gnuplot})
    {
        $gnuplot = $self->{gnuplot};
    }
    else
    {
        if ($^O =~ /MSWin/)
        {
            my $gnuplotDir = 'C:\Program Files\gnuplot';
            $gnuplotDir = 'C:\Program Files (x86)\gnuplot' if (!-e $gnuplotDir);

            my $binDir = $gnuplotDir.'\bin';
            $binDir = $gnuplotDir.'\binary' if (!-e $binDir);

            $gnuplot = $binDir.'\gnuplot.exe';
            if (!-e $gnuplot)
            {
                $gnuplot = $binDir.'\wgnuplot.exe';
                confess("Gnuplot command not found.") if (!-e $gnuplot);
            }
        }
    }

    # Execute gnuplot
    my $cmd = qq("$gnuplot" "$self->{_script}");
    $cmd .= " -" if ($self->{terminal} =~ /^(ggi|pm|windows|wxt|x11)(\s|$)/);
    my $err = `$cmd 2>&1`;
#    my $err;
#    system("$cmd");

    # Capture and process error message from Gnuplot
    if (defined $err && $err ne '')
    {
        my ($errTmp) = ($err =~ /\", line \d+:\s(.+)/);
        die "$errTmp\n" if (defined $errTmp);
        warn "$err\n";
    }

    # Convert the image to the user-specified format
    if (defined $self->{_terminal} && $self->{_terminal} eq 'auto')
    {
        my @a = split(/\./, $self->{output});
        my $ext = $a[-1];
        &convert($self, $ext) if ($ext !~ /^e?ps$/);
    }

    return($self);
}


# Unset the chart properties
# - called by multiplot()
sub _reset
{
    my ($self) = @_;
    open(PLT, ">>$self->{_script}") || confess("Can't write $self->{_script}");
    foreach my $opt (@{$self->{_sets}})
    {
        print PLT "unset $opt\n";

        if ($opt =~ /range$/)
        {
            print PLT "set $opt [*:*]\n";
        }
        elsif (!grep(/^$opt$/, qw(arrow grid label logscale object parametric))
			&& $opt !~ /tics$/)
        {
            print PLT "set $opt\n";
        }
    }
    close(PLT);
}


# Arbitrary labels placed in the chart

lib/Chart/Gnuplot.pm  view on Meta::CPAN

multi-plot such as inset chart. E.g.

    size => "0.5, 0.4"

=head3 origin

Origin of the chart. This is useful in some multi-plot such as inset chart.
E.g.

    origin => "0.1, 0.5"

=head3 timestamp

Time stamp of the plot. To place the time stamp with default setting,

    timestamp => 'on'

Properties of the time stamp (such as date-time format) can also be set, e.g.

    timestamp => {
       fmt    => '%d/%m/%y %H:%M',
       offset => "10,-3",
       font   => "Helvetica",
    }

Supported properties are:

    fmt    : date-time format
    offset : offset relative to the default position
    font   : font face (and optionally font size)

=head3 bg

Background color of the chart. This option has no effect in the sub-chart of
multiplot. E.g. to give the chart a yellow background,

    bg => "yellow"

Properties can be specified in hash. E.g.,

    bg => {
        color   => "yellow",
        density => 0.2,
    }

Supported properties are:

    color   : color (name ot RRGGBB value)
    density : density of the coloring

=head3 plotbg

Background color of the plot area. This option has no effect in 3D plots. See
L<bg> for supported properties.

=head3 gnuplot

The path of Gnuplot executable. This option is useful if you are using Windows
or have multiple versions of Gnuplot installed. E.g.,

    gnuplot => "C:\Program Files\...\gnuplot\bin\wgnuplot.exe"   # for Windows

=head3 convert

The path of convert executable of ImageMagick. This option is useful if you
have multiple convert executables.

=head3 terminal

The terminal driver that Gnuplot uses. E.g.,

    terminal => 'svg mousing'

The default value is C<postscript enhanced color>. Terminal is not necessarily
related to the output image format. E.g., you may use gif terminal and then
convert the image format to jpg by the L<convert()> method.

=head2 Chart Options Not Mentioned Above

If Chart::Gnuplot encounters options not mentions above, it would convert them
to Gnuplot set statements. E.g. if the chart object is

    $chart = Chart::Gnuplot->new(
        ...
        foo => "FOO",
    );

the generated Gnuplot statements would be:

    ...
    set foo FOO

This mechanism lets Chart::Gnuplot support many features not mentioned above
(such as "cbrange", "samples", "view" and so on).

=head2 Chart Methods

=head3 new

    my $chart = Chart::Gnuplot->new(%options);

Constructor of the chart object. If no option is specified, default values
would be used. See L<Chart Options> for available options.

=head3 set

General set methods for arbitrary number of options.

    $chart->set(%options);

E.g.

    $chart->set(view => '30,60');

will be translated to the Gnuplot statement

    set view 30,60

=head3 plot2d

    $chart->plot2d(@dataSets);



( run in 0.489 second using v1.01-cache-2.11-cpan-39bf76dae61 )