📄 pikelet.php
字号:
<?
include("db_conn.php");
include("db_func.php");
// 定义一把角度转换为弧度的函数 BEGIN
function Deg2Arc($degrees)
{
return($degrees * (pi()/180.0));
}
// 定义一把角度转换为弧度的函数 END
// 定义一取得圆弧上之 x,y 点的函数 BEGIN
function Center2Pie($deg,$length)
{
$x= cos(Deg2Arc($deg)) * $length;
$y= sin(Deg2Arc($deg)) * $length;
return (array($x, $y));
}
// 定义一取得圆弧上之 x,y 点的函数 END
class pikelet // 声明一个 pikelet 的类别
{
var $radius; // 圆半径
var $DataArray; // 每个扇形所代表的数据
var $sellData; // 销售量数组
var $productID; // 产品序号
var $DataTotal; // 销售总额
var $num; // 销售标的个数
function pikelet($res, $length = 250) // 扇形建构子
{
$this->radius = $length; // 设定圆半径
$this->num = db_num_rows($res);
for($i=0;$i<$this->num;$i++)
{
$row = db_fetch_array($res);
$this->sellData[$i] = $row['p_sell']; // 产品销售量
$this->productID[$i] = $row['p_id']; // 产品序号
$this->DataTotal += $row['p_sell']; // 总销售量
}
}
function DrawPie() // 画饼图
{
$im = ImageCreate($this->radius*2+40,$this->radius*2+40);
$PieCenterX=$this->radius+10; // 圆心 X坐标
$PieCenterY=$this->radius+10; // 圆心 Y坐标
$radius=$this->radius*2; // 圆半径
// 定义所使欲使用的颜色 BEGIN
$colorBorder = ImageColorAllocate($im, 0, 0, 0); // 黑
$colorback = ImageColorAllocate($im, 255, 255, 255); // 白
$colors[0] = ImageColorAllocate($im, 255, 128, 128); // 粉红
$colors[1] = ImageColorAllocate($im, 100, 149, 237); // 粉蓝
$colors[2] = ImageColorAllocate($im, 50, 205, 50); // 亮绿
$colors[3] = ImageColorAllocate($im, 244, 20, 190); // 紫
$colors[4] = ImageColorAllocate($im, 255, 215, 0); // 米黄
$colors[5] = ImageColorAllocate($im, 144, 238, 144); // 浅绿
$colors[6] = ImageColorAllocate($im, 70, 130, 180); // 海军蓝
$colors[7] = ImageColorAllocate($im, 244, 164, 96); // 棕
$colors[8] = ImageColorAllocate($im, 139, 121, 94); // 土
$colors[9] = ImageColorAllocate($im, 190, 190, 190); // 灰
// 定义所使欲使用的颜色 END
ImageFill($im, 0, 0, $colorback); // 填充背景
// 开始画每一个扇形
for($i=0;$i<$this->num;$i++)
{
// 四舍五入画弧的起始角度
$StartDegrees = round($Degrees);
// 累积角度
$Degrees += (($this->sellData[$i] / $this->DataTotal)*360);
// 四舍五入画弧的结束角度
$EndDegrees = round($Degrees);
// 算出每一产品所占的百分比
$percent = number_format(($this->sellData[$i] / $this->DataTotal)*100, 1);
// 分配颜色 BEGIN
$tmp_color = $i%10; // 取10的余数
$CurrentColor = $colors[$tmp_color]; // 指定颜色
// 分配颜色 END
// 画扇形的弧
ImageArc($im, $PieCenterX, $PieCenterY, $radius, $radius, $StartDegrees, $EndDegrees, $CurrentColor);
// 画扇形的一边直线 BEGIN
list($ArcX, $ArcY) = Center2Pie($StartDegrees, $this->radius, $this->radius);
ImageLine($im, $PieCenterX, $PieCenterY, floor($PieCenterX + $ArcX), floor($PieCenterY + $ArcY), $CurrentColor);
// 画扇形的一边直线 END
// 画画扇形的另一边直线 BEGIN
list($ArcX, $ArcY) = Center2Pie($EndDegrees, $this->radius, $this->radius);
ImageLine($im, $PieCenterX, $PieCenterY, ceil($PieCenterX + $ArcX), ceil($PieCenterY + $ArcY), $CurrentColor);
// 画画扇形的另一边直线 END
// 填充扇形 BEGIN
$MidPoint = round((($EndDegrees - $StartDegrees)/2) + $StartDegrees);
list($ArcX, $ArcY) = Center2Pie($MidPoint, $this->radius*3/4, $this->radius*3/4);
ImageFilltoBorder($im, floor($PieCenterX + $ArcX), floor($PieCenterY + $ArcY), $CurrentColor, $CurrentColor);
// 填充扇形 END
// 写上字符串
ImageString($im, 2, floor($PieCenterX + $ArcX-20), floor($PieCenterY + $ArcY-2),$this->productID[$i] . " - " . $this->sellData[$i], $colorBorder);
ImageString($im, 2, floor($PieCenterX + $ArcX-10), floor($PieCenterY + $ArcY-20), $percent . "%" , $colorback);
// 写上字符串 END
}
Imagepng($im); // 生成png文件
ImageDestroy($im); // 移除图形所占的内存
}
}
// 取出类别为计算机书籍的所有产品 BEGIN
$SQLStr = "SELECT * FROM product WHERE c_id='1'";
$res = db_query($SQLStr);
// 取出类别为计算机书籍的所有产品 END
if (db_num_rows($res)>0)
{
header("Content-type: image/png");
$pie = new pikelet($res); // 声明 pie 为一个 pikelet 对象
$pie->DrawPie();
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -