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;
Replies are listed 'Best First'.
Re: CGI bar-graph maker
by JSchmitz (Canon) on May 02, 2001 at 05:46 UTC
    You can make a easy to alter webpage version that looks something like this - widgets galore and let them edit away.
    <HTML> <HEAD> <TITLE> GraphMaker 2.1 </TITLE> </HEAD> <BODY BGCOLOR="#ffffff"> <!-- <SERVICE NAME="geoguide"> --> <table bgcolor=#666666 cellpadding=1 cellspacing=0 border=0 width=468> <tr><td><table bgcolor=#eeeeee cellpadding=2 cellspacing=0 border=0 wi +dth=100%> <tr><td bgcolor="FFFFFF"><a href="http://geocities.yahoo.com/addons/in +teract/mbe.html"><IMG SRC="http://pic.geocities.com/images/mbe/mbeFFF +FFF.gif" ALT="Yahoo! GeoCities Member Banner Exchange" WIDTH="335" HE +IGHT="20" border=0></a></td> <td align=right bgcolor="FFFFFF"><font face="Arial" size="-1"><a href= +"http://geocities.yahoo.com/addons/interact/mbe.html">Info</a></font> +&nbsp;</td></tr></table></td></tr> </table> <table cellpadding=0 cellspacing=0 width=468 border=0> <tr><td valign=top height=1></td></tr> <tr><td valign=top><SCRIPT LANGUAGE="javascript" src="http://us.toto.g +eo.yahoo.com/toto?s=76001074&b=2"></SCRIPT></td></tr> </table> <!-- </SERVICE> --> <H1>GraphMaker</H1> <H2>by <A HREF="mailto:Pivari@geocities.com">Fabrizio Pivari</A></H2> <H3>version 2.1 1997/06/19</H3> <P> <FORM METHOD="post" ACTION="http://www.agip.it/cgi-bin/GRAPH.cgi"> <TABLE BORDER=0 CELLSPACING=2 CELLPADDING=2> <TR> <TD VALIGN=top> <B>Data</B>:<BR> <TEXTAREA NAME="Data" ROWS=31 COLS=15> 2 6890 3 7250 4 7560 5 6964 6 6510 9 6725 10 7500 11 7690 12 7640 13 6640 16 6910 17 6980 18 7180 19 7290 20 6840 23 7450 24 7330 25 7290 26 7200 27 7590 30 7345</TEXTAREA> </TD> <TD VALIGN=top> <B>Separator</B>: <input name="separator" value=" " size=1> (Default i +s blank)<BR> <B>Min Y Value</B>: <input name="MINYVALUE" value="6400" size=10><BR> <B>Max Y Value</B>: <input name="MAXYVALUE" value="7800" size=10><BR> <B>Title</B>: <input name="Title" value="GraphMaker 2.1" size=50><BR> <B>Month</B>: <input name="MONTH" value="" size=2> (Default is the cur +rent) Remember 0 is January, 1 February and so on<BR> <B>Year</B>: <input name="YEAR" value="" size=2> (Default is the curre +nt)<BR> <B>Line Graph<INPUT TYPE="radio" NAME="Bar" value="0" CHECKED> Bar Gra +ph</B><INPUT TYPE="radio" NAME="Bar" value="1"><BR> <B>Average Line</B><INPUT TYPE="checkbox" NAME="Average" value="1" CHE +CKED><BR> <B>Colours:</B><BR> <TABLE BORDER=0> <TR> <TD><INPUT TYPE="checkbox" NAME="Transparent" value="yes" CHECKED>< +/TD> <TD>Transparent Background</TD> </TR> <TR> <TD> <TR> <TD> <input name="Rbgcolour" value="255" size=3> <input name="Gbgcolour" value="255" size=3> <input name="Bbgcolour" value="255" size=3> </TD> <TD>Background Colour in RGB mode</TD> </TR> <TR> <TD> <input name="Rfgcolour" value="0" size=3> <input name="Gfgcolour" value="0" size=3> <input name="Bfgcolour" value="0" size=3> </TD> <TD>Foreground Colour in RGB mode</TD> </TR> <TR> <TD> <input name="Rcolour" value="0" size=3> <input name="Gcolour" value="0" size=3> <input name="Bcolour" value="255" size=3> </TD> <TD>Line Colour in RGB mode</TD> </TR> <TD> <input name="Racolour" value="255" size=3> <input name="Gacolour" value="255" size=3> <input name="Bacolour" value="0" size=3> </TD> <TD>Average Line Colour in RGB mode</TD> </TR> </TABLE> </TD> </TR> </TABLE> Press <INPUT TYPE="submit" VALUE="here"> to submit your query. </FORM> NOTE! If the gif isn't correct you have to repost the result with the button Reload.<P> After saving your Graph you can use the gif to <A HREF="VRML.html">create a graph Texture Map in VRML</A>.<P> <A HREF="GraphMaker.tar.gz"> Source Code</A><P> <IMG SRC="new.gif" WIDTH=28 HEIGHT=11>You can also find the source cod +e at <A HREF="http://www.perl.org/CPAN/authors/Fabrizio_Pivari"> CPAN authors/Fabrizio_Pivari/</A><P> <IMG SRC="new.gif" WIDTH=28 HEIGHT=11>With version 2.0 in the source c +ode you can find a CGI-BIN program with the same features of the PERL script <IMG SRC="new.gif" WIDTH=28 HEIGHT=11><P> <H2>Usage for the stand alone script</H2> <PRE> graphmaker [-options ...] where options include: -help print out this message -conf file the configuration file (default graph +maker.cnf) </PRE> If you want to change the default parameters you have to edit graphmak +er.cnf (de fault) or you can to use other configuration file using the option [-conf fil +e]<P> These are the parameters that you can change in graphmaker.cnf<P> <PRE> # # Configure the value of the variables # # It's better if $MAXVALUE -$MINVALUE is a $NUMBERYCELLGRIDESIZE divis +ible # number NUMBERYCELLGRIDSIZE: 8 MAXYVALUE: 7748 MINYVALUE: 6500 XCELLGRIDSIZE: 18 # both 01 or 2001 are correct # Remember 0 is January, 1 February ... YEAR: 01 MONTH: 5 #($MONTH,$YEAR) = (localtime(time))[4,5]; # input file in the form # day:number Data: ./graphmaker.dat # output gif Graph: ./graphmaker.gif # $Bar: 1 Bar Diagram Bar: 1 # $Average: 1 for Average Line Average: 1 #$Title: Graph Maker 2.1 Title: Whatever you want it to be # # Colour # Default Transparent Background: yes Transparent: no # Background RGB Colour: 255,255,255 Rbgcolour: 0 Gbgcolour: 255 Bbgcolour: 255 # Foreground RGB Colour: 0,0,0 Rfgcolour: 0 Gfgcolour: 0 Bfgcolour: 0 # Default line or bar RGB Colour: 0,0,255 Rcolour: 0 Gcolour: 0 Bcolour: 255 # Average colour Racolour: 255 Gacolour: 255 Bacolour: 0 </PRE> </BODY> </HTML>
      Well if you like that maybe I should post my Pie chart creater.

      --BigJoe

      Learn patience, you must.
      Young PerlMonk, craves Not these things.
      Use the source Luke.