2017-07-18 40 views
-3
use GD::Graph; 
use GD::Graph::bars; 
use GD::Graph::Data; 




print "Content-disposition:attachment;filename=UsageReportChart.csv\n\n"; 
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', 

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

試圖使用此代碼在csv文件中繪製圖形,但未下載csv文件中的任何內容。 有人可以幫助我嗎?試圖在csv文件中繪製一個圖表

+3

將圖像包含在CSV文件中沒有任何意義。 CSV =逗號分隔值。這只是純文本。 –

+0

從csv讀取並在另一個csv文件中繪製圖形。我怎樣才能做到這一點? –

+0

否則如何繪製一個PNG文件中的圖形 –

回答

0

正如其他人在評論中指出的那樣,將圖形(這是一個圖像)打印到CSV文件是一個可怕的想法。

但是,您創建圖形的代碼看起來非常接近。你只需要仔細看看the bits of the documentation,其中包括輸出最終圖像。

您可以撥打plot()方法,但在此之後還有更多要做。 plot()方法返回一個GD::Image對象,您需要調用該對象上的另一個方法(gif()png()或類似的東西)來獲取圖像。然後你需要對這些數據做些什麼(可能在某處打印)。

此代碼將您的圖形打印到STDOUT。在CGI程序中,這會將圖形圖像發送回瀏覽器。 Content-type標題爲瀏覽器提供足夠的信息來顯示該圖像。

my $img = $graph->plot($data); 

# This assumes you haven't already printed a header 
# so remove the incorrect one from above in your code 
print "Content-type: image/png\n\n"; 

print $img->png;