饼图不会显示

时间:2013-01-09 17:01:22

标签: php mysql graph charts pie-chart

我正在尝试创建一个饼图,其中显示了我的“书”表中的前10本书(我正在做一个图书馆系统)。

我已经计算出前十名中的每本书被一个借款人在一个循环中借出多少次,并且还计算了每本书的百分比和度数以形成饼图,但我似乎无法找到一种实际形成饼图本身的方法。

我也无法弄清楚如何将百分比变成数组,以便在第二个循环中,它将显示每本书的弧。

到目前为止,这是我的代码:

<?php
    //execute the SQL query and return records
    $result = mysql_query("SELECT book_id, COUNT(*) FROM loan GROUP BY book_id ASC ORDER BY         count(*) DESC LIMIT 10");
    $book_count = mysql_num_rows($result);

    $step = 1;

    while($row = mysql_fetch_array($result))
    {
    $total = $total + $row['COUNT(*)'];
    $i[$step] = $row['COUNT(*)'];
    $step++;
    }

    $pie = imagecreate(500,500);
    $white = imagecolorallocate($pie,255,255,255);
    imagefill($pie,0,0,$white);

    $font = imageloadfont('calibri.gdf');
    $black = imagecolorallocate($pie,0,0,0);  //This is for the labeling also

    $angle = 0;

    for($index = 1; $index <= 10; $index++)
    {
        $percentage = (($i[$index]/$total)*100);
        #$degrees = (($percentage/100)*360);
    }

    for ($index2=0, $count=count($percentage); $index2<$count; $index2++)
    {
        #$percentage = (($i[$index]/$total)*100);
        $degrees = (($percentage[$index]/100)*360);
        $color = imagecolorallocate($pie,mt_rand(20,255),mt_rand(20,255),mt_rand(20,255));

        imagefilledarc($pie,100,100,125,125,$angle,($angle+$degrees),$color,IMG_ARC_PIE);
        imagefilledrectangle($pie,250,(50+(25*($index2+1))),275,(75+(25*($index2+1))),$color);
        imagestring($pie,$font,276,(55+(25*($index2+1))),$percentage[$index2]."  %",$black);

        $angle+=$degrees;
    }

    header("Content-type: image/png");
    imagepng($pie);
    imagedestroy($pie);

    #echo "<br>";
    #echo 'The sum is: ' . $total;

?>

0 个答案:

没有答案