首页 > 开发 > PHP > 正文

PHP对象编程实现3D饼图

2024-05-04 22:54:06
字体:
来源:转载
供稿:网友


<?php

//公用函数

//把角度转换为弧度
function deg2arc($degrees) {
return($degrees * (pi()/180.0));
}

//rgb
function getrgb($color){
$r=($color>>16) & 0xff;
$g=($color>>8) & 0xff;
$b=($color) & 0xff;
return (array($r,$g,$b));
}

// 取得在椭圆心为(0,0)的椭圆上 x,y点的值
function pie_point($deg,$va,$vb){
$x= cos(deg2arc($deg)) * $va;
$y= sin(deg2arc($deg)) * $vb;
return (array($x, $y));
}


//3d饼图类

class pie3d{

var $a; //椭圆长半轴
var $b; //椭圆短半轴
var $dataarray; //每个扇形的数据
var $colorarray; //每个扇形的颜色 要求按照十六进制书写但前面不加0x
//为边缘及阴影为黑色

function pie3d($pa=100,$pb=60,$sdata="100,200,300,400,500", $scolor="ee00ff,dd0000,cccccc,ccff00,00ccff")
{
$this->a=$pa;
$this->b=$pb;
$this->dataarray=split(",",$sdata);
$this->colorarray=split(",",$scolor);
}

function seta($v){
$this->a=$v;
}

function geta(){
return $this->a;
}

function setb($v){
$this->b=$v;
}

function getb(){
return $this->b;
}

function setdataarray($v){
$this->dataarray=split(",",$v);
}

function getdataarray($v){
return $this->dataarray;
}

function setcolorarray($v){
$this->colorarray=split(",",$v);
}

function getcolorarray(){
return $this->colorarray;
}


function drawpie(){
$image=imagecreate($this->a*2+40,$this->b*2+40);
$piecenterx=$this->a+10;
$piecentery=$this->b+10;
$doublea=$this->a*2;
$doubleb=$this->b*2;
list($r,$g,$b)=getrgb(0);
$colorborder=imagecolorallocate($image,$r,$g,$b);
$datanumber=count($this->dataarray);

//$datatotal
for($i=0;$i<$datanumber;$i++) $datatotal+=$this->dataarray[$i]; //算出数据和

//填充背境
imagefill($image, 0, 0, imagecolorallocate($image, 0xff, 0xff, 0xff));

/*
** 画每一个扇形
*/
$degrees = 0;
for($i = 0; $i < $datanumber; $i++){
$startdegrees = round($degrees);
$degrees += (($this->dataarray[$i]/$datatotal)*360);
$enddegrees = round($degrees);
$percent = number_format($this->dataarray[$i]/$datatotal*100, 1);
list($r,$g,$b)=getrgb(hexdec($this->colorarray[$i]));
$currentcolor=imagecolorallocate($image,$r,$g,$b);
if ($r>60 and $r<256) $r=$r-60;
if ($g>60 and $g<256) $g=$g-60;
if ($b>60 and $b<256) $b=$b-60;
$currentdarkcolor=imagecolorallocate($image,$r,$g,$b);
//画扇形弧
imagearc($image,$piecenterx,$piecentery,$doublea,$doubleb,$startdegrees,$enddegrees,$currentcolor);
//画直线
list($arcx, $arcy) = pie_point($startdegrees , $this->a , $this->b);
imageline($image,$piecenterx,$piecentery,floor($piecenterx + $arcx),floor($piecentery + $arcy),$currentcolor);
//画直线
list($arcx, $arcy) = pie_point($enddegrees,$this->a , $this->b);
imageline($image,$piecenterx,$piecentery,ceil($piecenterx + $arcx),ceil($piecentery + $arcy),$currentcolor);
//填充扇形
$midpoint = round((($enddegrees - $startdegrees)/2) + $startdegrees);
list($arcx, $arcy) = pie_point($midpoint, $this->a*3/4 , $this->b*3/4);

imagefilltoborder($image,floor($piecenterx + $arcx),floor($piecentery + $arcy), $currentcolor,$currentcolor);
imagestring($image,2,floor($piecenterx + $arcx-5),floor($piecentery + $arcy-5),$percent."%",$colorborder);

//画阴影
if ($startdegrees>=0 and $startdegrees<=180){
if($enddegrees<=180){
for($k = 1; $k < 15; $k++)
imagearc($image,$piecenterx, $piecentery+$k,$doublea, $doubleb, $startdegrees, $enddegrees, $currentdarkcolor);
}else{
for($k = 1; $k < 15; $k++)
imagearc($image,$piecenterx, $piecentery+$k,$doublea, $doubleb, $startdegrees, 180, $currentdarkcolor);
}

}
}

/*到此脚本已经生了一幅图像了
**现在需要的是把它发到浏览器上,重要的一点是要将标头发给浏览器,让它知道是一个gif文件。不然的话你只能看到一堆奇怪的乱码
*/
//输出生成的图片
header("content-type: image/gif");
imagegif($image);
imagedestroy($image);
}//end drawpie()
}//end class


//实现

$objp = new pie3d();
$objp->drawpie();
?>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表