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

Hi All,
Hope everyone is having a good Monday Tuesday. I started to use GD Graph, and (for the most part), I really like the module. I do have a quick question though:
I want to be able to generate a graph straight from the db call. Yesterday, I had an array of x values, an array of y values, and I pushed them together on to the same array. This resulted in an invalid plot (or similiar type of error). I was, however, able to generate a plot by using the nifty GD::Graph::File, but this required the extra step of generating files. If anyone has a working example of generating the data from within perl to produce a gd::graph, it would be great. Thanks, Zak

----
Pluralitas non est ponenda sine neccesitate - mysql's philosphy

Replies are listed 'Best First'.
Re: Coaking GDGraph to work...
by iguanodon (Priest) on Feb 04, 2003 at 14:11 UTC
    Here's a snippet from something I did with GD::Graph that read from a flat file:
    foreach (@lines) { # Lines in the file look like: # Sun Jul 07 2002 10:03:03 Sensor 0 [100D973000080069] C: 25.1 F: +77.1 chomp; my ($day, $mon, $date, $year, $time, undef, $sensor, undef, undef, + $c, undef, $f) = split; push (@{$data[0]}, $day); push (@{$data[$sensor + 1]}, $f); $y_max = $y_max > $f ? $y_max : $f; $y_min = $y_min < $f ? $y_min : $f; } my $y_axis_max = ceil($y_max / 10) * 10; my $y_axis_min = floor($y_min / 10) * 10; my $y_ticks = ($y_axis_max - $y_axis_min) / 10; my $graph = new GD::Graph::lines(); $graph->set( x_label => 'Day', y_label => 'Temperature (F)', title => $title, y_max_value => $y_axis_max, y_min_value => $y_axis_min, y_tick_number => 16, y_label_skip => 2, box_axis => 1, line_width => 3, y_label_position => 1, x_label_skip => 48, x_tick_offset => 48, x_long_ticks => 1, transparent => 0, ); $graph->set_legend(@legend_labels); $graph->plot($data); save_chart($graph, "$img_path/$img_file");
    Note that all arrays passed to plot() should be the same size. If they're not, pad the beginning of any short arrays with undef.

    BTW, I've since switched from using a flat file to a MySQL database, and from GD::Graph to Chart::Graph::Gnuplot. I like Chart::Graph::Gnuplot because of the amount of control Gnuplot gives over the graph. If you've ever created Gnuplot input files yourself, you'll appreciate this module. Of course, some may consider it a drawback that it relies on an external program.

Re: Coaking GDGraph to work...
by Aragorn (Curate) on Feb 04, 2003 at 14:08 UTC
    You have to use an array of arrays:
    my @data = ( ["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"], [ 1, 2, 5, 6, 3, 1.5, 1, 3, 4], [ sort { $a <=> $b } (1, 2, 5, 6, 3, 1.5, 1, 3, 4) ] );
    The first array contains the X-values, and the rest of the arrays contain the Y-values to be plotted. After that, you do something like this: (untested)
    # Create GD::Graph object my $graph = GD::Graph->new(400, 300); # Optionally set graph attributes $graph->set(x_label => "X Label", y_label => "Y Label"); # Create the graph itself my $gd = $graph->plot(\@data); # And output the file on STDOUT (redirect to a file, # or open a file yourself print $gd->png;

    Most of this comes from the excellent GD::Graph documentation. Read it :-)

    Arjen

      Yes, I read the doc, but actually *getting* to the array of array parts was the problem... The numbers were not constant so I could not manually create the array... the answer below yours was the one I was looking for, thanks. :)

      ----
      Zak
      Pluralitas non est ponenda sine neccesitate - mysql's philosphy
Re: Coaking GDGraph to work...
by elwarren (Priest) on Feb 04, 2003 at 15:01 UTC
    DBD::Chart lets you build charts and graphs with SQL statements. You can easily take a result set from one db call, pass it in, and get a chart back with minimal work on your part.