CHN-mk CHN-mk - 1 year ago 60
Perl Question

Perl GD::Graph Invalid data set: 0 at (pie)

i am trying to make a little PERL-Script (i am an beginner!)

I took an example Code and editet it to my needs.

So the task is to read data from a csv file put them into an html-table and also to show a diagram in pie form.

The table already works, only the pie diagram is my problem. I already looked and tried many changes within the diagram part in the code but not win bringing.

Here is my code:

#!C:\Perl64\bin\perl.exe -w
### Variablendeklarationen und Moduleinbindungen ###
use strict;

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use DBI;
my $DBH = DBI->connect('DBI:CSV:');
my $STH;
use CGI::Carp 'fatalsToBrowser';

### Statement-Vorbereitung ###
$DBH->{'csv_tables'}->{'daten'} = { 'file' => 'daten.csv'}
or die "Konnte Datenbank nicht oeffnen:$!";
$STH = $DBH->prepare("SELECT * FROM daten")
or die "Konnte SQL-Statement nicht ausfuehren:$!";

or die "Ausfuehren der Datenbankabfrage nicht moeglich:$!";

print <<HERE_TEXT;

<title>Datenanzeige CSV-File</title>

<h1>Folgende Umsatzdaten sind ausgelesen worden:</h1>
<table border>
<td width="200"><b>Filiale:</b></td>
<td width="100"><b>Leiter:</b></td>
<td width="200"><b>Mitarbeiter:</b></td>
<td width="100"><b>Umsatz:</b></td>


my @data;
my @diagarray;

while (@data = $STH->fetchrow_array()) {
my $filiale = $data[0];
my $leiter = $data[1];
my $mitarbeiter = $data[2];
my $umsatz = $data[3];

push (@diagarray, $umsatz);
print qq§<tr>\n<td><b>$filiale</b></td>\n<td>$leiter</td>\n<td>$mitarbeiter</td>\n<td>$umsatz</td>\n</tr>\n§;
print ("<br><br>");

use GD::Graph::pie;
my $graph = GD::Graph::pie->new(300, 300);

title => 'Umsatzverteilung Filialen',
) or die $graph->error;

#my @diagram = (\@data,\@diagarray);

#my $diagram;
# foreach $diagram(@diagram)
# {
# print ("$diagram\n");
# }

my $gd = $graph->plot(\@diagarray) or die $graph->error;

my $format = $graph->export_format;
print header("image/$format");
binmode STDOUT;
print $graph->plot(\@diagarray)->$format();

Would be great if anyone could give me the last needed hint.

Answer Source

Getting two array refs into @diagarray isn't any different to how you're pushing a scalar in to it.


But you want that to happen outside of the while-loop. Inside the while-loop is where you'd populate @labels and @values. Both arrays have to be the same size.

Also your script is trying to output the HTML and piechart in one go which won't work as your browser will just treat it all as just one lump of HTML. Your HTML needs to have an "img" tag in it that points at another URL. That URL can be the same script but with a different query string. For example

use CGI
my $query=new CGI;
if($query->param("piechart")) {
   # print out the graph
} else {
   print "<img src=\"",$ENV{"SCRIPT_NAME"},"?piechart=1\"/>";

Or alternatively you could split the piechart code into an entirely separate script, but that makes it less easy to maintain as you'd have to update two scripts if the code for reading in the data ever changed.