plotpipe: --nox = first column is not x-axis.

This commit is contained in:
Ole Tange 2020-11-06 09:48:29 +01:00
parent fb1c5b18ce
commit 4f131005cb

View file

@ -9,7 +9,7 @@ plotpipe - Plot CSV data from a pipe
=head1 SYNOPSIS =head1 SYNOPSIS
I<datagenerator> | B<plotpipe> [-H] [-0] [-C str] [-h] [-V] I<datagenerator> | B<plotpipe> [-n] [-H] [-0] [-C str] [-h] [-V]
=head1 DESCRIPTION =head1 DESCRIPTION
@ -48,6 +48,14 @@ B<--header>.
Show help. Show help.
=item B<--nox>
=item B<-n>
No x-value. In a multi-column input the first value will normally be
used as x-value. B<--nox> will use line number as x-value.
=item B<--null> =item B<--null>
=item B<-0> =item B<-0>
@ -153,6 +161,7 @@ sub options_hash() {
"colsep|col-sep|C=s" => \$opt::colsep, "colsep|col-sep|C=s" => \$opt::colsep,
"help|h" => \$opt::help, "help|h" => \$opt::help,
"null|0" => \$opt::null, "null|0" => \$opt::null,
"nox|n" => \$opt::nox,
"header|H" => \$opt::header, "header|H" => \$opt::header,
); );
} }
@ -327,23 +336,22 @@ $ENV{'TMPDIR'} ||= "/tmp";
my($filehandle,$filename) = my($filehandle,$filename) =
tempfile(DIR=>$ENV{'TMPDIR'}, TEMPLATE => 'plotXXXXX'); tempfile(DIR=>$ENV{'TMPDIR'}, TEMPLATE => 'plotXXXXX');
my $ncols = split /$opt::colsep/, $csv[0]; my $ncols = split /$opt::colsep/, $csv[0];
if($ncols >= 2) { if($ncols >= 2 and not $opt::nox) {
# Column 1 = x-axis # Column 1 = x-axis
for(@csv) { for(@csv) {
chomp; chomp;
print $filehandle ((join "\001", split /$opt::colsep/, $_),"\n"); print $filehandle ((join "\001", split /$opt::colsep/, $_),"\n");
} }
} else { } else {
# Column 1 = y-axis # All data = y-axis, invent x-axis
my $t = 1; my $x = 1;
# Convert 1 column to 2 column
for(@csv) { for(@csv) {
chomp; chomp;
print $filehandle ((join "\001", $t++, $_),"\n"); print $filehandle ((join "\001", $x++, split /$opt::colsep/, $_),"\n");
} }
# Plot 2 columns # Prepend dummy header for x-axis
unshift(@header,""); unshift(@header,"");
$ncols = 2; $ncols += 1;
} }
# Generate the variant part of Gnuplot script # Generate the variant part of Gnuplot script
@ -367,7 +375,13 @@ set datafile separator "\001";
plot @plotscript plot @plotscript
_EOS _EOS
if(fork) {
open GNUPLOT,"|-", "gnuplot -p -e ".Q($plotscript) or die; open GNUPLOT,"|-", "gnuplot -p -e ".Q($plotscript) or die;
#print "gnuplot -p -e ".($plotscript); #print "gnuplot -p -e ".($plotscript);
close GNUPLOT; close GNUPLOT;
unlink $filename; unlink $filename;
} else {
# If script dies unexpectedly, remove the file after 5 sec
sleep(5);
unlink $filename;
}