in reply to Re: Bar chart of binned timestamp data
in thread Bar chart of binned timestamp data
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.
|
|---|