in reply to Bar chart of binned timestamp data

Could you use Time::Piece to convert your timestamps to seconds since the epoch using epoch(), then you'll have simple XY data and you can use GD::Graph::histogram?

I've never used that module so I'm not sure what will happen with the X labels, but hopefully you'll be able to set those yourself.

Replies are listed 'Best First'.
Re^2: Bar chart of binned timestamp data
by v4169sgr (Sexton) on May 21, 2013 at 15:31 UTC

    Thanks for the tip!

    In the end I used timegm() to convert my datetime to epoch seconds, and just sucked these into GD::Graph::histogram, using x_number_format to deal with the x bin label formatting as a datetime not seconds. Note that this does not get executed unless you also set x_tick_number!

    Results are fine; my only gripe is that the horizontal position of the label appears to drift to the right in the bin as the bin size count increases, so that I start with a centred bin label, and end up with the label hard on the right hand side.

    Here's a code snippet:

    my $graph = new GD::Graph::histogram(600,600); $graph->set( x_label => 'Date and Time', y_label => 'Messages', title => 'Number of messages per bin', x_labels_vertical => 1, bar_spacing => 0, shadow_depth => 0, transparent => 0, histogram_bins => 10, show_values => 1, x_number_format => \&x_format, x_tick_number => 10, ); sub x_format { my $value = shift; # print $value . " " . localtime(int($value)) . " " . strftime( "% +Y-%m-%d %H:%M:%S", localtime(int($value))) . "\n"; # test return strftime( "%Y-%m-%d %H:%M:%S", localtime(int($value))); } my $timevalues_ref = \@timevalues_secs; my $IMG; open($IMG, '> test_plot.png'); binmode $IMG; print $IMG $graph->plot($timevalues_ref)->png; close ($IMG);

    @timevalues_secs contain the timeseries data in epoch seconds.