我一直在努力弄清楚如何使Google Line Chart动态。错误出现在控制台上:
未捕获的错误:不是数组
function pensionDawChart( DataPoints ){
console.log( [$.trim(DataPoints)] );
var data = google.visualization.arrayToDataTable( $.trim(DataPoints) );
var options = {
title: 'Pension',
curveType: 'function',
legend: { position: 'bottom' }
};
var chart = new google.visualization.LineChart(document.getElementById('pension-calculator-chartContainer'));
chart.draw(data, options);
}
//Plot the chart
function pension_chart(){
var ageAtRetirement = 29;
var GeneralDataPoints, inflatedClosing, DataPoints;
years = $('#retirement-inputs #years-to-retirement').val();
DataPoints = [];
for (year = 1; year <= years; year++){
inflatedClosing = parseInt($('#lookup-table-preserved #row-' + year + ' .inflated-closing').text());
GaInflatedClosing = parseInt($('#lookup-table-preserved #row-' + year + ' .ga-inflated-closing').text());
DataPoints.push([year, inflatedClosing, GaInflatedClosing] + ',');
}
google.charts.setOnLoadCallback( pensionDawChart(DataPoints) );
}
控制台日志
这是对DataPoints的转储
["1,10994,5248,,2,13988,6498,,3,16983,7747,,4,19977,…,,27,88848,37911,,28,91842,39174,,29,94837,40437,"]0: "1,10994,5248,,2,13988,6498,,3,16983,7747,,4,19977,8996,,5,22971,10247,,6,25966,11499,,7,28961,12752,,8,31956,14005,,9,34950,15258,,10,37944,16512,,11,40939,17767,,12,43933,19022,,13,46928,20277,,14,49922,21534,,15,52916,22791,,16,55910,24048,,17,58905,25306,,18,61899,26564....
最佳答案
为了获得预期的结果,请使用DataPoints [0] .split将数据拆分为字符串
DataPoints[0].split(",")