软讯网络 > 网站建设 > PHP > php生成html统计图
【标 题】:php生成html统计图
【关键字】:
php,html
【来 源】:http://www.cublog.cn/u/16928/showart.php?id=207846
php生成html统计图
<?php
/****************************************************************************
| @Author: etng
| @Email: etng2004[at]gmail.com etng2004[at]hotmail.com
| @QQ: 35605690
| @WangWang: etng
| @Homepage: [url]http://www.etng.net/[/url]
@FileName : draw.bar.php
@Description : 生成统计图
@CreateTime : 2004-12-10
@LastModify : 2004-12-10
@Version : 1.0beta
@Copyright : etng.net
|
| 做一个快乐的CODER! PHP = Programme Help Perfect!
|
****************************************************************************/
header("Content-Type:text/html; charset=utf-8");
header("Content-Language:utf-8");
?>
<script language="JavaScript">
<!--
function toggleNext(o){
var obj=o.nextSibling;
if(obj.style.display=="none"){
obj.style.display="block";
}else{
obj.style.display="none";
}
}
//-->
</script>
<?php
$bar = new bar(200,15,5);
$pair = $arr=array(
'1月' => 250,
'2月' => 370,
'3月' => 621,
'4月' => 720,
'5月' => 326,
'6月' => 455,
'7月' => 132,
'8月' => 345,
'9月' => 611,
'10月' => 126,
'11月' => 551,
'12月' => 266
);
$bar->setData($pair);
echo "<h3>月平均销售额,单位:(万元)</h3>";
$bar->draw();
echo "<h3>月平均销售额,单位:(万元)</h3>";
$bar->draw(false);
echo "<h3 onclick=\"toggleNext(this);\">调试数据如下:</h3>";
echo "<pre style='display:none;'>";
var_dump($bar);
echo "</pre>";
?>
<?php
/*
@柱状统计图
*/
class bar
{
//颜色, 默认为20种, 如果客户数据超过20种, 则回到第一种, 如此循环.
var $color = array(
'#97bd00','#009900','#cc3300',
'#ffcc00','#3366cc','#33cc33',
'#ff9933','#cccc99','#99cc66',
'#66ff99','#4f6600','#003300',
'#481000','#7d6400','#173064',
'#1a6a1a','#974b00','#78793c',
'#557e27','#009337'
);
var $total = 399;
var $per = 25;
//柱与柱之间的间隔
var $space = 5;
var $max = 0;
/*
构造函数
*/
function bar($T=399 ,$P=25 ,$S=5)
{
$this->total = $T;//总高度或者宽度
$this->per = $P;//单元高度或者宽度
$this->space = $S;//各柱间隔
}
/*
给出具体数据
*/
function setData($pair)
{
$this->DATA = $pair;
$values = array_values($pair);
asort($values);
$this->max = array_pop($values);
}
function draw($H=true)
{
$H?$this->drawH():$this->drawV();
}
function drawH(){
$cntColor=count($this->color);
$i=0;
?>
<table border="0" cellpadding="0" cellspacing="<?php echo $this->space; ?>">
<?php
foreach($this->DATA as $k=>$v){
$curColor = $this->color[($i++)%$cntColor];
?>
<tr>
<td><p align="right"><?php echo /*打印左边标签*/ $k; ?></td>
<td>
<table border="0" cellpadding="0" cellspacing="0" height="<?php echo $this->per; ?>">
<tr>
<td width="<?echo floor(($this->total/$this->max)*$v);?>" bgstyle="color: <?php /*柱状颜色*/ echo $curColor; ?>"> </td>
<td> <?php /*在柱状图的顶端输出数值*/ echo $v; ?></td>
</tr>
</table>
</td>
</tr>
<?php
}
?>
</table>
<?php
}//end function
function drawV(){
$cntColor=count($this->color);
$i=0;
?>
<table border=0>
<tr valign="bottom"><?php /*一定要设置为低部对齐*/ ?>
<?php
foreach($this->DATA as $k=>$v){
$curColor = $this->color[($i++)%$cntColor];
?>
<td align="center">
<?php
/*在柱状图的顶端输出数值*/
echo $v;
?>
<table height="<?echo floor(($this->total/$this->max)*$v);?>" border=0>
<tr>
<td bgstyle="color: <?php /*柱状颜色*/ echo $curColor; ?>" width="<?php /*第条柱的宽度*/ echo $this->per; ?>"></td>
</tr>
</table><font style="color: #3F7F9F"><?php /*打印低部对应标签*/ echo $k;?></font>
</td>
<?php
}//end for
?>
</tr>
</table>
<?php
}//end function
}//end class
?>
;