2017-06-19 21 views
1

我需要使用Perl中的数据创建一个图形,从MySQL数据库中提取数据。如何使用Perl将图添加到网页?

我见过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

SO不是代码写入服务。请向我们展示您尝试过并需要帮助的代码。 – stevieb

+0

我并不是要求某人为我写信,只是帮助我指出正确的方向。 – Nicolas

+3

您显示的一段代码会创建一个图像,并将其打印到一个文件中。如果您想将其包含在HTML页面中,则必须包含该图像。或者,您可以将此代码用作CGI脚本,而不是打开文件,只需打印到STDOUT即可。首先发送正确的'image/png'标题。在你的HTML中的脚本中使用URL''标签。 – simbabque

回答

-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"; 
+1

为什么这更习惯,然后用'image/png'内容类型和图像数据进行响应? –

+0

您需要'binmode $ out'或'打开我的$','>:unix',...'以确保[Windows上的正确行为](https://www.nu42.com/2014/12/when- bits-dont-stick.html) –

+0

鼓励在CGI程序中使用原始HTML永远不是一个好主意。 –

1

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

得到该程序的工作,这是一个简单的过程,把它变成一个CGI程序,将图像写入STDOUT。这将是这个样子:

#!/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 :-) 

如果你把这些代码在一个文件保存在某个地方被认为是一个CGI程序的Web服务器上(这样做是这个答案的范围 - 但也有很多在这个网站上有很好的答案),那么你将能够在浏览器中输入文件的URL来查看图像。一旦有效,您还可以使用<img src="...">标记将图像嵌入到HTML页面中。

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

+0

这是非常有用的,我得到了我正在寻找的结果。谢谢 – Nicolas

+0

@Nicolas:[当某人回答我的问题时该怎么办](https://stackoverflow.com/help/someone-answers) –

相关问题