2017-06-19 9 views
1

私は、PerlのデータをMySQLデータベースから取得してグラフを作成する必要があります。Perlを使ってWebページにグラフを追加するには?

私は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

イメージファイル(たとえば、PNGファイル)を生成するGD::Graphについて十分に知っている場合は、通常のWebブラウザで表示するためのイディオムは

... 
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

[Windowsでの正しい動作]を確実にするには、 'binmode $ out'または' open my $ out、 ':unix'、... 'が必要です(https://www.nu42.com/2014/12/when- bits-dont-stick.html) –

+0

CGIプログラムで生のHTMLを使用することを推奨することは決してありません。 –

1

サンプルプログラムが私に役立ちます。私はbars.pngと呼ばれるファイルを入手し、それには期待される画像が含まれています。あなたがそのプログラムを稼働させるのに役立つ必要があれば、「私が働くことができなかったプログラムの例を聞く」よりも多くの情報を私たちに与える必要があります。まったく予期しない動作が起こりましたか?ファイルは書き込まれましたか?正しい画像が含まれていましたか?あなたのコンピュータは炎に爆発しましたか?

プログラムを動作させると、イメージをSTDOUTに書き込むCGIプログラムに変換するのは簡単なプロセスです。あなたはそれをやってCGIプログラムは(この答えの範囲外であるとして、それが認識されているWebサーバー上のどこかのファイルにそのコードを配置する場合

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

- しかし、たくさんあります:それはこのようになりますこのサイトでそれについての良い回答がある場合)、ブラウザにファイルのURLを入力して画像を見ることができます。それが機能すると、<img src="...">タグを使用してHTMLページに画像を埋め込むこともできます。

これ以上のヘルプが必要な場合は、さらに具体的な質問をしてください。

+0

これは非常に役に立ち、私が探していた結果を正確に得ました。ありがとう – Nicolas

+0

@ニコラス:[誰かが私の質問に答えるときに何をするか](https://stackoverflow.com/help/someone-answers) –

関連する問題