sanku has asked for the wisdom of the Perl Monks concerning the following question:

hai monks, i am using GD:Graph module to generate bar charts,thanks for your helps regarding my previous doubts in the module. i need one more clarification about the bar charts heights and widths. According to the data's in the bar chart the height and width of the chart should change as per the query. how do i go about it . here is the code which i have.
my $mygraph = GD::Graph::bars->new(600,500); $mygraph->set( title => "$pairs[0]", x_label => "@xaxis", x_labels_vertical => 90, # xaxis.stubvert => 'yes', # xaxis.stubs => 'text', bar_width => 10, bar_spacing => 1, show_values => 1, dclrs => ['green'] , axislabelclr => 'red', # logo => 'bgcolor1.png', /corect on/ # logo_resize =>5.5, transparent => 1, shadowclr => 'gray', shadow_depth =>3, borderclr=>'green', transparent => '0', bgclr => 'lgray', boxclr => 'lgray', fgclr => 'white', cycle_clrs => '1', ) or warn $mygraph->error; $mygraph->set_title_font(GD::gdGiantFont); $mygraph->set_x_axis_font(GD::gdMediumBoldFont); $mygraph->set_y_axis_font(GD::gdMediumBoldFont); $mygraph->set_values_font(GD::gdSmallFont);

Replies are listed 'Best First'.
Re: GD module doubt
by ady (Deacon) on Nov 06, 2006 at 08:03 UTC
    Enclosed a couple of code snippets from a stat. program I currently have in production, that might help you.
    You have to explicitly calculate the Y min and max from your data sets, and then set the graph y_min_value and y_max_value options accordingly to scale the graph.

    <more> You may chose to do this in a plot 'wrapper' function.
    ### ====================================================== ### UTIL: plot (using GD::Graph::$type) ### ====================================================== sub plot { ### Set options for graph, cf params and default my ($rOpt, $rData, $type, $x, $y) = @_; # %Opt, @data, ... $type ||= 'hb'; # Default horiz. bars $x ||= max(scalar(@{$rData->[1]})*20, 150); # Scale to num. data.p +ts $y ||= 400; # Fixed (dyn: max(ceil +($s/2), 400);) my @opt = %{$rOpt}; # Flatten option hash +to array my $graph; # (width:y, height:x) if ($type eq 'lp') { $graph = GD::Graph::linespoints->new($y, $x); + } if ($type eq 'hb') { $graph = GD::Graph::hbars->new($y, $x); } $graph->set_x_axis_font(gdTinyFont); $graph->set(logo => ".\\KMD.gif", logo_resize => 0.5, logo_position => "LL", ); $graph->set(@opt) or die $graph->error; ### Plot graph, cf @$rData array my $gd = $graph->plot($rData) or die $graph->error; my $dir = Win32::GetCwd() . "\\plot"; unless (-e $dir and -d $dir) { mkdir($dir) or die "can't mkdir $di +r: $!"; } open(IMG, ">$dir\\$rOpt->{title}.png") or die "can't open $dir\\$t +.png: $!"; binmode IMG; print IMG $gd->png; }
    Example of calling the plot util function:
    ### ------------------------------------------------------ ### Plot SDPI-$y-$mm-Load-($avg[0],$avg[1]) my $max = max(@{$data[1]},@{$data[2]}); # max val. data pt. $avg[0] = sum(@{$data[1]})/scalar(@{$data[1]}); $avg[1] = sum(@{$data[2]})/scalar(@{$data[2]}); $avg = sprintf( "%.1f,%.1f", $avg[0],$avg[1] ); my %opt = ( title => "SDPI-$y-$month[0][$m]-Load-($avg)", x_label => "Hour / Day", y_label => 'Forms/Min: Avg.(green) & Max(red)', y_min_value => 0, y_max_value => $max+1, #ceil($max + $max*0.1), y_tick_number => $max+1, y_number_format => "%3d", two_axes => 1, show_values => 1, values_vertical => 1, values_space => 8, ); plot(\%opt, \@data, 'lp', 400); # Float $opt{title} .= '-'; # Fixed $opt{y_max_value} = $opt{y_tick_number} = 40; plot(\%opt, \@data, 'lp', 400);
    </more> Best regards
    allan
Re: GD module doubt
by Khen1950fx (Canon) on Nov 06, 2006 at 11:51 UTC
    Hi, sanku. I just noticed that you used borderclr. It should be borderclrs.
A reply falls below the community's threshold of quality. You may see it by logging in.