8月 24, 2014

JpGraph - PHP上的繪圖函式庫

在 PHP 下的一套繪圖函式庫:JpGraph (An Object-Oriented Graph creating library)

字型參數:
  • 字型目錄定義:jpgraph.php
    預設使用函式庫路徑 /fonts 下的字型檔,備用是系統 /usr/share/fonts/truetype/
  • 字型名稱定義:jpgraph_ttf.inc.php
設定標題、X軸、Y軸字型大小:
$graph = new Graph(800,600);
$graph->title->SetFont(FF_ARIAL,FS_BOLD,24);
$graph->xaxis->SetFont(FF_ARIAL,FS_BOLD,14);
$graph->yaxis->SetFont(FF_ARIAL,FS_BOLD,14);
設定Y軸邊界(參考Graph類別手冊):
/* SetMargin($lef, $right, $top, $bottom) */
$graph->SetMargin(40,20,60,20);
配合讀入 csv 畫圖:
$dataArray = array();
$_gData = array();
$csvfile = fopen("diskusage.csv", "r");
while ( !feof($csvfile) )
{
  array_push( $dataArray, fgetcsv($csvfile) );
  $count++;
}
fclose( $csvfile );

for ($i=0; $i < $count-1; $i++)
{
  /* 取出橫列第5元素 存入一維陣列 */
  array_push( $_gData, round($dataArray[$i][4],2) );
}

/* setup the graph */  
$graph = new Graph(800,600);
$p1 = new LinePlot($_gData);
$graph->Add($p1);

/* Go */
$graph->Stroke();

沒有留言:

張貼留言