Hi again, thank you for trying to help! I have been looking at the script once again, I could not really get your suggestion to work, but I have been doing some new changes thought. With this script I am pasting her, I manage to print the graph to a .png file, but the histogram does not show. The x and y axes should be correct, but there is some thing wrong whit the $xydata I think, I have also been trying to only print the @ydata but the histograms will not show.
Any suggestions? #!/usr/bin/perl # Needed pkg use Getopt::Std; use strict; use warnings; use GD::Graph::bars; # Global variables my $VERBOSE = 0; my $DEBUG = 0; ################################################################ # handle flags and # Example: c == "-c", c: == "-c argument" my $opt_string = 'hvdf:'; getopts( "$opt_string", \my %opt ) or usage() and exit 1; # exit other than 0 = error!! # print help message if -h is invoked if ( $opt{'h'} ){ usage(); exit 0; } $VERBOSE = 1 if $opt{'v'}; $DEBUG = 1 if $opt{'d'}; my $FILENAME = $opt{'f'}; # main program content my $filename = $FILENAME; my $line; my @dataarray; my @array; my $counter; my $histmin; my $histmax; open (FILE, "$filename") or die ("ERROR: $!\n"); while ($line = <FILE>){ debug($line); chomp $line; @array = split / /, $line; @array = sort @array; $histmin = $array[0]; $histmax = $array[-1]; $counter++; } my %data; foreach my $tmp (@array) { $data{$tmp}+=1; } my @xdata = keys(%data); my @ydata = keys(%data); my @xydata=(\...@xdata, \...@ydata); printHistogram(\...@xydata,"$filename",$histmin,$histmax,"Title"); ########################################## # Helper routines sub usage { # print the correct use of this script print "Usage:\n"; print "-h Usage\n"; print "-v Verbose\n"; print "-d Debug\n"; print "-f Filename\n"; } sub verbose { print $_[0] if ( $VERBOSE or $DEBUG ); } sub debug { print $_[0] if ( $DEBUG ); } sub printHistogram { my ($xydata,$filename,$histmin,$histmax,$title) = @_; my $graph = GD::Graph::bars->new(600, 300); $graph->set( y_label => 'Frequency', title => $title, y_max_value => 10, y_min_value => 0, x_max_value => $histmax, x_min_value => $histmin, y_tick_number => 10, x_tick_number => 16, y_label_skip => 2, x_label_skip => 1, x_tick_offset => 1 ) or die $graph->error; $graph->set_legend('Frequency'); my $gd = $graph->plot($xydata) or die $graph->error; open(IMG, ">$filename.png") or die $!; binmode IMG; print IMG $gd->png; close IMG; } Den 7. apr. 2010 kl. 15.05 skrev Jins Thomas: > Hi alekto, > > If this is the case, you should sort help of hash in perl imho,may be others > can suggest some other ways as well.. > > See the %data and population of @xydata below. And supply the @xydata to > plot() . Did u get me. So finally a working code will look some thing like > this... See the ##Modified begin/end## comment to separate out the changes. > > Others please suggest if someother better way :) > > > # Needed pkg > use Getopt::Std; > use strict; > use warnings; > use GD::Graph::bars; > > # Global variables > my $VERBOSE = 0; > my $DEBUG = 0; > > ################################################################ > # handle flags and > # Example: c == "-c", c: == "-c argument" > my $opt_string = 'hvdf:'; > getopts( "$opt_string", \my %opt ) or usage() and exit 1; # exit other than 0 > = error!! > > # print help message if -h is invoked > if ( $opt{'h'} ){ > usage(); > exit 0; > } > > $VERBOSE = 1 if $opt{'v'}; > $DEBUG = 1 if $opt{'d'}; > my $FILENAME = $opt{'f'}; > > # main program content > > my $filename = $FILENAME; > my $line; > my @dataarray; > my @array; > my $counter; > my $histmin; > my $histmax; > > open (FILE, "$filename") or die ("ERROR: $!\n"); > > while ($line = <FILE>){ > debug($line); > chomp $line; > @array = split / /, $line; > @array = sort @array; > $histmin = $array[0]; > $histmax = $array[-1]; > $counter++; > } > #######Modified begin ######################### > my %data; > foreach my $tmp (@array) { > $data{$tmp}+=1; > } > my @xdata = keys(%data); > my @ydata = values(%data); > my @xydata=(\...@xdata, \...@ydata); > #######Modified end######################### > # print "$histmin\n$histmax\n"; > # print "min: $array[0]\nmax: $array[-1]\n"; > # print "@dataarray\n"; > #######Modified begin ######################### > printHistogram(\...@xydata,$filename,$histmin,$histmax,'Age'); > #######Modified end ######################### > > > ########################################## > # Helper routines > > sub usage { > # print the correct use of this script > print "Usage:\n"; > print "-h Usage\n"; > print "-v Verbose\n"; > print "-d Debug\n"; > print "-f Filename\n"; > } > > sub verbose { > print $_[0] if ( $VERBOSE or $DEBUG ); > } > > sub debug { > print $_[0] if ( $DEBUG ); > } > > sub printHistogram { > #######Modified begin######################### > my ($xydata,$filename,$histmin,$histmax,$title) = @_; > #######Modified end ######################### > my $graph = GD::Graph::bars->new(400, 300); > > $graph->set( > x_label => 'X Label', > y_label => 'Y label', > title => '$title', > y_max_value => $histmax, > y_min_value => $histmin, > y_tick_number => 8, > y_label_skip => 2 > ) or die $graph->error; > #######Modified begin ######################### > my $gd = $graph->plot($xydata) or die $graph->error; > #######Modified end ######################### > > open(IMG, ">$filename.png") or die $!; > binmode IMG; > print IMG $gd->png; > close IMG; > } > > On Wed, Apr 7, 2010 at 6:03 PM, alekto <alekto.antarct...@gmail.com> wrote: > Hi, Thanks for helping! > The ting is that my age file only contain age values like this: 22 22 23 24 > 26 26 26 26 28 28 30 30, and I have no input for a second array! > I want these age values to be the bars(x-values), and a want the number of > times each of these are represented, to be the y-values. > Could this be done with the script I am working on, or must I do some changes > in order to make this work? > > X Alekto > > > > Den 7. apr. 2010 kl. 11.44 skrev Jins Thomas: > >> Hi Alekto, >> >> How your file content looks like? Please paste few lines from your input >> file age. >> >> Are you supplying X axis and Y axis values properly, basically you should >> supply two arrays, one for X axis and and one for Y axis. >> >> See the example in >> >> http://gdgraph.com/samples/sample11.html >> >> >> Cheers >> Jins >> >> >> >> On Wed, Apr 7, 2010 at 12:34 PM, alekto <alekto.antarct...@gmail.com> wrote: >> Hi guys! >> Tnx for answering! Of course it was the /misplacing of the \ that caused my >> error;) >> But after changing this error I still get an other error msg like this: >> >> Invalid data set: 1 at ./bars.pl line 90, <FILE> line 1. >> >> which is referring to this line in my script: >> >> my $gd = $graph->plot(\...@array) or die $graph->error; >> >> I'm not sure what this error means?? >> >> Xalekto >> >> >> >> Den 7. apr. 2010 kl. 07.23 skrev Jins Thomas: >> >> > Hi alekto, >> > >> > My guess >> > >> > Main error is due to >> > my $gd = $graph->plot(/@array) (Line 90) Did u meant \ (bless operator) >> > instead of / . Same is being repeated in printHistogram(/@array >> > >> > Also hope last } was for printHistogram subroutine was copy paste problem >> > while emailing. Other than that this looks fine. >> > >> > Let me know you require some further help on this. >> > >> > >> > Cheers >> > Jins Thomas >> > >> > >> > >> > On Wed, Apr 7, 2010 at 9:20 AM, alekto <alekto.antarct...@gmail.com> wrote: >> > Hi, >> > I got this scrip that is suppsed to use the use the Getopt::Std; and >> > GD::Graph::bars; to input generate an array from a file (age). >> > With this array, the GD::Graph::bars is going to create a subroutine >> > printHistogram() which generates a png file based on a supplied array. >> > The signature of the method could be like this: >> > printHistogram(\...@histy,"$filename",$histmin,$histmax,"Title"); >> > >> > I manage to generate the array from the input file, but it seems like >> > there is something wrong with my subroutine at the end, I have been using >> > the examples at cpan.org as an templat for this subroutine. >> > Following is the error msg, as well as the complete script. >> > Tnx! >> > >> > >> > The ERROR msg: >> > >> > hostname$ ./bars.pl -f age >> > ./bars.pl: line 5: use: command not found >> > Array found where operator expected at ./bars.pl line 90, at end of line >> > (Might be a runaway multi-line // string starting on line 53) >> > (Missing operator before ?) >> > Global symbol "$title" requires explicit package name at ./bars.pl line 53. >> > Global symbol "$graph" requires explicit package name at ./bars.pl line 53. >> > Global symbol "$graph" requires explicit package name at ./bars.pl line 53. >> > Global symbol "$title" requires explicit package name at ./bars.pl line 53. >> > Global symbol "$graph" requires explicit package name at ./bars.pl line 53. >> > Global symbol "$gd" requires explicit package name at ./bars.pl line 53. >> > Global symbol "$graph" requires explicit package name at ./bars.pl line 53. >> > syntax error at ./bars.pl line 90, near "my $gd = $graph->plot(/@array" >> > Global symbol "$graph" requires explicit package name at ./bars.pl line 90. >> > Global symbol "$gd" requires explicit package name at ./bars.pl line 95. >> > Unmatched right curly bracket at ./bars.pl line 97, at end of line >> > ./bars.pl has too many errors.' >> > >> > >> > >> > #!/usr/bin/perl -w >> > >> > # Needed pkg >> > use Getopt::Std; >> > use strict; >> > use warnings; >> > use GD::Graph::bars; >> > >> > # Global variables >> > my $VERBOSE = 0; >> > my $DEBUG = 0; >> > >> > ################################################################ >> > # handle flags and >> > # Example: c == "-c", c: == "-c argument" >> > my $opt_string = 'hvdf:'; >> > getopts( "$opt_string", \my %opt ) or usage() and exit 1; # exit other >> > than 0 = error!! >> > >> > # print help message if -h is invoked >> > if ( $opt{'h'} ){ >> > usage(); >> > exit 0; >> > } >> > >> > $VERBOSE = 1 if $opt{'v'}; >> > $DEBUG = 1 if $opt{'d'}; >> > my $FILENAME = $opt{'f'}; >> > >> > # main program content >> > >> > my $filename = $FILENAME; >> > my $line; >> > my @dataarray; >> > my @array; >> > my $counter; >> > my $histmin; >> > my $histmax; >> > >> > open (FILE, "$filename") or die ("ERROR: $!\n"); >> > >> > while ($line = <FILE>){ >> > debug($line); >> > chomp $line; >> > @array = split / /, $line; >> > @array = sort @array; >> > $histmin = $array[0]; >> > $histmax = $array[-1]; >> > $counter++; >> > } >> > # print "$histmin\n$histmax\n"; >> > # print "min: $array[0]\nmax: $array[-1]\n"; >> > # print "@dataarray\n"; >> > printHistogram(/@array,$filename,$histmin,$histmax,'Age'); >> > >> > >> > ########################################## >> > # Helper routines >> > >> > sub usage { >> > # print the correct use of this script >> > print "Usage:\n"; >> > print "-h Usage\n"; >> > print "-v Verbose\n"; >> > print "-d Debug\n"; >> > print "-f Filename\n"; >> > } >> > >> > sub verbose { >> > print $_[0] if ( $VERBOSE or $DEBUG ); >> > } >> > >> > sub debug { >> > print $_[0] if ( $DEBUG ); >> > } >> > >> > sub printHistogram { >> > my (@array,$filename,$histmin,$histmax,$title) = @_; >> > my $graph = GD::Graph::bars->new(400, 300); >> > >> > $graph->set( >> > x_label => 'X Label', >> > y_label => 'Y label', >> > title => '$title', >> > y_max_value => $histmax, >> > y_min_value => $histmin, >> > y_tick_number => 8, >> > y_label_skip => 2 >> > ) or die $graph->error; >> > >> > my $gd = $graph->plot(/@array) or die $graph->error; >> > >> > >> > open(IMG, '>$filename.png') or die $!; >> > binmode IMG; >> > print IMG $gd->png; >> > close IMG; >> > >> > >> > -- >> > To unsubscribe, e-mail: beginners-unsubscr...@perl.org >> > For additional commands, e-mail: beginners-h...@perl.org >> > http://learn.perl.org/ >> > >> > >> > >> >> > >