2013-05-13 129 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();`