Computing: Website and Database Applications

Web/DB Home   Home   Contact

Website visits analysis.


6. Sections visits percentages chart.

6.1. What to display?
 
Aim: Creating a graphical display (pie chart) of the total visits percentages on the website's sections (Computer Basics, Lazarus/Free Pascal Programming, Website and Database Applications and the different subsections of Homeless Life in Luxembourg). Approach: The same, as used for the monthly charts (cf. point 5), i.e. reading the totals for each of the sections/subsections considered from the database, and creating a webpage with the pie chart, dynamically displaying the actual values.
6.2. What to do?
 
To do:
  • Create template HTML files with my standard site header and footer.
  • Make sure that the Perl "Chart" module is installed.
  • Create a Perl module (stats.pm), containing some general functions to be used by the charting scripts.
  • Create a script that outputs a webpage (with my standard site headers and footers) displaying an image whose source will be the dynamic chart wanted.
  • Create a script reading the sections/subsections counts from the database and use them to create a dynamic pie chart to be used as source for a website image.
Code preview for the chart creating Perl script:
  1. Read the totals for the sections/subsections considered from the database.
  2. Rearrange the data read in order to get the dataset arrays to be used by the "Chart" module.
  3. Create the pie chart as dynamic JPG image.
6.3. Template HTML files.
 
These files have been described in the Total visits per month charts document.
6.4. Perl "sections percentages" charting scripts.
 
You can view the code by opening the tabs below. Or click the following link to download all you need to install this appliation. Download file not yet available!
a. Charting scripts common functions (/cgi-bin/sitestats/stats.pm).
This module has already been described in the Total visits per month charts document.
b. Sections visits percentages (/cgi-bin/sitestats/sections_percent.pl and /cgi-bin/sitestats/sections_percent_chart.pl).
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"."

#
# Display total sections visits percentages
#

use strict; use warnings;
use stats;
# Print site header
my $cgi = site_header('Sections visits percentages.');
# Print totals per section as a chart
print ' <img src="sections_percent_chart.pl" alt="?" title="Sections visits percentages" border="1">', '<br/><br/>', "\n";
# Print site footer
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"."

#
# Create total sections visits percentages chart
#

use strict; use warnings;
use stats;
use Chart::Pie;
use Encode qw( encode_utf8 );
# Connect to "statistics" database
my $dbh = db_dbconnect();
# Read total visits for the different sections from database
# This script ignores the Biology computing section (actually not under development)!

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);
# Create arrays to be used as chart labels resp. as dataset
my @sections = (); my @counts = ();
foreach my $row (@$all) {
    my ($sectionid, $section, $count) = @$row;
    push(@sections, ShortSectionName($section));
    push(@counts, $count);
}
# Create a pie chart and output it as "dynamic CGI image"
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);
# Output the dynamic JPG image
$chart->cgi_jpeg();
# Transform section names for use as chart labels
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.
6.5. Running the scripts.
 
Here's the output of the total visits percentages chart script, executed end January 2019 on my local Apache server by entering "localhost/cgi-bin/sitestats/sections_percent.pl" in the address bar of my web browser. The header and footer used are the template HTML files described in point 5 of this document.
Sections visits percentages chart