php 报表开发

 什么是报表
报表( 报: 报告 表: 表格 图表)
所谓报表就是,以表格或者是图表的方式 显示报告数据.

演示了jpgrapf的一个案例

 php绘图的坐标系统

 php绘图技术
1. php绘图的基本原理和步骤
 创建画布
 绘制需要的各种图形(圆,直线,矩形,弧线,扇形...)
 输出图像到网页,也可以另存
 销毁该图片(释放内存)


☞ 目前网站开发常见的图片格式有 gif jpg/jpeg png bmp ....
总结:
 gif 图片压缩率高,但是只能显示256色,可能造成颜色丢失,可以显示动画
 jpg/jpeg 图片的压缩率高(有损压缩),可以用较小的文件来显示,网页上用的比较图
 png , 该格式综合了gif 和jpg的优势,缺点是不能显示动画
怎么选择:

 php绘图技术快速入门
前提 : 首先请大家确认你的 gd库启用 php.ini
;启用图像库
extension=php_gd2.dll
记住,需要重启启动apache


image1.php案例:
<?php
$im=imagecreatetruecolor(400,300);
$red=imagecolorallocate($im,255,0,0);
//圆
//imageellipse($im,20,20,20,20,$red);
//直线
//imageline($im,0,0,400,300,$red);
//矩形
//imagerectangle($im,2,2,40,50,$red);
//填充矩形
//imagefilledrectangle($im,2,2,40,50,$red);
//弧线
//imagearc($im,100,100,50,50,180,270,$red);
//扇形
//imagefilledarc($im,100,100,80,50,180,270,$red,IMG_ARC_PIE);

//拷贝图片到画布
//1.加载源图片
//$srcImage=imagecreatefromgif("2.GIF");
//这里我们可以使用一个getimagesize()
//$srcImageInfo=getimagesize("2.GIF");

//拷贝源图片到目标画布
//imagecopy($im,$srcImage,0,0,0,0,$srcImageInfo[0],$srcImageInfo[1]);

//写字
$str="hello,world,中文";
//imagestring($im,5,0,0,"hello,world,中文",$red);
//在字体库中去找中文
imagettftext($im,20,10,50,50,$red,"simhei.ttf",$str);
header("content-type: image/png");
imagepng($im);
imagedestory($im);
?>

综合案例:(综合使用)


代码:
<?php


//分析思路(先画出扇形)


//1.画布
$im=imagecreatetruecolor(400,300);

//默认是黑色背景(一会告诉大家怎么修改)
$white=imagecolorallocate($im,255,255,255);
imagefill($im,0,0,$white);

//2.画出扇形
//创建三个颜色
$red=imagecolorallocate($im,254,0,0);
$darkred=imagecolorallocate($im,144,0,0);
$blue=imagecolorallocate($im,0,0,128);
$darkblue=imagecolorallocate($im,0,0,80);
$gary=imagecolorallocate($im,192,192,192);
$darkgary=imagecolorallocate($im,144,144,144);

for($i=60;$i>=50;$i--){
imagefilledarc($im,100,$i,100,50,0,35,$darkblue,IMG_ARC_PIE);
imagefilledarc($im,100,$i,100,50,35,75,$darkgary,IMG_ARC_PIE);
imagefilledarc($im,100,$i,100,50,75,360,$darkred,IMG_ARC_PIE);
}

//在上面加盖
imagefilledarc($im,100,50,100,50,0,35,$blue,IMG_ARC_PIE);
imagefilledarc($im,100,50,100,50,35,75,$gary,IMG_ARC_PIE);
imagefilledarc($im,100,50,100,50,75,360,$red,IMG_ARC_PIE);

//输出图片
header("content-type: image/png");
imagepng($im);
imagedestory($im);

//有兴趣的同学可以将其封装成一个函数.


?>

 思考
如果每次都必须自己去画,这样的图,是不是很麻烦,有没有现成可以用于做图表开发的库
->jpgrapf

 jpgraph的介绍

jpgraph的安装和配置
1. 下载 官网
2. 解压 (先拷贝到htdocs目录)
3. 配置 完毕使用( 就是把 emample 目录的其它文件 剪切到 emamlpe 文件夹内,注意要新建一个文件夹名字一定是 jpgraph )
4. 测试


 jpgraph 的实际使用案例(网名调查统计图

完成案例示意图如下:


数据库和数据
--参加选举人的表
create table elector(
electorId int,
name varchar(64),
voteNums int,
voteMonth int);

insert into elector values(1,'布什',10,1);
insert into elector values(1,'布什',12,2);
insert into elector values(1,'布什',34,3);


insert into elector values(2,'奥巴马',34,1);
insert into elector values(2,'奥巴马',30,2);
insert into elector values(2,'奥巴马',12,3);
insert into elector values(2,'奥巴马',30,4);

☞如果希望吧jpgraph 的图表嵌入到其它的php文件中,可以使用<img />

代码:

静态显示数据(没有实时的取数据)


实时取数据(动态更新)

vote.php
<html>
<head>
<title>请投票</title>
<script language="javascript">
function look(){
window.location.href="showAll.php";
}
</script>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<form action="" method="">
<table>
<tr><td>请投票</td></tr>
<tr><td>
<input type="radio" name="vote" value="1">布什
<input type="radio" name="vote" value="2">奥巴马
<input type="submit" value="投票"/>
</td></tr>
</table>
</form>
<form action="" method="">
<tr>
<td><input type="button" onclick="look();" value="查看投票统计图表"/></td>
</tr>
</form>
</html>

showAll.php

<html>
<head>
<title>显示</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<h1>显示网民支持情况</h1>
<img src="showVote.php?id=1" />
<img src="showvote.php?id=2" />
</body>
</html>


showVote.php (最核心的)
<?php // content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');

//$datay1=array(13,8,119,7,17,6);
//$datay2=array(0,0,0,0,0,0);

//$datay1=array(13,8,11);
//$datay2=array(0,0,0);
// Create the graph.
$graph = new Graph(350,250);
$graph->SetScale('textlin');
$graph->SetMarginColor('silver');


// Setup title
/*$str="";
$id=$_REQUEST['id'];
if($id==1){
$str="支持布什的统计情况(万)";
}
else if($id==2){
$str="支持奥巴马的统计情况(万)";
}*/

//从数据库

$id=$_REQUEST["id"];
//组织sql
$sql="select * from elector where electorId=$id order by voteMonth";


$conn=mysql_connect("localhost","root","root") or die("连接失败".mysql_error());
mysql_select_db("test",$conn) or die(mysql_error());
mysql_query("set names gbk") or die(mysql_error());
$res=mysql_query($sql,$conn) or die(mysql_error());

$datay1=array();
$datay2=array();
$i=0;
$title="";
while($row=mysql_fetch_array($res))
{
$datay1[$i]=$row[2];
$datay2[$i]=0;

if($i==0){
$title="支持".$row[1]."情况统计图";
}
$i++;
}
mysql_free_result($res);
mysql_close($conn);


$graph->title->Set($title);
$graph->title->setFont(FF_SIMSUN,FS_BOLD,14);
// Create the first bar
$bplot = new BarPlot($datay1);
$bplot->SetFillGradient('AntiqueWhite2','AntiqueWhite4:0.8',GRAD_VERT);
$bplot->SetColor('darkred');

// Create the second bar
$bplot2 = new BarPlot($datay2);
$bplot2->SetFillGradient('olivedrab1','olivedrab4',GRAD_VERT);
$bplot2->SetColor('darkgreen');

// And join them in an accumulated bar
$accbplot = new AccBarPlot(array($bplot,$bplot2));
$graph->Add($accbplot);

$graph->Stroke();
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值