The "Noob Link" Technical Reference
Pantz.org Index About Homepage

 ____  _   _ ____
|  _ \| | | |  _ \
| |_) | |_| | |_) |
|  __/|  _  |  __/ 
|_|   |_| |_|_|

Make a piechart using PHP and MYSQL.

This code uses data taken from a MYSQL database and makes a piechart from it. I did the MYSQL mods and a guy named Xiao did the pie chart. The PHP install on the webserver has to be compiled with the GD, Freetype2, and libjpeg (libpng looks better for graphs IMHO). To make the graph appear just access it like its a picture file.

Example: <img src="picture.php" height="200" width="200">
<?php
/********************************
2D Pie Chart Version 1.0
Programer: Xiao Bin Zhao
E-mail: love1001_98@yahoo.com
Date: 03/31/2001
All Rights Reserved 2001.
********************************/

/*************Configuration Starts Here******************/

$chartTitle = "Percentage of Products in Database"; //pie chart name

/*************************End****************************/

/*****************For Programers Only********************/
$imageWidth = 600; //image width
$imageHeight = 400; //image height
$diameter = 250; //pie diameter
$centerX = 225; //pie center pixels x
$centerY = 225; //pie center pixels y
$labelWidth = 10; //label width, no need to change
/*************************End****************************/

//db connection info.
$dbuser = 'db';
$dbhost = 'database.here.com';
$dbpass = 'poe23451!@#';
$dbname = 'test';
$mysql_link = mysql_connect($dbhost,$dbuser,$dbpass) or die ("Could not connect"); //connect to db
mysql_select_db ($dbname) or die ("DB select failed"); //select db
$query1 = "select productname, count(*) as groupcount from product group by productname"; //get product count by groups of products
$query2 = "select count(*) as countammount from product"; //get the ammount of products in the db
$result1 = mysql_db_query($dbname,$query1); //queryresult1
$result2 = mysql_db_query($dbname,$query2); //queryresult2
$result3 = mysql_db_query($dbname,$query1); //queryresult3
$num_products = mysql_num_rows($result1); //count number of products
while ($row = mysql_fetch_assoc ($result2)) //put total ammount of products in db from query in a var
{
    $dataTotal = $row[countammount];
}

function circlePoint( $deg, $dia )
{
    $x = cos( deg2rad( $deg ) ) * ( $dia / 2 );
    $y = sin( deg2rad( $deg ) ) * ( $dia / 2 );
    return array( $x, $y );
}

$im = ImageCreate( $imageWidth, $imageHeight ); //make image area

//color array for chart. addmore colors if you have more data than colors below.
$color[] = ImageColorAllocate( $im, 255, 0, 0 ); //red
$color[] = ImageColorAllocate( $im, 255, 204, 0 );//yellow
$color[] = ImageColorAllocate( $im, 153, 204, 0 );//green
$color[] = ImageColorAllocate( $im, 153, 51, 255 );//purple
$color[] = ImageColorAllocate( $im, 0, 128, 255 );//blue
$color[] = ImageColorAllocate( $im, 255, 0, 128 );//pink
$color[] = ImageColorAllocate( $im, 153, 51, 255 );//purple
$color[] = ImageColorAllocate( $im, 192, 192, 192 );//grey
$color[] = ImageColorAllocate( $im, 204, 204, 0 );
$color[] = ImageColorAllocate( $im, 64, 128, 128 );
$color[] = ImageColorAllocate( $im, 204, 102, 153 );
$white = ImageColorAllocate( $im, 255, 255, 255 );
$black = ImageColorAllocate( $im, 0, 0, 0 );
$grey = ImageColorAllocate( $im, 215, 215, 215 );

ImageFill( $im, 0, 0, $white ); //make image background

$degree = 0;

/**********************************
make the pie chart with percentages
**********************************/

$i=-1; //set color array counter

while ($row = mysql_fetch_assoc ($result1))
{
    $i < sizeof( $row ); $i++; //counter for color array
    
    $startDegree = round( $degree );
    $degree += ( $row[groupcount] / $dataTotal ) * 360;
    $endDegree = round( $degree );
    
    $currentColor = $color[ $i % ( count( $color ) ) ];
    
    ImageArc( $im, $centerX, $centerY, $diameter, $diameter, $startDegree, $endDegree, $currentColor );
    
    list( $arcX, $arcY ) = circlePoint( $startDegree, $diameter );
    ImageLine( $im, $centerX, $centerY, floor( $centerX + $arcX ), floor( $centerY + $arcY ), $currentColor );
    
    list( $arcX, $arcY ) = circlePoint( $endDegree, $diameter );
    ImageLine( $im, $centerX, $centerY, ceil( $centerX + $arcX ), ceil( $centerY + $arcY ), $currentColor );
    
    $midPoint = round( ( ( $endDegree - $startDegree ) / 2 ) + $startDegree );
    list( $arcX, $arcY ) = circlePoint( $midPoint, $diameter / 1.5 );
    ImageFillToBorder( $im, floor( $centerX + $arcX ), floor( $centerY + $arcY ), $currentColor, $currentColor );

    //joe: i added -6 to center %'s better
    ImageString( $im, 2, floor( $centerX + $arcX - 6 ), floor( $centerY + $arcY - 6 ), intval( round( $row[groupcount] / $dataTotal * 100 ) ) . "%", $black );
}

/**********************************
setup for the menu and print title
**********************************/

$labelX = $centerX + $diameter / 2 + 10;
$labelY = $centerY - $diameter / 4;
$titleX = $labelX - $diameter / 4;
$titleY = $centerY - $diameter / 2;
//ImageString( $im, 3, $titleX + 1, $titleY + 1, $chartTitle, $grey );
ImageString( $im, 3, $titleX, $titleY, $chartTitle, $black ); //print chart title
ImageString( $im, 1, $labelX, $titleY + 14, date( "Y-m-d H:i:sa" ), $black ); //print date

/**********************************
make the menu,lables,totals
**********************************/

$i=-1; //set color array counter

while ($row1 = mysql_fetch_assoc ($result3))
{
    $i < sizeof( $row1 ); $i++; //counter for color array
    
    $currentColor = $color[ $i % ( count( $color ) ) ];
    ImageRectangle( $im, $labelX, $labelY, $labelX + $labelWidth, $labelY + $labelWidth, $black );
    ImageFilledRectangle( $im, $labelX + 1, $labelY + 1, $labelX + $labelWidth, $labelY + $labelWidth, $currentColor );
    ImageString( $im, 2, $labelX + $labelWidth + 5, $labelY, $row1[productname], $black );

    //ImageString( $im, 2, $labelX + $labelWidth + 60, $labelY, $row1[groupcount], $black );
    $labelY += $labelWidth + 2;
}

/**********************************
make the total
**********************************/

ImageString( $im, 3, $labelX, $labelY, "Total:", $black );
ImageString( $im, 3, $labelX + $labelWidth + 60, $labelY, $dataTotal, $black );
//ImageString( $im, 2, $labelX, $labelY + 15, $logo, $black );

Header( "Content-type: image/jpeg" ); //output image
Imagejpeg( $im );
ImageDestroy( $im ); //remove image from memory
?>