PHP使用JPGRAPH制作圓柱圖的方法詳解
本文實例講述了PHP使用JPGRAPH制作圓柱圖的方法。分享給大家供大家參考,具體如下:
制作圓柱圖像的要點
首先,要使用jpgraph庫,我們先要去官網(wǎng)進行下載,網(wǎng)址:https://jpgraph.net/。 下載完畢后將他解壓到
這個文件夾需要自己手動添加,然后在相同的路徑下創(chuàng)建一個文件 命名為jpgraph.php
| 函數(shù)名 | 作用 |
|---|---|
| new Graph | 創(chuàng)建一個新的Graph對象 |
| jpgraph_bar.php | 加載畫出圓柱的文件 |
| jpgraph.php | 加載使用jp庫的文件 |
| SetScale | 設(shè)置刻度樣式 |
| new BarPlot | 創(chuàng)建一個新的BarPlot對象 |
| SetFillColor | 用于指定條形的填充顏色 |
| SetFont | 設(shè)置字體 |
| xaxis -> Set | 設(shè)置x軸標(biāo)題 |
| yaxis -> Set | 設(shè)置y軸標(biāo)題 |
| title -> Set | 設(shè)置主標(biāo)題 |
| Stroke | 輸出圖像 |
| SetColor | 設(shè)置標(biāo)題顏色 |
| SetMargin | 設(shè)置間距 |
| SetTickLabels | 獲取數(shù)組里的元素并輸出 |
| value -> Show | 顯示值 |
| graph_theme | 設(shè)置主題 |
這是我們本次需要用到的一些函數(shù),鑒于我的表達(dá)能力不是特別好,你們覺得有點看不懂的話,可以去看一下別的博客來幫助自己理解
現(xiàn)在開始編寫我們的代碼
首先,我們要輸出的是圓柱,那么我們則要輸入
require_once ("jpgraph/src/jpgraph.php");
require_once ("jpgraph/src/jpgraph_bar.php");
ok,這個時候文件已經(jīng)加載了,為了避免你們目錄和我不一致導(dǎo)致報錯無法實現(xiàn),我把我的文件存在位置截了下來,如下:

接下來,我們要創(chuàng)建兩個數(shù)組,一個是圓柱數(shù)據(jù),另一個是x軸標(biāo)題數(shù)據(jù)
$date = array(19,23,34,38,45,67,71,78,85,87,90,96);//此處是圓柱數(shù)據(jù)
$xdate = array("1","2","3","4","5","6","7","8","9","10","11","12");//此處是x軸的標(biāo)題數(shù)據(jù)
這個時候,我們已經(jīng)完成了我們繪制圖形所需要的數(shù)據(jù)了,接下來就是創(chuàng)建圓柱和調(diào)整它的顏色了
所要做的代碼如下:
$graph = new Graph (500,400);//創(chuàng)建一個新的Graph對象,其寬和高分別為500,300
$graph -> SetScale("textlin");//設(shè)置其刻印樣式
$graph -> SetShadow();//設(shè)置其陰影樣式
$graph -> img -> SetMargin(40,30,40,50);//設(shè)置其上間距40,右間距30,下間距40,左間距50
$graph -> graph_theme = null;//設(shè)置他的主題為空,使得下面的元素可實現(xiàn)
$bplot = new BarPlot ($date);//創(chuàng)建BarPlot對象
$bplot -> SetColor("pink");//設(shè)置BarPlot的顏色
$bplot -> value -> Show("");//顯示他的值
$graph ->Add($bplot);//把他的值放入$graph里
$graph -> title -> Set(iconv("utf-8","gb2312//IGNORE","年度收支表"));//設(shè)置標(biāo)題名字并進行轉(zhuǎn)換
$graph -> xaxis -> title -> Set(iconv("utf-8","gb2312//IGNORE","月份"));//同上,設(shè)置x軸標(biāo)題
$graph -> yaxis -> title -> Set(iconv("utf-8","gb2312//IGNORE","總金額(兆美元)"));//同上,設(shè)置y軸標(biāo)題
$graph -> title -> SetColor("red");//設(shè)置標(biāo)題顏色
$graph -> title -> SetMargin(10);//設(shè)置標(biāo)題間距
$graph -> xaxis -> title -> SetMargin(1);//設(shè)置x軸標(biāo)題間距
$graph -> xaxis ->SetTickLabels($xdate);//接收xdate數(shù)組里的元素
$graph -> title -> SetFont(FF_SIMSUN,FS_BOLD);//設(shè)置字體樣式
$graph -> xaxis -> title ->SetFont(FF_SIMSUN,FS_BOLD);
$graph -> yaxis -> title ->SetFont(FF_SIMSUN,FS_BOLD);
$graph -> xaxis -> SetFont(FF_SIMSUN,FS_BOLD);//設(shè)置x軸里所有的字體樣式
$graph -> Stroke();//輸出
到這里,我們的圓柱就已經(jīng)完成了,完整的代碼如下:
<?php
require_once ("jpgraph/src/jpgraph.php");
require_once ("jpgraph/src/jpgraph_bar.php");
$date = array(19,23,34,38,45,67,71,78,85,87,90,96);
$xdate = array("1","2","3","4","5","6","7","8","9","10","11","12");
$graph = new Graph (500,400);
$graph->SetScale("textlin");
$graph->SetShadow();
$graph->img->SetMargin(40,30,40,50);
$graph->graph_theme = null;
$barplot = new BarPlot($date);
$barplot->SetFillColor("pink");
$barplot->value->Show();
$graph->Add($barplot);
$graph->title->Set(iconv("utf-8","GB2312//IGNORE","年度收支表"));
$graph->xaxis->title->Set(iconv("utf-8","GB2312//IGNORE","月份"));
$graph->yaxis->title->Set(iconv("utf-8","GB2312//IGNORE","總金額(兆美元)"));
$graph->title->SetColor("red");
$graph->title->SetMargin(10);
$graph->xaxis->title->SetMargin(1);
$graph->xaxis->SetTickLabels($xdate);
$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();
?>
最終效果如下圖:

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php不使用copy()函數(shù)復(fù)制文件的方法
這篇文章主要介紹了php不使用copy()函數(shù)復(fù)制文件的方法,涉及php讀寫文件的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03
SQL server不支持utf8 php卻用utf8的矛盾問題解決方法
這篇文章主要介紹了SQL server不支持utf8 php卻用utf8的矛盾問題解決方法,需要的朋友可以參考下2020-03-03
PHP字符串與數(shù)組處理函數(shù)用法小結(jié)
這篇文章主要介紹了PHP字符串與數(shù)組處理函數(shù)用法,結(jié)合實例形式詳細(xì)分析了PHP字符串與數(shù)組常用處理函數(shù)功能、定義、使用方法與操作注意事項,需要的朋友可以參考下2020-01-01

