Hallo ik heb een cirkeldiagram klasse gemaakt.
Er gaat alleen iets fout en ik kan er maar niet achter komen hoe dit kan.
Je maakt dus een instantie van de klasse, en doet steeds de kleur instellen en daarna de hoek van de taartpunt.
Ik krijg de volgende afbeelding eruit:

Bij de tweede punt gaat het mis. Je ziet een wit stuk tussen de groene en blauwe punt. Dit zal wel door een reken fout zijn.
Als je een taartpunt in de diagram erbij wil, en je geeft het aantal graden, dan worden deze graden relatief aan de vorige taartpunt gezien.


<?PHP
// piechart.class.php

/* 
 * Class for creating pie charts
 */

 
class Piechart {
    
    private $_image;
	private $_width;
	private $_height;
	
	private $_center_x;
	private $_center_y;
	private $_arc_width;
	private $_arc_height;
	private $_arc_start;
	private $_arc_color;
	
	private $_arc_count;
	
	private $_effect_thickness;
	
	public function __construct($width = 200, $height = 200) {
		
		$this->_width = $width;
		$this->_height = $height;
		$this->_image = imagecreate($width, $height);
		
		$white = imagecolorallocate($this->_image, 255, 255, 255);
		imagefill($this->_image, 0, 0, $white);
		
		$this->_center_x = intval($width / 2);
		$this->_center_y = intval($height / 2);
		
		$this->_arc_width = $width;
		$this->_arc_height = intval($height / 2);
		$this->_arc_start = 0;
		$this->_arc_color = imagecolorallocate($this->_image, 255, 0, 0);
		
		$this->_arc_count = 0;
		
		// effect
		$this->_effect_thickness = 10;
	}
	public function setArcColor($r, $g, $b) {
		
		$this->_arc_color = imagecolorallocate($this->_image, $r, $g, $b);
	}
	
	// Makes it possible to get the color also from outside the class
	public function getArcColor() {
		
		return $this->_arc_color;
	}
	private function _getArcStart() {
		
		return $this->_arc_start;
	}
	public function setEffectThickness($thickness) {
		
		$this->_effect_thickness = $thickness;
	}
	private function _createArcEffect() {
		
		for($i = $this->_effect_thickness + $this->_arc_height; $i > $this->_arc_height; $i--) {
			
			
		}
	}
	public function createArc($degrees) {
		
		$this->_createArcEffect();
		
		imagefilledarc($this->_image,			
						$this->_center_x,		// Center point x of the image, x of arc
						$this->_center_y,		// Center point y of the image, y of arc
						$this->_arc_width,		// Width of arc, relative to the x of the arc
						$this->_arc_height,		// Height of arc, relative to the y of the arc
						$this->_arc_start,		// Start degrees of arc, this is the end degrees of the previous arc
						$degrees,				// End degrees of the arc, given as paramater
						$this->_arc_color,		// Color of arc
						IMG_ARC_PIE				// Style
						);
		
		$this->_arc_start = $this->_arc_start + $degrees;
		$this->_arc_count++;
	}
	public function getArcCount() {
		
		return $this->_arc_count;
	}
	public function getImage() {
		
		return $this->_image;
	}
}

$p = new Piechart;
$p->createArc(40);
$p->setArcColor(0, 255, 0);
$p->createArc(50);
$p->setArcColor(0, 0, 255);
$p->createArc(200);
// flush image
header('Content-type: image/png');
imagepng($p->getImage());
imagedestroy($p->getImage());
?>

Hoe kan ik dit probleem oplossen?
Ik denk dat het komt doordat de variabele _arc_start nog 0 is bij de tweede arc, maar dat zou niet moeten kunnen, want na het maken van de eerste arc, bevat deze variabele al het aantal graden van de vorige (eerste) arc.
verander regel 83 maar eens in
<?php

$degrees + $this->_arc_start,

?>
Bedankt! Nu werkt het. Het was dus toch een rekenfout.

Reageren