我正在使用小型服务器来提供包含与我的家庭自动化设置相关的数据的页面。我有一个充满传感器数据的数据库,如果我取消注释底部的部分,我可以成功查询并输出到图形图像。当通过浏览器作为 cgi 脚本访问它时,我还可以通过将 sqlite 代码替换为在中间附近注释掉的手动创建的 out1 和 out2 来打印图表。问题在于试图将它们放在一起并将图形化的 sqlite 数据作为浏览器中的 cgi 脚本查看。我已将问题缩小到连接代码。当我将它添加到其他工作脚本中时,您不会得到任何图像输出;但我知道连接代码有效。它必须是显而易见的,但我就是找不到。想法?
#!/usr/bin/perl -w
use CGI ':standard';
use DBI;
use GD::Graph::lines;
use strict;
my $dbh = DBI->connect(
"dbi:SQLite:dbname=/root/distancetest",
"",
"",
{ RaiseError => 1}
) or die $!;
my $sth = $dbh->prepare("SELECT * FROM distance");
$sth->execute();
my $row;
my @data;
my @out1;
my @out2;
while ($row = $sth->fetchrow_arrayref()) {
push(@out1, @$row[0]);
push(@out2, @$row[1]);
}
#@out1 = (1,2,3,4,5);
#@out2 = (1,2,3,4,5);
$data[0] = \@out1;
$data[1] = \@out2;
$sth->finish();
$dbh->disconnect();
my $skip = int ((@data * 8)/(600-50) + 1);
my $mygraph = GD::Graph::lines->new(600, 300);
$mygraph->set(
x_label => 'Timestamp',
y_label => 'cm',
title => 'Distance measuted at time',
line_types => [1],
line_width => 2,
x_labels_vertical => 1,
x_label_skip => $skip,
dclrs => ['blue'],
) or warn $mygraph->error;
$mygraph->set_legend_font(GD::gdMediumBoldFont);
$mygraph->set_legend('sensor1');
my $myimage = $mygraph->plot(\@data) or die $mygraph->error;
print "Content-type: image/png\n\n";
#open(OUTPUT, ">$0.png") or die "Can't open $0.png: $!\n";
#print OUTPUT $myimage->png();
print $myimage->png;