Problems with Pie Chart

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • euniceee
    New Member
    • Mar 2008
    • 3

    Problems with Pie Chart

    Hello I have created this php file in order to create a pie chart, however the whole pie chart turns into yello colour rather than showing the information in different colors. What can I do to make it works?Thank you. I have created 2 files, one is testingpiechart .php and the other one is testingpie.php.
    Here's my code:
    Code:
     
    
    
    <?php
    $slide = $one + $two + $third + $fourth + $fifth; 
    // + $sixth;
    header('Content-type: image/png');
    $handle = imagecreate(100, 100);
    $background = imagecolorallocate($handle, 255, 255, 255);
    $red = imagecolorallocate($handle, 255, 0, 0);
    $green = imagecolorallocate($handle, 0, 255, 0);
    $blue = imagecolorallocate($handle, 0, 0, 255);
    $purple = imagecolorallocate($handle, 186, 85, 211);
    $pink = imagecolorallocate($handle, 255, 181, 197);
    $yellow = imagecolorallocate($handle, 255, 255, 0);
    $black = imagecolorallocate($handle, 0, 0, 0);
    $white = imagecolorallocate($handle, 255, 255, 255);
    $grey = imagecolorallocate($handle, 100, 100, 100);
    
    $darkred = imagecolorallocate($handle, 150, 0, 0);
    $darkgreen = imagecolorallocate($handle, 0, 150, 0);
    $darkblue = imagecolorallocate($handle, 0, 0, 150);
    $darkpurple = imagecolorallocate($handle, 148, 0, 211);
    $darkpink = imagecolorallocate($handle, 255, 20, 147);
    $darkyellow = imagecolorallocate($handle, 205, 205, 0);
    
    
    // 3D look
    for ($i = 60; $i > 50; $i--)
    {
    imagefilledarc($handle, 50, $i, 100, 50, 0, $one, $darkred, IMG_ARC_PIE);
    //imagefilledarc($handle, 50, $i, 100, 50, $one, $two , $darkblue, IMG_ARC_PIE);
    //imagefilledarc($handle, 50, $i, 100, 50, $two, $third , $darkgreen, IMG_ARC_PIE);
    //imagefilledarc($handle, 50, $i, 100, 50, $third, $fourth, $darkpurple, IMG_ARC_PIE);
    //imagefilledarc($handle, 50, $i, 100, 50, $fourth, $fifth , $darkpink, IMG_ARC_PIE);
    //imagefilledarc($handle, 50, $i, 100, 50, $sixth, 360 , $darkyellow, IMG_ARC_PIE);
    
    imagefilledarc($handle, 50, $i, 100, 50, $one, $slide , $darkblue, IMG_ARC_PIE);
    imagefilledarc($handle, 50, $i, 100, 50, $slide, $slide , $darkgreen, IMG_ARC_PIE);
    imagefilledarc($handle, 50, $i, 100, 50, $slide, $slide, $darkpurple, IMG_ARC_PIE);
    imagefilledarc($handle, 50, $i, 100, 50, $slide, $slide , $darkpink, IMG_ARC_PIE);
    imagefilledarc($handle, 50, $i, 100, 50, $slide, 360 , $darkyellow, IMG_ARC_PIE);
    }
    
    imagefilledarc($handle, 50, 50, 100, 50, 0, $one , $red, IMG_ARC_PIE);
    //imagefilledarc($handle, 50, 50, 100, 50, $one, $two , $blue, IMG_ARC_PIE);
    //imagefilledarc($handle, 50, 50, 100, 50, $two, $third , $green, IMG_ARC_PIE);
    //imagefilledarc($handle, 50, 50, 100, 50, $third, $fourth , $purple, IMG_ARC_PIE);
    //imagefilledarc($handle, 50, 50, 100, 50, $fourth, $fifth , $pink, IMG_ARC_PIE);
    //imagefilledarc($handle, 50, 50, 100, 50, $fifth, 360 , $yellow, IMG_ARC_PIE);
    
    imagefilledarc($handle, 50, 50, 100, 50, $one, $slide , $blue, IMG_ARC_PIE);
    imagefilledarc($handle, 50, 50, 100, 50, $slide, $slide , $green, IMG_ARC_PIE);
    imagefilledarc($handle, 50, 50, 100, 50, $slide, $slide , $purple, IMG_ARC_PIE);
    imagefilledarc($handle, 50, 50, 100, 50, $slide, $slide , $pink, IMG_ARC_PIE);
    imagefilledarc($handle, 50, 50, 100, 50, $slide, 360 , $yellow, IMG_ARC_PIE);
    
    imagepng($handle);
    
    ?>
    testingpie.php
    Code:
    <?
    // Connects to your Database 
    mysql_connect("localhost", "", "") or die(mysql_error()); 
    mysql_select_db("db") or die(mysql_error()); 
     
    $getnumofvote = mysql_query("select Number_of_Vote from Candidate");
    $sum = mysql_query("SELECT SUM(Number_of_Vote) FROM Candidate");
    $total = mysql_result($sum,0,0);
    
    //first row and first column
    $getvote1 = mysql_result($getnumofvote,0,0);
    $getvote2 = mysql_result($getnumofvote,1,0);
    $getvote3 = mysql_result($getnumofvote,2,0);
    $getvote4 = mysql_result($getnumofvote,3,0);
    $getvote5 = mysql_result($getnumofvote,4,0);
    $getvote6 = mysql_result($getnumofvote,5,0);
    
    $one = round (360 * $getvote1 / $total); 
    $two = round (360 * $getvote2 / $total); 
    $third = round (360 * $getvote3 / $total); 
    $fourth = round (360 * $getvote4 / $total); 
    $fifth = round (360 * $getvote5 / $total); 
    //$sixth = round (360 * $getvote6 / $total); 
    
    
    
    $per1 = round ($getvote1 / $total * 100); 
    $per2 = round ($getvote2 / $total * 100); 
    $per3 = round ($getvote3 / $total * 100); 
    $per4 = round ($getvote4 / $total * 100); 
    $per5 = round ($getvote5 / $total * 100); 
    $per6 = round ($getvote6 / $total * 100); 
    
    
    echo "<img src=testingpiechart.php?one=".$one."&two=".$two."&third=".$third."&fourth=".$fourth."&fifth=".$fifth."><br>"; 
    echo "<font color=ff0000>ee</font> = $getvote1 Vote(s), $per1 %<br>
    <font color=0000ff>SECOND </font> = $getvote2 Vote(s), $per2 %<br> 
    <font color=00ff00>THIRD </font> = $getvote3 Vote(s), $per3 %<br>
    <font color=ba55d3>FOURTH </font> = $getvote4 Vote(s), $per4 %<br>
    <font color=ffb5c5>Fifth </font> = $getvote5 Vote(s), $per5 %<br>
    <font color=ffff00>Sixth </font> = $getvote6 Vote(s), $per6 %<br>
    "; 
    
    ?><?
    // Connects to your Database 
    mysql_connect("localhost", "", "") or die(mysql_error()); 
    mysql_select_db("db") or die(mysql_error()); 
     
    $getnumofvote = mysql_query("select Number_of_Vote from Candidate");
    $sum = mysql_query("SELECT SUM(Number_of_Vote) FROM Candidate");
    $total = mysql_result($sum,0,0);
    
    //first row and first column
    $getvote1 = mysql_result($getnumofvote,0,0);
    $getvote2 = mysql_result($getnumofvote,1,0);
    $getvote3 = mysql_result($getnumofvote,2,0);
    $getvote4 = mysql_result($getnumofvote,3,0);
    $getvote5 = mysql_result($getnumofvote,4,0);
    $getvote6 = mysql_result($getnumofvote,5,0);
    
    $one = round (360 * $getvote1 / $total); 
    $two = round (360 * $getvote2 / $total); 
    $third = round (360 * $getvote3 / $total); 
    $fourth = round (360 * $getvote4 / $total); 
    $fifth = round (360 * $getvote5 / $total); 
    //$sixth = round (360 * $getvote6 / $total); 
    
    echo "<img src=testingpiechart.php?one=".$one."&two=".$two."&third=".$third."&fourth=".$fourth."&fifth=".$fifth."><br>"; 
    echo "<font color=ff0000>ee</font> = $getvote1 Vote(s<br>
    <font color=0000ff>SECOND </font> = $getvote2 Vote(s)<br> 
    <font color=00ff00>THIRD </font> = $getvote3 Vote(s)<br>
    <font color=ba55d3>FOURTH </font> = $getvote4 Vote(s)<br>
    <font color=ffb5c5>Fifth </font> = $getvote5 Vote(s)<br>
    <font color=ffff00>Sixth </font> = $getvote6 Vote(s)<br>
    "; 
    
    ?>
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Line 2:[php]$slide = $_GET['one'] + $_GET['two'] + $_GET['third'] + $_GET['fourth'] + $_GET['fifth'];[/php]

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Also, rather then sending data as one, two, third etc, try to send it as a string separated by comas. On the other end, exploded() it into array to get the parameters. With that you can have more than 5 parameters.

      Comment

      • euniceee
        New Member
        • Mar 2008
        • 3

        #4
        Originally posted by hsriat
        Line 2:[php]$slide = $_GET['one'] + $_GET['two'] + $_GET['third'] + $_GET['fourth'] + $_GET['fifth'];[/php]
        it's getting better now, it's showing 2 slides out of 6. What else can I change to make it display the whole pie chart?Thank you.

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Originally posted by euniceee
          it's getting better now, it's showing 2 slides out of 6. What else can I change to make it display the whole pie chart?Thank you.
          Add[php]$one = $_GET[one];
          $two = $_GET[two];
          $third = $_GET[third];
          $fourth = $_GET[fourth];
          $fifth = $_GET[fifth];[/php] in the starting of testingpiechart .php.

          Also the arguments you have used for the function imagefilledarc in testingpiechart .php will not give you the result you want. Modify the $start and $end accordingly.

          Comment

          Working...