| 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;
$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);
$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; }
$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;
}
$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);
}
sub ddiff {
local($ny,$nm,$nd,$oy,$om,$od) = @_;
return ($ny-$oy) * 365 + $monthdays[$nm-1]-$monthdays[$om-1] + ($nd-$od);
}
| |