App-GnuplotUtils
view release on metacpan or search on metacpan
lib/App/GnuplotUtils.pm view on Meta::CPAN
args_rels => {
req_one => [qw/dataset_datas dataset_files/],
},
deps => {
prog => 'gnuplot',
},
links => [
{url=>'prog:tchart', summary=>'From App::tchart Perl module, to quickly create ASCII chart, currently sparklines'},
{url=>'prog:asciichart', summary=>'From App::AsciiChart Perl module, to quickly create ASCII chart'},
],
};
sub xyplot {
require Chart::Gnuplot;
require File::Slurper::Dash;
require File::Temp;
require Scalar::Util;
my %args = @_;
my $output_format = $args{output_format} // 'png';
my $fieldsep_re = qr/\s*,\s*|\s+/s;
if (defined $args{delimited}) {
$fieldsep_re = qr/\Q$args{delimited}\E/;
}
my ($outputfilename);
if (defined $args{output_file}) {
$outputfilename = $args{output_file};
if (-f $outputfilename && !$args{overwrite}) {
return [412, "Not overwriting existing file '$outputfilename', use --overwrite (-O) to overwrite"];
}
} else {
my $tempfh;
($tempfh, $outputfilename) = File::Temp::tempfile();
$outputfilename .= ".$output_format";
}
log_trace "Output filename: %s", $outputfilename;
my $chart = Chart::Gnuplot->new(
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
( run in 1.048 second using v1.01-cache-2.11-cpan-6aa56a78535 )