Subject: | Divide by zero error on two axis graph when all data points are zero |
This bug is similar to, but different from bug 512.
Using GD::Graph 1.40 and also checked against 1.42, perl 5.8.0 under linux.
When creating a 2 axis graph, if there is no data, or it's all zeros, you get a divide by zero error in axestype.pm. Line 1705 of axestype.pm rev1.42 has a comment asking if $scale_2 should be checked for zero, which is exactly what causes the problem.
I modified the test case code from bug 512 to demonstrate this error:
#!/usr/bin/perl
use strict;
use warnings;
use GD::Graph::lines;
my($graph, $image, $margin, @data, $filename);
$filename = "/tmp/gd_graph_axis_bug.png";
$margin = 5;
$graph = GD::Graph::lines::->new(700 ,450);
$graph->set( two_axes => 1,
y1_label => 'y label',
y2_label => 'other y label',
y1_min_value => 0,
y2_min_value => 0,
y2_ticks => 1,
y2_plot_values => 1,
y_tick_number => 6,
x_label => 'x_label',
title => 'title',
x_label_position => 0,
y_label_skip => 2,
x_labels_vertical => 1,
y_long_ticks => 1,
y_number_format => "%i",
bar_width => 5,
);
@data = ([ 'label1', 'label2' ],
[ 0,0 ],
[ 0,0 ],
);
$image = eval {
$graph->plot(\@data);
};
if($@ or !defined $image) {
die("PLOT FAILED: $@\n");
}
print "READY TO WRITE IMAGE: $filename\n";
open(IMG, ">", $filename) or die("Unable to open $filename : $!");
binmode IMG;
print IMG $image->png;
close IMG;