基於GD庫和JPgraph庫
說明:對於具體的Jpgraph類庫的用法,請參照官網的文檔:http://jpgraph.net/download/manuals/classref/index.html
對於下面涉及到的代碼,裡面的JPgraph類檔案可能路徑不一樣,我是按照我自己檔案存放的路徑引入的。你們可自行更改
1、使用直條圖統計月銷量額
<?php /* 具體的Jpgraph類庫的用法,請參照官網的文檔:http://jpgraph.net/download/manuals/classref/index.html */ /*****************************使用直條圖統計月銷量額**************************/ include("D:/PHP_Config/jpgraph-3.5.0b1/src/jpgraph.php"); include("D:/PHP_Config/jpgraph-3.5.0b1/src/jpgraph_bar.php"); //引用直條圖對象所在的檔案 $datay = array(160,180,203,289,405,488,489,408,299,166,187,105); //定義數組 $graph = new Graph(600,300,"auto"); //建立畫布 --要JPgraph庫的支援 $graph->SetScale("textlin"); $graph->yaxis->scale->SetGrace(20); $graph->SetShadow(); //建立畫布陰影 //設定統計圖所在畫布的位置,左邊距40、右邊距30、上邊距30、下邊距40,單位為像素 $graph->img->SetMargin(40,30,30,40); $bplot = new BarPlot($datay); //建立一個矩形的對象 $bplot->SetFillColor('orange'); //設定直條圖的顏色 $bplot->value->Show(); //設定顯示數字 $bplot->value->SetFormat('%d'); //在直條圖中顯示格式化的圖書銷量 $graph->Add($bplot); //將矩形圖添加到映像中 $graph->SetMarginColor("lightblue"); //設定畫布背景色為淺藍色 $graph->title->Set("<<PHP for Introduction 2009>> Annual sales statistics"); //建立標題 //設定X軸座標文字 $a=array("1 月","2 月","3 月","4 月","5 月","6 月","7 月","8 月","9 月","10 月","11 月","12 月"); $graph->xaxis->SetTickLabels($a); //設定X軸 $graph->title->SetFont(FF_SIMSUN); //設定標題字型 $graph->xaxis->SetFont(FF_SIMSUN); //設定X軸的字型 $graph->Stroke(); //輸出映像?>
View Code 2、使用折線圖統計月銷量額
<?php /***************************使用折線圖統計月銷量額*******************************/ include("D:/PHP_Config/jpgraph-3.5.0b1/src/jpgraph.php"); include("D:/PHP_Config/jpgraph-3.5.0b1/src/jpgraph_line.php"); //引用折線圖LinePlot類檔案 //定義數組 $datay = array(8320,9360,14956,17028,13060,15376,25428,16216,28548,18632,22724,28460); $graph = new Graph(600,300,"auto"); //設定統計圖所在畫布的位置,左邊距50,右邊距40,上邊距30,下邊距40,單位為像素 $graph->img->SetMargin(50,40,30,40); $graph->img->SetAntiAliasing(); //設定折線的平滑狀態 $graph->SetScale("textlin"); //設定刻度樣式 $graph->SetShadow(); //建立畫布陰影 $graph->title->Set("<<PHP for Introduction 2009>> Annual sales statistics"); //設定標題 $graph->title->SetFont(FF_SIMSUN,FS_BOLD); //設定標題字型 $graph->SetMarginColor("lightblue"); //設定畫布背景色為淡藍色 $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD); //設定Y軸標題的字型 $graph->xaxis->SetPos("min"); // $graph->yaxis->HideZeroLabel(); $graph->ygrid->SetFill(true,'#EFEFEF@0.5','#BBCCFF@0.5'); //X軸 $a=array("1 月","2 月","3 月","4 月","5 月","6 月","7 月","8 月","9 月","10 月","11 月","12 月"); $graph->xaxis->SetTickLabels($a); //設定X軸 $graph->xaxis->SetFont(FF_SIMSUN); //設定X坐軸的字型 $graph->yscale->SetGrace(20); $p1 = new LinePlot($datay); //建立折線圖對象 $p1->mark->SetType(MARK_FILLEDCIRCLE); //設定資料座標點為圓形標記 $p1->mark->SetFillColor("red"); //設定填充顏色 $p1->mark->SetWidth(4); //設定圓形標記的直徑為4像素 $p1->SetColor("blue"); //設定折線顏色為藍色 $p1->SetCenter(); //在X軸的各座標點中心位置繪製折線 $graph->Add($p1); //在統計圖上繪製折線 $graph->Stroke(); //輸出映像?>
View Code 3、使用餅形圖(扇形圖)統計月銷量額
<?php /*************************使用3D餅圖進行統計***************************/ include("D:/PHP_Config/jpgraph-3.5.0b1/src/jpgraph.php"); include("D:/PHP_Config/jpgraph-3.5.0b1/src/jpgraph_pie.php"); //引用 3D 餅形圖 PiePlot3D對象所在的類檔案 include("D:/PHP_Config/jpgraph-3.5.0b1/src/jpgraph_pie3d.php"); //定義數組 $data = array(266036,295621,335851,254256,254254,685425); $graph = new PieGraph(540,260,'auto'); //建立畫布 $graph->SetShadow(); //設定畫布陰影 //建立標題 $graph->title->Set("<<PHP for Introduction 2009>> Annual sales statistics -3DPie"); $graph->title->SetFont(FF_SIMSUN,FS_BOLD); //設定標題字型 $graph->legend->SetFont(FF_SIMSUN,FS_NORMAL); //設定圖例字型 $p1 = new PiePlot3D($data); //建立3D餅圖對象 $p1->SetLegends(array("IT","Appliance","Daily","Clothing","Health","Food")); $targ = array("D:/PHP_Config/jpgraph-3.5.0b1/src/Examples/pie3d_csimex1.php?v=1","D:/PHP_Config/jpgraph-3.5.0b1/src/Examples/pie3d_csimex1.php?v=2","D:/PHP_Config/jpgraph-3.5.0b1/src/Examples/pie3d_csimex1.php?v=3","D:/PHP_Config/jpgraph-3.5.0b1/src/Examples/pie3d_csimex1.php?v=4","D:/PHP_Config/jpgraph-3.5.0b1/src/Examples/pie3d_csimex1.php?v=5","D:/PHP_Config/jpgraph-3.5.0b1/src/Examples/pie3d_csimex1.php?v=6"); $alts = array("val=%d","val=%d","val=%d","val=%d","val=%d","val=%d"); $p1->SetCSIMTargets($targ,$alts); $p1->SetCenter(0.4,0.5); //設定餅圖所在畫布的位置 $graph->Add($p1); //將3D餅形圖添加到映像中 $graph->StrokeCSIM(); //將映像輸出到遊覽器?>
View Code