0

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

我计算出前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;

?>
4

0 回答 0