#!/usr/bin/perl -wT use strict; use CGI qw(:standard); use GD::Graph::bars; use DBI; my $dbname = 'book'; my $dbhost = 'localhost'; my @res; my $dsn = "DBI:mysql:database=$dbname;host=$dbhost"; my $dbh=DBI->connect($dsn,"guest","cheese"); if (!defined($dbh)) { print header; print "\nerror: There is a problem connecting to the MySQL database:\n"; print DBI->errmsg; print "-" x 25; exit; } my $sth = $dbh->prepare(qq{select IMAGE, CLICKS, IMPRESSIONS from image_tracker order by CLICKS desc limit 5}); $sth->execute; while (my @results = $sth->fetchrow_array) { push @res, @results; } $dbh->disconnect; my @data = ( [$res[0],$res[3],$res[6],$res[9],$res[12]], [$res[1],$res[4],$res[7],$res[10],$res[13]], [$res[2],$res[5],$res[8],$res[11],$res[14]], ); my $my_graph = new GD::Graph::bars(640, 480); $my_graph->set_legend("Clicks", "Impressions"); $my_graph->set( dclrs => [ qw(lgreen lyellow) ], title => "Top Banner Stats", x_label => "Images", y_label => "Count", long_ticks => 1, x_ticks => 0, x_label_position => '.5', # centered x label y_label_position => '.5', # centered y label bgclr => 'white', # makes background transparent transparent => 0, interlaced => 1, # show like venetian blind opens x_labels_vertical=> 1, # display tick values vertically lg_cols => 2, # num legend columns bar_spacing => 8, shadow_depth => 4, shadowclr => 'red', ); my $format = $my_graph->export_format; print header("image/$format"); binmode STDOUT; print $my_graph->plot(\@data)->$format();