如何使用Perl将图表添加到网页?

时间:2017-06-19 20:26:33

标签: html mysql perl graph charts

我需要使用Perl中的数据从MySQL数据库中创建一个图形。

我已经看到了GD::Graph推荐和其他方式,但我不知道如何使用其中任何一种,并且在网上找不到多少。

我为网页输入了GD::Graph脚本但没有出现任何内容。我假设我必须以某种方式合并HTML来显示图形,但我不知道如何做到这一点。

我只需要一个基本的折线图,所以任何简单的例子都会有很大的帮助。

这是我正在玩的一个示例程序,我无法开始工作:

use GD::Graph::bars;
use GD::Graph::Data;

my $data = GD::Graph::Data->new([
    ["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"],
    [    1,    2,    5,    6,    3,  1.5,    1,     3,     4],
]) or die GD::Graph::Data->error;

my $graph = GD::Graph::bars->new;

$graph->set( 
     x_label         => 'X Label',
     y_label         => 'Y label',
     title           => 'A Simple Bar Chart',

     #y_max_value     => 7,
     #y_tick_number   => 8,
     #y_label_skip    => 3,

     #x_labels_vertical => 1,

     #bar_spacing     => 10,
     #shadow_depth    => 4,
     #shadowclr       => 'dred',

     #transparent     => 0,
) or die $graph->error;

$graph->plot($data) or die $graph->error;

my $file = 'bars.png';
open(my $out, '>', $file) or die "Cannot open '$file' for write: $!";
binmode $out;
print $out $graph->gd->png;
close $out;

2 个答案:

答案 0 :(得分:1)

您的示例程序适合我。我得到一个名为bars.png的文件,其中包含预期的图像。如果您需要帮助使该程序正常工作,那么您需要向我们提供更多信息,而不是“Heres我正在玩的示例程序,我无法开始工作”。究竟是什么意外的行为?文件是写的吗?它包含正确的图像吗?你的电脑爆炸了吗?

让程序运行后,将其转换为将图像写入STDOUT的CGI程序是一个简单的过程。它看起来像这样:

#!/usr/bin/perl

use strict;
use warnings;

# Added: Use the CGI library
# (not essential, but makes your life easier)
use CGI 'header';
use GD::Graph::bars;
use GD::Graph::Data;

my $data = GD::Graph::Data->new([
    ["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"],
    [    1,    2,    5,    6,    3,  1.5,    1,     3,     4],
]) or die GD::Graph::Data->error;

my $graph = GD::Graph::bars->new;

$graph->set(
     x_label         => 'X Label',
     y_label         => 'Y label',
     title           => 'A Simple Bar Chart',

     #y_max_value     => 7,
     #y_tick_number   => 8,
     #y_label_skip    => 3,

     #x_labels_vertical => 1,

     #bar_spacing     => 10,
     #shadow_depth    => 4,
     #shadowclr       => 'dred',

     #transparent     => 0,
) or die $graph->error;

$graph->plot($data) or die $graph->error;

# Added: print the content-type header
print header('image/png');

# Removed: opening an image file
# Changed: Run binmode against STDOUT, not your image file
binmode STDOUT;
# Changed: Print image to STDOUT, not your image file
print $graph->gd->png;
# Removed: Don't close the filehandle, that you didn't open :-)

如果您将该代码放在Web服务器上的某个文件中,并将其识别为CGI程序(这样做超出了本答案的范围 - 但在此站点上有很多好的答案),那么您将能够在浏览器中输入文件的URL以查看图像。一旦有效,您还可以使用<img src="...">标记将图像嵌入到HTML页面中。

如果您需要更多帮助,请提出更具体的问题。

答案 1 :(得分:-2)

如果您对GD::Graph了解生成图像文件(例如,PNG文件)的知识,那么在网络浏览器中显示它的常用习惯就是

...
use GD::Graph;
my $graph = ...;
my $file = "bars-$$.png";     # a unique file name
# replace  /var/www/htdocs  here with the root document directory for your server
open my $out, '>', '/var/www/htdocs/bars/$file';
print $out $graph->gd->png;
close $out;

# now write the web page
print "Content-type: text/html\n\n";
print "<html><head><title>Bar Graph</title><head>\n";
print "<body>";
print ...
print "<h2>Here is a bar graph</h2>\n";
print "<img src='/bars/$file'>\n";
print ...
print "</body></html>\n";