2016-02-11 90 views
1

我正在使用GoogleCharts API顯示與我的性別重新分區的餅,但我有一個小問題,我的餡餅是不完整的,如下圖所示。 enter image description here谷歌圖表餡餅不完整

我這個JSON陣列產生的呢:

{"cols":[{"id":"","label":"Topping","pattern":"","type":"string"},{"id":"","label":"Slices","pattern":"","type":"number"}],"rows":[{"c":[{"v":"Homme","f":null},{"v":"2","f":null}]},{"c":[{"v":"Femme","f":null},{"v":"1","f":null}]}]} 

這是我的Javascript代碼

function drawSexChart() { 
     var jsonData = $.ajax({ 
      url: "http://localhost/studentlink/web/js/ajax.php?sexChart", 
      dataType: "json", 
      async: false 
      }).responseText; 
     var data = new google.visualization.DataTable(jsonData); 
     console.log(jsonData); 
     var chart = new google.visualization.PieChart(document.getElementById('chartUsersBySex')); 
     chart.draw(data, null); 
    } 

我沒有任何想法,爲什麼我的餡餅是uncomplete。

如果有人能幫助我?

預先感謝您

托馬斯

回答

0

最後我只是覺得爲什麼我的餡餅是uncomplete,我張貼我的解決方案,以幫助其有同樣的問題在未來的人。

實際上,爲了生成我的JSON字符串,我在一個PHP對象中使用了json_encode PHP函數,該函數包含有關圖表的所有信息。

但實際上,我使用這個函數是這樣的:

return json_encode($this); 

結果

{"cols":[{"id":"","label":"Topping","pattern":"","type":"string"},{"id":"","label":"Slices","pattern":"","type":"number"}],"rows":[{"c":[{"v":"Homme","f":null},{"v":"2","f":null}]},{"c":[{"v":"Femme","f":null},{"v":"1","f":null}]}]} 

在這個結果我們可以看出,數字代表我的價值觀是"之間。

但現在,如果我生成我JSON字符串是這樣的:

return json_encode($this,JSON_NUMERIC_CHECK); 

結果

{"cols":[{"id":"","label":"Topping","pattern":"","type":"string"},{"id":"","label":"Slices","pattern":"","type":"number"}],"rows":[{"c":[{"v":"Homme","f":null},{"v":3,"f":null}]},{"c":[{"v":"Femme","f":null},{"v":1,"f":null}]}]} 

"在我diseapeared號人物,我有我全派!

0

這是因爲你的值是字符串而不是整數。

See here

所以你需要做的是要麼

1)改變是從服務器返回的數據,以便該行的數據反映了這一

"rows":[{"c":[{"v":"Homme","f":null},{"v":2,"f":null}]},{"c":[{"v":"Femme","f":null},{"v":1,"f":null}] 

2)改變數據在與圖表應用程序一起使用之前。例如:

var rowData = jsonData.rows; 
for (var i = 0, l = rowData.length; i < l; i++) { 
    rowData[i].c[1].v = +rowData[i].c[1].v; 
} 
0

取消引號(「」)的值。

{"cols":[{"id":"","label":"Topping","pattern":"","type":"string"},{"id":"","label":"Slices","pattern":"","type":"number"}],"rows":[{"c":[{"v":"Homme","f":null},{"v":2,"f":null}]},{"c":[{"v":"Femme","f":null},{"v":1,"f":null}]}]}