文档章节

jpgraph 实例文档

o
 osc_2ch77h9m
发布于 07/08 09:27
字数 1903
阅读 9
收藏 0
php

「深度学习福利」大神带你进阶工程师,立即查看>>>

下载
在官方网站http://www.aditus.nu/jpgraph/ 下载jpgraph,其中1.X系列是用于PHP4的,2.X系列是用于PHP5的。

安装
将下载的得到的jpgraph压缩文件解压至相应的路径。



 

配置
首先需要注意的是:要想适用jpgraph,你的PHP必须开启了GD2扩展。
jpgraph.php中有以下这样一段代码是设置字体文件路径的
if (!defined('TTF_DIR')) {


   if (strstr( PHP_OS, 'WIN') ) {

       $sroot = getenv('SystemRoot');

       if( empty($sroot) ) {

           $t = new ErrMsgText();

           $msg = $t->Get(12,$file,$lineno);

           die($msg);

       }

       else {

         define('TTF_DIR', $sroot.'/fonts/');

       }

   } else {

       define('TTF_DIR','/usr/share/fonts/truetype/');ç (我的作法是将windows下的fonts文件夹下的字体全部COPY/usr/local/fonts/truetype)

   }

}



要支持中文需要用到simhei.ttfsimsun.ttc这两个字体,在使用中文的时候需要使用SetFont(FF_SIMSUN,FS_BOLD)设置字体。
 
如果你的文件编码为utf-8,修改方法如下:
代码:
方法一,在程序中修改
$title="流量图";
$title = iconv("UTF-8", "gb2312", $title);
$graph->title->Set($title);
方法二,修改源文件jpgraph_ttf.inc.php
在第99-106行,改成下面这样子
    elseif( $aFF === FF_SIMSUN ) {
        // Do Chinese conversion
        /*
        if( $this->g2312 == null ) {
        include_once 'jpgraph_gb2312.php' ;
        $this->g2312 = new GB2312toUTF8();
        }
        return $this->g2312->gb2utf8($aTxt);
        */
        return $aTxt;
    }

jpgraph默认显示汉字时是把汉字编码认为gb2312,转化为utf-8以后再显示。
这样的话,如果你的文件编码是gb2312,SetFont方法的第一个参数为FF_SIMSUN即可。
如果你是utf-8编码你还需要先把汉字编码转化为gb2312,这样你的汉字才可以正常显示。

使用
可以参照jpgraph-2.3.4\src\Examples中的例子。下面是一些常用的:
$graph->title->Set(‘设置图表的标题’);
$graph->xaxis->title->Set("设置X轴的标题");
$graph->yaxis->title->Set("设置Y轴的标题");

//设置字体 如果是中文,第一个参数一般设置为FF_SIMSUN
SetFont(FF_SIMSUN,FS_BOLD,14);
//如设置图表标题的字体
$graph->title->SetFont(FF_SIMSUN,FS_BOLD,14);

//设置颜色
SetColor('red');





































 

Example:

例1. php Jpgraph绘制简单的X-Y坐标图

<?php

include ("../jpgraph.php");

include ("../jpgraph_line.php");

 

//将要用于图表创建的数据存放在数组中

$data = array(19,23,34,38,45,67,71,78,85,90,96,145);

 

$graph = new Graph(500,300);                                                    //创建新的Graph对象

$graph->SetScale("textlin");                                                   //设置刻度样式

$graph->img->SetMargin(30,30,80,30);                    //设置图表边界

$graph->title->Set("CDN Traffic Total");        //设置图表标题

$graph->title->SetColor("blue");

$graph->title->SetMargin(20);

 

// Create the linear plot

$lineplot=new LinePlot($data);              // 创建新的LinePlot对象

$lineplot->SetLegend("Line(Mbits)");   //设置图例文字

$lineplot->SetColor("red");                 // 设置曲线的颜色

 

// Add the plot to the graph

$graph->Add($lineplot);                     //在统计图上绘制曲线

 

// Display the graph

$graph->Stroke();                         //输出图像

?>

 

例2. php Jpgraph绘制复杂的X-Y坐标图

<?php

include ("../jpgraph.php");

include ("../jpgraph_line.php");

 

$data1 = array(523,634,371,278,685,587,490,256,398,545,367,577);                //第一条曲线的数组

$data2 = array(19,23,34,38,45,67,71,78,85,87,90,96);                                    //第二条曲线的数组

 

$graph = new Graph(500,300,auto);                                                                              //创建新的Graph对象

$graph->SetScale("textlin");

$graph->SetShadow();                                                                                    //设置图像的阴影样式

 

$graph->img->SetMargin(60,30,30,70);                                                            //设置图像边距

$graph->title->Set("CDN流量图");                                               //设置图像标题

$graph->title->SetMargin(10);

 

$lineplot1=new LinePlot($data1);                                                                       //创建设置两条曲线对象

$lineplot2=new LinePlot($data2);

 

$lineplot1->value->Show();

$lineplot1->value->SetColor("black");

 

$graph->Add($lineplot1);                                                                                       //将曲线放置到图像上

$graph->Add($lineplot2);

 

$graph->xaxis->title->Set("月份");                                                                     //设置坐标轴名称

$graph->yaxis->title->Set(" (Gbits)");

$graph->xaxis->title->SetMargin(10);

$graph->yaxis->title->SetMargin(10);

 

$graph->title->SetFont(FF_SIMSUN,FS_BOLD);                                                     //设置字体

$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);

$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);

 

$graph->xaxis->SetTickLabels($gDateLocale->GetShortMonth());

 

$lineplot1->SetColor("red");                                                                           //设置颜色

$lineplot2->SetColor("blue");

 

$lineplot1->SetLegend("Max");                                                           //设置图例名称

$lineplot2->SetLegend("Min");

 

$graph->legend->SetLayout(LEGEND_HOR);                                                 //设置图例样式和位置

$graph->legend->Pos(0.5,0.96,"center","bottom");

 

$graph->Stroke();                                                                                              //输出图像

?>



 

例3. php Jpgraph绘制柱形图

<?php

include ("../jpgraph.php");

include ("../jpgraph_bar.php");

 

$data = array(19,23,34,38,45,67,71,78,85,87,96,145);         //定义数组

$ydata = array("","","","","","","","","","","十一","十二");

$graph = new Graph(500,300);                               //创建新的Graph对象

$graph->SetScale("textlin");

 

$graph->SetShadow();                                       //设置阴影

$graph->img->SetMargin(40,30,40,50);                        //设置边距

$barplot = new BarPlot($data);                                                          //创建BarPlot对象

$barplot->SetFillColor('blue');                                                         //设置颜色

$barplot->value->Show();                                                                                       //设置显示数字

$graph->Add($barplot);                                                                                      //将柱形图添加到图像中

 

$graph->title->Set("CDN流量图");                                                                       //设置标题和X-Y轴标题

$graph->title->SetColor("red");

$graph->title->SetMargin(10);

$graph->xaxis->title->Set("月份");

$graph->xaxis->title->SetMargin(5);

$graph->xaxis->SetTickLabels($ydata);

$graph->yaxis->title->Set(" (Mbits)");

 

$graph->title->SetFont(FF_SIMSUN,FS_BOLD);                                                      //设置字体

$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);

$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);

$graph->xaxis->SetFont(FF_SIMSUN,FS_BOLD);

$graph->Stroke();

?>



例4. php Jpgraph绘制饼图

<?php

include ("../jpgraph.php");

include ("../jpgraph_pie.php");

 

$data = array(19,23,34,38,45,67,71,78,85,87,90,96);

$lable_data = range(1,12);

 

$graph = new PieGraph(400,300);

$graph->SetShadow();

 

$graph->title->Set("CDN流量比例");

$graph->title->SetFont(FF_SIMSUN,FS_BOLD);

 

$pieplot = new PiePlot($data);

$pieplot->SetLegends($lable_data);

$pieplot->SetCenter(0.4);

$graph->Add($pieplot);

$graph->Stroke();

?>

 

例5. php Jpgraph绘制3D饼图

<?php

include ("../jpgraph.php");

include ("../jpgraph_pie.php");

include ("../jpgraph_pie3d.php");

 

$data = array(19,23,34,38,45,67,71,78,85,87,90,96);

 

$graph = new PieGraph(550,400);

$graph->SetShadow();

 

$graph->title->Set("CDN流量比例");

$graph->title->SetFont(FF_SIMSUN,FS_BOLD);

 

$pieplot = new PiePlot3D($data);                                                                       //创建PiePlot3D对象

$pieplot->SetCenter(0.4);                                                                                                      //

置饼图中心的位置

$pieplot->SetLegends($gDateLocale->GetShortMonth());            //设置图例

 

$graph->Add($pieplot);

$graph->Stroke();

?>

 

例6.

index.html

 

<html>

<head>

<title>CDN流量查询系统统计</title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<mce:style type="text/css"><!--

.style1 {

       font-size: 16px;

       font-weight: bold;

}

--></mce:style><style type="text/css" mce_bogus="1">.style1 {

       font-size: 16px;

       font-weight: bold;

}</style>

</head>

 

<body>

<form name="form1" method="get" action="result.php">

 <p align="center" class="style1"> CDN流量查询系统统计</p>

 <table width="300" border="1" align="center" cellpadding="3" cellspacing="3">

   <tr>

     <td width="85"><strong>查询年份</strong></td>

<td width="188"><select name="acct_yr" id="acct_yr">

  <option value="2009" selected>2008</option>

        <option value="2009" selected>2009</option>

     </select></td>

   </tr>

   <tr>

     <td><strong>起始月份</strong></td>

     <td><select name="start_mth" id="start_mth">

       <option selected>01</option>

       <option>02</option>

       <option>03</option>

       <option>04</option>

       <option>05</option>

       <option>06</option>

<option>07</option>

       <option>08</option>

       <option>09</option>

       <option>10</option>

       <option>11</option>

       <option>12</option>

 

     </select></td>

   </tr>

   <tr>

     <td><strong>终止月份</strong></td>

     <td><select name="end_mth" id="end_mth">

       <option >01</option>

       <option>02</option>

       <option>03</option>

       <option>04</option>

       <option>05</option>

       <option>06</option>

<option>07</option>

       <option>08</option>

       <option>09</option>

       <option>10</option>

       <option>11</option>

       <option selected >12</option>

       </select></td>

   </tr>

   <tr>

     <td><strong>统计图类别</strong></td>

     <td><select name="graph" id="graph">

       <option value="1" selected>线型图</option>

       <option value="2">柱形图</option>

       <option value="3">饼图</option>

       <option value="4">3D饼图</option>

     </select></td>

   </tr>

 </table>

 <p align="center">

   <input type="submit" value="Submit">

   <input type="reset" name="Submit2" value="Reset">

 </p>

</form>

</body>

</html>

 

 

result.php

 

<?php

include ("../jpgraph.php");

include ("../jpgraph_line.php");

include ("../jpgraph_bar.php");

include ("../jpgraph_pie.php");

include ("../jpgraph_pie3d.php");

 

$conn = mysql_connect("localhost", "root", "woxiangnileyali");         //连接数据库

 

$acct_yr = $_GET['acct_yr'];                            //获取年份

$start_mth = $_GET['start_mth'];                      //获取超始月份

$end_mth = $_GET['end_mth'];                         //获取结束月份

$choose = $_GET['graph'];                        //获取图形类型

 

mysql_select_db("test", $conn);                                //执行SQL, 获得销量值

$query_rs_prod = "SELECT acct_mth, amount FROM traffic WHERE acct_yr = '$acct_yr' and acct_mth between '$start_mth' and '$end_mth'";

$rs_prod = mysql_query($query_rs_prod, $conn) or die(mysql_error());

$row_rs_prod = mysql_fetch_assoc($rs_prod);

$totalRows_rs_prod = mysql_num_rows($rs_prod);

 

$data = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);                   //初始化数组

do                                                                                                                                                                            //循环设置各月份数值

{

       $i = (int)$row_rs_prod['acct_mth']-1;

       $data[$i] = $row_rs_prod['amount'];

} while($row_rs_prod = mysql_fetch_assoc($rs_prod));

 

 

switch($choose)

{

       case 1:

              $graph = new Graph(400,300);                                               //创建新的Graph对象

              $graph->SetScale("textlin");                                             //设置刻度样式

              $graph->img->SetMargin(30,30,80,30);                     //设置图表边界

              $graph->title->SetFont(FF_SIMSUN,FS_BOLD);                                                 //设置字体

              $graph->title->Set("CDN流量查询");  //设置图表标题

              $lineplot=new LinePlot($data);

              $lineplot->SetLegend("Line");

              $lineplot->SetColor("red");

              $graph->Add($lineplot);

              break;

       case 2:

              $graph = new Graph(400,300);    

              $graph->SetScale("textlin");         

              $graph->SetShadow();         

              $graph->img->SetMargin(40,30,20,40);

              $barplot = new BarPlot($data);                                                      //创建BarPlot对象

              $barplot->SetFillColor('blue');                                                        //设置颜色

              $barplot->value->Show();                                                                           //设置显示数字

              $graph->Add($barplot);                                                                              //将柱形图添加到图像中           

              $graph->title->Set("CDN流量查询");                                                                 //设置标题和X-Y轴标题

              $graph->xaxis->title->Set("月份");

              $graph->yaxis->title->Set(" (Gbits)");

              $graph->title->SetFont(FF_SIMSUN,FS_BOLD);                                                 //设置字体

              $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);

              $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);

              break;

       case 3:

              $graph = new PieGraph(400,300);

              $graph->SetShadow();

             

              $graph->title->Set("CDN流量查询");

              $graph->title->SetFont(FF_SIMSUN,FS_BOLD);

              $pieplot = new PiePlot($data);

              $pieplot->SetLegends($gDateLocale->GetShortMonth());          //设置图例

              $graph->Add($pieplot);

              break;

       case 4:

              $graph = new PieGraph(400,300);

              $graph->SetShadow();

             

              $graph->title->Set("CDN流量查询");

              $graph->title->SetFont(FF_SIMSUN,FS_BOLD);

              $pieplot = new PiePlot3D($data);                                                          //创建PiePlot3D对象

              $pieplot->SetCenter(0.4);                                                                                          //设置饼图中心的位置

              $pieplot->SetLegends($gDateLocale->GetShortMonth());          //设置图例

              $graph->Add($pieplot);

              break;

       default:

              echo "graph参数错误";

              exit;

}

$graph->Stroke();

?>

 

 











如果想了解更多,请关注我们的公众号
公众号ID:opdevos
扫码关注


gongzhouhao.jpg





o
粉丝 0
博文 80
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。
beego API开发以及自动化文档

beego API开发以及自动化文档 beego1.3版本已经在上个星期发布了,但是还是有很多人不了解如何来进行开发,也是在一步一步的测试中开发,期间QQ群里面很多人都问我如何开发,我的业余时间实在...

astaxie
2014/06/25
2.7W
22
文档重复检测软件--DuplicateDetector

重复检测软件(Python实现) 可以检测英文文档之间的重复,包括完全重复(除标点,格式外都相同),和相近重复(部分(90%)抄袭) TODO: 1. 数据抄袭(英文文档中数据集中的地方,比较多的数据相...

zoowii
2012/11/01
783
0
异步Tor控制协议的实现--txtorcon

txtorcon是一个基于Twisted的异步Tor控制协议的实现。Twisted 是一个用Python写的事件驱动的网络引擎,和洋葱路由网络Tor是一个旨在促进人们的隐私和安全的互联网。它包括单元测试有96% +覆盖...

匿名
2012/11/23
620
0
程序文档生成工具--Flatdoc

Flatdoc 是一个小型的 JavaScript 文件,可读取 Markdown 文件并渲染成页面,特别适合用来为开源项目生成文档。 特点: 无需服务端组件支持 无需构建过程 可通过 Github Pages 发布 可获取来...

匿名
2013/05/28
1K
0
jquery对象/标签映射扩展--NickName

jquery对象/标签映射扩展-NickName OTM是什么 以往把这样的一个json对象 var data = {}; data.UserId = "8888"; data.UserName = "赵六"; data.School="湖北工业大学"; data.schoolNo=100002......

知鸣
2013/06/13
1K
0

没有更多内容

加载失败,请刷新页面

加载更多

创建具有列表理解的字典 - Create a dictionary with list comprehension

问题: I like the Python list comprehension syntax. 我喜欢Python列表理解语法。 Can it be used to create dictionaries too? 它也可以用来创建字典吗? For example, by iterating over......

技术盛宴
40分钟前
15
0
从JS数组中删除重复的值[duplicate] - Remove duplicate values from JS array [duplicate]

问题: This question already has answers here : 这个问题已经在这里有了答案 : Get all unique values in a JavaScript array (remove duplicates) (79 answers) 获取JavaScript数组中的......

法国红酒甜
今天
7
0
如何使用AngularJS在浏览器的控制台中访问$ scope变量?

问题: I would like to access my $scope variable in Chrome's JavaScript console. 我想在Chrome的JavaScript控制台中访问$scope变量。 How do I do that? 我怎么做? I can neither see ......

fyin1314
今天
18
0
ImageMagick - 添加水印

背景 最近制作思维导图想添加自己的水印,网上很多例子都是使用ImageMagick来完成。但是不少代码在本地并不可行。经过一番试验,找到两个方法。 方法一 代码 stackoverflow方法改良: conver...

wffger
今天
11
0
OSChina 周四乱弹 —— 到底是怎样的饕餮盛宴在等待着我!

Osc乱弹歌单(2020)请戳(这里) 【今日歌曲】 小小编辑推荐 :《你 能 來 保 護 我 的 世 界 嘛》- 歪门 《你 能 來 保 護 我 的 世 界 嘛》- 歪门 手机党少年们想听歌,请使劲儿戳(这里)...

小小编辑
今天
77
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部