我正在嘗試創建一個JSON對象來存放與FLOT圖表配合使用的數據。該文件說,數據的格式應該是點,像這樣的數組:修改與FLOT圖一起使用的JSON數據格式
[ [1, 3], [2, 14.01], [3.5, 3.14] ]
,我遇到的問題是,我的AJAX調用返回一個JSON對象,具有這樣的數據格式:
Rate: [96, 74, 73, 96, 124, 104]
Year: [2008, 2009, 2010, 2011, 2012, 2013]
有人可以解釋我應該如何修改此代碼,以便它創建FLOT圖表所需的正確數據格式?謝謝。
function ajaxmuniChart1c(){
$muni = $this->input->POST('muni');
$q = $this->db->query("SELECT SaleYear AS Y, NewSaleType AS T, count(*) AS C, format((sum(SalePrice)/sum(Quantity1)),0) AS R FROM tblsales WHERE tblsales.SaleYear > 2007 AND tblsales.Quantity1 > 2000 AND (tblsales.NewSaleType = 'Industrial') AND tblsales.Municipality = '".$muni."' GROUP BY T,Y ORDER BY T,Y");
$json_SaleYear = array();
$json_AvrgRate = array();
foreach ($q->result_array() as $row){
$json_SaleYear[] = $row['Y'];
$json_AvrgRate[] = $row['R'];
}
$json['Year'] = $json_SaleYear;
$json['Rate'] = $json_AvrgRate;
echo json_encode($json);
exit;
}
@DanielAttard更新我的回答是一個小更精緻= ) – MackieeE
感謝您的幫助。非常感激。奇蹟般有效。 – DanielAttard
另一個快速問題給你。我怎樣才能修改你的代碼,以便最終得到數據的標籤? – DanielAttard