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 |