in reply to GD::Graph issue with similar datapoints

Hi

Looks like a bug, unfortunately; it only seems to get tripped if you've got a data series with all the same values which are all negative numbers.

No promises but you might try inserting the following after you use the packages, but before you write your main code. It redefines the method with the bug in it to a version that may work better. You'll see from the original comments that the author (mjv) has already guessed it's going to happen, so there may be some regression problems with applying it.

use GD::Graph::lines; package GD::Graph::axestype; # this is the function which I think is buggy sub _best_ends { my ($min, $max, $n_ref, $min_range) = @_; # Adjust for the min range if need be ($min, $max) = _fit_vals_range($min, $max, $min_range); my ($best_min, $best_max, $best_num) = ($min, $max, 1); # mgjv - Sometimes, for odd values, and only one data set, this wi +ll be # necessary _after_ the previous step, not before. Data sets of on +e # long with negative values were causing infinite loops later on. # THE BUG'S AROUND HERE - I swapped this line and the one after # Check that min and max are not the same, and not 0 ($min, $max) = ($min) ? ($min * 0.5, $min * 1.5) : (-1,1) if ($max == $min); ($min, $max) = ($max, $min) if ($min > $max); my @n = ref($n_ref) ? @$n_ref : $n_ref; if (@n <= 0) { @n = (3..6); } else { @n = map { ref($_) ? @$_ : /(\d+)/i ? $1 : (3..6) } @n; } my $best_fit = 1e30; my $range = $max - $min; # create array of interval sizes my $s = 1; while ($s < $range) { $s *= 10 } while ($s > $range) { $s /= 10 } my @step = map {$_ * $s} (0.2, 0.5, 1, 2, 5); for my $n (@n) { # Try all numbers of intervals next if ($n < 1); for my $step (@step) { next if ($n != 1) && ($step < $range/$n); # $step too smal +l my ($nice_min, $nice_max, $fit) = _fit_interval($min, $max, $n, $step); next if $best_fit <= $fit; $best_min = $nice_min; $best_max = $nice_max; $best_fit = $fit; $best_num = $n; } } return ($best_min, $best_max, $best_num) } package main; # now continue ...

As I say, I haven't tested it extensively, and it might break other stuff, but it might just work for you. You might also want to see about setting the 'y_min_value' and 'y_max_value' parameters explicitly.

cheers
ViceRaid

Replies are listed 'Best First'.
Re: Re: GD::Graph issue with similar datapoints
by scottj (Monk) on Feb 05, 2004 at 05:08 UTC

      OK, but really, really, don't let it put you off. It's unlucky to hit a bug, but GD::Graph is a very good package. I first used it several years back, and have just had cause to use it again (financial data ... yawn ...) and I get the feel that it's been substantially improved from what was already a very useful package. I've introduced it alongside a (closed-source) Java charting tool, and the speed and ease of getting very good-looking charts out of it quickly made me feel warm and happy about Perl.

      cheers
      ViceRaid