首页 > 开发 > PHP > 正文

用GD图库生成横竖柱状图折线图的类

2024-05-04 23:05:17
字体:
来源:转载
供稿:网友

最近写的一个gd图库用以生成横竖柱状图和折线图的类库,算是一个教学例程吧  
<?
class imagereport{
var $x;//图片大小x轴
var $y;//图片大小y轴
var $r;//背影色r值
var $g;//...g.
var $b;//...b.
var $transparent;//是否透明1或0
var $image;//图片对像
//-------------------
var $arraysplit;//指定用于分隔数值的符号
var $itemarray;//数值
var $reporttype;//图表类型,1为竖柱形2为横柱形3为折线形
var $border;//距离
//-------------------
var $fontsize;//字体大小
var $fontcolor;//字体颜色
//--------参数设置函数
function setimage($sizex,$sizey,$r,$g,$b,$transparent){
$this->x=$sizex;  
$this->y=$sizey;  
$this->r=$r;  
$this->g=$g;  
$this->b=$b;  
$this->transparent=$transparent;  
}  
function setitem($arraysplit,$itemarray,$reporttype,$border){  
$this->arraysplit=$arraysplit;  
$this->itemarray=$itemarray;  
$this->reporttype=$reporttype;  
$this->border=$border;  
}  
function setfont($fontsize){  
$this->fontsize=$fontsize;  
}  
//----------------主体  
function printreport(){  
header( "content-type: image/gif");  
//建立画布大小  
$this->image=imagecreate($this->x,$this->y);  
//设定画布背景色  
$background=imagecolorallocate($this->image,$this->r,$this->g,$this->b);  
if($this->transparent=="1"){  
//背影透明  
imagecolortransparent($this->image,$background);  
}else{  
//如不要透明时可填充背景色  
imagefilledrectangle($this->image,0,0,$this->x,$this->y,$background);  
}  
//参数字体文小及颜色  
$this->fontcolor=imagecolorallocate($this->image,255-$this->r,255-$this->g,255-$this->b);  
switch ($this->reporttype){  
case "0":  
break;  
case "1":  
$this->imagecolumns();  
break;  
case "2":  
$this->imagecolumnh();  
break;  
case "3":  
$this->imageline();  
break;  
}  
$this->printxy();  
$this->printall();  
}  
//-----------打印xy坐标轴  
function printxy(){  
//画xy坐标轴*/  
$color=imagecolorallocate($this->image,255-$this->r,255-$this->g,255-$this->b);  
$xx=$this->x/10;  
$yy=$this->y-$this->y/10;  
imageline($this->image,$this->border,$this->border,$this->border,$this->y-$this->border,$color);//x轴  
imageline($this->image,$this->border,$this->y-$this->border,$this->x-$this->border,$this->y-$this->border,$color);//y轴  
//y轴上刻度  
$rulery=$this->y-$this->border;  
while($rulery>$this->border*2){  
$rulery=$rulery-$this->border;  
imageline($this->image,$this->border,$rulery,$this->border-2,$rulery,$color);  
}  
//x轴上刻度  
$rulerx=$rulerx+$this->border;  
while($rulerx<($this->x-$this->border*2)){  
$rulerx=$rulerx+$this->border;  
//imageline($this->image,$this->border,10,$this->border+10,10,$color);  
imageline($this->image,$rulerx,$this->y-$this->border,$rulerx,$this->y-$this->border+2,$color);  
}  
}  

//--------------竖柱形图  
function imagecolumns(){  
$item_array=split($this->arraysplit,$this->itemarray);  
$num=count($item_array);  
$item_max=0;  
for ($i=0;$i<$num;$i++){
$item_max=max($it

em_max,$item_array[$i]);
}
$xx=$this->border*2;  
//画柱形图  
for ($i=0;$i<$num;$i++){
srand((double)microtime()*1000000);
if($this->r!=255 && $this->g!=255 && $this->b!=255){  
$r=rand($this->r,200);  
$g=rand($this->g,200);  
$b=rand($this->b,200);  
}else{  
$r=rand(50,200);  
$g=rand(50,200);  
$b=rand(50,200);  
}  
$color=imagecolorallocate($this->image,$r,$g,$b);  
//柱形高度  
$height=($this->y-$this->border)-($this->y-$this->border*2)*($item_array[$i]/$item_max);  
imagefilledrectangle($this->image,$xx,$height,$xx+$this->border,$this->y-$this->border,$color);  
imagestring($this->image,$this->fontsize,$xx,$height-$this->border,$item_array[$i],$this->fontcolor);  
//用于间隔  
$xx=$xx+$this->border*2;  
}  
}  

//-----------横柱形图  
function imagecolumnh(){  
$item_array=split($this->arraysplit,$this->itemarray);  
$num=count($item_array);  
$item_max=0;  
for ($i=0;$i<$num;$i++){
$item_max=max($item_max,$item_array[$i]);
}
$yy=$this->y-$this->border*2;  
//画柱形图  
for ($i=0;$i<$num;$i++){
srand((double)microtime()*1000000);
if($this->r!=255 && $this->g!=255 && $this->b!=255){  
$r=rand($this->r,200);  
$g=rand($this->g,200);  
$b=rand($this->b,200);  
}else{  
$r=rand(50,200);  
$g=rand(50,200);  
$b=rand(50,200);  
}  
$color=imagecolorallocate($this->image,$r,$g,$b);  
//柱形长度  
$leight=($this->x-$this->border*2)*($item_array[$i]/$item_max);  
imagefilledrectangle($this->image,$this->border,$yy-$this->border,$leight,$yy,$color);  
imagestring($this->image,$this->fontsize,$leight+2,$yy-$this->border,$item_array[$i],$this->fontcolor);  
//用于间隔  
$yy=$yy-$this->border*2;  
}  
}  

//--------------折线图  
function imageline(){  
$item_array=split($this->arraysplit,$this->itemarray);  
$num=count($item_array);  
$item_max=0;  
for ($i=0;$i<$num;$i++){
$item_max=max($item_max,$item_array[$i]);
}
//$xx=$this->border;  
//画柱形图  
for ($i=0;$i<$num;$i++){
srand((double)microtime()*1000000);
if($this->r!=255 && $this->g!=255 && $this->b!=255){  
$r=rand($this->r,200);  
$g=rand($this->g,200);  
$b=rand($this->b,200);  
}else{  
$r=rand(50,200);  
$g=rand(50,200);  
$b=rand(50,200);  
}  
$color=imagecolorallocate($this->image,$r,$g,$b);  
//柱形高度  
$height_now=($this->y-$this->border)-($this->y-$this->border*2)*($item_array[$i]/$item_max);  
if($i!="0"){  
imageline($this->image,$xx,$height_next,$xx+$this->border,$height_now,$color);  
}  
imagestring($this->image,$this->fontsize,$xx+$this->border,$height_now-$this->border/2,$item_array[$i],$this->fontcolor);  
$height_next=$height_now;  
//用于间隔  
$xx=$xx+$this->border;  
}  
}  

//--------------完成打印图形yemaosheng.com  
function printall(){  
imagegif($this->image);  
imagedestroy($this->image); &nbs

p;
}  
//--------------调试  
function debug(){  
echo "x:".$this->x."<br>y:".$this->y;  
echo "<br>border:".$this->border;  
$item_array=split($this->arraysplit,$this->itemarray);  
$num=count($item_array);  
echo "<br>数值个数:".$num."<br>数值:";  
for ($i=0;$i<$num;$i++){
echo "<br>".$item_array[$i];  
}  
}  
}  

$report=new imagereport;  
$report->setimage(600,300,255,255,255,1);//参数(长,宽,背影色r,g,b,是否透明1或0)  
$temparray="50,25,100,250,180,200,150,220,200,150,50,25,100,250,180,200,150,220,200,150";//数值,用指定符号隔开  
$report->setitem(',',$temparray,3,20);//参数(分隔数值的指定符号,数值变量,样式1为竖柱图2为横柱图3为折线图,距离)  
$report->setfont(1);//字体大小1-10  
$report->printreport();  
//$report->debug();//调式之用  
?>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表