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

Dear Monks,

I've been messing around with GD::Graph for a while as an option for dynamically serving up some graphs via a CGI script. For the most part I'm happy, it runs pretty fast and gives me pretty pictures. I have encountered some strange behavior with data running off the graph... for simplicity, here is an example of a very basic graph that works normally:
use strict; use GD::Graph::lines; my @data = ( [1, 2, 3, 4, 5, 6, 7, 8, 9,], [ 8, 8.25, 8.5, 8.35, 8.76, 8.21, 8.6, 8.8, 8.5], [ 7.4, 7.6, 7.8, 7.9, 7.4, 8.5, 7, 7.87, 8.34], [ 4, 5, 6, 5.6, 5.5, 6.1, 6.3, 5.2, 5.6]); my $graph = new GD::Graph::lines (600, 400); $graph->set( title => "Blah, Blah, Blah", x_label => 'X axis label', y_label => 'Y axis label', boxclr => '#C0C0C0' ); ## Next code snippet goes here $graph->set_legend("data set 1", "data set 2", "data set 3"); print "Content-type: image/png\n\n"; binmode(STDOUT); print $graph->plot(\@data)->png();
Now lets say you want to allow the user to "zoom in" on an area of the graph. Pretend that after some CGI magic and some passed parameters, the min/max for the x and y axes are as follows and you insert this code where I have marked in the above script:
$graph->set( y_min_value => 6, y_max_value => 10, x_min_value => 3, x_max_value => 7 );
The lines run off the edge of the x and y axes, the x_max_value does not appear to be properly set, etc. Am I dumb and have an error in the code somewhere or do you folks encounter the same problem? Could be a better looking graph, eh? Didn't find a fix for this in the documentation. So far it does this on my Linuxbox (forgot what flavor, I don't manage it), and a WinNT machine, both with what I think is the most recent module of GD::Graph. If this is a known problem and there is a fix, do let me know.

I understand I could modify my data sets so that the data "fits" within the plotting area. But, I'm lazy and I feel like this is something GD::Graph should be able to handle internally. Any wisdom you may offer will be much appreciated.

Thank you,
-C

Replies are listed 'Best First'.
Re: GD::Graph - data plotting off the charting area
by terra incognita (Pilgrim) on Apr 08, 2005 at 20:35 UTC
    The activestate manual states

    The range (y_min_value..y_max_value) has to include all the values of the data points, or GD::Graph will die with a message.

    Which doesn't look like it is true, but it does appear to be causing your issue.
    If you change your "Next Code Snippet" to
    ## Next code snippet goes here $graph->set( y_min_value => 4, y_max_value => 10, x_min_value => 1, x_max_value => 7 );
    It will work correctly. The only difference being that the y_min_value and the x_min_value are within the range of the data.