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
Share on other sites

well you would just change those $r, $g, and $b values to whatever you want in the loop, its kinda simple.

additionally, you could use a formula in order to do like a gradient or rainbow of colors, if you wish.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.