Jump to content

PHP Pie chart


stelthius

Recommended Posts

Hi guys i use a script to show me the quota a user is using and it shows a pie chart

 

Example - pie.JPG

 

instead of it being the pie chart i want it to show like this exampl below

 

bar.JPG

 

Can anyone help me with this please :) also if you need to see code just let me know and ill show you what i use :) thanks and regards :)

Link to comment
Share on other sites

excuse me ? why would i be telling lies about how the script works that simply just doesnt make sense, i do NOT have GD installed and the script does NOT call any out of the server functions doesnt call images from out of the box either, please dont call me a liar for explaining what the script does.

Link to comment
Share on other sites

fair enough, you still have no rights to call me a liar,

 

 

<?php
class userQuota
{
var $qType;        // output type
var $qQuota;       // quota value
var $qUsed;        // used value
var $qUser;        // input user name
var $gWidth;       // width of pie chart
var $gHeight;      // height of pie chart (determined by width)
var $g3DHeight;    // do we want a 3D look or not
var $gLegend;      // display the legend or not
var $gQuotaColour; // colour of quota
var $gUsedColour;  // colour of used space
var $gBackColour;  // colour of the background
var $gTextColour;  // colour of the legend text
var $gCentreUsed;  // centre the 'used' wedge to bottom of pie

function userQuota($quota, $used, $username = '')
{
	$this->qUser = $username;
	$this->setOutputImage();
	$this->setImageProperties();

	$this->qQuota   = $quota;
	$this->qUsed    = $used;
	$this->oPercent = ($this->qUsed / (!$this->qQuota ? 1 : $this->qQuota)) * 100;
	$this->qUser    = $username;
	if ($this->oPercent > 100)
	{
		$this->oPercent = 100;
	}
}
function setOutputText()
{
	$this->qType = 0;
}
function setOutputImage()
{
	$this->qType = 1;
}
function setImageProperties($width = 150, $threeD = TRUE, $legend = TRUE, $centre = TRUE, $qc = 'DDDDDD', $uc = 'ED1C24', $bc = 'FFFFFF', $tc = '000000')
{
	$this->gWidth = (!$width ? 150 : $width);
	if ($threeD)
	{
		$this->gHeight   = $width/2;
		$this->g3DHeight = $width/10;
	}
	else
	{
		$this->gHeight   = $width;
		$this->g3DHeight = 0;
	}
	$this->gLegend      = ($legend == TRUE ? TRUE : FALSE);
	$this->gCentreUsed  = ($centre == TRUE ? TRUE : FALSE);
	$this->gQuotaColour = $this->_htmlHexToBinArray($qc);
	$this->gUsedColour  = $this->_htmlHexToBinArray($uc);
	$this->gBackColour  = $this->_htmlHexToBinArray($bc);
	$this->gTextColour  = $this->_htmlHexToBinArray($tc);
}
function displayQuota()
{
	($this->qType == 0) ? $this->_displayText() : $this->_displayImage();
}
function _htmlHexToBinArray($hex)
{
	for ($i=0; $i<3; $i++)
	{
		$foo = substr($hex, 2*$i, 2); 
		$rgb[$i] = 16 * hexdec(substr($foo, 0, 1)) + hexdec(substr($foo, 1, 1)); 
	}
	return $rgb;
}
function _displayText()
{
	if ($this->qUser) echo 'The quota for ', $this->qUser;
	else echo 'Your quota';
	echo ' is ', $this->_formatSize($this->qQuota), ' and ', 
		($this->qUser ? 'they' : 'you'), ' have used ',
		$this->_formatSize($this->qUsed), ' (',
		number_format($this->oPercent, 2), '%) of it.';
}
function _displayImage()
{
	// the graph variables
	$sStart = $this->g3DHeight * 2;
	$wStart = $this->gWidth * 2;
	$hStart = $this->gHeight * 2;
	if ($this->qUsed >= $this->qQuota) $usedPercent = 359;
	else $usedPercent = $this->oPercent * 3.6;
	if ($this->gCentreUsed)
	{
		$sWedge = (int)(90 - ($usedPercent / 2));
		if ($sWedge < 0) $sWedge += 360;
		$mWedge = (int)(90 + ($usedPercent / 2));
		$mWedge = ($mWedge == 90 ? 91 : $mWedge);
		$eWedge = (int)$sWedge;
	}
	else
	{
		$sWedge = 0;
		$mWedge = (int)($usedPercent ? $usedPercent : 1);
		$eWedge = 360;
	}
       	$im = @ImageCreateTrueColor($wStart, $hStart + $sStart);
	if ($im)
	{
		$cBg = ImageColorAllocate($im, $this->gBackColour[0], $this->gBackColour[1], $this->gBackColour[2]);
		$cUsed = ImageColorAllocate($im, $this->gUsedColour[0], $this->gUsedColour[1], $this->gUsedColour[2]);
		$cQuota = ImageColorAllocate($im, $this->gQuotaColour[0], $this->gQuotaColour[1], $this->gQuotaColour[2]);
		ImageFill($im, 0, 0, $cBg);
		if ($this->g3DHeight)
		{
			$qDarkArray = $this->gQuotaColour;
			for ($i=0; $i<3; $i++)
			{
				($qDarkArray[$i] > 99) ? $qDarkArray[$i] -= 100 : $qDarkArray[$i] = 0;
			}
			$uDarkArray = $this->gUsedColour;
			for ($i=0; $i<3; $i++)
			{
				($uDarkArray[$i] > 99) ? $uDarkArray[$i] -= 100 : $uDarkArray[$i] = 0;
			}
			$cQuotaDark = ImageColorAllocate($im, $qDarkArray[0], $qDarkArray[1], $qDarkArray[2]);
			$cUsedDark = ImageColorAllocate($im, $uDarkArray[0], $uDarkArray[1], $uDarkArray[2]);

			$shadow_start = ($hStart/2) + $sStart;
			$shadow_end = $hStart/2;
			for ($i=$shadow_start - 1; $i>$shadow_end; $i--)
			{
				ImageFilledArc($im, ($wStart/2), $i, $wStart, $hStart,  $sWedge, $mWedge, $cUsedDark, IMG_ARC_PIE);
				ImageFilledArc($im, ($wStart/2), $i, $wStart, $hStart, $mWedge, $eWedge, $cQuotaDark, IMG_ARC_PIE);
			}
		}

		ImageFilledArc($im, ($wStart/2), ($hStart/2), $wStart, $hStart, $sWedge, $mWedge, $cUsed, IMG_ARC_PIE);
		ImageFilledArc($im, ($wStart/2), ($hStart/2), $wStart, $hStart, $mWedge, $eWedge, $cQuota, IMG_ARC_PIE);

		if ($this->gLegend)
		{

			$lHeight = $lWidth = 0;
			$spacer = 10;

			$qText[0] = 'Quota: ' . $this->_formatSize($this->qQuota);
			if ($this->qUser) $qText[1] = '       (' . $this->qUser . ')';
			else $qText[1] = '';
			$uText[0] = 'Used : ' . $this->_formatSize($this->qUsed);
			$uText[1] = '       (' . number_format($this->oPercent,2) . '%)';

			$lHeight = (ImageFontHeight(2) * ($qText[1] == '' ? 3 : 4)) + $spacer;

			$qMax = (strlen($qText[0]) > strlen($qText[1])) ? strlen($qText[0]) : strlen($qText[1]);
			$uMax = (strlen($uText[0]) > strlen($uText[1])) ? strlen($uText[0]) : strlen($uText[1]);
			$tMax = ($qMax > $uMax ? $qMax : $uMax);
			$lWidth = ($tMax * ImageFontWidth(2)) + $spacer + ImageFontHeight(2);

			$lim = ImageCreateTrueColor($lWidth, $lHeight);
			ImageFill($lim, 0, 0, $cBg);
			$cText = ImageColorAllocate($lim, $this->gTextColour[0], $this->gTextColour[1], $this->gTextColour[2]);
			$lx = 0;
			$ly = 0;

			ImageFilledRectangle($lim, $lx, $ly, ($lx + ImageFontHeight(2)), ($ly + ImageFontHeight(2)), $cQuota);
			ImageString($lim, 2, ($lx + ImageFontHeight(2) + $spacer), $ly, $qText[0], $cText);
			if ($qText[1] != '')
			{
				$ly += ImageFontHeight(2);
				ImageString($lim, 2, ($lx + ImageFontHeight(2) + $spacer), $ly, $qText[1], $cText);
			}

			$ly += ($spacer + ImageFontHeight(2));

			ImageFilledRectangle($lim, $lx, $ly, ($lx + ImageFontHeight(2)), ($ly + ImageFontHeight(2)), $cUsed);
			ImageString($lim, 2, ($lx + ImageFontHeight(2) + $spacer), $ly, $uText[0], $cText);
			$ly += ImageFontHeight(2);
			ImageString($lim, 2, ($lx + ImageFontHeight(2) + $spacer), $ly, $uText[1], $cText);

			$gsx = ImageSX($im);
			$gsy = ImageSY($im);
			$lsx = ImageSX($lim);
			$lsy = ImageSY($lim);
			$gnx = ($gsx >> 1);
			$gny = ($gsy >> 1);
			$fx = ($gnx > $lsx) ? $gnx : $lsx;
			$fy = $gny + $lsy + ($spacer * 2);
			$final = ImageCreateTrueColor($fx, $fy);
			ImageFill($final, 0, 0, $cBg);
			ImageCopyResampled($final, $im, (($fx/2)-($gnx/2)), 0, 0, 0, $gnx, $gny, $gsx, $gsy);
			ImageCopyResampled($final, $lim, (($fx/2)-($lsx/2)), $gny + ($spacer * 2), 0, 0, $lsx, $lsy, $lsx, $lsy);
			ImageDestroy($lim);
		}
		else
		{
			$sx = ImageSX($im); 
			$sy = ImageSY($im); 
			$nx = ($sx>>1); 
			$ny = ($sy>>1);
			$final = ImageCreateTrueColor($nx, $ny);
			ImageCopyResampled($final, $im, 0, 0, 0, 0, $nx, $ny, $sx, $sy);
		}

		header("Content-type: image/jpeg");
		ImageJPEG($final, NULL, 100);
		ImageDestroy($im);
		ImageDestroy($final);
	}
}

function _formatSize($size = 0)
{
	if ($size >= 1073741824) $size = round($size/1073741824*100)/100 . " Gb";
	else if ($size >= 1048576) $size = round($size/1048576*100)/100 . " Mb";
	else if ($size >= 1024) $size = round($size/1024*100)/100 . " kb";
	else $size = $size . " bytes";
	return $size;
}
}
?>

Link to comment
Share on other sites

ok im gonna end this here as its obvious something isnt how it should be plain and simple i dont have GD installed, the script is running on my box, what more can i say its running it works, all i want to do is change the way it shows the data, sorry for bothering you!

 

 

Rick,

Link to comment
Share on other sites

Hey guys no need to fight.  stelthius isn't necessarily lying; as he said, what's the point in lying about it? He may simply not know and he's just assuming it's not installed because he didn't go install it.  But nonetheless, stelthius, the following:

 

ImageCreateTrueColor

ImageColorAllocate

ImageFill

ImageFilledArc

ImageFilledRectangle

ImageFontHeight

ImageString

ImageSX

ImageSY

ImageCopyResampled

ImageDestroy

ImageJPEG

 

are in your script.  Those are all functions of the GD library, so you somehow do indeed have the GD library installed and enabled on your system.  You had to have installed apache/php or maybe a package like WAMP or XAMPP or something, maybe it came with the bundle. Who knows? But the bottom line is that you do have it installed, because your script wouldn't run without it.

 

Check out what the GD library has to offer, in the way of doing what you want. 

Link to comment
Share on other sites

1) You don't have to install GD to have it, it's like the yahoo toolbar :) That said, it is a GD function which means you are probably running GD, and you can find out for sure by using the phpinfo() function.

 

2) You don't have to have GD to make an image on the server. There are tons of other libraries, and you don't even need a library, because you could run something like Image Magick command line and then route the image.

 

3) In a world where people are generally very tactful, it's sometimes funny to see something blatantly hostile and completely unprovoked.

 

 

Link to comment
Share on other sites

1) You don't have to install GD to have it, it's like the yahoo toolbar :) That said, it is a GD function which means you are probably running GD, and you can find out for sure by using the phpinfo() function.

 

2) You don't have to have GD to make an image on the server. There are tons of other libraries, and you don't even need a library, because you could run something like Image Magick command line and then route the image.

 

3) In a world where people are generally very tactful, it's sometimes funny to see something blatantly hostile and completely unprovoked.

 

 

 

1) Yes you do it could be part of the installation you used (such as wamp xamp uniform server)  and yahoo toolbar needs to be installed.

 

2) It isn't image magick because of the functions being used

 

3) I am not hostile I just hate when people are stubborn about something and they don't even take the time to check if a prediction is correct.

Link to comment
Share on other sites

that being said I go back to my original statement that you need to use GD to generate the images as needed.

 

Since this class looks like it wasn't written by you then this will be a challenge to do ti without any help.

 

I suggest you google php graphing with GD tutorials as I suggested 20 post ago.

Link to comment
Share on other sites

Let's settle this once and for all.  Stelthius, no one touched your box, it's just the PHP has GD bundled already. =/  And it's not so easy to magically present data in a different fashion.  You're going to need the background image, first of all, and a "circle" image that you will need to calculate the location of based on percentages.

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.