<?php //本段代码将生成横状图、柱状图及折线图的图片。
//各相关函数更详细的说明请参考php手册。 class ColumnImage//类名称
{
var $X;
//图像的宽 var $Y;
//图像的高 var $R;
//背影色R值 var $G;
//背影色G值 var $B;
//背影色B值 var $transparent;
//背景:1透明、0不透明 var $image;
//图片对像标识 var $items;
//图像中的元素(数组) var $colors;
//系统颜色数组 var $type;
//图表类型:1为竖柱形、2为横柱形、3为折线形 var $border;
//图像中元素间的距离 var $fontSize;
//文字大小 var $fontColor;
//文字颜色 var $font;
//中文字库的绝对路径名称 var $useRandColor;
//元素颜色:1随机、0系统自定义 function ColumnImage( $width, $height, $R, $G, $B, $fontSize, $transparent, $type, $useRandColor, $border )
{
$this->X = $width;
//初始化图像的宽 $this->Y = $height;
//初始化图像的高 $this->R = $R;
//背影色R值 $this->G = $G;
//背影色G值 $this->B = $B;
//背影色B值 $this->transparent = $transparent;
//初始化背景类型 $this->fontSize = $fontSize;
//初始化文字大小 $this->type = $type;
//设置图表的类型 $this->useRandColor = $useRandColor;
//初始化元素的颜色的类型 $this->border = $border;
//初始化图像中元素间分隔的距离 $this->image = imageCreate( $this->X, $this->Y );
//创建图像,返回图像标识符$this->image,其中$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 );
//设置文字颜色 if ( $this->useRandColor != 1 )
{
//定义系统颜色数组
$this->colors[] = imagecolorallocate( $this->image, 128, 212, 255 );//80D4FF
$this->colors[] = imagecolorallocate( $this->image, 255, 240, 128 );//FFF080
$this->colors[] = imagecolorallocate( $this->image, 255, 177, 140 );//FFB18C
$this->colors[] = imagecolorallocate( $this->image, 196, 158, 255 );//C49EFF
$this->colors[] = imagecolorallocate( $this->image, 179, 238, 160 );//B3EEA0
$this->colors[] = imagecolorallocate( $this->image, 127, 152, 235 );//7F98EB
$this->colors[] = imagecolorallocate( $this->image, 233, 111, 197 );//E96FC5
$this->colors[] = imagecolorallocate( $this->image, 91, 193, 2 );//5BC116
$this->colors[] = imagecolorallocate( $this->image, 147, 44, 219 );//932CDB
$this->colors[] = imagecolorallocate( $this->image, 186, 255, 1 );//BAFF01
$this->colors[] = imagecolorallocate( $this->image, 177, 177, 177 );//B1B1B1
$this->colors[] = imagecolorallocate( $this->image, 228, 228, 228 );//E4E4E4
}
} function drawPic()
//选择相应图表的画法
{
Switch( $this->type )
{
case 1:
$this->imageColumnS();
//输出竖柱形图
break;
case 2:
$this->imageColumnH();
//输出横柱形图
break;
case 3:
$this->imageHinge();
//输出折线图
break;
} $this->drawXY();
//输出xy坐标轴
} function drawXY()
//输出xy坐标轴
{
$color = imageColorAllocate( $this->image, 255 - $this->R, 255 - $this->G, 255 - $this->B );
//定义坐标轴的颜色,与图像背景色相反,以突出效果 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轴 $rulerY = $this->Y - $this->border;
while( $rulerY > $this->border * 2 )
//画y轴上的刻度
{
$rulerY = $rulerY - $this->border;
imageline( $this->image, $this->border, $rulerY, $this->border-2, $rulerY, $color );
} $rulerX = $rulerX + $this->border;
while( $rulerX < ($this->X - $this->border * 2) )
//画y轴上的刻度
{
$rulerX = $rulerX + $this->border;
imageline( $this->image, $rulerX, $this->Y - $this->border, $rulerX, $this->Y - $this->border + 2, $color );
//画出标尺上的一个刻度
}
}
//各相关函数更详细的说明请参考php手册。 class ColumnImage//类名称
{
var $X;
//图像的宽 var $Y;
//图像的高 var $R;
//背影色R值 var $G;
//背影色G值 var $B;
//背影色B值 var $transparent;
//背景:1透明、0不透明 var $image;
//图片对像标识 var $items;
//图像中的元素(数组) var $colors;
//系统颜色数组 var $type;
//图表类型:1为竖柱形、2为横柱形、3为折线形 var $border;
//图像中元素间的距离 var $fontSize;
//文字大小 var $fontColor;
//文字颜色 var $font;
//中文字库的绝对路径名称 var $useRandColor;
//元素颜色:1随机、0系统自定义 function ColumnImage( $width, $height, $R, $G, $B, $fontSize, $transparent, $type, $useRandColor, $border )
{
$this->X = $width;
//初始化图像的宽 $this->Y = $height;
//初始化图像的高 $this->R = $R;
//背影色R值 $this->G = $G;
//背影色G值 $this->B = $B;
//背影色B值 $this->transparent = $transparent;
//初始化背景类型 $this->fontSize = $fontSize;
//初始化文字大小 $this->type = $type;
//设置图表的类型 $this->useRandColor = $useRandColor;
//初始化元素的颜色的类型 $this->border = $border;
//初始化图像中元素间分隔的距离 $this->image = imageCreate( $this->X, $this->Y );
//创建图像,返回图像标识符$this->image,其中$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 );
//设置文字颜色 if ( $this->useRandColor != 1 )
{
//定义系统颜色数组
$this->colors[] = imagecolorallocate( $this->image, 128, 212, 255 );//80D4FF
$this->colors[] = imagecolorallocate( $this->image, 255, 240, 128 );//FFF080
$this->colors[] = imagecolorallocate( $this->image, 255, 177, 140 );//FFB18C
$this->colors[] = imagecolorallocate( $this->image, 196, 158, 255 );//C49EFF
$this->colors[] = imagecolorallocate( $this->image, 179, 238, 160 );//B3EEA0
$this->colors[] = imagecolorallocate( $this->image, 127, 152, 235 );//7F98EB
$this->colors[] = imagecolorallocate( $this->image, 233, 111, 197 );//E96FC5
$this->colors[] = imagecolorallocate( $this->image, 91, 193, 2 );//5BC116
$this->colors[] = imagecolorallocate( $this->image, 147, 44, 219 );//932CDB
$this->colors[] = imagecolorallocate( $this->image, 186, 255, 1 );//BAFF01
$this->colors[] = imagecolorallocate( $this->image, 177, 177, 177 );//B1B1B1
$this->colors[] = imagecolorallocate( $this->image, 228, 228, 228 );//E4E4E4
}
} function drawPic()
//选择相应图表的画法
{
Switch( $this->type )
{
case 1:
$this->imageColumnS();
//输出竖柱形图
break;
case 2:
$this->imageColumnH();
//输出横柱形图
break;
case 3:
$this->imageHinge();
//输出折线图
break;
} $this->drawXY();
//输出xy坐标轴
} function drawXY()
//输出xy坐标轴
{
$color = imageColorAllocate( $this->image, 255 - $this->R, 255 - $this->G, 255 - $this->B );
//定义坐标轴的颜色,与图像背景色相反,以突出效果 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轴 $rulerY = $this->Y - $this->border;
while( $rulerY > $this->border * 2 )
//画y轴上的刻度
{
$rulerY = $rulerY - $this->border;
imageline( $this->image, $this->border, $rulerY, $this->border-2, $rulerY, $color );
} $rulerX = $rulerX + $this->border;
while( $rulerX < ($this->X - $this->border * 2) )
//画y轴上的刻度
{
$rulerX = $rulerX + $this->border;
imageline( $this->image, $rulerX, $this->Y - $this->border, $rulerX, $this->Y - $this->border + 2, $color );
//画出标尺上的一个刻度
}
}
{
$this->items[] = $value;
//添加新元素值到数组
} function imageHinge()
//输出折线图
{
$num = Count( $this->items );
//获取元素的个数(数组大小) $maxItem = 0;
//初始化最大值 for( $i = 0; $i < $num; $i++ )
//取得元素的最大值
{
$maxItem = Max( $maxItem, $this->items[$i] );
} for( $i = 0; $i < $num; $i++ )
//画折线图
{
if ( $this->useRandColor == 1 )
//选择随机颜色或系统自带颜色
{
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 );
}
else
{
$color = $this->colors[$i];
//读取系统自带颜色
} $currentHeight = ( $this->Y - $this->border ) - ( $this->Y - $this->border * 2 ) * ( $this->items[$i] / $maxItem );
//柱形高度 if ( $i != "0" )
//第一个元素无需连上直线,因为无前一元素
{
imageLine( $this->image, $xPosition, $nextHeight, $xPosition + $this->border, $currentHeight, $color );
//连上前后元素所表示的点成一直线
} imageString( $this->image, $this->fontSize, $xPosition + $this->border, $currentHeight - $this->border / 2, $this->items[$i], $this->fontColor );
//输出元素的值 $nextHeight = $currentHeight;
//把当前元素值赋给下一元素 $xPosition = $xPosition + $this->border;
//用于间隔以便分开
}
} function imageColumnS()
//输出竖柱形图
{
$num = count( $this->items );
//获取元素的个数(数组大小) $maxItem = 0;
//初始化最大值 for( $i = 0; $i < $num; $i++ )
//取得元素的最大值
{
$maxItem = max( $maxItem, $this->items[$i] );
} $xPosition = $this->border * 2;
//下一元素的x起始坐标 for( $i = 0; $i < $num; $i++ )
//画竖柱形图
{
if ( $this->useRandColor == 1 )
//选择随机颜色或系统自带颜色
{
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 );
}
else
{
$color = $this->colors[$i];
//读取系统自带颜色
} $height = ( $this->Y-$this->border ) - ( $this->Y - $this->border * 2 ) * ( $this->items[$i] / $maxItem );
//竖柱形的高度 imageFilledRectangle( $this->image, $xPosition, $height, $xPosition + $this->border, $this->Y - $this->border, $color ); //画出矩型并填色 imageString( $this->image, $this->fontSize, $xPosition, $height - $this->border, $this->items[$i], $this->fontColor );
//输出元素的值 $xPosition = $xPosition + $this->border*2;
//用于间隔以便分开
}
} function imageColumnH()
//输出横柱形图
{
$num = count( $this->items );
//获取元素的个数(数组大小) $maxItem = 0;
//初始化最大值 for( $i = 0; $i < $num; $i++ )
//取得元素的最大值
{
$maxItem = max( $maxItem, $this->items[$i] );
} $yPosition = $this->Y - $this->border * 2;
//下一元素的y起始坐标 for( $i = 0; $i < $num; $i++ )
//画横柱形图
{
if ( $this->useRandColor == 1 )
//选择随机颜色或系统自带颜色
{
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 );
}
else
{
$color = $this->colors[$i];
//读取系统自带颜色
} $width = ( $this->X - $this->border * 2 ) * ( $this->items[$i] / $maxItem );
//横柱形的长度 imageFilledRectangle( $this->image, $this->border, $yPosition - $this->border, $width, $yPosition, $color );
//画出矩型并填色 imageString( $this->image, $this->fontSize, $width + 2, $yPosition - $this->border, $this->items[$i], $this->fontColor );
//输出元素的值 $yPosition = $yPosition - $this-> border * 2;
//用于间隔以便分开
}
} function showPic()
//显示图像
{
header('Content-type: image/png');
//通知浏览器显示图片的类型 imagepng( $this->image );
//把图像输出至浏览器 imagedestroy( $this->image );
//释放与$this->image关联的内存并结束
} function writeChinese( $str, $fontSize, $startX, $startY, $R, $G, $B )
//输出中英文字至底图
{
$str = iconv( "gb2312", "utf-8", $str );
//把$str中的gb2312规格转换为utf-8 $color = imagecolorallocate( $this->image, $R, $G, $B );
//为$image所代表的图像定义一个颜色 imagettftext( $this->image, $fontSize, 0, $startX, $startY, $color, $this->font, $str );
//以$fontSize大小从图片的坐上角($startX, $startY)坐标开始,将字符串$str画到$this->image所代表的图像上
}
}
?>
$data[] = $w->getTotalDay();
$data[] = $w->getYearCount();
$data[] = $w->getAverageCount();
$data[] = $w->getMonthCount();
$data[] = $w->getOnedayCount();
$data[] = $w->getYesterdayCount();while( $data[$i] )
{
$c->addItem( $data[$i] );
$i++;
}$c->drawPic();
$c->showPic();?>
很好用