2016-10-27 78 views
0

即時嘗試從json數組創建一個chartist線圖形。PHP Json陣列數據與憲章

echo json_encode($json_data); 

回報

[{"labels": [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27], 
    "series":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,17,0,2,0,0]}] 

我的jQuery

var url = "/appx/array.php" 
    var resp = jQuery.parseJSON(
    jQuery.ajax({ 
     url: url, 
     async: false, 
     dataType: 'json' 
    }).responseText 
); 


var data = resp; // here i dont know how to do 

/*var data = labels: ['1', '2', '3','4'], series: [[1, 3, 7, 12]] original data*/ 

new Chartist.Line("#teamCompletedWidget .ct-chart", data,options); 

系列必須是數組的數組

series[[1,2,3]] 

我的PHP代碼

for($i=1; $i<=date("d"); $i++){ 

    $json_array['labels'][] = $i; 

    $json_array['series'][] = intval($clicks[$i]); 

} 
echo json_encode($json_data); 

回報

{"labels":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28], 
    "series":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,57,5,17,0,2,0,0,0]} 

回答

0

閱讀JSON響應,並將其重新組合成格式圖表專家期待:

//JSON returned: 

var resp ='{"labels": [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28],"series":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,57,5,17,0,2,0,0,0]}'; 

var JSONObject = JSON.parse(resp); 
console.log(JSONObject.labels); 
//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28] 
console.log(JSONObject.series); 
//[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 57, 5, 17, 0, 2, 0, 0, 0] 

// In your particular case, your response becomes the JSONObject 
// So you would use resp.labels and resp.series 

var data = {"labels":JSONObject.labels, "series":[JSONObject.series]}; 

// var data = {"labels":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27],"series":[[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,17,0,2,0,0]]}; 


new Chartist.Line('#chart1', data); 

結果:

enter image description here Codepen:Chartist JSON data format

+0

系列必須數組系列的數組:[[0,2,4,5]]; –

+0

對,從你的問題中我發現你已經覆蓋了,但並不是說整個數據應該在裏面和對象。 – Keno

+0

有我的問題,我是初學者,我不知道我怎麼能返回我的數據正確的格式。我會爲我的問題添加更多信息。 –