You are on page 1of 7

٢٠١٨/٨/٢٣ Pie Chart class by PHP Code Exchange (from psc cd)

(http://domain.com/)

Dream it. Code it. ™


Login (http://domain.com/vb/authentication/Login.asp?lngWId=8&txtReturnURL=%2Fvb%2Fscripts%2FShowCode%2Easp%3FtxtCodeId%3D208%26lngWId%3D8)

.Net (VB and C#) (http://domain.com/vb/default.asp?lngWId=10)


PHP (http://domain.com/vb/default.asp?lngWId=8)
HTML5 / Javascript (http://domain.com/vb/default.asp?lngWId=14)
Java (http://domain.com/vb/default.asp?lngWId=2)
C/C++ (http://domain.com/vb/default.asp?lngWId=3)
SQL (http://domain.com/vb/default.asp?lngWId=5)
Perl (http://domain.com/vb/default.asp?lngWId=6)
ASP classic (http://domain.com/vb/default.asp?lngWId=4)
VB classic (http://domain.com/vb/default.asp?lngWId=1)
Delphi (http://domain.com/vb/default.asp?lngWId=7)
Cold Fusion (http://domain.com/vb/default.asp?lngWId=9)

Code/Articles (#)
Newest (http://domain.com/vb/scripts/BrowseCategoryOrSearchResults.asp?
grpCategories=-1&optSort=DateDescending&txtMaxNumberOfEntriesPerPage=10&blnNewestCode=TRUE&blnResetAllVariables=TRUE&lngWId=8)
Browse categories (http://domain.com/vb/scripts/BrowseAllCategories.asp?lngWId=8)
Search (http://domain.com/vb/scripts/search.asp?lngWId=8)
----------------------- (#)
Upload code / articles (http://domain.com/vb/authors/determine_author_type.asp?lngWId=8)
Open letter from moderators (http://domain.com/vb/misc/OpenLetterFromPSCModerators.asp?lngWId=8)
----------------------- (#)
Articles & tutorials (http://domain.com/vb/Tutorial/default.asp?lngWId=8)
Recommended reading (http://domain.com/vb/Bookstore/default.asp?lngWId=8)
Top Code (#)
Coding contest (http://domain.com/vb/contest/ContestInfo.asp?lngWId=8)
Coding contest leader board (http://domain.com/vb/contest/ContestAndLeaderBoard.asp?lngWId=8)
Coding contest 'All-Time Hall of Fame' (http://domain.com/vb/scripts/BrowseCategoryOrSearchResults.asp?
grpCategories=-1&txtMaxNumberOfEntriesPerPage=10&blnTopCode=True&blnResetAllVariables=TRUE&lngWId=8)
----------------------- (#)
Code of the day newsletter (http://domain.com/vb/CodeOfTheDay/signup.asp?lngWId=8)
Community (#)
Ask a pro discussion forum (http://domain.com/vb/discussion/default.asp?lngWId=8)
Code of the day newsletter (http://domain.com/vb/CodeOfTheDay/signup.asp?lngWId=8)
Open letter from moderators (http://domain.com/vb/misc/OpenLetterFromPSCModerators.asp?lngWId=8)
Other (#)
My profile (http://domain.com/vb/authentication/MyPlanetSourceCode/CustomizeInfo.asp?lngWId=8)
How to link to us (http://domain.com/vb/LinkToUs/default.asp?lngWId=8)
Awards/Reviews/Raves! (http://domain.com/vb/about/Awards.asp?lngWId=8)
Advertising/Media kit (http://domain.com/vb/advertisement/scripts/rates.asp?lngWId=8)
Feedback (http://domain.com/vb/feedback/feedback.asp?lngWId=8)
About the site (http://domain.com/vb/about/AboutTheSiteAndAuthor.asp?lngWId=8)
Goto… (#)
PHP home (http://domain.com/vb/default.asp?lngWId=8)
Site home (http://domain.com/?lngWId=-1)

(http://domain.com/vb/authors/ShowBio.asp?
lngAuthorId=8855657&lngWId=8)

Pie Like 0 Share


(http://domain.com/vb/scripts/EmailToFriend.asp?

Chart lngWId=8&txtCodeId=208&txtCodeName=Pie%2520Chart%2520class&txtCodeDescription=Simple%2520class%2520that%2520uses%2520GD%2520to%2520
Email (http://domain.com/vb/scripts/EmailToFriend.asp?
class lngWId=8&txtCodeId=208&txtCodeName=Pie%2520Chart%2520class&txtCodeDescription=Simple%2520class%2520that%2520uses%2520GD%2520to%2520

Submitted on: 1/1/2015 3:28:00 AM


PHP Code Exchange (from psc cd) (http://domain.com/vb/authors/ShowBio.asp?
By:
lngAuthorId=8855657&lngWId=8)
Level: Intermediate
User Rating: By 7 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 3620

Simple class that uses GD to draw pie charts. After the class definition there's some sample code to demonstrate how you use the class. by Bj�rn Borud

code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly (http://domain.com/vb/scripts/ShowCodeAsText.asp?txtCodeId=208&lngWId=8) version of this code!

https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=208&lngWId=8 1/7
٢٠١٨/٨/٢٣ Pie Chart class by PHP Code Exchange (from psc cd)

//**************************************
// Name: Pie Chart class
// Description:Simple class that uses GD to draw pie charts. After the class definition there's some sample code
// By: PHP Code Exchange (from psc cd)
//**************************************

<?php /* -*- C -*- */


/*
** $Id: piechart.phl,v 1.2 1998/02/03 16:55:06 borud Exp $
**
** PHP Class for creating pie charts using the GD library functions.
**
** There is a bug in the GD library somewhere that seems to kick in
** when you try to return images that are larger than 4K. We probably
** need a workaround for this...
**
** Pie charts look a bit shabby. There seems to be one or more
** roundoff errors lurking about making life hard for us. To fix this
** we should perhaps investigate how the Arc-drawing thingey works and
** try to find out how it gets the endpoints. Also the flood-filler
** doesn't quite cope with filling the pieces very well.
**
** Authors: Bj�rn Borud, Guardian Networks AS, <borud@guardian.no>
*/
/* {{{ piechart */
/*
** This is a class for creating pie charts. Generally you just have
** to instantiate it, and then make a call to the "init" method to
** set the size and transfer the data.
**
** The data is an array of arrays that consist the following data:
**o numeric value
**o value legend
**o red **o green > the RGB values for the color of the slice/legend
**o blue /
**
*/
class piechart {
/* {{{ attributes */
var $im;
var $width, $height;
var $data;
var $colors;
var $angles;
var $left=10;
var $right=200;
var $top=100;
var $bottom=10;
var $head_top=10;
var $head_space=5;
var $legend_left=20;
var $center_x;
var $center_y;
var $diameter;
/* sum of values */
var $sum;
/* font sizes */
var $fx, $fy;
var $legend_num = "";
/* }}} */
/* {{{ constants */
var $PI = 3.1415926535897931;
/* }}} */
/* {{{ roundoff */
/*
** PHP has no function for rounding off doubles to the nearest
** integer so we have to roll our own.
*/
function roundoff ($v) {
if ( $v - floor($v) >= 0.5) {
return(ceil($v));
} else {
return(floor($v));
}
}
/* }}} */
/* {{{ deg2rad */
/*
** The built-in trig functions use radians and there's no
** function in PHP to convert between degrees and radians
*/
function deg2rad ($degrees) {
return (($this->PI * $degrees) / doubleval(180));
}
/* }}} */
/* {{{ get_xy_factors */
/*
** Calculate the directional vector for the sides of the
** piece of pie.
*/
function get_xy_factors ($degrees) {
$x = cos($this->deg2rad($degrees));
$y = sin($this->deg2rad($degrees));
return (array($x, $y));
}
/* }}} */

https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=208&lngWId=8 2/7
٢٠١٨/٨/٢٣ Pie Chart class by PHP Code Exchange (from psc cd)
/* {{{ init */
/*
** Initialize the object and draw the pie. This would be the
** constructor in an ordinary OO scenario -- just that we haven't
** got constructors in PHP, now do we? ;-)
*/
function init ($w, $h, $d) {
$this->im= ImageCreate($w, $h);
$this->width = $w;
$this->height = $h;
$this->data = $d;
$this->da_width = ($this->width - $this->left - $this->right);
$this->da_height = ($this->height - $this->top - $this->bottom);
$this->center_x = intval($this->left + ($this->da_width / 2));
$this->center_y = intval($this->top + ($this->da_height / 2));
/* font sizes */
$this->fx = array(0, 5,6,7,8,9);
$this->fy = array(0, 7,8,10,14,11);
/* decide the diameter of the pie */
if ($this->da_height > $this->da_width) {
$this->diameter = $this->da_width;
} else {
$this->diameter = $this->da_height;
}
$this->white = ImageColorAllocate($this->im, 255, 255, 255);
$this->black = ImageColorAllocate($this->im,0,0,0);
$n = count($this->data);
for ($i = 0; $i < $n; $i++) {
$this->colors[$i] = ImageColorAllocate($this->im, $this->data[$i][2],
$this->data[$i][3],
$this->data[$i][4]);
$this->sum += $this->data[$i][0];
}
$from = 0;$to = 0;
for ($i = 0; $i < $n; $i++) {
$this->angles[$i] = $this->roundoff(($this->data[$i][0] * 360)
/ doubleval($this->sum));
$to = $from + $this->angles[$i];
$col = $this->colors[$i];

$foo = $this->angles[$i];
$this->draw_slice($this->center_x,
$this->center_y,
$from,
$to,
$this->colors[$i]);
$from += $this->angles[$i];
}
}
/* }}} */
/* {{{ set_legend_percent */
/* utility function to set an attribute so we display percentages */
function set_legend_percent () {
$this->legend_num = "p";
}
/* }}} */
/* {{{ set_legend_value */
/* utility function to set an attribute so we display values */
function set_legend_value () {
$this->legend_num = "v";
}
/* }}} */
/* {{{ draw_point */
/*
** This function is just here for debugging purposes. It is
** sometimes very useful to be able to draw an X to check
** coordinates.
*/
function draw_point($x, $y) {
ImageLine($this->im, $x-4, $y-4, $x+4, $y+4, $this->black);
ImageLine($this->im, $x-4, $y+4, $x+4, $y-4, $this->black);
}
/* }}} */
/* {{{ draw_margins */
/*
** Also a debugging function to show where the margins are at
*/
function draw_margins () {
ImageLine($this->im, 0, $this->top, $this->width,
$this->top,
$this->black);
ImageLine($this->im, 0, $this->height - $this->bottom,
$this->width, $this->height - $this->bottom,
$this->black);
ImageLine($this->im, $this->left, 0, $this->left,
$this->height, $this->black);
ImageLine($this->im, $this->width - $this->right,
0, $this->width - $this->right,
$this->height, $this->black);
}
/* }}} */
/* {{{ draw_legends */
/*
** Draw legends at the right side of the pie chart. This function
** accepts a fontsize and gathers all the other information from
** the multilevel data array
*/

https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=208&lngWId=8 3/7
٢٠١٨/٨/٢٣ Pie Chart class by PHP Code Exchange (from psc cd)
function draw_legends ($fontsize) {
$n = count($this->data);
$x1 = $this->width - $this->right + $this->legend_left;
$x2 = $x1 + $this->fy[$fontsize];;
for ($i = 0; $i < $n; $i++) {
/* determine Y coordinates */
$y1 = ($i * $this->fy[$fontsize] * 1.5) + $this->top;
$y2 = $y1 + $this->fy[$fontsize];
/* draw the legend color rectangle */
ImageFilledRectangle($this->im, $x1, $y1, $x2, $y2, $this->colors[$i]);
ImageRectangle($this->im, $x1, $y1, $x2, $y2, $this->black);
$legend = $this->data[$i][1];
/* decide what to show after legend */
switch ($this->legend_num) {
case "v":
$legend .= sprintf(" (%.2f)", $this->data[$i][0]);
break;
case "p":
$legend .= sprintf(" (%.2f%%)",
($this->data[$i][0]
* 100 / doubleval($this->sum)));
break;
}
ImageString($this->im, $fontsize, $x2 + 5, $y1,
$legend, $this->black);
}
}
/* }}} */
/* {{{ draw_heading */
/*
** This function accepts an array of arrays containing (in order):
** o The text of the heading as a string
** o The fontsize as an integer
** o The justification ("c"=center)
**
*/
function draw_heading($head_data) {
$n = count($head_data);
$y = $this->head_top;
for ($i = 0; $i < $n; $i++) {

switch($head_data[$i][2]) {
case "c":
$x = ($this->width - $this->fx[$head_data[$i][1]]
* strlen($head_data[$i][0])) / 2;
break;
case "r": /* uses left margin here... */
$x = $this->width - $this->left -
($this->fx[$head_data[$i][1]] * strlen($head_data[$i][0]));
break;
default:
$x = $this->left;
break;
}
ImageString($this->im, $head_data[$i][1], $x, $y, $head_data[$i][0],
$this->black);
$y += ($this->fy[$head_data[$i][1]] + $this->head_space);
}
}
/* }}} */
/* {{{ draw_slice */
/*
** This function draws a piece of pie centered at x,y starting at
** "from" degrees and ending at "to" degrees using the specified color.
*/
function draw_slice ($x, $y, $from, $to, $color) {
# Awful Kludge!!!
if ($to > 360) {
$to = 360;
}
ImageArc($this->im, $this->center_x, $this->center_y,
$this->diameter, $this->diameter, $from, $to, $color);
/* First line */
$axy2 = $this->get_xy_factors($from);
$ax2 = floor($this->center_x + ($axy2[0] * ($this->diameter /2)));
$ay2 = floor($this->center_y + ($axy2[1] * ($this->diameter /2)));
ImageLine($this->im, $this->center_x, $this->center_y, $ax2, $ay2, $color);
/* Second line */
$bxy2 = $this->get_xy_factors($to);
$bx2 = ceil($this->center_x + ($bxy2[0] * ($this->diameter /2)));
$by2 = ceil($this->center_y + ($bxy2[1] * ($this->diameter /2)));
ImageLine($this->im, $this->center_x, $this->center_y, $bx2, $by2, $color);
/* decide where to start filling, then fill */
$xy2 = $this->get_xy_factors((($to - $from) / 2) + $from);
$x2 = floor($this->center_x + ($xy2[0] * ($this->diameter /3)));
$y2 = floor($this->center_y + ($xy2[1] * ($this->diameter /3)));
ImageFillToBorder($this->im, $x2, $y2, $color, $color);
}
/* }}} */
/* {{{ display */
/*
** Make sure the legends are drawn, then output the image to the
** client
*/
function display() {
$this->draw_legends(2);
#$this->draw_margins();

https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=208&lngWId=8 4/7
٢٠١٨/٨/٢٣ Pie Chart class by PHP Code Exchange (from psc cd)
ImageGif($this->im, "/tmp/pie.gif");
Header("Content-type: image/gif");
ImageGif($this->im);
}
/* }}} */
};
/* }}} */
/* {{{ Test code */
$vals = array(
array(100, "First value", 190, 0 ,0),
array(100, "Second value", 0, 190, 0),
array(100, "Third value", 0, 0 ,190),
array(100, "Fourth value", 0, 190 ,190),
array(301.2437, "Fifth value", 204, 0 ,204),
array(308, "Sixth value", 204,204,0)
);
$heads = array(
array("First line (centered)", 4, "c"),
array("Second line (left justified)", 4, "l"),
array("Third line (right justified)", 4, "r")
);
$pie = new piechart;
$pie->init(400, 300, $vals);
$pie->draw_heading($heads);
$pie->set_legend_percent();
$pie->display();
/* }}} */
/*
* Local Variables:
* tab-width: 3
* End: */
?>

Other 13 submission(s) by this author (http://domain.com/vb/scripts/BrowseCategoryOrSearchResults.asp?


lngWId=8&blnAuthorSearch=TRUE&lngAuthorId=8855657&strAuthorName=PHP%20Code%20Exchange%20(from%20psc%20cd)&txtMaxNumberOfEntriesPerPage=25)

Report Bad Submission

Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because: Report it!

Your Vote

What do you think of this code (in the Intermediate category)?


(The code with your highest vote will win this month's coding contest!) (http://domain.com/vb/contest/contest.asp?lngWId=8)
Excellent Good Average Below Average Poor Rate It! (See voting log ... (http://domain.com/vb/scripts/voting/VoteLog.asp?
txtCodeId=208&txtCodeName=Pie%20Chart%20class&intUserRatingTotal=31&intNumOfUserRatings=7&lngWid=8) )

Other User Comments

https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=208&lngWId=8 5/7
٢٠١٨/٨/٢٣ Pie Chart class by PHP Code Exchange (from psc cd)

There are no comments on this submission.

Add Your Feedback

Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely,
or they will be deleted. (For feedback not related to this particular code, please click here (http://domain.com/vb/feedback/feedback.asp?lngWId=8&intTypeOfInquiry=3) instead.)

To post feedback, first please login (http://domain.com/vb/authentication/Login.asp?


lngWId=8&txtReturnURL=%2Fvb%2Fscripts%2FShowCode%2Easp%3FtxtCodeId%3D208%26lngWId%3D8#AddYourFeedback) .

Search: Enter search here…

Newest postings for PHP.


By Gaurav Singh on
5/5

(javascript:ShowPopupWindowXYWithScrollbars('/vb/misc/NewUpdateIconExplanation.asp',200,300))

Prime Number Checker


(http://domain.com/vb/scripts/S howCode.asp?
txtCodeId=3133&lngWId=8)

By Gaurav Singh on
5/5

(http://domain.com/Upload_PSC/ScreenShots/PIC20184211120509405.jpg)
Add this ticker to my site (http://domain.com/vb/LinkToUs/default.asp?lngWId=8#NewCodeTicker)

Daily code email

https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=208&lngWId=8 6/7
٢٠١٨/٨/٢٣ Pie Chart class by PHP Code Exchange (from psc cd)

Click here to join the 'Code of the Day' mailing list


(http://domain.com/vb/CodeOfTheDay/signup.asp?lngWId=8) !

(http://domain.com/vb/CodeOfTheDay/signup.asp?
lngWId=8)

This site on CD

Over 7,000 submissions on a super fast CD


(http://www.exhedra.com/exhedra/PlanetSourceCode/CD.aspx) !
(http://www.exhedra.com/exhedra/PlanetSourceCode/CD.aspx)

About (http://domain.com/vb/about/AboutTheSiteAndAuthor.asp?lngWId=8)
Contact (http://domain.com/vb/feedback/feedback.asp?lngWId=8)
Awards (http://domain.com/vb/about/Awards.asp?lngWId=8)
Link to us (http://domain.com/vb/LinkToUs/default.asp?lngWId=8)
Privacy (http://domain.com/vb/scripts/privacy.asp?lngWId=8)
Advertising (http://domain.com/vb/advertisement/scripts/rates.asp?lngWId=8)

Code/Articles

Newest (http://domain.com/vb/scripts/BrowseCategoryOrSearchResults.asp?
grpCategories=-1&optSort=DateDescending&txtMaxNumberOfEntriesPerPage=10&blnNewestCode=TRUE&blnResetAllVariables=TRUE&lngWId=8)
Browse categories (http://domain.com/vb/scripts/BrowseAllCategories.asp?lngWId=8)
Search (http://domain.com/vb/scripts/search.asp?lngWId=8)
Upload code / articles (http://domain.com/vb/authors/determine_author_type.asp?lngWId=8)
Open letter from moderators (http://domain.com/vb/misc/OpenLetterFromPSCModerators.asp?lngWId=8)
Articles & tutorials (http://domain.com/vb/Tutorial/default.asp?lngWId=8)
Recommended reading (http://domain.com/vb/Bookstore/default.asp?lngWId=8)

Top Code

Coding contest (http://domain.com/vb/contest/ContestInfo.asp?lngWId=8)


Coding contest leader board (http://domain.com/vb/contest/ContestAndLeaderBoard.asp?lngWId=8)
Coding contest 'All-Time Hall of Fame' (http://domain.com/vb/scripts/BrowseCategoryOrSearchResults.asp?
grpCategories=-1&txtMaxNumberOfEntriesPerPage=10&blnTopCode=True&blnResetAllVariables=TRUE&lngWId=8)
Code of the day newsletter (http://domain.com/vb/CodeOfTheDay/signup.asp?lngWId=8)

Community

Ask a pro discussion forum (http://domain.com/vb/discussion/default.asp?lngWId=8)


Code of the day newsletter (http://domain.com/vb/CodeOfTheDay/signup.asp?lngWId=8)

Other

My profile (http://domain.com/vb/authentication/MyPlanetSourceCode/CustomizeInfo.asp?lngWId=8)
How to link to us (http://domain.com/vb/LinkToUs/default.asp?lngWId=8)
Awards/Reviews/Raves! (http://domain.com/vb/about/Awards.asp?lngWId=8)
Advertising/Media kit (http://domain.com/vb/advertisement/scripts/rates.asp?lngWId=8)
Feedback (http://domain.com/vb/feedback/feedback.asp?lngWId=8)
About the site (http://domain.com/vb/about/AboutTheSiteAndAuthor.asp?lngWId=8)

Goto…

PHP home (http://domain.com/vb/default.asp?lngWId=8)


Site home (http://domain.com/?lngWId=-1)

Copyright© 1997-2018 by Exhedra Solutions, Inc. (http://www.exhedra.com/) All Rights Reserved.


By using this site you agree to its Terms and Conditions (http://domain.com/vb/scripts/TermsAndConditions.asp?lngWId=8) . Planet Source Code™ and the phrase "Dream It.
Code It"™ are trademarks of Exhedra Solutions, Inc. (http://www.exhedra.com/)

My nonprogramming sites: The Real Estate Crowdfunding Review (http://www.TheRealEstateCrowdfundingReview.com) : Investing tools that cut through the clutter and the hype.
Real information and reviews. Real-time investment feeds. Real profitable. | Dollar candy (http://www.dollarcandygame.com) : Play 60 second puzzles against three other people
for real money! Legal in 36 states and overseas.

https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=208&lngWId=8 7/7

You might also like