<div dir="ltr">See below<br><br><div class="gmail_quote">On Fri, Sep 26, 2008 at 8:05 AM, Laura McCord <span dir="ltr"><<a href="mailto:mccordl@southwestern.edu">mccordl@southwestern.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I have my data loaded into an array but everytime I try to graph the<br>
data I get a "Can't call method "png" on an undefined value...<br>
<br>
I am using Perl and the Graph module to produce a bar graph. Here is a<br>
sample of my data and my source code. Perhaps someone will notice what I<br>
am doing wrong.<br>
<br>
Thanks.<br>
<br>
<br>
Here is some sample data:<br>
<br>
Fri Sep 26 00:00:00 2008 0.1<br>
Fri Sep 26 01:00:00 2008 0.0<br>
Fri Sep 26 02:00:00 2008 0.1<br>
Fri Sep 26 03:00:00 2008 0.1<br>
Fri Sep 26 04:00:00 2008 0.2<br>
Fri Sep 26 05:00:00 2008 0.1<br>
Fri Sep 26 06:00:00 2008 0.2<br>
Fri Sep 26 07:00:00 2008 0.1<br>
Fri Sep 26 08:00:00 2008 0.2<br>
Fri Sep 26 09:00:00 2008 0.2<br>
Fri Sep 26 10:00:00 2008 0.2<br>
<br>
Here is a snippet of code:</blockquote><div><br># Got use strict; use warnings; in your code? They'll help.<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
#Every Hour<br>
my $res = '60';<br>
<br>
# Start on Sep 1st<br>
my $start_t = "1220227200";</blockquote><div><br># This is really Aug 31 <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
my $start_t_res = int($start_t/$res)*$res;</blockquote><div><br># 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.<br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
#End on Today's Date<br>
my $end_t = "(int($start_t/$res)*$res)";</blockquote><div><br># unused. But you don't want to quote it anyway. That won't work.<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
my ($start,$step,$names,$data) =<br>
RRDs::fetch("stats.rrd","AVERAGE","-r","$res","-s",<br>
"$start_t_res","-e","now");</blockquote><div><br># see above about starting at wrong time. <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
<br>
print "Start: ", scalar localtime($start), " ($start)\n";<br>
print "Step size: $step seconds\n";<br>
print "DS names: ", join (", ", @$names)."\n";<br>
print "Data points: ", $#$data + 1, "\n";</blockquote><div><br># Try scalar @data here instead. <br></div><div><br># Consider renaming @array1 to @timestamps and @array2 to @values or <br># better: @whatever_those_values_actually_are. That makes it easier for<br>
# us to read your code. <br>#<br># There are faster ways to do this, but if it works, then I'll skip over it for the most part. <br># The only point I'll make is that if there are multiple DS's in your RRD, then you'll <br>
# only see the last one because the inner foreach loop will overwrite all of the prior<br># values in @line with just the last.<br>#<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
print "Data:\n";<br>
foreach my $line (@$data) {<br>
$start += $step;<br>
$min++;<br>
#Here I am only loading hourly data into the array<br>
if($min == 60){<br>
$array1[$i] = localtime($start);<br>
foreach my $val (@$line) {<br>
$array2[$i] = $val;<br>
}<br>
$i++;<br>
$min=0;<br>
}<br>
}<br>
<br>
for($i=0;$i < @array1; $i++){<br>
print $array1[$i]." ";<br>
printf "%12.1f ", $array2[$i];<br>
print "\n";<br>
}</blockquote><div><br># I prefer something like this here:<br>#<br># for my $i (0..$#timestamps) {<br># printf "$timestamps[$i]: %12.1f\n", $values[$i];<br># }<br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
@actualdata = (\@array1,\@array2);</blockquote><div><br># Let's comment the above line out and ...<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
my $graph = new GD::Graph::bars;<br>
$graph->set(<br>
x_label => 'Date/Time',<br>
y_label => 'Messages',<br>
title => 'Dropped Messages',<br>
y_max_value => 10,<br>
y_tick_number => 1,<br>
y_label_skip => 1,<br>
<br>
# shadows<br>
bar_spacing => 10,<br>
shadow_depth => 4,<br>
shadowclr => 'dred',<br>
<br>
transparent => 0,<br>
) or warn $graph->error; <br></blockquote><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
my $image = $graph->plot(\@actualdata)->png();</blockquote><div># ...move it down here where it's a little closer to the action thusly:<br>#<br># my $image = $graph->plot( [\@timestamps, \@values] )->png;<br>
<br>Maybe that'll get you a little closer.<br><br>Good luck,<br>Joshua<br></div></div><br></div>