From 4f131005cbe8b590ce5a8dbbcc358378f173eadb Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Fri, 6 Nov 2020 09:48:29 +0100 Subject: [PATCH] plotpipe: --nox = first column is not x-axis. --- plotpipe/plotpipe | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/plotpipe/plotpipe b/plotpipe/plotpipe index 18da3c3..79556c9 100755 --- a/plotpipe/plotpipe +++ b/plotpipe/plotpipe @@ -9,7 +9,7 @@ plotpipe - Plot CSV data from a pipe =head1 SYNOPSIS -I | B [-H] [-0] [-C str] [-h] [-V] +I | B [-n] [-H] [-0] [-C str] [-h] [-V] =head1 DESCRIPTION @@ -48,6 +48,14 @@ B<--header>. 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<-0> @@ -153,6 +161,7 @@ sub options_hash() { "colsep|col-sep|C=s" => \$opt::colsep, "help|h" => \$opt::help, "null|0" => \$opt::null, + "nox|n" => \$opt::nox, "header|H" => \$opt::header, ); } @@ -327,23 +336,22 @@ $ENV{'TMPDIR'} ||= "/tmp"; my($filehandle,$filename) = tempfile(DIR=>$ENV{'TMPDIR'}, TEMPLATE => 'plotXXXXX'); my $ncols = split /$opt::colsep/, $csv[0]; -if($ncols >= 2) { +if($ncols >= 2 and not $opt::nox) { # Column 1 = x-axis for(@csv) { chomp; print $filehandle ((join "\001", split /$opt::colsep/, $_),"\n"); } } else { - # Column 1 = y-axis - my $t = 1; - # Convert 1 column to 2 column + # All data = y-axis, invent x-axis + my $x = 1; for(@csv) { chomp; - print $filehandle ((join "\001", $t++, $_),"\n"); + print $filehandle ((join "\001", $x++, split /$opt::colsep/, $_),"\n"); } - # Plot 2 columns -unshift(@header,""); - $ncols = 2; + # Prepend dummy header for x-axis + unshift(@header,""); + $ncols += 1; } # Generate the variant part of Gnuplot script @@ -367,7 +375,13 @@ set datafile separator "\001"; plot @plotscript _EOS -open GNUPLOT,"|-", "gnuplot -p -e ".Q($plotscript) or die; -#print "gnuplot -p -e ".($plotscript); -close GNUPLOT; -unlink $filename; +if(fork) { + open GNUPLOT,"|-", "gnuplot -p -e ".Q($plotscript) or die; + #print "gnuplot -p -e ".($plotscript); + close GNUPLOT; + unlink $filename; +} else { + # If script dies unexpectedly, remove the file after 5 sec + sleep(5); + unlink $filename; +}