2014-09-23 78 views
0

我第一次使用JPgraph,並試圖將mysql查詢結果導入基於bargradsmallex4.php示例的代碼的數組中。我嘗試了各種語法,但不太熟悉mysql_fetch_array(),並且無法將數據插入到數組中。任何幫助,將不勝感激。查詢已經過測試並生成結果,因此它不是查詢本身。將mysql結果導入Jpgraph

$sql= mysql_query('SELECT agency,current,sum(current) 
FROM table2 WHERE MATCH(agency) AGAINST("health" IN BOOLEAN MODE) GROUP BY agency '); 


// We need some data 
$datay = array(); 

while($row = mysql_fetch_array($sql)) 

$datay[] = array(
'current' => $row['sum(current)'], 

); 


// Setup the graph. 
$graph = new Graph(400,300);  
$graph->SetScale("textlin"); 
$graph->img->SetMargin(25,15,25,25); 

$graph->title->Set('"GRAD_VER"'); 
$graph->title->SetColor('darkred'); 

// Setup font for axis 
$graph->xaxis->SetFont(FF_FONT1); 
$graph->yaxis->SetFont(FF_FONT1); 

// Create the bar pot 
$bplot = new BarPlot($datay); 
$bplot->SetWidth(0.6); 

// Setup color for gradient fill style 
$bplot->SetFillGradient("navy","lightsteelblue",GRAD_VER); 

// Set color for the frame of each bar 
$bplot->SetColor("navy"); 
$graph->Add($bplot); 

// Finally send the graph to the browser 
$graph->Stroke(); 

回答

0

我在你的代碼中做了一些修改。

1-設置愛麗絲總和(電流)

2-設置while循環和$ DATAY陣列。

請嘗試一下,希望它會給你需要的輸出。

$sql= mysql_query('SELECT agency,current,sum(current) as sum_of_current FROM table2 WHERE MATCH(agency) AGAINST("health" IN BOOLEAN MODE) GROUP BY agency'); 


// We need some data 
$datay = array(); 

while($row = mysql_fetch_array($sql)) { 
    $datay[] = $row['sum_of_current']; 
} 


// Setup the graph. 
$graph = new Graph(400,300);  
$graph->SetScale("textlin"); 
$graph->img->SetMargin(25,15,25,25); 

$graph->title->Set('"GRAD_VER"'); 
$graph->title->SetColor('darkred'); 

// Setup font for axis 
$graph->xaxis->SetFont(FF_FONT1); 
$graph->yaxis->SetFont(FF_FONT1); 

// Create the bar pot 
$bplot = new BarPlot($datay); 
$bplot->SetWidth(0.6); 

// Setup color for gradient fill style 
$bplot->SetFillGradient("navy","lightsteelblue",GRAD_VER); 

// Set color for the frame of each bar 
$bplot->SetColor("navy"); 
$graph->Add($bplot); 

// Finally send the graph to the browser 
$graph->Stroke(); 
+0

我沒有得到錯誤,但結果是一個破碎的圖像鏈接。我不確定這是因爲上面的代碼是錯誤的還是因爲並非所有的路徑都是正確的。例如,我必須將src文件夾添加到require_once的現有路徑中才能在上述文件中工作。 – user3470715 2014-09-23 07:45:40

+0

我的例子工作,所以我想這表明路徑不是問題。 – user3470715 2014-09-23 08:08:51

+0

@ user3470715如果這顯示圖像已損壞,則圖形或其路徑等必定存在問題。在此處添加鏈接,可以獲得一些幫助。 http://stackoverflow.com/questions/6989701/jpgraph-wont-change-colors-on-my-barplot – JazyK 2014-09-23 09:00:51