in reply to Re: Histogram in Perl
in thread Histogram in Perl

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

Replies are listed 'Best First'.
Re^3: Histogram in Perl
by atcroft (Abbot) on Feb 18, 2005 at 04:24 UTC

    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.
        I figured it out!! its the data. just multiply the data by some decimal number say foreach $i (@data){ $i = $i *1000; } get the distinct values and proceed to the histogram. The bins seem to + work for only integers.(I am still a novice , please correct me if i + am wrong). So if anyone has decimal numbers and need to plot histogram. just scal +e it!!! Thanks a lot for the all help! This site is very helpful. I am learnin +g a lot