Hi,
Some GD docs GD.pm or the GD::Graph FAQ or the GD::Graph::Map site
I have whacked a simple piece of code together for this type of thing. I am outputting the GD stuff to a PNG file and then displaying this in a web page.
This script is purely a sample it displays GD::Graph as well as GD::Graph::Map which allows you to click on the bar and output a further query that is run in from the @urls script not show here.
#!perl.exe use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use GD; use GD::Graph; use GD::Graph::bars; use GD::Graph::Map; use DBI; use DBD::ODBC; my $query = new CGI; print $query->header; print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"10\">"; print $query->start_html( -title =>'Number Of Calls', -BGCOLOR=>"#A8A8DO"); # Print the first form print $query->startform; print "<center>\n"; print "<H1>Total Calls Graph v1.0.1</H1>\n"; print "<p><br></p><p>\n"; my @mds = (); my @tmp_mds = ("ServerA","ServerB","ServerC","ServerD"); my @vals = (); my $tot_call = 0; my @urls = ["../cgi-bin/CallWeb.pl?Md=ServerA", "../cgi-bin/CallWeb.pl?Md=ServerB", "../cgi-bin/CallWeb.pl?Md=ServerC", "../cgi-bin/CallWeb.pl?Md=ServerD", ]; my $dbh = DBI->connect("dbi:ODBC:driver={SQL Server};server=DBServer;d +atabase=Calls;uid=sa;pwd=password;") or die "Cannot connect to DB :$! +\n"; foreach my $md ( @tmp_mds ) { my $sql_query = "SELECT COUNT(MD) from OpenCalls where MD like '%$ +md%'"; my $sth = $dbh->prepare($sql_query) or die "Problems Preparing the + SQL Statement\n"; $sth->execute() or die "Problems Executing the SQL Statement\n"; my $num = $sth->fetchrow; push (@vals, $num); } foreach my $call (@vals) { $tot_call = $tot_call + $call; } $mds[0] = [@tmp_mds]; $mds[1] = [@vals]; my $graph = GD::Graph::bars->new(800, 500); my $maxval = $vals[ my $index = 0 ] if @vals; for (0 .. $#vals) { if ($maxval < $vals[$_]) { $index = $_; $maxval = $vals[$_]; } } $maxval = ($maxval + 10); $graph->set( x_label => 'Servers', x_label_position => '.5', y_label => 'Number of Calls', title => "Auto Calls PER Server - Total Calls = $tot_call", y_max_value => $maxval, y_tick_number => ($maxval/5), y_label_skip => 2, #Set the bar colours to Green dclrs => [ qw(green) ], # Draw bars with width 3 pixels bar_width => 3, # Sepearte the bars with 4 pixels bar_spacing => 10, # Show values on top of each bar show_values => 1, #Add a drop shadow shadow_depth => 4, shadowclr => 'dgreen', ); open PNG, ">../htdocs/pics/call_graph.png"; binmode PNG; print PNG $graph->plot(\@mds)->png; close PNG; my $map = new GD::Graph::Map($graph, hrefs =>\@urls , noImgMarkup =>1, img_TARGET => 'query', mapName =>"call_graph", info => 'No. of Calls for %x : %y' ); #finally, show the html print '<IMG BORDER = 0 SRC="../pics/call_graph.png" usemap="#call_grap +h">', ($map->imagemap("call_graph.png", \@mds)), "\n"; print "<P><B>Number of events in buffer : " . &Num_Event; print "</B></P></center><hr>\n";
>/readmore> I am doing several things here, some of which are bad but the concept should be right..? at the end of the day you should be able to see that I get the values for the BAR graph from a db.
Update: The Password and db stuff were only for demo purposes I would NEVER suggest you do this.
-----In reply to Re: Creating and writing Graphs to File
by AcidHawk
in thread Creating and writing Graphs to File
by toddy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |