Subject: | Poor choice of axes for bar graphs |
As the following code demonstrates, the code for choosing the axes needs to be tweaked to take into account the need for bar graphs to have their axes zeroed. The attached patch seems to fix the problem in the case of graphs with only one axis, but I'm sure there must be a cleaner way of doing this.
Without the patch, the following data ends up with range [1,6], five ticks => step size of 1, but then the min_y is subsequently set to zero, resulting in a step size of 1.2 - not very aesthetically pleasing (the graph is counting whole objects). With the patch, the range is [0,6], so _best_ends chooses a step size of 2 and increases the range to [0,10] - which is exactly what we want in this case.
Cheers,
Jamie Walker.
#!/usr/bin/perl
use GD::Graph::bars;
my $graph=GD::Graph::bars->new(580,250);
$graph->set(
bar_spacing => 8,
x_labels_vertical => 1,
x_label => 'Location',
y_label => 'No of xxxx',
title => 'Location',
dclrs => ['blue']
);
my @data = (
[ qw/A B C D E F G H I J K L M N O Other Unrecorded/ ],
[1, 1, 1, 2, 1, 3, 6, 2, 1, 1, 1, 3, 5, 4, 1, 1, 5 ]
);
$graph->plot(\@data);
print($graph->gd->png());
--- axestype.pm.orig 2003-06-15 13:01:48.000000000 +0100
+++ axestype.pm 2003-06-15 13:03:24.000000000 +0100
@@ -1451,10 +1451,12 @@
}
else
{
($y_min, $y_max) = $self->{_data}->get_min_max_y_all;
}
+ $y_min = 0 if ($self->isa("GD::Graph::bars") or $self->isa("GD::Graph::area")) && $y_min > 0;
+ $y_max = 0 if ($self->isa("GD::Graph::bars") or $self->isa("GD::Graph::area")) && $y_min < 0;
($self->{y_min}[1], $self->{y_max}[1], $self->{y_tick_number}) =
_best_ends($y_min, $y_max, @$self{'y_tick_number','y_min_range'});
}
if (defined($self->{x_tick_number}))