PHP Json 图表师数组数据

PHP Json Array Data with Chartist

我尝试从 json 数组创建图表线图。

 echo json_encode($json_data);

return

[{"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);

returns

{"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]}

阅读 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);

结果:

代码笔:Chartist JSON data format