use strict;
use warnings;
use GD;

# Datenfeld
my @y = (1,3,5,7,8,6,4,2,3,4,5,6,1,3,1,4,2,7);

# Bildparameter
my $width  = 300;
my $height = 200;
# Skalenpositionen
my $basis  = $height-20;
my $links  = 20;
# Balkenbreite
my $delta  = 10;
# Abstand der Balken voneinander
my $step   = ($width-2*$links-5)/($#y+1);

# Maximum ermitteln
my $i = 0;
my $max_y = $y[0];
for $i (0..$#y) 
  {
  if ( $y[$i] > $max_y ) { $max_y = $y[$i]; }
  }

# Datenwerte skalieren
for $i (0..$#y) 
  {
  $y[$i] = $height - $y[$i]*($height-40)/$max_y - 20 ;
  }
  
# Neues Bild anlegen
my $img = new GD::Image($width,$height);

# Farben definieren
my @color;
my $white = $img->colorAllocate(255,255,255);
my $black = $img->colorAllocate(0,0,0);
my $grey = $img->colorAllocate(100,100,100);
$color[0] = $img->colorAllocate(0,150,100);
$color[1] = $img->colorAllocate(255,0,0);
$color[2] = $img->colorAllocate(0,0,255);
$color[3] = $img->colorAllocate(0,255,255);
$color[4] = $img->colorAllocate(0,255,0);
$color[5] = $img->colorAllocate(255,255,0);
$color[6] = $img->colorAllocate(255,0,255);
$color[7] = $img->colorAllocate(255,155,0);

# Legende zeichnen
$img->string(gdLargeFont,5,20-8,$max_y,$black);
$img->string(gdLargeFont,5,($height/2)-8,$max_y/2,$black);
$img->string(gdLargeFont,5,$basis-8,"0",$black);
# X-Achse zeichnen
$img->line($links,$basis,$width-5,$basis,$black);
# Stützlinien zeichnen
$img->dashedLine($links,20,$width-5,20,$grey);
$img->dashedLine($links,$height/2,$width-5,$height/2,$grey);

# Balken farbig eintragen
for $i (0..$#y) 
  {
  my $point = 2*$links + $i*$step-$delta/2;
  $img->filledRectangle($point,$y[$i],$point+$delta,$basis,$color[$i%8]);
  }
# Ausgeben
open(OUTPUT, '>', 'balken.gif') or die "Cannot open colors.gif: $!";
binmode OUTPUT;
print OUTPUT $img->gif;
close OUTPUT;