App-GnuplotUtils

 view release on metacpan or  search on metacpan

lib/App/GnuplotUtils.pm  view on Meta::CPAN

        output => $outputfilename,
        title => $args{chart_title} // "(chart created by xyplot on ".scalar(localtime).")",
        xlabel => "x",
        ylabel => "y",
    );

    my $n;
    if ($args{dataset_datas}) {
        $n = $#{ $args{dataset_datas} };
    } else {
        $n = $#{ $args{dataset_files} };
    }

    my @datasets;
    for my $i (0..$n) {
        my (@x, @y);
        if ($args{dataset_datas}) {
            my $dataset = [split $fieldsep_re, $args{dataset_datas}[$i]];
            while (@$dataset) {
                my $item = shift @$dataset;
                warn "Not a number in --dataset-data: '$item'" unless Scalar::Util::looks_like_number($item);
                push @x, $item;

                warn "Odd number of numbers in --dataset-data" unless @$dataset;
                $item = shift @$dataset;
                warn "Not a number in --dataset-data: '$item'" unless Scalar::Util::looks_like_number($item);
                push @y, $item;
            }
        } else {
            my $filename = $args{dataset_files}[$i];
            my $content = File::Slurper::Dash::read_text($filename);

            chomp $content;
            my @numbers = split $fieldsep_re, $content;
            warn "Odd number of numbers in dataset file '$filename'" unless @numbers % 2 == 0;
            while (@numbers) {
                my $item = shift @numbers;
                warn "Not a number in dataset file '$filename': '$item'" unless Scalar::Util::looks_like_number($item);
                push @x, $item;

                $item = shift @numbers;
                warn "Not a number in dataset file '$filename': '$item'" unless Scalar::Util::looks_like_number($item);
                push @y, $item;
            }
        }

        my $dataset = Chart::Gnuplot::DataSet->new(
            xdata => \@x,
            ydata => \@y,
            title => $args{dataset_titles}[$i] // "(dataset #$i)",
            style => $args{dataset_styles}[$i] // 'linespoints',
        );
        push @datasets, $dataset;
    }
    $chart->plot2d(@datasets);

    if (defined $args{output_file}) {
        return [200];
    } else {
        require Desktop::Open;
        my $res = Desktop::Open::open_desktop("file:$outputfilename");
        if (defined $res && $res == 0) {
            return [200];
        } else {
            return [500, "Can't open $outputfilename"];
        }
    }
}

1;
# ABSTRACT: Utilities related to plotting data using gnuplot

__END__

=pod

=encoding UTF-8

=head1 NAME

App::GnuplotUtils - Utilities related to plotting data using gnuplot

=head1 VERSION

This document describes version 0.006 of App::GnuplotUtils (from Perl distribution App-GnuplotUtils), released on 2023-10-21.

=head1 DESCRIPTION

This distributions provides the following command-line utilities. They are
mostly simple/convenience wrappers for gnuplot:

=over

=item * L<xyplot>

=back

=head1 FUNCTIONS


=head2 xyplot

Usage:

 xyplot(%args) -> [$status_code, $reason, $payload, \%result_meta]

Plot XY dataset(s) using gnuplot.

This utility is a wrapper for gnuplot to quickly generate a graph from the
command-line and view it using an image viewer program or a browser.

B<Specifying dataset>

You can specify the dataset to plot directly from the command-line or specify
filename to read the dataset from.

To plot directly from the command-line, specify comma-separated list of X & Y
number pairs using C<--dataset-data> option:

 % xyplot --dataset-data '1,1, 2,3, 3,5.5, 4,7.9, 6,11.5' ; # whitespaces are optional



( run in 1.625 second using v1.01-cache-2.11-cpan-e1769b4cff6 )