http://qs321.pair.com?node_id=196684

RMGir has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to plot data as it arrives in realtime with gnuplot.

I've arrived at a fairly simple perl solution, but it involves writing the data to a temporary file and then replotting the whole file every second.

I'm afraid this will result in too much overhead towards the end of the day, as the number of data points accumulates.

I have a perl implementation of my approach, and I'd like suggestions from any gnuplot gurus out there as to more efficient ways to do this.

I've looked at Chart::Graph::Gnuplot, and looked at a few gnuplot related nodes here, but I don't think they solves this problem.

Here's a dummy data source. In reality, this could take input from anything that outputs a time and a value.

#!/usr/bin/perl -w use strict; use POSIX qw(strftime); my $value=rand(100); $|=1; while(1) { sleep(1); print strftime("%H:%M:%S",localtime),",$value\n"; # jitter the value $value+=rand(2)-1; }
Here's the actual gnuplot-calling code. This assumes gnuplot is on your path, and that you have File::Temp installed.

If you don't have File::Temp, it should be simple to adjust the script to use a fixed file name.

#!/usr/bin/perl -w use strict; use File::Temp qw(tempfile tempdir); use IO::Handle; my $tempdir=tempdir(CLEANUP=>1); my ($plotFh, $plotFilename)=tempfile(DIR=>$tempdir); # replace this with whatever the real data src is if needed open(DATASRC,"perl dataSrc.pl|") or die "Can't run data source, error +$!"; open(GNUPLOT,"|gnuplot"); $plotFh->autoflush(1); select GNUPLOT; $|=1; # set up plot options for time series graph print GNUPLOT "set xdata time\n"; print GNUPLOT q(set timefmt "%H:%M:%S"),qq(\n); my $prevTime=""; my $prevValue=0; while(<DATASRC>) { chomp; my ($time, $value)=split /,/; if($time ne $prevTime) { if($prevTime ne "") { print $plotFh "$prevTime $prevValue\n"; print GNUPLOT "plot '$plotFilename' using 1:2\n"; } $prevTime=$time; } $prevValue=$value; }
Any ideas?

TIA!
--
Mike