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

Hey monks!
So i am working on this project to get temperature data from a device and graph it. That works great, i read the temperature from the device and stick it into a hash with the time, and put that all inside an array for every sample. I then graph the data using PlotPTk. Everything is peachy until the time rolls over to the next hour (eg sample 1 - 12:59:12, sample 2 - 01:00:12) i get a huge gap of 20090415130012 - 20090415125912 = 4100 in my graph. I'm using date::manip to create the time values.

## Creates hash ##

my $time_ = localtime; my $date = ParseDate($time_); $date =~ s/://g; my $unix_date = UnixDate($date, @d_format); $unix_date =~ s/ //g; $ash_{date} = $unix_date; $ash_{temp} = gct();

## Creates AOH ##
my %cash = build_comp_hash(); push @aoh, { %cash }; graphable_arrays();

## Creates arrays plotptk likes ##
@xdata = (); @ydata = (); my($i, $datum); for $i (0 .. $#aoh){ for $datum (keys %{ $aoh[$i] }){ if($datum =~ m/date/){ push @xdata, $aoh[$i]{$datum}; } elsif($datum =~ m/temp/){ push @ydata, $aoh[$i]{$datum}; } else{} }

##Creates plot##
my @plot_title = ("CryoCooler Temperature Data", -5); if(!defined $gwidth){$gwidth = '700';} if(!defined $gheight){$gheight = '500';} $mw->destroy if Tk::Exists($mw); $mw = MainWindow->new; $mw -> title('Thoth -- Plot'); my $pw_plot = $mw->PlotPTk(-width=>$gwidth, -height=>$gheight, -xlabel +=> "Time", -ylabel=>"Temperature", -autoScaleY => "On +", -scale => "@scale_vals", -plotTitle => \@plot_title)->grid; my $ds = DataSet->new(-yData=>\@ydata, -xData=>\@xdata, -name=>"Cryoco +oler"); $pw_plot->plot(-dataSet=>$ds); MainLoop;

However, I'm beginning to think this is not the best way to handle time data. I could use seconds since epoch but thats horribly unreadable for the user, not that this way is much better. Any suggestions on handling time data in graphs?
Thank you for you time

PS I can post the entirety of the code if need be, just didnt because its ~1000 lines.

-----------------------------------------

foreach(@the_wicked){sleep(0);}

Replies are listed 'Best First'.
Re: Problems with graphing time vs data with Tk
by jwkrahn (Abbot) on Apr 16, 2009 at 00:34 UTC
    my $time_ = localtime; my $date = ParseDate($time_); $date =~ s/://g; my $unix_date = UnixDate($date, @d_format); $unix_date =~ s/ //g; $ash_{date} = $unix_date; $ash_{temp} = gct();

    What does @d_format contain?   Why put the spaces in the format string and then just remove then?   You should probably use POSIX::strftime, for example:

    use POSIX 'strftime'; $ash_{date} = strftime $d_format[0], localtime;

    my %cash = build_comp_hash(); push @aoh, { %cash }; graphable_arrays();

    Why are you copying the hash twice?   Just copy it once:

    push @aoh, { build_comp_hash() };

    @xdata = (); @ydata = (); my($i, $datum); for $i (0 .. $#aoh){ for $datum (keys %{ $aoh[$i] }){ if($datum =~ m/date/){ push @xdata, $aoh[$i]{$datum}; } elsif($datum =~ m/temp/){ push @ydata, $aoh[$i]{$datum}; } else{} }

    It looks like your keys exactly match 'date' and 'temp' so there is no need to iterate through all the keys to find them:

    @xdata = (); @ydata = (); for my $hash ( @aoh ) { push @xdata, $hash->{ date }; push @ydata, $hash->{ temp }; }

      Ok thanks for your tips, but I am still at a loss as to how to deal with this time problem...
Re: Problems with graphing time vs data with Tk
by zentara (Cardinal) on Apr 16, 2009 at 12:38 UTC