The script sections_percent.pl creates a webpage including the common website header and footer and an image, whose source is set to the
sections_percent_chart.pl script (that contains all the database and charting related code for the graph wanted and that returns a dynamic JPG image
that will then be displayed here).
|
#!C:/Programs/Strawberry/win64/perl/bin/perl.exe -I"."
use strict; use warnings;
use stats;
my $cgi = site_header('Sections visits percentages.');
print ' <img src="sections_percent_chart.pl" alt="?" title="Sections visits percentages" border="1">', '<br/><br/>', "\n";
site_footer();
|
The script sections_percent_chart.pl creates a pie chart of the total visits percentages on the different site's sections/subsections and displays this
chart as a dynamic JPG image, that may be catched on a webpage by setting the "src" attribute of an "img" tag to the URL of the script; in my actual
application, the image is catched and displayed on the website, cretaed by sections_percent.pl (described above).
|
#!C:/Programs/Strawberry/win64/perl/bin/perl.exe -I"."
use strict; use warnings;
use stats;
use Chart::Pie;
use Encode qw( encode_utf8 );
my $dbh = db_dbconnect();
my $sql = "SELECT web_site AS _sectionid, site_description AS _section, SUM(access_count) AS _hits FROM site_hits, web_sites\n";
$sql .= " WHERE ((web_site = 'CB' OR web_site = 'CL' OR web_site = 'CW') OR (SUBSTR(web_site, 1, 1) = 'H' and SUBSTR(web_site, 2, 1) <> '0'))\n";
$sql .= " AND web_site = site_id\n";
$sql .= "GROUP BY _sectionid\n";
$sql .= "ORDER BY site_seqnum";
my $sth = $dbh->prepare($sql); $sth->execute();
my $all = $sth->fetchall_arrayref();
$sth->finish();
db_dbdisconnect($dbh);
my @sections = (); my @counts = ();
foreach my $row (@$all) {
my ($sectionid, $section, $count) = @$row;
push(@sections, ShortSectionName($section));
push(@counts, $count);
}
my $chart = Chart::Pie->new(1000, 550);
$chart->add_dataset(@sections);
$chart->add_dataset(@counts);
my %colors = (
'background' => 'seashell',
'dataset0' => 'blue',
'dataset1' => 'green',
'dataset2' => 'orange',
'dataset3' => 'SlateGray4',
'dataset4' => 'SlateGray3',
'dataset5' => 'black',
'dataset6' => 'SlateGray2',
'dataset7' => 'SlateGray1',
);
my %hash = (
'grey_background' => 'false',
'colors' => \%colors,
'label_values' => 'percent',
'legend' => 'none',
);
$chart->set( %hash);
$chart->cgi_jpeg();
sub ShortSectionName {
my @longnames = (
'Computer basics', 'Lazarus / Free Pascal programming', 'Website and database applications',
'Société et système social', 'Expériences et réflexions', 'Dignité humaine', 'Articles', 'Photos'
);
my @shortnames = (
'Computer basics', 'Free Pascal', 'Website/Database',
'SDF - Systeme social', 'SDF - Experiences', 'SDF - Dignite humaine', 'SDF - Articles', 'SDF - Photos'
);
(my $name) = @_; $name = Encode::encode_utf8($name);
my $shortname = $name;
for (my $i = 0; $i < scalar @shortnames; $i++) {
if ($name eq $longnames[$i]) {
$shortname = $shortnames[$i];
}
}
return $shortname;
}
|
The shebang at the beginning of the script points to the Perl interpreter to be used (in my case the 64bit version of Strawberry Perl
installed on my Windows 10) and has to be adapted to your actual development environment! Please note the presence of the argument -I"." passed to the Perl interpreter. This is because I stored the stats.pm module together with the scripts in
/cgi-bin/sitestats/ and Strawberry Perl does not automatically include the current directory (".") in the path where to search for modules.
|
|