DelphiFAQ Home Search:
General :: Programming :: perl
General questions about perl.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.
Recommended links on this topic:
Featured Article

Greating GIF, JPEG or PNG images with perl

Question:

I need to create a chart with statistical numbers for my web site. How can I do this?

Answer:

If you have perl and module GD installed then you can create the graphic on the fly as the page gets requested or maybe once every 3 hours, just as you need it.

The example below should get you started with GD. It creates a PNG file. For statistics, PNG is more suitable than JPEG. The lossless compressed PNG file will be of better quality than the JPEG and even be smaller. JPEG is more suitable for photos.
use GD;
 
 $HEIGHT = 480;
 $WIDTH  = 800;
 
 open(PNGFILE, ">/var/www/stats/usage.png") || die "Cannot open usage.png for write: $!\n";
 
 binmode PNGFILE;
 
 $MAXX = &ddiff($t_y,$t_m,$t_d,2003,1,0);
 $MAXY   = 2000;
 $XOFFSET= 30;
 $YOFFSET= 60; # for agenda
 
 
 $im = new GD::Image($WIDTH, $HEIGHT);
 $black = $im->colorAllocate(0, 0, 0);
 $red = $im->colorAllocate(255, 0, 0);
 $silver = $im->colorAllocate(192, 192, 192);
 $green = $im->colorAllocate(0, 255, 0);
 $yellow = $im->colorAllocate(255, 255, 0);
 $blue = $im->colorAllocate(0, 0, 255);
 $white = $im->colorAllocate(255, 255, 255);
 $im->setStyle($silver, $silver, gdTransparent, gdTransparent, gdTransparent);
 
 # numbers to the left from 0 to MAXY
 
 $y1=0;
 while ($y1<=$MAXY) {
   $im->string(gdSmallFont, 5, ($HEIGHT-$YOFFSET)-$y1*($HEIGHT-$YOFFSET)/$MAXY, $y1, $silver);
   $y1 = $y1 + ($MAXY/5);
 }
 $x1=0;
 $imonth=0;
 $im->setStyle($black, $black, $black, $silver, $silver, $silver);
 while ($x1<$MAXX) {
   $nn=$x1-$monthdays[$imonth]+1;
   $cx = $XOFFSET + $x1 * ($WIDTH-$XOFFSET)/$MAXX;
   if ($x1>=$monthdays[$imonth+1]) {
     $imonth=$imonth+1;
     $nn=$x1-$monthdays[$imonth]+1;
   }
   $mname = $monthnames[$imonth];
   if ($nn<10) { $nn = '0' . $nn; }
   $im->stringUp(gdSmallFont, $cx-10, $HEIGHT-5, "$nn.$mname", $silver);
   $im->line($cx,$HEIGHT-$YOFFSET,$cx,0,gdStyled);
   $x1 = $x1 + 5;
 }
 $im->line($XOFFSET,$HEIGHT-$YOFFSET,$WIDTH,$HEIGHT-$YOFFSET,$silver);
 $im->line($XOFFSET,$HEIGHT-$YOFFSET,$XOFFSET,0,$silver);
 
 open(FILE1, $FILENAME_USAGETXT);
 $output = '';
 $lasty1=$lasty2=$lasty3=$HEIGHT-$YOFFSET;
 $lastx1=$XOFFSET;
 $firstday = $lastday = '';
 while ($line = <FILE1>) {
   $line =~ /(.*)\:(.*)\,(.*)\,(.*)/;
   $dat = $1;
   $n1 = $2;
   $n2 = $3;
   $n3 = $4;
   if (!$firstday) { $firstday = $dat; }
 
   # plot the graphic..
 
   $y1 = $HEIGHT - $YOFFSET - $n1 * ($HEIGHT-$YOFFSET)/$MAXY;
   $y2 = $HEIGHT - $YOFFSET - $n2 * ($HEIGHT-$YOFFSET)/$MAXY;
   $y3 = $HEIGHT - $YOFFSET - $n3 * ($HEIGHT-$YOFFSET)/$MAXY;
 
   $dat =~ /([\d]+)\-([\d]+)\-([\d]+)/;
   $a_y = $1; $a_m = $2; $a_d = $3;
   $x1 = ($a_y-2003) * 365 + ($a_m-1) * 30 + $a_d;
 
   $cx = $XOFFSET + $x1 * ($WIDTH-$XOFFSET)/$MAXX;
 
   if (!$lasty1) {
     $lasty1=$y1;
     $lasty2=$y2;
     $lasty3=$y3;
   }
 
   $im->setStyle($red, $red, $red, $white, $white, $white);
   $im->line($lastx1,$lasty1,$cx,$y1,gdStyled);
   $im->setStyle($blue, $blue, $blue, $white, $white, $white);
   $im->line($lastx1,$lasty2,$cx,$y2,gdStyled);
   $im->setStyle($yellow, $yellow, $yellow, $white, $white, $white);
   $im->line($lastx1,$lasty3,$cx,$y3,gdStyled);
 
   $lasty1=$y1;
   $lasty2=$y2;
   $lasty3=$y3;
   $lastx1=$cx;
 } # while days..
 
 $lastday = $dat;
 $im->string(gdSmallFont, $XOFFSET+5, 20, "Zeitraum: $firstday - $lastday", $white);
 
 print PNGFILE $im->jpeg || die "Error writing to PNG file: $!\n";
 close (PNGFILE);
 close(FILE1);
 } # end of drawing the graphic
 
 
 
 #==============================================================================
 # difference between 2 dates:
 # 2003-8-1   and    2003-7-29   -->
 # ddiff(2003,8,1,2003,7,29) = 3
 sub ddiff {
   local($ny,$nm,$nd,$oy,$om,$od) = @_;
   return ($ny-$oy) * 365 + $monthdays[$nm-1]-$monthdays[$om-1] + ($nd-$od);
 }
 

Generated 12:01:56 on Jan 21, 2018