轴 #0 的数据列不能是字符串类型 Error with Javascript
Data column(s) for axis #0 cannot be of type string Error with Javascript
当我尝试使用 google 图表库创建自己的图表时,它给了我 Data column(s) for axis #0 cannot be of type string error.
我正在通过 Ajax 库的 http 请求接收数据。我创建了这样的数组;
0: (2) ["Time", "Value"]
1: (2) ["Tue Nov 05 2019 00:00:00 GMT+0300 (GMT+03:00)", 49.4638863]
2: (2) ["Tue Nov 05 2019 01:00:00 GMT+0300 (GMT+03:00)", 73.19763]
3: (2) ["Tue Nov 05 2019 02:00:00 GMT+0300 (GMT+03:00)", 91.66373]
当我检查示例和文档时,我的数组类型看起来是正确的。
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
var endpoint = "http://127.0.0.1:8000/api/api"
var chartData = [["Time", "Value"]];
console.log(typeof chartData)
$.ajax({
method: "GET",
url: endpoint,
success: function(data){
var valueData = data;
for(i = 0; i< data.length; i++){
//console.log("Time Data: ", data[i].time)
chartData.push([String(new Date(data[i].time)), Number(data[i].value)]);
}
//console.log("Chart Data: ", chartData);
},
error: function(error_data){
console.log("error")
console.log(error_data)
}
});
function drawChart() {
console.log(chartData)
var data = google.visualization.arrayToDataTable([
chartData
]);
var options = {
title: 'Tag Value',
curveType: 'function',
legend: { position: 'bottom' }
};
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
chart.draw(data, options);
}
图表数据是;
0: (2) ["Time", "Value"]
1: (2) ["Tue Nov 05 2019 00:00:00 GMT+0300 (GMT+03:00)", 49.4638863]
2: (2) ["Tue Nov 05 2019 01:00:00 GMT+0300 (GMT+03:00)", 73.19763]
3: (2) ["Tue Nov 05 2019 02:00:00 GMT+0300 (GMT+03:00)", 91.66373]
4: (2) ["Tue Nov 05 2019 03:00:00 GMT+0300 (GMT+03:00)", 98.4258347]
我应该怎么做才能将数组推送到 google 图表而不出现此错误。
需要删除多余的括号,这里...
var data = google.visualization.arrayToDataTable(chartData);
arrayToDataTable
获取您在此处创建的数组数组...
var chartData = [["Time", "Value"]];
在此处添加附加...
chartData.push([String(new Date(data[i].time)), Number(data[i].value)]);
目前,您最终得到...
[[["Time", "Value"],[...],[...]]]
注意:鉴于您的数据格式,无需在此处转换类型...
chartData.push([new Date(data[i].time), data[i].value]);
当我尝试使用 google 图表库创建自己的图表时,它给了我 Data column(s) for axis #0 cannot be of type string error.
我正在通过 Ajax 库的 http 请求接收数据。我创建了这样的数组;
0: (2) ["Time", "Value"]
1: (2) ["Tue Nov 05 2019 00:00:00 GMT+0300 (GMT+03:00)", 49.4638863]
2: (2) ["Tue Nov 05 2019 01:00:00 GMT+0300 (GMT+03:00)", 73.19763]
3: (2) ["Tue Nov 05 2019 02:00:00 GMT+0300 (GMT+03:00)", 91.66373]
当我检查示例和文档时,我的数组类型看起来是正确的。
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
var endpoint = "http://127.0.0.1:8000/api/api"
var chartData = [["Time", "Value"]];
console.log(typeof chartData)
$.ajax({
method: "GET",
url: endpoint,
success: function(data){
var valueData = data;
for(i = 0; i< data.length; i++){
//console.log("Time Data: ", data[i].time)
chartData.push([String(new Date(data[i].time)), Number(data[i].value)]);
}
//console.log("Chart Data: ", chartData);
},
error: function(error_data){
console.log("error")
console.log(error_data)
}
});
function drawChart() {
console.log(chartData)
var data = google.visualization.arrayToDataTable([
chartData
]);
var options = {
title: 'Tag Value',
curveType: 'function',
legend: { position: 'bottom' }
};
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
chart.draw(data, options);
}
图表数据是;
0: (2) ["Time", "Value"]
1: (2) ["Tue Nov 05 2019 00:00:00 GMT+0300 (GMT+03:00)", 49.4638863]
2: (2) ["Tue Nov 05 2019 01:00:00 GMT+0300 (GMT+03:00)", 73.19763]
3: (2) ["Tue Nov 05 2019 02:00:00 GMT+0300 (GMT+03:00)", 91.66373]
4: (2) ["Tue Nov 05 2019 03:00:00 GMT+0300 (GMT+03:00)", 98.4258347]
我应该怎么做才能将数组推送到 google 图表而不出现此错误。
需要删除多余的括号,这里...
var data = google.visualization.arrayToDataTable(chartData);
arrayToDataTable
获取您在此处创建的数组数组...
var chartData = [["Time", "Value"]];
在此处添加附加...
chartData.push([String(new Date(data[i].time)), Number(data[i].value)]);
目前,您最终得到...
[[["Time", "Value"],[...],[...]]]
注意:鉴于您的数据格式,无需在此处转换类型...
chartData.push([new Date(data[i].time), data[i].value]);