陈力:传智播客古代 珍宝币 泡泡龙游戏开发第49讲:PHP报表开发(JpGraph)
描述:在php绘图技术的编程基础上,本文结合贵阳网站建设中的报表开发的实践经验对JpGraph、网民支持情况统计图等进行介绍。陈力:传智播客古代 珍宝币 泡泡龙游戏开发第49讲:PHP报表开发(JpGraph)
一、JpGraph介绍
以前用PHP作图时必须要掌握复杂抽象的画图函数,或者借助一些网上下载的花柱形图、饼形图的类来实现。没有一个统一的chart类来实现图表的快速开发。http://jpgraph.net/
现在我们有了一个新的选择:JpGraph。专门提供图表的类库。它使得作图和开发报表变成了一件非常简单的事情,你只需从数据库中取出相关数据,定义标题,图表类型,然后的事情就交给JpGraph,只需掌握为数不多的JpGraph内置函数(可以参照JpGraph附带例子学习),就可以画出非常炫目的图表。
二、JpGraph安装配置
1. 下载 http://jpgraph.net/
2. 确保你的PHP版本最低为4.04(最好是4.1.1),并且支持GD库。必须确保GD库可以正常运行,可以通过运行phpinfo()来查看GD库的信息是否存在的方法来判断。同时要有要求GD库的版本应为2.0,而不是1.0
3. 配置(见演示)
4. 多参考JpGraph 自带的文档和案例
accbarframeex01.php 柱状统计图
pie3d_csimex1.php , 统计各个地区比例
comb90dategraphex01.php 风向,风速
basic_contourex01.php 轮廓图,gis利用
gantthourminex1.php , 比较复杂的图表
*******showAllElector.php*****
<html>
<head>
<title>支持人数报表图</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<h1>支持人数情况统计图</h1>
<table>
<tr><td><img src="showVote.php?id=1"/></td><td><img src="showVote.php?id=2"/></td></tr>
</table>
</html>
******showStaticVote.php***********************
<?php // content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');
$datay1=array(13,8,19,7,17,6);
$datay2=array(0,0,0,0,0,0);
// Create the graph.
$graph = new Graph(350,250);
$graph->SetScale('textlin');
$graph->SetMarginColor('silver');
// Setup title
$id=$_REQUEST["id"];
if($id==1){
$title="支持布什人数(万)";
}else if($id==2){
$title="支持奥巴马人数(万)";
}
$graph->title->Set($title);
$graph->title->SetFont(30,FS_BOLD);
// 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();
?>
添加数据库:
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);
程序设计源代码:
****vote.php***
<html>
<head>
<title>请投票</title>
<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="submit" value="查看投票统计图表"/></td>
</tr>
</form>
</html>
***voteProcess.php***[可以最后写]
**************showAllElector.php*************************
<html>
<head>
<title>支持人数报表图</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<h1>支持人数情况统计图</h1>
<table>
<tr><td><img src="showVote.php?id=1"/></td><td><img src="showVote.php?id=2"/></td></tr>
</table>
</html>
************showVote.php******************
<?php // content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');
//从数据库
$id=$_REQUEST["id"];
$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("失败2");
mysql_query("set names gbk") or die("失败3");
$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);
// Create the graph.
$graph = new Graph(350,250);
$graph->SetScale('textlin');
$graph->SetMarginColor('silver');
$graph->title->Set($title);
$graph->title->SetFont(FF_SIMSUN,FS_BOLD);
// 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();
?>
在默认情况下,JpGraph 不支持中文,怎样才能让JpGraph 支持中文呢?
【推荐阅读】陈力:传智播客古代 珍宝币 泡泡龙游戏开发第49讲:PHP报表开发(JpGraph)