PHP绘图入门以及JPGraph库的使用

php绘图的座标系统(X轴向右,Y轴向下)


1.      php绘图的基本原理和步骤

l  创建画布

l  绘制需要的各种图形(圆,直线,矩形,弧线,扇形...)

l  输出图像到网页,也可以另存

l  销毁该图片(释放内存)

  

☞ 目前网站开发常见的图片格式有gif  jpg/jpeg  png bmp ....

l  gif 图片压缩率高,但是只能显示256色,可能造成颜色丢失,可以显示动画

l  jpg/jpeg 图片的压缩率高(有损压缩),可以用较小的文件来显示,网页上用的比较图

l  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);
      
       //有兴趣的同学可以将其封装成一个函数.
 
 
?>

 介绍一个取色器,FastStone截图软件附带的一个功能,截取我们要的颜色,可以看到其RGB值


u  思考

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

->jpgrapf

 

u  jpgraph的介绍

 

jpgraph的安装和配置

1.      下载 官网

2.      解压 (先拷贝到htdocs目录)

3.      配置 完毕使用( 就是把 emample 目录的其它文件 剪切到 emamlpe 文件夹内,注意要新建一个文件夹名字一定是  jpgraph )

4.      测试


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

 

完成案例示意图如下:

 

数据库和数据

--参加选举人的表

create tableelector(

electorId int,

namevarchar(64),

voteNums int,

voteMonth int);

 

insert intoelector values(1,'布什',10,1);

insert intoelector values(1,'布什',12,2);

insert intoelector values(1,'布什',34,3);

 

 

insert intoelector values(2,'奥巴马',34,1);

insert intoelector values(2,'奥巴马',30,2);

insert intoelector values(2,'奥巴马',12,3);

insert intoelector values(2,'奥巴马',30,4);

 

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

 如果您的图片无法正常显示,有可能是你在  PHP文件最开始放入了一个空行

代码:

 

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

实时取数据(动态更新)

 

vote.php

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


showAll.php

 

<html>
<head>
<title>显示</title>
<metahttp-equiv="content-type"content="text/html;charset=utf-8"/>
</head>
<body>
<h1>显示网民支持情况</h1>
<imgsrc="showVote.php?id=1" />
<imgsrc="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 thegraph.
$graph = newGraph(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("setnames 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 thefirst bar
$bplot = newBarPlot($datay1);
$bplot->SetFillGradient('AntiqueWhite2','AntiqueWhite4:0.8',GRAD_VERT);
$bplot->SetColor('darkred');
 
// Create thesecond bar
$bplot2 = new BarPlot($datay2);
$bplot2->SetFillGradient('olivedrab1','olivedrab4',GRAD_VERT);
$bplot2->SetColor('darkgreen');
 
// And join themin an accumulated bar
$accbplot = newAccBarPlot(array($bplot,$bplot2));
$graph->Add($accbplot);
 
$graph->Stroke();
?>


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章