http://qs321.pair.com?node_id=77099
Category: CGI programming
Author/Contact Info Joseph Harnish
big_joe1008@yahoo.com
Description: This is a quick little script I wrote up to generate a PNG graph. It is using the GD module. It is almost like a CGI wrapper for the GD::Graph::bars module. By using IFrames in your HTML you can send this script all the information it needs to generate a graph. You send the column names as param cols seperated by commas. This is also done with values through param vals. Optional params are title, xlabel, ylabel.
#!/usr/bin/perl
############################################################
#   Author      Joseph Harnish    big_joe1008@yahoo.com       #
#   Date    5/1/2001                         #
#                                    #
#   This script is a wrapper for GD::Graph::Bars for use on#
#   web pages.  It is a quicky that is used inside of an   #
#   IFRAME.                                     #
#                               #
#   Usage:  bargraph.pl?cols=A,B,C&vals=1,2,3           #
#   It is required to have a "cols" and a "vals" but there #
# are  options you can also add.  These options are:       #
#     title                               #
#    ylabel                           #
#    xlabel                           #
############################################################

use GD::Graph::bars;
use CGI;
use strict;

my $q = new CGI;
my $cols = $q->param("cols");
my $title = $q->param("title");
my $vals = $q->param("vals");
my $xlabel = $q->param("xlabel");
my $ylabel = $q->param("ylabel");

my @col = split(/\,/, $cols);
my @val = split(/\,/, $vals);
if($#col != $#val){
    print $q->header;
    print "<b><h1>Error:  Parameters are not ballanced</h1></b>";
    exit;
}
my @data = ( [@col], [@val] );
my $graph = new GD::Graph::bars(400,300);
if($title ne ''){
    $graph->set(title => "$title");
}
if($ylabel ne ''){
    $graph->set(y_label => "$ylabel");
}

if($xlabel ne ''){
    $graph->set(x_label => "$xlabel");
}

$graph->set(cycle_clrs => 'TRUE',
            bar_spacing => '10', 
            legend_placement => 'RB' );

$graph->set_values_font('ARIAL.TTF', 24);
$graph->set_legend_font('ARIAL.TTF', 24);
$graph->set_x_label_font('ARIAL.TTF', 24);
$graph->set_y_label_font('ARIAL.TTF', 24);
print $q->header("image/png");
binmode STDOUT;
print $graph->plot(\@data)->png;