hrdzp
Posts: 2
|
Posted: 10/25/2005, 5:53 AM |
|
Hi all,
I'm new programming in perl and I found some code samples on how to use GD::Graph to write a chart.
I did modify the code to read a text file, get some values, conveting this values to a decimal value and built the graph. The idea of the script is to break each time the name changes and adding up the different values to an array.
For the firts Graph work fine, but in the next the graph is keeping the values from the previous dataset.
Could some of you help me out reviewing my code and let me know what I'm doing wrong.
I did look to reinitialize the variables but is not working either.
I would appreciate your help.
The script:
#!/usr/bin/perl
#use GD::Graph::linespoints;
# Reading the dat file
print "Enter a filename: ";
chomp($fname = <STDIN>);
if (-T $fname) # Check if it's a textfile, and how old
{
print "File $fname is textfile. ";
print "It was modified ", int(-M $fname), " days ago.\n";
open (FPTR,$fname) || die "Sorry. Can't Open File: $fname\n";
}
elsif (-B $fname) # Check if it's a binary file, and some other stuff
{
print "File $fname is executable.\n" if (-x $fname);
print "This file is ", -s $fname, " bytes.\n";
die "Since it is Binary file, we will not try to \"upcase\" this file.\n";
}
else
{die "File $fname is neither text nor binary, so it may not exist. \n" ;
}
## Better approach for large files... Work with just current input line.
$i=0;
$idproc = 'x';
$line=<FPTR>;
while ($line) # While still input lines in the file...
{
@row = split(/,/, $line);
$sch = $row[0];
$data0 = $row[1];
$data1 = $row[2];
$data2 = $row[3];
$init = $row[4];
$idproc = $sch;
while ($idproc eq $sch ) {
@time = split(/:/, $init);
$hour = $time[0];
$min = $time[1];
$sec = $time[2];
$decval = ($hour + ($min/60) + (sec/3600));
@xaxis[$i] = "'" . $data0 . "'";
@val[$i] = $decval;
$i += 1;
$line = <FPTR>;
if ($line ne undef) {
@row = split(/,/, $line);
$sch = $row[0];
$data0 = $row[1];
$data1 = $row[2];
$data2 = $row[3];
$init = $row[4];
}else{
$sch = 'exit';
}
}
@data = ([@xaxis],[@val]);
&writegraph( $idproc);
@xaxis =();
@val =();
@data =();
}
close (FPTR);
sub writegraph {
use GD;
use GD::Graph::linespoints;
my $dataset = shift;
my $graph;
my $graph = GD::Graph::linespoints->new(600,300);
$graph->set(
x_label => 'Dates',
y_label => 'Hours',
title => $dataset,
y_max_value => 24,
y_tick_number => auto,
x_labels_vertical => 1
) or die $graph->error;
#Plot Graph
my $graph_imagea;
my $graph_image = $graph->plot(\@data) or die $graph->error;
#write to file in PNG format
my $dataset = substr($dataset,0,(length($dataset)-1));
my $dataset = substr($dataset,1,(length($dataset)));
my $filename = '/cygdrive/c/OtherFiles/graphs/' . $dataset . '.gif';
print "$filename\n";
open(IMG, ">$filename") or die $!;
binmode IMG;
print IMG $graph_image->gif;
close IMG;
undef $graph_image;
}
the data file
"COMMITMENT",2005-10-17,2005-10-18 06:37AM,2005-10-17 08:00PM,"6:37:39"
"COMMITMENT",2005-10-18,2005-10-19 06:52AM,2005-10-18 08:00PM,"6:52:40"
"CVN",2005-05-01,2005-05-02 05:58AM,2005-05-01 10:00PM,"5:58:20"
"CVN",2005-05-02,2005-05-03 07:32AM,2005-05-02 10:00PM,"7:32:54"
"CVN",2005-05-03,2005-05-04 07:57AM,2005-05-03 10:00PM,"7:57:23"
I can't add the images.....
Thanks
|
|
|
Last Hero
|
Posted: 10/25/2005, 7:23 AM |
|
>YesSoftware Forums -> CodeCharge Studio -> Perl
|
|
|
hrdzp
Posts: 2
|
Posted: 10/25/2005, 10:03 AM |
|
Hi Last Hero,
mmmm I don't see your comments.
Thanks anyway.
|
|
|
|