TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-13-2008, 10:11 PM   #1 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default Pie Charts In PHP

Ok well Orc i believe wanted a pie chart tutorial. SO here i go,

http://www.jooney.co.uk/php/piechart/pieChart/pie.php

You can see the example above. It automatically adds pieces to the pie depending on how many items there are in an array and of course made it abit more interesting in having randomly changing slices :)

first things first lets define the header information

PHP Code:
header ("Content-type: image/pjpeg"); //define the page to be a image 
Ok now we can create the array.

PHP Code:
$array = array(9885,2350,5775,9228,7800,2689,9889,10000);
$total array_sum($array);
sort($array);
$counter 0
Now we need to work out some values that will be used in the pie of course the chart has to equal 360 and no more.

PHP Code:
foreach($array as $value)
{
      
$result[$counter] =  round($value $total 360,0);
      
$counter++;

Its kind of up to you if you wish to round the numbers or not.

Now lets create the image

PHP Code:
$im imagecreate(500,600);
imagecolorallocate ($im0,12,225); 
Ok now its time to go through and make the pieces of the pie....wow that sounds good doesnt it. lol

PHP Code:
$i count($array)-1;
$prevTotal 360;
foreach(
$result as $value)
{
        
$rand1 rand(0,255);
        
$rand2 rand(0,255);
        
$rand3 rand(0,255);
        
$colour imagecolorallocate ($im$rand1,$rand2,$rand3);

        
imagefilledarc ($im250230,400,400,0,$prevTotal,$colourIMG_ARC_PIE);
        
$i--;
        
$prevTotal $prevTotal $result[$i];

You maybe wondering why i used a for each loop, but i couldnt be bothered to use a for loop lol you can do so if you wish, but as long as you get the idea then its all good.

Now the rest is up to use its a very basic way of showing the values of the data. The way i did it was to make it so it didnt make the values appear off the bottom.

PHP Code:
    $rand1 rand(0,255);
    
$rand2 rand(0,255);
    
$rand3 rand(0,255);

    
$j count($array)-1;
    
$start 500 count($array) * 10;
    
$add 0;

    foreach(
$array as $value)
    {
        
imagestring($im,2,10,$start+$add,$value,$colour);
        
$j--;
        
$add += 10;
    }
    
//Add the text
    
imagestring($im,5,10,580," Coded By: Dale ( Rendair )",$colour);


    
imagejpeg ($im); 
and thats it...very simple id say...if you want anymore detail let me know....when i have time ill try make it better, but im sure from what is here you can work out ways of making it abit better. It gets the job done.
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
The Following User Says Thank You to Rendair For This Useful Post:
Orc (12-14-2008)
Old 12-13-2008, 10:24 PM   #2 (permalink)
The Contributor
 
Izym's Avatar
 
Join Date: Sep 2007
Posts: 32
Thanks: 0
Izym is on a distinguished road
Default

Nice tutorial, but I think he wanted a tutorial on how to make one that uses AA.
Izym is offline  
Reply With Quote
Old 12-13-2008, 10:28 PM   #3 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default

Oki i shall expand on this tutorial tomorrow with the solution to that also :P
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-15-2008, 03:35 AM   #4 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

nice tutorial.
__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st is offline  
Reply With Quote
Old 12-15-2008, 05:48 PM   #5 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Looks good so far but yes I'd like AA
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 12-15-2008, 05:53 PM   #6 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

Hmmm... Idk maybe it's just me, but I don't think the GD library in PHP does a very good job with images.

Good tutorial though!
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 12-15-2008, 07:30 PM   #7 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by ETbyrne View Post
Hmmm... Idk maybe it's just me, but I don't think the GD library in PHP does a very good job with images.

Good tutorial though!
That's what I'm thinking, that's why I wnat ImageMagick in the core library. >=(
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 12-15-2008, 11:15 PM   #8 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

What a coincidence, I just created a pie chart program much like that in vb.net, here is a pie chart it created


This isn't as blurry as the PHP generated one, but still need anti-aliasing. I'll see if there is some generic way of doing it.
__________________

Village Idiot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
10 PHP Myths Dispelled Wildhoney General 9 06-15-2009 06:55 AM
PHP Compressor Kalle Script Giveaway 8 05-28-2008 12:14 AM
3D Pie charts with PHP GD Rendair Advanced PHP Programming 2 03-24-2008 04:34 PM
what are all the subjects in php? sarmenhb General 7 01-21-2008 05:41 PM
Pie Charts in PHP Rendair Advanced PHP Programming 5 12-04-2007 11:14 PM


All times are GMT. The time now is 11:46 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design