Diese Webseite verwendet Cookies. Durch die Nutzung der Webseite stimmen Sie der Verwendung von Cookies zu. Datenschutzinformationen [X]

 Mike‘s Heimseite

  by Mike Sperl



Klasse "balken"

"balken" ist ein Klasse für die Darstellung eines Balkendiagramms. Ich wollte einfach wissen wie sowas funktioniert. Ist auch sicher noch erweiterungsfähig. DEMO

Sie dürfen die Skripts gerne verwenden. Haftung gibt es von meiner Seite keine. Download

'balken.php'

<?php 
/*
Klasse balken     Darstellung einer Balkengrafik.
--------------------------------------------------------------------------------------------------                                           
*/
class balken
{
 public 
$bild;
 public 
$breite;
 public 
$hoehe;
 public 
$tsize 24;
 public 
$maxhoehe 100;
 public 
$randoben 40;
 public 
$fusshoehe 40;
 public 
$randlinks 100;
 public 
$randrechts 50;
 public 
$schrittweite 10;
 public 
$ybereich;
 public 
$ytext;
 
//Farben
 
public $black;
 public function 
__construct($b,$h,$f) {
  
$f $this->farbe($f);
  
$this->breite $b;
  
$this->hoehe  $h;
  
$this->bild imagecreatetruecolor($b,$h);
  
$rgb $this->html2rgb($f);
  
$hintergrund=imagecolorallocate $this->bild$rgb[0], $rgb[1], $rgb[2] );
  
imagefill($this->bild,0,0,$hintergrund);
  
$this->black imagecolorallocate $this->bild00);
  
imagerectangle $this->bild00$b-1$h-1$this->black );  
 }
 public function 
farbe($farbe) {
   
$farben = array(
    
'black'   =>  '#000000'
    
'maroon'  =>  '#800000'
    
'green'   =>  '#008000'
    
'olive'   =>  '#808000',
    
'navy'    =>  '#000080'
    
'purple'  =>  '#800080'
    
'teal'    =>  '#008080'
    
'silver'  =>  '#C0C0C0'
    
'gray'    =>  '#808080'
    
'red'     =>  '#FF0000',
    
'lime'    =>  '#00FF00',
    
'yellow'  =>  '#FFFF00'
    
'blue'    =>  '#0000FF',
    
'fuchsia' =>  '#FF00FF'
    
'aqua'    =>  '#00FFFF',
    
'white'   =>  '#FFFFFF');
    if ( 
array_key_exists $farbe$farben ) )
     return(
$farben[$farbe]);
    else
     return(
$farbe);
}
 public function 
anzeigen() {
 
  
imagepng($this->bild);
  
 }
 public function 
speichern($datei) {
 
  
imagepng($this->bild,$datei);
  
 }
 public function 
titel($text,$farbe) {
  
$farbe $this->farbe($farbe);
  
$text utf8_decode($text);
  
$rgb $this->html2rgb($farbe);
  
$font_c imagecolorallocate $this->bild$rgb[0], $rgb[1], $rgb[2] );
  
$textwerte imagettfbbox $this->tsize0'arialbd.ttf'$text );
  
$x = ($this->breite 2) - ($textwerte[2] / 2);
  
$y abs($textwerte[5])+ (abs($textwerte[5]) / 2); 
  
$this->randoben $y abs($textwerte[1])+10;
  
imagettftext ($this->bild$this->tsize0$x$y$font_c'arialbd.ttf',$text);
 }
 public function 
drawbalken($x,$y,$b,$h,$farbe) {
  
$farbe $this->farbe($farbe);
  
$rgb $this->html2rgb($farbe);
  
$farbe_b imagecolorallocate $this->bild$rgb[0], $rgb[1], $rgb[2] );
  
$h $this->hoehe $h $y;
  
$y $this->hoehe $y;
  
$b $x $b;
  
imagefilledrectangle $this->bild$x$y$b$h$farbe_b );  
  
imagerectangle $this->bild$x$y$b$h$this->black );  
 }
 public function 
beschriftung_y($y,$wert) {
   
imageline($this->bild,$this->randlinks-5,$y,$this->breite-$this->randrechts,$y,$this->black);
   
$textbreite imagefontwidth ) * strlen $wert );
   
$x  $this->randlinks -  $textbreite 10;
   
imagestring $this->bild,4,$x,$y-8$wert$this->black );
 }
 public function 
balkengrafik($array,$farbe) {
  
$farbe $this->farbe($farbe);
  
$anzahl count($array);
  
$ebreite round(($this->breite $this->randlinks $this->randrechts)  / ($anzahl),0);
  
$x2 $ebreite $this->randlinks;
  
$y2 $this->hoehe $this->randoben - ($this->maxhoehe * ($this->hoehe) / $this->maxhoehe);
  
$textbreite2 imagefontwidth ) * strlen $this->ytext );
  
$pos $this->hoehe /$textbreite2 /2;
  
imagestringup $this->bild4,10$pos$this->ytext$this->black );
  
$this->ybereich round($this->hoehe-$this->fusshoehe $y2,0);
  
$this->beschriftung_y($y2,$this->maxhoehe);
  
$s $this->maxhoehe-$this->schrittweite;
  
$s2 0;
  
$y3 $y2 + ($this->ybereich $this->maxhoehe $this->schrittweite);
  while ( 
$s 0) {
   
$this->beschriftung_y($y3,$s);
   
$s $s $this->schrittweite;
   
$y3 $y3 + ($this->ybereich $this->maxhoehe $this->schrittweite);
  }
  for(
$i=0;$i $anzahl;$i++) {
   
$x $i+1;
   
$oy $array[$i][1];
   
$y $oy * ($this->hoehe $this->fusshoehe-$this->randoben) / $this->maxhoehe;
   
$this->drawbalken($x2,$this->fusshoehe,$ebreite,$y,$farbe);
   
$x2 $x2 $ebreite;
  }
  
// Linie für X-Achse
  
$this->beschriftung_y($this->hoehe-$this->fusshoehe,'0');
  
// Linie für Y-Achse
  
imageline($this->bild,$this->randlinks,$this->hoehe-$this->fusshoehe+5,$this->randlinks,$y2,$this->black);
 }
  public function 
beschriftung_x($array) {
    
$anzahl count($array);
    
$ebreite round(($this->breite $this->randlinks $this->randrechts) / ($anzahl),0);
    
$x2 $ebreite $this->randlinks;    $x3 $ebreite $this->randlinks;
    for(
$i=0;$i $anzahl;$i++) {
       
$black imagecolorallocate $this->bild00);
       
$text utf8_decode($array[$i][0]);
       
$textbreite imagefontwidth ) * strlen $text ); 
      
// $x2 = $x2 - ($textbreite / 2);
       
$y $this->hoehe $this->fusshoehe 10;
       
$x3 $x3 +  ($ebreite/2) - ($textbreite 2);
       
imagestring $this->bild,4,$x3,$y$text$black );
       
$x2 $x2 $ebreite;
       
$x3 $x2 ;
    }
  }
 public function 
html2rgb($color)
   {
    if (
$color[0] == '#')
        
$color substr($color1);
    if (
strlen($color) == 6)
        list(
$r$g$b) = array($color[0].$color[1],
                                 
$color[2].$color[3],
                                 
$color[4].$color[5]);
    elseif (
strlen($color) == 3)
        list(
$r$g$b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
    else
        return 
false;
    
$r hexdec($r); $g hexdec($g); $b hexdec($b);
    return array(
$r$g$b);
 }
 public function 
__destruct() {     
  
imagedestroy($this->bild);
 }
}

?>

Beispiel 'balkentest.php'

Nach oben

<?php
include('balken.php');
$datei 'balken.txt';
$fp fopen $datei'r' );
while ( ! 
feof $fp ) )
{
  
$str fgetcsv $fp200';' );
  list(
$a,$b) = $str;
  
$a iconv('UTF-8''ISO-8859-1//IGNORE'$a);
  
$a utf8_encode($a);
  
$grafik[] = array($a$b);
}
fclose $fp );
$bild = new balken(950,450,'olive');
$bild->maxhoehe 120;
$bild->schrittweite 20;
$bild->tsize 24;
$bild->randrechts 50;
$bild->ytext 'Auslastung in %';
$bild->titel('Meine erste Balkengrafik','yellow');
$bild->balkengrafik($grafik,'red');
$bild->beschriftung_x($grafik);
header("Content-type:image/png; charset= utf-8");
$bild->anzeigen();
unset(
$bild);
?>

Beispiel 'balken.txt'˜ für balkentest.php

Nach oben

Hamburg;45
München;90
Berlin;70
London;50
Eggelstetten;100
Frankfurt;10