Pie Charts

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Pie Charts

Post by andylyon87 »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Code: Select all

<?php
    include('includes/mysql_connect.php');
    
    $query = "SELECT student_Id FROM booking";
    $result = mysql_query($query);
    $num_rows = mysql_num_rows($result);
    
    $query = "SELECT student.school, COUNT(student.school) FROM student, booking AS b WHERE b.student_Id = student.student_Id  GROUP BY student.school ORDER BY student.school"; //returns to rows, will be four eventually
     $result = mysql_query($query) or die(mysql_error());
    
    
    while($row = mysql_fetch_array($result)){
        
        $degree[] = round($row['COUNT(student.school)'] / $num_rows * 3.60 * 100, 2);
    }
 
    $myImage = ImageCreate(300,300);
 
    
    $white = ImageColorAllocate($myImage, 255, 255, 255);
    $red = ImageColorAllocate($myImage, 255, 0, 0);
    $green = ImageColorAllocate($myImage, 0, 255, 0);
        
    $zero = $degree[0];
    $one = $degree[0]+$degree[1];
    
    
    ImageFilledArc($myImage, 100,100,150,150,0,$zero, $red, IMG_ARC_PIE); //this needs to have more than just two slices
    ImageFilledArc($myImage, 100,100,150,150,$zero,$one, $green, IMG_ARC_PIE);
    
    header ("Content-type: image/png");
    ImagePng($myImage);
    
    ImageDestroy($myImage);
    
    
?>
I have been trying to generate pie charts dynamically for about 6 hours now and Im gettin fed up. The thing is just not working for me. I have had a look at loads of websites but they just don't work for me.

The code above shows me a simple 2 slice pie, but in the future I want to use this code on pies that will display more complex statements. All I need is to know how to cycle through and change the colour and alter the size of the slice (basically how the hell do I automate this thing lol)

thanks in advance for your help

Andy


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Re: Pie Charts

Post by andylyon87 »

hi guys does if anyone has ever made a pie chart if they could post their code up, just so I could take a look at other peoples takes on this.

thanks
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Pie Charts

Post by pickle »

I've never built a pie chart from scratch because there are libraries out there that already do it for you. Namely:

pChart
JpGraph
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply