Jpgraph类库GD2函数PHP图形处理

时间:2014-03-04  发布人:SHX  浏览次数:3035  评论:0

Jpgraph 是基于GD2函数库编写的主要用于创建统计图的类库。在绘制统计图方面不仅功能非常强大,而且代码编写方便,只需要简单的几行代码就可以绘制出非常复杂的统计图效果,从而很大程度的提高了编程人员的开发效率。

下面我们来看几个实例

(1)将Jpgraph类库导入到程序中

使用Jpgraph类库,可去官方下载(百度Jpgraph即可)

(2)将src文件夹复制到实例根目录下。

(3)创建index.php文件如下:

<?php
header ( "Content-type: text/html; charset=UTF-8" ); //设置文件编码格式
require_once '../src/jpgraph.php';     //导入Jpgraph类库
require_once '../src/jpgraph_bar.php';     //导入Jpgraph类库的柱状图功能
$data = array(80, 73, 89, 85, 92);     //设置统计数据
$datas = array("C#", "VB", "VC", "JAVA", "ASP.NET");     //设置统计数据
$graph = new Graph(600, 300);     //设置画布大小
$graph->SetScale('textlin');     //设置坐标刻度类型
$graph->SetShadow();    //设置画布阴影
$graph->img->SetMargin(40, 30, 20, 40);    //设置统计图边距
$barplot = new BarPlot($data);    //实例化BarPlat对象
$barplot->SetFillColor('blue');    //设置柱形图前景色
$barplot->value->Show();
$graph->Add($barplot);
$graph->title->Set(iconv("utf-8","gb2312","吉林省明日科技有限公司6月份编程词典销售量分析"));     //设置统计图标题
$graph->xaxis->title->Set(iconv("utf-8","gb2312","部门"));     //设置X轴名称
$graph->xaxis->SetTickLabels($datas);
$graph->yaxis->title->Set(iconv("utf-8","gb2312",'总数量(本)'));     //设置y轴名称
$graph->title->SetFont(FF_SIMSUN, FS_BOLD);     //设置标题字体
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);    //设置X轴字体
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);    //设置Y轴字体
$graph->Stroke();     //输出图像 
?>

在多给几个实例:

<?php
header ( "Content-type: text/html; charset=UTF-8" ); //设置文件编码格式
require_once '../src/jpgraph.php';     //导入Jpgraph类库
require_once '../src/jpgraph_bar.php';     //导入Jpgraph类库的柱状图功能
$data = array(80, 73, 89, 85, 92, 75 ,85 ,65, 84, 79, 85, 98);     //设置统计数据
$graph = new Graph(500, 270);     //设置画布大小
$graph->SetScale('textlin');     //设置坐标刻度类型
$graph->SetShadow();    //设置画布阴影
$graph->img->SetMargin(40, 30, 20, 40);    //设置统计图边距
$barplot = new BarPlot($data);    //实例化BarPlat对象
$barplot->SetFillColor('blue');    //设置柱形图前景色
$barplot->value->Show();  //输出数据
$graph->Add($barplot);   //添加数据
$graph->title->Set(iconv("utf-8","gb2312",'公司2009年度收支'));     //设置统计图标题
$graph->xaxis->title->Set(iconv("utf-8","gb2312",'月份'));     //设置X轴名称
$graph->yaxis->title->Set(iconv("utf-8","gb2312",'总金额(万元)'));     //设置y轴名称
$graph->title->SetFont(FF_SIMSUN, FS_BOLD);     //设置标题字体
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);    //设置X轴字体
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);    //设置Y轴字体
$graph->Stroke();     //输出图像
?>

最后,注意你可能会出现几个问题

1.在图形输出前不能有任何输出;2.中文乱码是因为没有找到相应的中文字体; 3.输出的是一张图片,可以直接用<img>调用

<img src='index.php' width=300 height=200 />

 

返回

上一篇:网页制作小工具合集下一篇:街机三国战纪之四剑传奇

我要评论

    loading
 

关闭

loading

回顶部