in reply to Histogram in Perl

I installed the module, and tried it using the bits and snippets from the module's POD, with no luck. In digging through the package's contents, though, I found 'scripts/histogram.pl', which did work. In comparing the code there with the snippets from the POD, the issue appears to be with the data. The example in the POD for the 1.0 version shows:

$data = [1,5,7,8,9,10,11,3,3,5,5,5,7,2,2];
When I replaced this with:
@data = (1,5,7,8,9,10,11,3,3,5,5,5,7,2,2);
I had no problem with it working.

Hope that helps.

Replies are listed 'Best First'.
Re^2: Histogram in Perl
by Jaya (Acolyte) on Feb 18, 2005 at 03:57 UTC
    Thank You very much. The example is working. since i need 3 histograms, I tried using subroutine call to draw the h +istogram chart & i am not getting the results. the code in main is as follows: my $image2 = Covarion_Modules::draw_hist(\@hist); open(IMG, '>hist.png') or die $!; binmode IMG; print IMG $image2->png; and the subroutine is : sub draw_hist{ my ($h) = @_; use CGI ':standard'; use GD::Graph::histogram; @data = @$h; my $graph = new GD::Graph::histogram(400,600); $graph->set( y_label => 'Count', x_label => 'Data', title => 'A Histogram Chart', x_labels_vertical => 1, bar_spacing => 0, shadow_depth => 1, shadowclr => 'dred', transparent => 0, ) or warn $graph->error; my $image = $graph->plot(\@data) or die $graph->error; return($image); } I am getting the error: Useless use of a constant in void context at /usr/lib/perl5/site_perl/ +5.8.0/GD/Graph/histogram.pm line 203. Use of uninitialized value in string eq at /usr/lib/perl5/site_perl/5. +8.0/GD/Graph/histogram.pm line 42. Argument "" isn't numeric in numeric eq (==) at /usr/lib/perl5/site_pe +rl/5.8.0/GD/Graph/histogram.pm line 181. Use of uninitialized value in string eq at /usr/lib/perl5/site_perl/5. +8.0/GD/Graph/histogram.pm line 42. Use of uninitialized value in string eq at /usr/lib/perl5/site_perl/5. +8.0/GD/Graph/histogram.pm line 42. any suggestions please

      I'm not sure what is causing the error you are seeing, but I can suggest the following variation that might work for you:

      sub draw_graph { my ( $filename, @data ) = @_; my $graph = new GD::Graph::histogram( 400, 600 ); $graph->set( x_label => 'Data', y_label => 'Count', title => 'A Histogram Chart', x_labels_vertical => 1, bar_spacing => 0, shadow_depth => 1, shadowclr => 'dred', transparent => 0, ) or warn $graph->error; my $gd = $graph->plot( \@data ) or die $graph->error; open( IMG, '>' . $filename ) or die $!; binmode IMG; print IMG $gd->png; }

      The difference being that the graph object does not have to be passed around, since it would appear that it is only being generated and written out. To call this, you might do something like the following examples:

      foreach ( 1, 5, 7, 8, 9, 10, 11, 3, 3, 5, 5, 5, 7, 2, 2 ) { push( @data, $_ * ( $_ + 1 ) ); } draw_graph( 'test1.png', @data ); @data = ( 1, 5, 7, 8, 9, 10, 11, 3, 3, 5, 5, 5, 7, 2, 2 ); draw_graph( 'test2.png', @data );

      HTH.

        Thank You very much. I still get the errors. I rechecked my data and the data is perfect. my data are large double numbers , say 1.09005965871078 0.585952618303551 0.759547391474863 0.585952618303551 0.759547391474863 0.585952618303551 0.585952618303551 so i tried the test program with similar data & i was not able to prod +uce the graph. So could the error be cause of the data? the error is : Useless use of a constant in void context at /usr/lib/perl5/site_perl/ +5.8.0/GD/Graph/histogram.pm line 203. Use of uninitialized value in string eq at /usr/lib/perl5/site_perl/5. +8.0/GD/Graph/histogram.pm line 42. Argument "" isn't numeric in numeric eq (==) at /usr/lib/perl5/site_pe +rl/5.8.0/GD/Graph/histogram.pm line 181. Use of uninitialized value in string eq at /usr/lib/perl5/site_perl/5. +8.0/GD/Graph/histogram.pm line 42. Use of uninitialized value in string eq at /usr/lib/perl5/site_perl/5. +8.0/GD/Graph/histogram.pm line 42.