標籤:bit 預設 legend ini 部落格 一段 圖表 流量 表示
Jpgraph是PHP圖表類庫,可以產生折線圖、柱狀圖、大餅圖等等統計圖。如果你想使用PHP產生統計圖來統計資料,使用它再方便不過啦。
如果說你要親自使用GD庫來寫的話,那我只能膜拜大神啦(我不會哈哈哈)。
言歸正傳,首先你要確定你的PHP已經開啟GD庫啦,在php.ini設定檔中找到extension=php_gd2.dll確認沒有被;注釋。那表示已經開啟GD庫拉。
然後就可以去網上下一個Jpgraph的壓縮包啦。解壓儲存到一個任意檔案夾,之後會引用其中的檔案,所以最好儲存到一個你熟悉的檔案夾。
1)產生折線圖
<?php require_once ("jpgraph/src/jpgraph.php"); require_once ("jpgraph/src/jpgraph_line.php"); $data1 = array(523,634,371,278,685,587,490,256,398,545,367,577); //第一條曲線的數組 $graph = new Graph(500,300); $graph->SetScale("textlin"); $graph->SetShadow(); $graph->img->SetMargin(60,30,30,70); //設定映像邊距 $graph->graph_theme = null; //設定主題為null,否則value->Show(); 無效 $lineplot1=new LinePlot($data1); //建立設定兩條曲線對象 $lineplot1->value->SetColor("red"); $lineplot1->value->Show(); $graph->Add($lineplot1); //將曲線放置到映像上 $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->SetMargin(10); $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()); $graph->Stroke(); //輸出映像
效果
2)柱狀圖
<?php require_once ("jpgraph/jpgraph.php"); require_once ("jpgraph/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); //設定邊距 $graph->graph_theme = null; //設定主題為null,否則value->Show(); 無效 $barplot = new BarPlot($data); //建立BarPlot對象 $barplot->SetFillColor(‘blue‘); //設定顏色 $barplot->value->Show(); //設定顯示數字 $graph->Add($barplot); //將直條圖添加到映像中 $graph->title->Set("CDN流量圖"); $graph->xaxis->title->Set("月份"); //設定標題和X-Y軸標題 $graph->yaxis->title->Set("流 量(Mbits)"); $graph->title->SetColor("red"); $graph->title->SetMargin(10); $graph->xaxis->title->SetMargin(5); $graph->xaxis->SetTickLabels($ydata); $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();
效果
3)大餅圖
require_once ("jpgraph/src/jpgraph.php"); require_once ("jpgraph/src/jpgraph_pie.php"); require_once ("jpgraph/src/jpgraph_pie3d.php"); $data = array(19,23,34,38,45,67,71,78,85,87,90,96); $graph = new PieGraph(550,500); $graph->SetShadow(); $graph->title->Set("CDN流量比例"); $graph->title->SetFont(FF_SIMSUN,FS_BOLD); $pieplot = new PiePlot3D($data); //建立PiePlot3D對象 $pieplot->SetCenter(0.4, 0.5); //設定餅圖中心的位置 $pieplot->SetLegends($gDateLocale->GetShortMonth()); //設定圖例 $graph->Add($pieplot); $graph->Stroke();
效果
不知道各位看官發現沒有,貌似柱狀圖和大餅圖的文字不是正常的中文。折線圖倒是正確的,這是因為Jpgraph預設把字串轉換為UTF8格式的,如果你的檔案php檔案本身就是UTF8格式那麼久相當於轉換了2次,所以產生了亂碼。
這裡就要用到轉換編碼方式 iconv("UTF-8","gb2312",$x);需要先把漢字編碼轉化為gb2312,這樣漢字才能正常顯示。參考第一段代碼寫法就行。
下面附上更詳細的介紹部落格地址(感謝技術分享):http://blog.csdn.net/aoshilang2249/article/details/46956163
&& http://w3note.com/web/181.html && http://blog.csdn.net/liupengcheng201/article/details/44593713
PHP中使用Jpgraph產生統計圖