Jump to content

PHP Pie Graph


invisionx

Recommended Posts

I got this script off of a site right now can't remember but i want to make it so that the colors are not random for each person

 

<?php
class Pie
{
var $imageWidth = 400;
var $imageHeight = 300;
var $bgR = 91;
var $bgG = 91;
var $bgB = 91;
var $title = "1st Fleet Pie Chart";


function create($varDesc, $varValues)
{
	Header("Content-type: image/png");
	$image = ImageCreate($this->imageWidth, $this->imageHeight);


	$bgcolor = ImageColorAllocate($image, 
		$this->bgR, $this->bgG, $this->bgB);

	$white = ImageColorAllocate($image, 255, 255, 255);
	$black = ImageColorAllocate($image, 91, 91, 91);
	ImageFill($image, 0, 0, $bgcolor);

                     //  here is where random color comes from..
	$num = 0;
	foreach($varDesc as $v)
	{
		$r = rand (0, 255);
		$g = rand (0, 255);
		$b = rand (0, 255);

		$sliceColors[$num] = ImageColorAllocate($image, $r, $g, $b);
		$num++;
	}

	// now $num has the number of elements

	// draw the box
	ImageLine($image, 0, 0, $this->imageWidth - 1, 0, $black);
	ImageLine($image, $this->imageWidth - 1, 0, $this->imageWidth - 1, $this->imageHeight - 1, $black);
	ImageLine($image, $this->imageWidth - 1, $this->imageHeight - 1, 0, $this->imageHeight - 1, $black);
	ImageLine($image, 0, $this->imageHeight - 1, 0, 0, $black);


	$total = 0;
	for ($x = 0; $x < $num; $x++)
	{
		$total += $varValues[$x];
	}

	// convert each slice into corresponding percentage of 360-degree circle
	for ($x = 0; $x < $num; $x++)
	{
		$angles[$x] = ($varValues[$x] / $total) * 360;
	}


	for($x = 0; $x < $num; $x++)
	{
		// calculate and draw arc corresponding to each slice
		ImageArc($image, 
			$this->imageWidth/4, 
			$this->imageHeight/2, 
			$this->imageWidth/3, 
			$this->imageHeight/3, 
			$angle,
			($angle + $angles[$x]), $sliceColors[$x]);

		$angle = $angle + $angles[$x];

		$x1 = round($this->imageWidth/4 + ($this->imageWidth/3 * cos($angle*pi()/180)) / 2);
		$y1 = round($this->imageHeight/2 + ($this->imageHeight/3 * sin($angle*pi()/180)) / 2);

		// demarcate slice with another line
		ImageLine($image, 
			$this->imageWidth/4,
			$this->imageHeight/2, 
			$x1, $y1, $sliceColors[$x]);


	}

	// fill in the arcs
	$angle = 0;
	for($x = 0; $x < $num; $x++)
	{
		$x1 = round($this->imageWidth/4 + 
			($this->imageWidth/3 * cos(($angle + $angles[$x] / 2)*pi()/180)) / 4);
		$y1 = round($this->imageHeight/2 + 
			($this->imageHeight/3 * sin(($angle + $angles[$x] / 2)*pi()/180)) / 4);

		ImageFill($image, $x1, $y1, $sliceColors[$x]);

		$angle = $angle + $angles[$x];
	}


	// put the desc strings
	ImageString($image, 5, $this->imageWidth/2, 60, "Legend", $black);
	for($x = 0; $x < $num; $x++)
	{
		$fl = sprintf("%.2f", $varValues[$x] * 100 / $total);
		$str = $varDesc[$x]." (".$fl."%)";
		ImageString($image, 3	, $this->imageWidth/2, ($x + 5) * 20, $str, $sliceColors[$x]);
	}

	// put the title
	ImageString($image, 5, 20, 20, $this->title, $white);


	ImagePng($image);
	ImageDestroy($image);

}
}

$pie = new Pie;

if(isset($width))
{
$pie->imageWidth = $width;
}

if(isset($height))
{
$pie->imageHeight = $height;
}

if(isset($title))
{
$pie->title = $title;
}

$varDesc = explode(",", $desc);
$varValues = explode(",", $values);

$pie->create($varDesc, $varValues);



?>

 

can someone help me please

 

thank you

invisionx

Link to comment
https://forums.phpfreaks.com/topic/61049-php-pie-graph/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.