错误:没有在谷歌图表数组 [英] Error: Not an array in Google charts
问题描述
我必须设法输出从数据库中通过输出以下字符串的结果:
I have manage to output the results from the database by outputting the following string:
[['Grupo de edad','Mujeres','Hombres'],['Menos de 40',2,0],['De 40 a 49',7,3],['De 50 a 59',8,5],['De 60 a 69',20,25],['De 70 y más',6,10]]
如果我的正是这种文本粘贴到以下内容:
If i paste exactly this text into the following:
var data = google.visualization.arrayToDataTable([['Grupo de edad','Mujeres','Hombres'],['Menos de 40',2,0],['De 40 a 49',7,3],['De 50 a 59',8,5],['De 60 a 69',20,25],['De 70 y más',6,10]]);
我能正确可视化的图表。
I can visualize correctly the chart.
不过,如果我通过使用jQuery获得()函数来获取数据,然后我通过数据给谷歌类似下面的可视化功能:
However, if i get the data by using jQuery .get() function and then i pass the data to google visualization function like the following:
google.load("visualization", "1.1", {packages:["corechart","bar"]});
google.setOnLoadCallback(drawVisualization);
function drawVisualization() {
$.get("{{URL::action('ChartController@investigadoresEdadSexo')}}", function(datos, status){
var data = google.visualization.arrayToDataTable(datos);
var options = {
//many options ...
};
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(data, options);
});//End of jQuery .get
}
我得到的
不是数组误差
我知道我传递一个字符串,但为什么我若键入(复制和粘贴)google.visualization.arrayToDataTable()函数测试的字符串不工作?或如何让我的那个函数间preT该字符串作为一个数组?
I know that i am passing a string, but why doesn't it work if i have tested that string by typing (copying and pasting) google.visualization.arrayToDataTable() function? Or how do i make that function interpret that string as an array?
我要如何解决这个问题?
How do i fix this?
推荐答案
尝试转换 DATOS
来使用数组 JSON.parse
:
var data = google.visualization.arrayToDataTable(JSON.parse(datos));
更新
解析之前 DATOS
行情
应转化为:
Before parsing datos
quotes '
should be transformed to "
:
datos = datos.replace(/'/g, '"');
var data = google.visualization.arrayToDataTable(JSON.parse(datos));
这篇关于错误:没有在谷歌图表数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!