Generating Dynamic Graphs


I run monthly web statistics for my website, using Analog, which produce quite detailed and complex statistics. While quite useful, I'd also like a generic monthly summary of hits to graph to my About Page. The basic steps I initially took were: While this worked, it was getting to be too much work, and I was looking for something to run off the command line or a cron job. Here is a sample of the monthly text file I generate to feed to the script:
January,39332
February,37037
March,48966
April,68752
  
At this point, I am ready to write the script, given the raw data I update monthly. However, I'd like as much of a 'hands-off' process so I don't have to support alot of software for this process.

owtchart

owtchart is a nifty online tool to generate graph images. A Web Service, if you will. Using this tool, I don't have to maintain the software to produce graphs, and use it to my leisure while maintained by DM Solutions.

My plan at this point was to write a script which would:

As a result, whenever monthly statistics are generated, I run this script to update the image on my About page.

     1	#!/usr/bin/perl -w
     2	
     3	use strict;
     4	use LWP::Simple; # for fetching remote content via the Internet
     5	
     6	# config
     7	
     8	my $barColourHexValue  = "0000FF";
     9	my $outputImageWidth   = 500;
    10	my $outputImageHeight  = 300;
    11	my $outputImageBaseDir = "/www/websites/www.kralidis.ca/htdocs/about";
    12	my $XTitle             = 2003;
    13	my $YTitle             = "Total Hits";
    14	
    15	# you should be okay from here
    16	
    17	@ARGV == 1 or die "Usage: $0 <dataFile>\n";
    18	
    19	my @months;
    20	my @hits;
    21	my @barHexColours;
    22	
    23	my $dataFile = $ARGV[0];
    24	
    25	# extract data from data
    26	open(FILE,$dataFile) or die "$dataFile $!\n";
    27	while(<FILE>) {
    28	  chomp;
    29	  my $month = $1 and my $hit = $2 if $_ =~ /^(\w+),(\d+)$/;  
    30	  push @months, $month;
    31	  push @hits, $hit;
    32	  push @barHexColours, $barColourHexValue;
    33	}
    34	close(FILE);
    35	
    36	# assemble URL parameters
    37	my $numpts    = @months;
    38	my $xlabels   = join(';', @months);
    39	my $vals      = join('!', @hits);
    40	my $extcolors = join('!', @barHexColours);
    41	
    42	my $requestURL = "http://www2.dmsolutions.ca/cgi-bin/owtchart?type=3DBar&w=$outputImageWidth&h=$outputImageHeight&XTitle=$XTitle&YTitle=$YTitle&numpts=$numpts&vals=$vals&xlabels=$xlabels&extcolors=$extcolors";
    43	
    44	# fetch image and write to file
    45	my $content = get($requestURL);
    46	
    47	open(IMG, ">$outputImageBaseDir/graph$XTitle.gif") or die "2 $outputImageBaseDir/graph$XTitle.gif $!\n";
    48	print IMG $content;
    49	close(IMG);