Location: PHPKode > scripts > bm_chart > bm_chart/bm_chart.php
<?php
#----------------------------------------------------------
# bm_chart
#----------------------------------------------------------
# Copyright © 2005 - 2006 by bauer-martin.com.
# All rights reserved
#----------------------------------------------------------
# www.bauer-martin.com
# www.bauer-martin.com/forum
#----------------------------------------------------------

class bm_chart {
	var $img;
	var $margin = 0;
	var $textheight = 20;	 
	var $bgcolor;
	var $line;
	var $width;
	var $height;
	var $desc;
	var $value;
	var $color;
	var $farben;
	var $maxvalue;
	
	#------------------------------------
	# neues Chart
	#------------------------------------	
	function bm_chart($width, $height) {
		$this->width = $width;
		$this->height = $height;
	
		$this->img = imagecreate($this->width, $this->height);
	}
	
	#------------------------------------
	# Farben setzten
	#------------------------------------	
	function bm_chart_color($name, $red, $green, $blue) {
		$this->color[$name] = imagecolorallocate($this->img, $red, $green, $blue);
	}
	
	#------------------------------------
	# neues Balkendiagramm
	#------------------------------------	
	function bm_chart_draw_balken($margin, $bgcolor, $line, $desc, $value, $farben) {
		$this->margin = $margin;
		$this->bgcolor = $bgcolor;
		$this->line = $line;
		$this->desc = $desc;
		$this->value = $value;
		$this->farben = $farben;
		
		// Hintergrundfarbe
		imagefill($this->img, 0, 0, $this->color[$bgcolor]);
		// Linie mit Margin
		imagerectangle($this->img, $this->margin, $this->margin, ($this->width - $this->margin), ($this->height - $this->margin - $this->textheight), $this->color[$line]);
	
		// Max-Wert
		$this->maxvalue = $this->value[1];
		
		for($i = 0; $i < count($this->value); $i++) {
			// Maxwert
			if($this->maxvalue < $this->value[$i]) {
				$this->maxvalue = $this->value[$i];
			}
		}
		
		// Maximale Balkenhöhe
		$height_balken = $this->height - ($this->margin * 2) - 5 - $this->textheight;
		// Balken Width
		$width_balken =  ($this->width - ($this->margin * 2)) / count($desc) - 4;
		// Untere Linie
		$unten = round($this->height - $this->margin - $this->textheight);

		// Balken zeichnen
		for($i = 0; $i < count($this->desc); $i++) {
			// Balkenhöhe
			$balken = round($this->value[$i] * $height_balken / $this->maxvalue);
			// Ecke oben links
			$x1 = round(($margin + 2 + (($width_balken + 4) * $i)));
			// Füllfarbe
			$col = $i % count($this->farben);
			// Balken zeichnen
			if($this->value[$i] != 0) {
				imagefilledrectangle($this->img, $x1, $unten - $balken, $x1 + $width_balken, $unten, $this->color[$this->farben[$col]]);
				imagerectangle($this->img, $x1, $unten - $balken, $x1 + $width_balken, $unten, $this->color[$line]);
			}
					
			// Beschreibung und Wert
			imagestring($this->img, 2, $x1 + 2, $this->height - $this->textheight - $this->margin, $this->value[$i], $this->color[$line]);			
			imagestring($this->img, 2, $x1 + 2, $this->height - ($this->textheight / 2) - $this->margin, $this->desc[$i], $this->color[$line]);
		}
	}
	
	#------------------------------------
	# neues Liniendiagramm
	#------------------------------------	
	function bm_chart_draw_linie($margin, $bgcolor, $line, $desc, $value, $farben) {
		$this->margin = $margin;
		$this->bgcolor = $bgcolor;
		$this->line = $line;
		$this->desc = $desc;
		$this->value = $value;
		$this->farben = $farben;
		
		// Hintergrundfarbe
		imagefill($this->img, 0, 0, $this->color[$bgcolor]);
		// Linie mit Margin
		imagerectangle($this->img, $this->margin, $this->margin, ($this->width - $this->margin), ($this->height - $this->margin - $this->textheight), $this->color[$line]);
		
		// Max-Wert
		$this->maxvalue = $this->value[1];
		
		for($i = 0; $i < count($this->value); $i++) {
			// Maxwert
			if($this->maxvalue < $this->value[$i]) {
				$this->maxvalue = $this->value[$i];
			}
		}
		
		// Maximale Balkenhöhe
		$height_point = $this->height - ($this->margin * 2) - 5 - $this->textheight;
		// Balken Width
		$width_point =  ($this->width - ($this->margin * 2)) / count($desc);
		$point_halb = $width_point / 2;
		// Untere Linie
		$unten = round($this->height - $this->margin - $this->textheight);

		// Punkte zeichnen
		for($i = 0; $i < count($this->desc); $i++) {
			// Balkenhöhe
			$balken = round($this->value[$i] * $height_point / $this->maxvalue);

			$x1 = round(($this->margin + 2 + ($width_point * $i)));
			$y1 = $this->height - $balken;
			
			// Füllfarbe
			$col = $i % count($this->farben);
			// Punkte zeichnen
			imagefilledrectangle($this->img, $x1 + $point_halb, $y1, $x1 + 5 + $point_halb, $y1 + 5, $this->color[$this->farben[$col]]);
			imagerectangle($this->img, $x1 + $point_halb, $y1, $x1 + 5 + $point_halb, $y1 + 5, $this->color[$line]);
			
			$koor_linie[$i]['x'] = $x1 + $point_halb + 2;
			$koor_linie[$i]['y'] = $y1 + 2;
			// Beschreibung und Wert
			imagestring($this->img, 3, $x1 + 2, $this->height - $this->textheight - $this->margin, $this->value[$i], $this->color[$line]);			
			imagestring($this->img, 3, $x1 + 2, $this->height - ($this->textheight / 2) - $this->margin, $this->desc[$i], $this->color[$line]);
		}
		
		// Linie zeichnen
		for($i = 1; $i < count($this->desc); $i++) {
			imageline($this->img, $koor_linie[$i-1]['x'], $koor_linie[$i-1]['y'], $koor_linie[$i]['x'], $koor_linie[$i]['y'], $this->color[$line]);
		}
	}	
	
	#------------------------------------
	# neues Tortendiagramm zeichnen
	#------------------------------------	
	function bm_chart_draw_pie($margin, $bgcolor, $line, $desc, $value, $farben) {
		$this->margin = $margin;
		$this->bgcolor = $bgcolor;
		$this->line = $line;
		$this->desc = $desc;
		$this->value = $value;
		$this->farben = $farben;
		
		// Hintergrundfarbe
		imagefill($this->img, 0, 0, $this->color[$bgcolor]);
		// Linie mit Margin
		imagerectangle($this->img, $this->margin, $this->margin, ($this->width - $this->margin), ($this->height - $this->margin), $this->color[$line]);
		// Kreisgröße
		if($this->width < $this->height) {
			$kreis_width = $this->width - $this->margin - 20;
		} else {
			$kreis_width = $this->height - $this->margin - 20;
		}
		// Kreiskooridinaten
		$kreis_x = ($kreis_width / 2) + $this->margin + 5;
		$kreis_y = $this->height / 2;
		
		$sum_value = array_sum($this->value);
		$sum_winkel = 0;
		
		// Torte zeichnen
		for($i = 0; $i < count($this->desc); $i++) {
			// Wert
			$wert = round($value[$i] * 360 / $sum_value);
			// Füllfarbe
			$col = $i % count($this->farben);
			// Torte zeichnen
			imagefilledarc($this->img, $kreis_x, $kreis_y , $kreis_width, $kreis_width, $sum_winkel, $sum_winkel + $wert, $this->color[$this->farben[$col]], IMG_ARC_FILLED);
			// sum_winkel
			$sum_winkel += $wert;
			$my_winkel[$i] = $sum_winkel;

			// Beschreibung und Wert
			$x = $kreis_x + ($kreis_width / 2) + 10;
			$y = $this->margin + 5 + ($i * 14);
			
			imagefilledrectangle($this->img, $x, $y, $x + 10, $y + 10, $this->color[$this->farben[$col]]);
			imagerectangle($this->img, $x, $y, $x + 10, $y + 10, $this->color[$line]);			
			imagestring($this->img, 3, $x + 22, $y, $this->desc[$i].": ".$this->value[$i], $this->color[$line]);
		}
		
		// Ausenumrandung
		imagearc($this->img, $kreis_x, $kreis_y, $kreis_width, $kreis_width, 0, 360, $this->color[$line]);
	}	
	
	#------------------------------------
	# Diagramm zeichnen
	#------------------------------------	
	function bm_chart_show($ausgabe, $name = "") {
	
		if($ausgabe == 0) {
			imagepng($this->img);
		} else {
			imagepng($this->img, $name.".png");
		}
		imagedestroy($this->img);
	}
}
?>
Return current item: bm_chart