package grafik;

use GD;
use strict;

sub paint{
my $GRA=shift; # Grafiktyp
my $a=shift;   # Dateiname
my $b=shift;   # Pixelgröße
my $c=shift;   # Zeiger auf Liste der Teile
my $d=shift;   # X Breite der Skala bei Skalen
my $e=shift;   # Anzahl n Leute die Teilgenommen haben
my $f=shift;    #Sortierung
my $g=shift;  # Anzeige der Teil "n's" 

if ($GRA eq "PI") {paintpie($a,$b,$c,$e,$f,$g);}
if ($GRA eq "BA") {paintbar($a,$b,$c,$e,$f,$g);}
if ($GRA eq "SK") {paintskala($a,$b,$c,$d,$e,$f,$g);}
if ($GRA eq "VL") {paintverlauf($a,$b,$c,$e,$f,1);}
}


sub paintpie {

my $PI = 3.1415927;

# Werte uebernehmen
my $filename=shift;
my $rad=shift() * 3;      # Radius des Kreises
my $Lref=shift;
my $ges=shift;
my $sort=shift;
if ($ges==0){return;}
my $x=0; my $Zx=0; my $Zy=0;
my @L=@$Lref;
my @Lstr=0;

my $anzahl=@L;

my $zahl=0; my $name=0;

sub numer {$b<=>$a;}
@L = sort numer @L;
my $laengstes=0;
for ($x=0; $x<$anzahl; $x++) {
     ($zahl,$name)=$L[$x]=~m/(\S+) (.*)/;
     $L[$x]=$zahl; $Lstr[$x]=$name;
     if ($laengstes < length($name)) {
         $laengstes = length($name)
     }
}
$laengstes+=6;   # Zeichen für Prozente addieren
$laengstes*=6;   # Zeichenbreite

#print "Werte : @L\nNamen:@Lstr\nAnzahl:$anzahl\n";

#Alles größer 9 zusammenaddieren und als 10ter Wert ablegen
if (@L > 10) {
    my $sum=0;
    for ($x=9 ; $x<=@L ; $x++) {
       $sum+=$L[$x];
    }
    $anzahl=10;
    $L[9]=$sum;
    $Lstr[9]="Sonstige";
}

#print "Werte : @L\n<br>Anzahl:$anzahl\n<br>";

# Zeichenvorbereitungen treffen
#print "Laengstes : $laengstes\n";
my $Zx=int(1.05*$rad+$laengstes+4); #KreisZentrum x 
my $Zy=int(1.05*$rad+20); # und y

# Grundeinstellungen
my $img = new GD::Image(2*$Zx,2*$Zy+16);
my $black = $img->colorAllocate(0,0,0);
my $white = $img->colorAllocate(0xff,0xff,0xff);
my $grey  = $img->colorAllocate(0x66,0x66,0x66);

$img->transparent($white);
$img->fill(2,2,$white);

# Farben definieren
my @C=(
    $img->colorAllocate(0x00,0x99,0xcc),
    $img->colorAllocate(0xff,0x99,0x00),
    $img->colorAllocate(0xff,0xff,0x99),
    $img->colorAllocate(0x00,0xcc,0x66),
    $img->colorAllocate(0x33,0x33,0xcc),
    $img->colorAllocate(0xff,0x66,0x66),
    $img->colorAllocate(0xcc,0xff,0xcc),
    $img->colorAllocate(0x66,0xcc,0xff),
    $img->colorAllocate(0xff,0x99,0xff),
    $img->colorAllocate(0x99,0x99,0x99)
    );


# Schatten......
my $scha=$rad*0.15;
$img->arc($Zx+$scha,$Zy+$scha,2*$rad,2*$rad,0,360,$grey);
$img->fill($Zx,$Zy,$grey);
$img->arc($Zx,$Zy,2*$rad,2*$rad,0,360,$black);
$img->fill($Zx,$Zy,$black);
$img->fill($Zx-$rad+1,$Zy,$black);

#Gesamtsumme der Liste ermitteln

my $N=0;
for ($x=0; $x<$anzahl; $x++) {$N+=$L[$x];}
if ($N==0){return;}
#print "Ges: $ges\n<br>Lstr=@Lstr\n<br>L=@L";

my $F=360/$N;
my $PR=100/$ges;

my $winkel=0;
my $winkelneu=0;
my $prozent=0;
my ($start, $end, $color, $text, $p1x, $p1y, $p2x,$p2y,$p3x,$p3y,$p4x,$p4y,$bo1,$bo2,$bo3);
for ($x=0; $x<$anzahl; $x++) {
     $prozent=sprintf("%.1f",$PR*@L[$x]);
     $winkelneu=$winkel+$F*@L[$x];
     if ($winkelneu != $winkel) {
	pie($winkel,$winkelneu,$C[$x],"@Lstr[$x] ${prozent}%",$img,$rad,$Zx,$Zy);
     }
     $winkel=$winkelneu;
}

$img->string(gdSmallFont,1,2*$Zy,"Gesamt: n=$ges",$black);

# Datei Speichern
open FILE, ">$filename";
binmode FILE;
print FILE $img->gif;
close FILE;


# Ein Kuchenstück malen !
sub pie {
  my $start=@_[0]; my $end=@_[1]; my $color=@_[2]; my $text=@_[3]; my $img=@_[4];
  my $rad=@_[5]; my $Zx=@_[6]; my $Zy=@_[7];
  #Schwarzen Kreis malen
  $img->arc($Zx,$Zy,2*$rad,2*$rad,$start,$end,$color);
  #Radius1 im Bogenmass
  $bo1=((90-$start)*2*$PI)/360 ;
  #Radius2 im Bogenmaß
  $bo2=((90-$end)*2*$PI)/360 ;
  #Halber Radius , fuer Punkt mitten im Pie
  $bo3=($bo1+$bo2)/2;
  if ($bo2 > $bo1) {$bo3+=$PI;}
  #print "-- $bo1 -- $bo2 -- $bo3 --\n";
  $p1x=$rad*sin($bo1)+$Zx+0.5;
  $p1y=$rad*cos($bo1)+$Zy+0.5;
  $p2x=$rad*sin($bo2)+$Zx+0.5;
  $p2y=$rad*cos($bo2)+$Zy+0.5;
  $p3x=$rad*sin($bo3)/2+$Zx+0.5;
  $p3y=$rad*cos($bo3)/2+$Zy+0.5;
  $p4x=$rad*sin($bo3)*1.05+$Zx+0.5;
  $p4y=$rad*cos($bo3)*1.05+$Zy+0.5;
  #print "$start $end $color - $p1x $p1y $p2x $p2y $p3x $p3y $p4x $p4y\n";
  $img->line($Zx,$Zy,$p1x,$p1y,$color);
  $img->line($Zx,$Zy,$p2x,$p2y,$color);
  $img->fill($p3x,$p3y,$color);
  #$img->line($Zx,$Zy,$p4x,$p4y,$color);/
  if ($p4x < $Zx) {$p4x-=length($text)*6;}
  $img->string(gdSmallFont,$p4x,$p4y-6,$text,$black);
}
}



sub paintbar {

# Werte uebernehmen
my $filename=shift;
my $bar=shift() * 7;      # Breite de Balkens
my $barhi=$bar/10;  # Balkenhoehe;
my $Lref=shift;
my $ges=shift;
my $sort=shift;
my @L=@$Lref;
my @Lstr=0; my $x=0;

my $anzahl=@L;

sub numer {$b<=>$a;}
if ($sort eq "S") {@L = sort numer @L;}

my $laengstes=0;
my $zahl=0; my $name="";

for ($x=0; $x<$anzahl; $x++) {
     ($zahl,$name)=$L[$x]=~m/(\S+) (.*)/;
     $L[$x]=$zahl; $Lstr[$x]=$name;
     if ($laengstes < length($name)) {
         $laengstes = length($name)
     }
}
$laengstes+=6;   # Zeichen für Prozente addieren
$laengstes*=8;   # Zeichenbreite

#print "Werte : @L\nNamen:@Lstr\nAnzahl:$anzahl\n";

#Alles größer 9 zusammenaddieren und als 10ter Wert ablegen
#if (@L > 10) {
#    my $sum=0;
#    for ($x=9 ; $x<=@L ; $x++) {
#       $sum+=$L[$x];
#    }
#   $anzahl=10;
#    $L[9]=$sum;
#    $Lstr[9]="Sonstige";
#}

my $xanzahl=$anzahl;
#for ($x=0; $x<$anzahl; $x++) {if (@L[$x] !=0) {$xanzahl++} }


#print "Werte : @L\nAnzahl:$anzahl\n";

# Zeichenvorbereitungen treffen

# Grundeinstellungen
my $img = new GD::Image($bar+$laengstes, 2*$barhi*$xanzahl+$barhi/2+16);
my $black = $img->colorAllocate(0,0,0);
my $white = $img->colorAllocate(255,255,255);
$img->transparent($white);
$img->fill(2,2,$white);

# Farben definieren
my @C=(
    $img->colorAllocate(0x00,0x99,0xcc),
    $img->colorAllocate(0xff,0x99,0x00),
    $img->colorAllocate(0xff,0xff,0x99),
    $img->colorAllocate(0x00,0xcc,0x66),
    $img->colorAllocate(0x33,0x33,0xcc),
    $img->colorAllocate(0xff,0x66,0x66),
    $img->colorAllocate(0xcc,0xff,0xcc),
    $img->colorAllocate(0x66,0xcc,0xff),
    $img->colorAllocate(0xff,0x99,0xff),
    $img->colorAllocate(0x99,0x99,0x99)
    );
#Gesamtsumme der Liste ermitteln

my $Dx=8;
#print "Ges: $ges\n";

my $BA=$bar/$ges;
my $PR=100/$ges;
my $Y=5;
#print "$BA , $barhi , $bar";
my $prozent=0;
for ($x=0; $x<$anzahl; $x++) {
     $prozent=sprintf("%.1f",$PR*@L[$x]);
     $Y=($x+1)*$barhi*2;
     #if (@L[$x] != 0) {
	bar($Y,$BA*@L[$x],$C[$x%10],"@Lstr[$x] ${prozent}%",$img , $barhi);
     #}
}

$img->string(gdSmallFont,$Dx,2*$barhi*$xanzahl+$barhi/2,"Gesamt: n=$ges",$black);

# Datei Speichern
open FILE, ">$filename";
binmode FILE;
print FILE $img->gif;
close FILE;


sub bar {
  my $Ky=@_[0]; my $lang=@_[1]; my $color=@_[2]; my $text=@_[3]; my $img=@_[4];
  my $barhi=@_[5];
  my $half=$barhi/2; my $Dx=8;
  my $p1x=$Dx; my $p1y=$Ky; my $p2x=$Dx+$lang; my $p2y=$Ky;
  my $p3x=$p2x+$half; my $p3y=$p2y-$half;
  my $p4x=$Dx; my $p4y=$Ky-$barhi; my $p5x=$Dx+$lang; my $p5y=$Ky-$barhi;
  my $p6x=$p4x+$half; my $p6y=$p4y-$half; my $p7x=$p5x+$half; my $p7y=$p5y-$half;
  my $ptx=$p3x+3; my $pty=$p3y-12;  # Textkoord.
  my $pf1x=$p1x+1; my $pf1y=$p1y-1; my $pf2x=$p6x; my $pf2y=$p6y+1; my $pf3x=$p3x-1; my $pf3y=$p3y;  # Fuellkoordinaten
  #print "P1X : $p1x $p1y $p2x $p2y $p3x $p3y\n";
  $img->rectangle($p1x,$p1y,$p5x,$p5y,$black);      # Rechteck
  $img->line($p4x,$p4y,$p6x,$p6y,$black);           # Schräg ob. li.
  $img->line($p5x,$p5y,$p7x,$p7y,$black);           # Schräg ob. re.
  $img->line($p2x,$p2y,$p3x,$p3y,$black);           # Schräg un. re.
  $img->line($p6x,$p6y,$p7x,$p7y,$black);           # grade ob.
  $img->line($p7x,$p7y,$p3x,$p3y,$black);           # senkr. re.
  $img->fill($pf1x,$pf1y,$color);
  $img->fill($pf2x,$pf2y,$color);
  $img->fill($pf3x,$pf3y,$color);
  $img->string(gdSmallFont,$ptx,$pty,$text,$black);

}

}

sub paintskala {

# Werte uebernehmen
my $filename=shift;
my $diff=shift;      # Abstand
my $Lref=shift;
my $skala=shift;     # 5er oder 7er
if ($skala==0) {$skala=5;}
my $ges=shift;
my $sort=shift;

my @L=@$Lref;
my @Lstr;
my $anzahl=@L;

sub numer {$b<=>$a;}
if ($sort eq "S") {@L = sort numer @L;}

my $laengstes=0;
my $x=0;
my $zahl=0; my $name="";
for ($x=0; $x<$anzahl; $x++) {
     ($zahl,$name)=$L[$x]=~m/(\S+) (.*)/;
     $L[$x]=$zahl; $Lstr[$x]=$name;
     if ($laengstes < length($name)) {
         $laengstes = length($name)
     }
}
$laengstes*=6;   # Zeichenbreite

#print "Werte : @L\nNamen:@Lstr\nAnzahl:$anzahl\n";

# Zeichenvorbereitungen treffen

# Grundeinstellungen
my $img = new GD::Image($skala*$diff+$laengstes+30, $anzahl*$diff+32);
my $black = $img->colorAllocate(0,0,0);
my $white = $img->colorAllocate(255,255,255);
my $linie = $img->colorAllocate(0x00,0x99,0xcc),

$img->transparent($white);
$img->fill(2,2,$white);

# Farben definieren
my @C=(
    $img->colorAllocate(0x00,0x99,0xcc),
    $img->colorAllocate(0xff,0x99,0x00),
    $img->colorAllocate(0xff,0xff,0x99),
    $img->colorAllocate(0x00,0xcc,0x66),
    $img->colorAllocate(0x33,0x33,0xcc),
    $img->colorAllocate(0xff,0x66,0x66),
    $img->colorAllocate(0xcc,0xff,0xcc),
    $img->colorAllocate(0x66,0xcc,0xff),
    $img->colorAllocate(0xff,0x99,0xff),
    $img->colorAllocate(0x99,0x99,0x99)
    );

#print "Point 1\n";

for (my $x=0; $x<$anzahl; $x++) {
  $img->string(gdSmallFont,5,16+$diff*$x,$Lstr[$x],$black);
  $img->string(gdSmallFont,$laengstes+20+($skala-1)*$diff,16+$diff*$x,$L[$x],$black);
  $img->arc($laengstes+10+(@L[$x]-1)*$diff,22+$diff*$x,$diff/5,$diff/5,0,360,$black);
  $img->fill($laengstes+10+(@L[$x]-1)*$diff,22+$diff*$x,$black);
  $img->line($laengstes+5,22+$diff*$x,$laengstes+15+$diff*($skala-1),22+$diff*$x,$black);
}
#print "Point 2\n";
for ($x=0; $x<$skala; $x++) {
  $img->string(gdSmallFont,$laengstes+9+$x*$diff,3,$x+1,$black);
  $img->line($laengstes+10+$diff*$x,17,$laengstes+10+$diff*$x,22+$diff*($anzahl-1),$black);
}
#print "Point 3\n";
for ($x=0; $x<$anzahl; $x++) {
#  print "Point 3.$x\n";
  $img->arc($laengstes+10+(@L[$x]-1)*$diff,22+$diff*$x,$diff/5,$diff/5,0,360,$linie);
  #$img->fill($laengstes+10+(@L[$x]-1)*$diff,22+$diff*$x,$linie);
}

for ($x=0; $x<($anzahl-1); $x++) {
  $img->line($laengstes+10+(@L[$x]-1)*$diff,22+$diff*$x,$laengstes+10+(@L[$x+1]-1)*$diff,22+$diff*($x+1),$linie);
}
$img->string(gdSmallFont,1,16+$diff*$anzahl,"Gesamt: n=$ges",$black);

# Datei Speichern
open FILE, ">$filename";
binmode FILE;
print FILE $img->gif;
close FILE;
}


sub paintverlauf {

# Werte uebernehmen
my $filename=shift;
my $diff=shift;      # Abstand
my $Lref=shift;
my $ges=shift;
if ($ges==0) {return;}
my $sort=shift;
my $ganzzahl=shift;
my @N; my $y;
my @L=@$Lref;
my @Lstr;
my $anzahl=@L;

my $ydiff=15;
my $hoch=$ydiff*($anzahl-1);
my $breit=$diff*15;

my $xdiff=$breit/($ges);
my $EinzelN=0;

sub numer {$b<=>$a;}
if ($sort eq "S") {@L = sort numer @L;}

my $laengstes=0;
my $x=0;
my $zahl=0; my $name="";
for ($x=0; $x<$anzahl; $x++) {
     ($L[$x],$Lstr[$x])=$L[$x]=~m/(\S+) (.*)/;
     $N[$x]=$ges;
	 if ( ($y)=$Lstr[$x]=~/\((\d+)\)$/ ) {$N[$x]=$y; $EinzelN=1;}
	 
     if ($ganzzahl >0) {$Lstr[$x]=sprintf("%4.0f - $Lstr[$x]",$L[$x] );    }
	 if ($laengstes < length($Lstr[$x]) ) {
         $laengstes = length($Lstr[$x])
	 }
}
$laengstes*=6;   # Zeichenbreite
$laengstes+=7;
#print "Werte : @L\nNamen:@Lstr\nAnzahl:$anzahl\n";

# Zeichenvorbereitungen treffen

# Grundeinstellungen
my $img = new GD::Image($laengstes+$breit+52, $hoch+40);
my $black = $img->colorAllocate(0,0,0);
my $white = $img->colorAllocate(255,255,255);
my $linie = $img->colorAllocate(0x00,0x99,0xcc),

$img->transparent($white);
$img->fill(2,2,$white);

# Farben definieren
my @C=(
    $img->colorAllocate(0x00,0x99,0xcc),
    $img->colorAllocate(0xff,0x99,0x00),
    $img->colorAllocate(0xff,0xff,0x99),
    $img->colorAllocate(0x00,0xcc,0x66),
    $img->colorAllocate(0x33,0x33,0xcc),
    $img->colorAllocate(0xff,0x66,0x66),
    $img->colorAllocate(0xcc,0xff,0xcc),
    $img->colorAllocate(0x66,0xcc,0xff),
    $img->colorAllocate(0xff,0x99,0xff),
    $img->colorAllocate(0x99,0x99,0x99)
    );

#print "Point 1\n";
$img->line($laengstes,10,$laengstes,$hoch+10,$black);
 if ($N[0]==0){next;}
my $oldx=$laengstes+$xdiff*$L[0]/$N[0]*$ges;
my $oldy=10;
my $newx; my $newy;my $avx=0; my $prozent;
for (my $x=0; $x<$anzahl; $x++) {
 if ($N[$x]==0){next;}
 if ($L[$x]==0){next;}
 # $img->fill($xdiff*$x+5,$hoch+20-$L[$x],$black);
  $newx=$laengstes+$xdiff*$L[$x]/$N[$x]*$ges;
  $newy=$x*$ydiff+10;
  $img->string(gdSmallFont,1,$newy-7,$Lstr[$x],$black);
  $img->arc($newx,$newy, $diff/5,$diff/5,0,360,$black);
  $img->line($laengstes,$newy,$newx,$newy,$black);
  $img->line($oldx,$oldy,$newx,$newy,$C[0]);
  $prozent=sprintf("%.1f",100/$N[$x]*$L[$x]);
  $img->string(gdSmallFont,$newx+10,$newy-7,$prozent."%",$black);
  $oldx=$newx;
  $oldy=$newy;
  $avx+=$L[$x]/$N[$x]*$ges;
}
if (! $EinzelN) {$img->string(gdSmallFont,1,$hoch+25,"Gesamt: n=$ges",$black); }
$avx/=$anzahl;
$img->line($laengstes+$xdiff*$avx,5,$laengstes+$xdiff*$avx,$hoch+30,$C[1]);
$prozent=sprintf("%.1f",100/$ges*$avx);
$img->string(gdSmallFont,$laengstes+$xdiff*$avx+5,$hoch+25,$prozent."%",$black);

# Datei Speichern

open FILE, ">$filename";
binmode FILE;
print FILE $img->gif;
close FILE;
}





1;
