Page 1 of 1

Pie Charts

Posted: Tue Feb 24, 2009 12:28 pm
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.

Re: Pie Charts

Posted: Wed Feb 25, 2009 6:28 am
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

Re: Pie Charts

Posted: Wed Feb 25, 2009 10:09 am
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