PHP Pie Chart Script

Written by A.Jesin Thursday, 7 July 2011 09:26

A PHP Pie Chart script created by me using which any data and values can be represented using the pie chart. The PHP Pie Chart script is available for free download. By passing a query string you can compare any number of values. What follows is how to use my PHP Pie Chart Script and the code to download. The PHP Pie Chart script uses the following PHP image functions ImageCreate(), ImageColorAllocate(), imagefilledrectangle(), ImageFilledArc(), imagettftext(), ImagePNG() and ImageDestroy(). To edit the code you need to know how to use these functions.

Download the PHP Pie Chart Script from the link below

Download PHP Pie Chart Script

The zip file contains a True Type Font file named arial.ttf. This file is required by the script to display text in Arial font style. When you upload the PHP Pie Chart Script upload arial.ttf file too to the same location.

How to use the PHP Pie Chart Script ?

After you upload the file to your web server you need to access the URL of the script by passing values using GET method. I’ll explain an exam here, to represent the number of males and females use the following link

http://www.yourwebsite.com/pie_chart.php?names=Male,Female&values=23,20

You can pass any number of names and values but with the condition names=values. For the above example you’ll see a pie chart similar to the one below

php pie chartTo show this image in some other page call this URL in the src attribute of the <img> tag like this

<img src="http://www.yourwebsite.com/pie_chart.php?names=Male,Female&values=23,20" />

Remember to separate each value and name with a comma, and the values specified should be in the order of the names. You can use the implode() function of PHP to merge dynamically passed values. Take a look at the code below. It is a practical example of retrieving data from a MySQL database and representing it using the PHP Pie Chart Script.

<?php
$con=mysql_connect("hostname","username","password");
mysql_select_db("school",$con);
$query=mysql_query("SELECT `names`,`marks` FROM `marksheet`);
$i=0;
while($data=mysql_fetch_array($query))
{
$sname[$i]=$data['names'];
$smark[$i]=$data['marks'];
$i++;
}
mysql_close($con);
$all_names=implode(",",$sname);
$all_marks=implode(",",$smark);
print "<h1>Comparison of Student marks<h1>";
print "<img src='http://www.yourwebsite.com/pie_chart.php?names=$all_names&marks=$all_marks' />";
?>

Did that code confuse you ? Let me explain things, we are connecting to a MySQL database (read Connect PHP with MySQL) and retrieving information from a table “marksheet” the names and marks of all students are stored in the arrays $sname and $smark respectively. The implode function joins all elements in the array into a single line of string separating them with the “glue” specified. Here the glue is comma (,) so the names become “name1,name2,name3……..nameX” and marks become “56,87,34,65,…….X” we place this string in the src attribute of the <img> tag. When the page loads this is passed as a parameter for the pie_chart.php script which dynamically “draws” the Pie Chart.

All your doubts are welcome use the comment form below.

Also read:

Enjoyed reading this article, subscribe to stay up-to-date with more such articles


2 Comments

  1. BooRanger   |  Tuesday, 31 January 2012 at 9:12 pm

    hey,
    So I’ve my database nicepie

    Table Pie
    name price
    Cherry 45.500
    choc 54.500

    I’ve script on the my server. I’ve changed pie.php

    mysql_select_db(“nicepiel”,$con);
    $query=mysql_query(“SELECT `names`,`price` FROM `pie`);
    $i=0;
    while($data=mysql_fetch_array($query))
    {
    $sname[$i]=$data['name']; //line 8 in my code, where it breaks
    $smark[$i]=$data['price'];
    $i++;
    }

    when I run the script i get the following error
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING on line 8

    Any idea?

    Cheers Boo

  2. BooRanger   |  Tuesday, 31 January 2012 at 9:38 pm

    I had cut out a “, god its always the little things.

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>