Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
PHP ›
PHP Pie Chart Script

PHP Pie Chart Script

July 7, 2011 PHP Jesin A 5 Comments

php category thumbnail

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.

Related posts:

php category thumbnailCreating a PHP MySQL Login Page Script php category thumbnailPHP Captcha Tutorial php category thumbnailCheckbox in PHP php category thumbnailPHP Open Port Check Script windows category thumbnailActive Directory User Creation Script

Tags: php, php scripts

Comments

  1. BooRanger says

    January 31, 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

    Reply
  2. BooRanger says

    January 31, 2012 at 9:38 pm

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

    Reply
  3. @IraFromSyosset says

    June 12, 2014 at 8:37 pm

    Thank you! Nice tool for adding a little sizzle to my project!!

    Reply
  4. manzer anis says

    September 16, 2014 at 1:22 pm

    nice and thank for dynamic pie chart

    Reply
  5. Emz says

    June 8, 2015 at 1:29 pm

    Hi,
    would you explain me more the img src part.. I added my website to the link in the example(print “”;) while opt and value is my database columns, but it returns nothing.
    what should I do.??

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.