[rrd-users] Problems graphing
Joshua Keroes
joshua at keroes.com
Fri Sep 26 22:31:05 CEST 2008
See below
On Fri, Sep 26, 2008 at 8:05 AM, Laura McCord <mccordl at southwestern.edu>wrote:
> I have my data loaded into an array but everytime I try to graph the
> data I get a "Can't call method "png" on an undefined value...
>
> I am using Perl and the Graph module to produce a bar graph. Here is a
> sample of my data and my source code. Perhaps someone will notice what I
> am doing wrong.
>
> Thanks.
>
>
> Here is some sample data:
>
> Fri Sep 26 00:00:00 2008 0.1
> Fri Sep 26 01:00:00 2008 0.0
> Fri Sep 26 02:00:00 2008 0.1
> Fri Sep 26 03:00:00 2008 0.1
> Fri Sep 26 04:00:00 2008 0.2
> Fri Sep 26 05:00:00 2008 0.1
> Fri Sep 26 06:00:00 2008 0.2
> Fri Sep 26 07:00:00 2008 0.1
> Fri Sep 26 08:00:00 2008 0.2
> Fri Sep 26 09:00:00 2008 0.2
> Fri Sep 26 10:00:00 2008 0.2
>
> Here is a snippet of code:
# Got use strict; use warnings; in your code? They'll help.
>
> #Every Hour
> my $res = '60';
>
> # Start on Sep 1st
> my $start_t = "1220227200";
# This is really Aug 31
>
> my $start_t_res = int($start_t/$res)*$res;
# And if you start to --start from this time, then you've just overflowed
time_t and you're trying to start at Wed Dec 31 15:59:59 1969, or time 0.
>
> #End on Today's Date
> my $end_t = "(int($start_t/$res)*$res)";
# unused. But you don't want to quote it anyway. That won't work.
>
> my ($start,$step,$names,$data) =
> RRDs::fetch("stats.rrd","AVERAGE","-r","$res","-s",
> "$start_t_res","-e","now");
# see above about starting at wrong time.
>
>
> print "Start: ", scalar localtime($start), " ($start)\n";
> print "Step size: $step seconds\n";
> print "DS names: ", join (", ", @$names)."\n";
> print "Data points: ", $#$data + 1, "\n";
# Try scalar @data here instead.
# Consider renaming @array1 to @timestamps and @array2 to @values or
# better: @whatever_those_values_actually_are. That makes it easier for
# us to read your code.
#
# There are faster ways to do this, but if it works, then I'll skip over it
for the most part.
# The only point I'll make is that if there are multiple DS's in your RRD,
then you'll
# only see the last one because the inner foreach loop will overwrite all of
the prior
# values in @line with just the last.
#
> print "Data:\n";
> foreach my $line (@$data) {
> $start += $step;
> $min++;
> #Here I am only loading hourly data into the array
> if($min == 60){
> $array1[$i] = localtime($start);
> foreach my $val (@$line) {
> $array2[$i] = $val;
> }
> $i++;
> $min=0;
> }
> }
>
> for($i=0;$i < @array1; $i++){
> print $array1[$i]." ";
> printf "%12.1f ", $array2[$i];
> print "\n";
> }
# I prefer something like this here:
#
# for my $i (0..$#timestamps) {
# printf "$timestamps[$i]: %12.1f\n", $values[$i];
# }
@actualdata = (\@array1,\@array2);
# Let's comment the above line out and ...
>
> my $graph = new GD::Graph::bars;
> $graph->set(
> x_label => 'Date/Time',
> y_label => 'Messages',
> title => 'Dropped Messages',
> y_max_value => 10,
> y_tick_number => 1,
> y_label_skip => 1,
>
> # shadows
> bar_spacing => 10,
> shadow_depth => 4,
> shadowclr => 'dred',
>
> transparent => 0,
> ) or warn $graph->error;
>
> my $image = $graph->plot(\@actualdata)->png();
# ...move it down here where it's a little closer to the action thusly:
#
# my $image = $graph->plot( [\@timestamps, \@values] )->png;
Maybe that'll get you a little closer.
Good luck,
Joshua
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.oetiker.ch/pipermail/rrd-users/attachments/20080926/c36102fd/attachment.html
More information about the rrd-users
mailing list