使用谷歌可视化我得到Unix Epoch时间戳并尝试将其转换为字符串数组。我尝试将字符串用于谷歌图表,但收到错误
Type mismatch. Value 2017-8-25 16:23:54,2017-8-25 16:11:54,... does not match type string in column index 0
我的代码如下所示:
var jsonData = $.ajax({
url: "JSONurl",
type: "GET",
dataType: 'json',
}).done(function (dataJson) {
var mass = [];
for (var i = 0; i < dataJson.length; i++) {
//console.log(data[i].dateExecutes.toString());
var dateEx = new Date(dataJson[i].dateExecutes);
var year = dateEx.getFullYear();
var month = dateEx.getMonth()+1;
var day = dateEx.getDate();
var hours = dateEx.getHours();
var mins = dateEx.getMinutes();
var secs = dateEx.getSeconds();
var newDate = (year + '-' + month + '-' + day + ' ' + hours + ':' + mins + ':' + secs);
mass[i] = newDate;
}
//console.log(mass);
var data = new google.visualization.DataTable();
//var date1 = new Date(jsonData.dateExecutes);
//var date2 = date1.toGMTString();
//alert(jsonData.dateExecutes);
data.addColumn('string', mass);
data.addColumn('number', 'passed');
data.addColumn('number', 'failed');
//data.addColumn({type: 'number', role: 'tooltip', id: 'testRunId'});
dataJson.forEach(function (row) {
data.addRow([
mass,
row.passed,
row.failed
])
});
如何在Column中使用数组?如何从数组构建单元格? 请,任何帮助
答案 0 :(得分:0)
不清楚你要完成什么
如果您只想将格式化的日期字符串作为第一列,
建议使用谷歌的DateFormat
课程
不需要两个循环,日期可以直接在forEach
语句
请参阅以下摘录...
var jsonData = $.ajax({
url: "JSONurl",
type: "GET",
dataType: 'json',
}).done(function (dataJson) {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Date');
data.addColumn('number', 'passed');
data.addColumn('number', 'failed');
var formatDate = new google.visualization.DateFormat({
pattern: 'yyyy-MM-dd hh:mm:ss'
});
dataJson.forEach(function (row) {
var rowDate = new Date(row.dateExecutes);
data.addRow([
formatDate.formatValue(rowDate),
row.passed,
row.failed
]);
});
});