JPGRAPH 生成統計圖真的很方便,
而且有很多樣式可以選.下邊有四種使用實例(統計圖,柱形圖,餅形圖,3D餅乾形圖)
直接進入主題:
1.下載jpgraph http://www.aditus.nu/jpgraph/
有php4.x 版和 php5.x 版 我下載的是php5.x版
2 .整理 :
jpgraph/src
下邊的幾個實例文件分別在 jpgraph/ 下
3. 配置:
因爲我要用到中文,所以要針對中文做些修改.
jpgraph/src/jpg-config.inc.php 配置文件
//字體文件,它會自己找windows 或 linux系統中找 fonts目錄 也可以自己指定位置 DEFINE('CHINESE_TTF_FONT','bkai00mp.ttf'); |
jpgraph/src/jpgraph.php
替換 $font_family=FF_FONT1 爲 $font_family=FF_SIMSUN
如果和我一樣使用utf-8編碼,那麼簡單的修改一下 jpgraph_gb2312.php 中的
function gb2utf8($gb)
{ // 新加一行 , 如果是使用gb2312編碼,這裏看名字就知道什麼意思了. return $gb // ......... } |
直接看例字:
jpgraph/line.php
/** * http://www.zhaipeng.cn * 2008-1-30 * JPGRAPH 生成X-Y線形統計圖 */ include(’src/jpgraph.php’); //Graph類 include(’src/jpgraph_line.php’); //LinePlot 類 $data = array(19 , 23 , 34 ,36, 50 , 60 , 65, 70 , 78); //模擬數據 $graph = new Graph($width = 400 , $height = 300); //創建新的Graph對象 $graph->SetScale(”textlin”); //設置刻度模式 $graph->img->SetMargin(30 , 30 , 80 , 30) ; //設置圖表邊界 $graph->title->Set(”簡體中文 繁體中文 test”) ; //設置圖表標題 //$graph->title->SetFont(FF_SIMSUN,FS_BOLD); // 設置中文字體 $lineplot = new LinePlot($data); //創建新的LinePlot對象 $lineplot->SetLegend(”數據1″);//設置圖例文字 $graph->subtitle->SetFont(FF_SIMSUN); $graph->subsubtitle->SetFont(FF_SIMSUN); $lineplot->SetColor(”red”); //設置曲線顏色 $graph->Add($lineplot); //在統計圖上繪製曲線 $data2 = array(20 ,30 ,45 , 23 , 45 , 69 , 60 , 79 , 80); $lineplot = new LinePlot($data2); //創建新的LinePlot對象 $lineplot->SetLegend(”數據2″);//設置圖例文字 $lineplot->SetColor(”blue”); //設置曲線顏色 $graph->Add($lineplot); //在統計圖上繪製曲線 $graph->Stroke() ; //輸出圖像 |
jpgraph/line.php
/** * http://www.zhaipeng.cn * 2008-1-30 * JPGRAPH 生成柱形圖 */ include('src/jpgraph.php'); include('src/jpgraph_bar.php'); $data = array(18 ,23, 26 , 27 , 48 , 25 , 49); //模擬數據 $graph = new Graph(400 , 300); $graph->SetScale("textlin"); //設置刻度模式 $graph->SetShadow(); //設置陰影 $graph->img->SetMargin(40 , 30 , 20 , 40) ;//設置邊距 $barplot = new BarPlot($data); $barplot->SetFillColor('blue') ; // 設置顏色 $barplot->value->Show(); //設置顯示數字 $graph->Add($barplot); //將柱形圖添加到圖像中 //設置標題和X-Y軸標題 $graph->title->Set('測試柱形圖'); $graph->xaxis->title->Set("月份"); $graph->yaxis->title->Set("總金額(元)"); /** * 設置字體,因爲修改過jpgraph.php 所以可以不使用 $graph->title->SetFont(FF_SIMSUN , FS_BOLD); $graph->yaxis->title->SetFont(FF_SIMSUN , FS_BOLD); $graph->xaxis->title->SetFont(FF_SIMSUN , FS_BOLD); */ $graph->Stroke(); |
JPGRAPH 生成餅形圖
/** * http://www.zhaipeng.cn * 2008-1-30 * JPGRAPH 生成餅形圖 */ include(’src/jpgraph.php’); include(’src/jpgraph_pie.php’); $data = array(18 ,23, 26 , 27 , 48 , 25 , 49); //模擬數據 $graph = new PieGraph(400 , 300); $graph->SetShadow();$graph->title->Set("餅形圖"); $pieplot = new PiePlot($data); $graph->Add($pieplot); $graph->Stroke(); |
JPGRAPH 生成3D餅圖
/**
* http://www.zhaipeng.cn
* 2008-1-30
* JPGRAPH 生成3D餅圖
*/
include(’src/jpgraph.php’);
include(’src/jpgraph_pie.php’);
include(’src/jpgraph_pie3d.php’);
$data =
array(18
,23,
26 ,
27 , 48
, 25
, 49 ,
50 ,
45 , 23
, 20
,30);
//模擬數據
$month =
array('一月','二月','三月','四月'
, '五月'
, '六月' ,
'七月' ,
'八月' , '九月','十月','十一月','十二月');
$graph =
new PieGraph(400
, 300);
$graph->SetShadow();
$graph->title->Set("3D餅圖");
$pieplot =
new PiePlot3D($data);
$pieplot->SetCenter(0.4)
; //設置餅圖的中心位置
$pieplot->SetLegends($month);
//設置圖例
$graph->Add($pieplot);
$graph->Stroke('3d.jpg');