陈力:传智播客古代 珍宝币 泡泡龙游戏开发第49讲:PHP报表开发(JpGraph)

陈力:传智播客古代 珍宝币 泡泡龙游戏开发第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 , 比较复杂的图表

三、网民支持情况统计图(静态数据)

软件设计,PHP绘图技术,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();
 
?>


四、网民支持情况统计图(实时数据)

软件设计,PHP教程

添加数据库:

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值