2013-05-13 90 views
0

我希望能夠做一個查詢,從數據庫中獲取數字,並使用JpGraph將這些數據呈現在線圖上。來自mysql數據的JpGraph線圖

任何人都可以幫助我,或通過這個指導我嗎?

謝謝你們!

+0

請提供更多詳細信息。你已經有了什麼代碼,你試過了什麼? – Andrew 2013-05-13 12:25:34

回答

1

這是我的代碼。 db.php用於定義數據庫連接 `

require_once("db.php"); //include database connection 
require_once("constant.php"); //include database connection 
require_once ('jpgraph/src/jpgraph.php'); 
require_once ('jpgraph/src/jpgraph_line.php'); 
$default=true; 
$filter=""; 

if(isset($_GET["timelapse"])) $timelapse=$_GET["timelapse"]; 
else $timelapse=TODAY; 

$strQueryString="SELECT Date_Time, DissolvedOxygen as DissolvedOxygen FROM datareading"; 


//echo $strQueryString; 
$strQueryString.= " ORDER BY Date_Time ASC"; 
$query = mysql_query($strQueryString); 
$xdata=array(); 
$ydata=array(); 
while($row=mysql_fetch_array($query)){ 
    $date = date_create($row["Date_Time"]); 
    $xdata[]=date_format($date," h:i a"); 
    $ydata[]=$row["DissolvedOxygen"]; 

} 
//print_r($xdata); 
$graph = new Graph(920,500); // Initialize Graph 
$graph->SetScale("intlin"); 
$graph->SetMargin(80,30,40,50); 
$graph->SetMarginColor('white'); 
$graph->SetFrame(false,'blue',3); 
//Set Title Attributes 
$graph->title->Set("Dissolved Oxygen Level"); 
$graph->yaxis->SetTitleMargin(50); 
$graph->title->SetFont(FF_ARIAL,FS_BOLD,12); 

//Set Subtitle Attributes 
$graph->subtitle->Set("Last 24 Hours"); 
$graph->subtitle->SetFont(FF_ARIAL,FS_BOLD,10); 
$graph->subtitle->SetColor('black'); 
//$graph->xaxis->SetLabelAngle(90); 
$graph->xaxis->SetTickLabels($xdata); 
$graph->xaxis->SetTextLabelInterval(2); 

// Use Arial font 
$graph->xaxis->SetFont(FF_ARIAL,FS_BOLD,9); 
$graph->SetAxisLabelBackground(LABELBKG_XAXIS,'orange','red','lightblue','red'); 
$graph->yaxis->SetFont(FF_ARIAL,FS_BOLD,9); 
$graph->xgrid->Show(); 

// Create the plot line 
$p1 = new LinePlot($ydata); 
$p1->value->SetFont(FF_FONT1, FS_BOLD); 
$p1->value->SetAlign('center'); 

//Set y-axis title 
$graph->yaxis->title->Set("Dissolved Oxygen (mg/L or ppm)"); 
$graph->yaxis->SetLabelAlign('right','bottom'); 

$graph->Add($p1); 
//print_r($xdata); 
$graph->Stroke();`