我想从MySQL数据创建一个json格式的谷歌折线图。 JSON 数据采用以下格式:
{
"result": [
{
"Date": "Dec 17,2017",
"start_time": "22,08,17",
"selected_time": "[00,00,00]"
},
{
"Date": "Dec 18,2017",
"start_time": "21,48,57",
"selected_time": "[00,00,00]"
},
{
"Date": "Dec 21,2017",
"start_time": "13,23,05",
"selected_time": "[00,00,00]"
},
{
"Date": "Dec 22,2017",
"start_time": "00,13,33",
"selected_time": "[00,00,00]"
},
{
"Date": "Dec 23,2017",
"start_time": "14,48,34",
"selected_time": "[00,00,00]"
},
{
"Date": "Dec 27,2017",
"start_time": "19,21,34",
"selected_time": "[00,00,00]"
}
]
}
下面是我正在做的代码。但它不接受start_time和selected_time值,因为它给出了不匹配的错误。我有mysql db中所有这些字段的varchar数据类型。
function callbackFuncWithData(json){
var data = new google.visualization.DataTable();
data.addColumn('string', 'Date');
data.addColumn('timeofday', 'start_time');
data.addColumn('timeofday', 'selected_time');
$.each(json, function (key, value) {
$.each(value, function (key, value) {
var temp = [];
temp = value;
alert([temp['Date']+" "+temp['start_time']+" "+temp['selected_time']]);
data.addRows([
[ temp['Date'],temp['start_time'],temp['selected_time']]
]);
});
});
var options = {
tooltip: {
isHtml: true
},
title: 'Trendlines with multiple lines',
hAxis: {
title: 'Date',
},
vAxis: {
title: 'Time'
},
pointSize: 5,
series: {
0: { pointShape: 'circle' },
}
};
var chart = new google.visualization.LineChart(document.getElementById('multipleTrendChart'));
chart.draw(data, options);
}
}).fail(function (jq, text, err) {
console.log(text + ' - ' + err);
});
}
</script>
</head>
<body>
<div id="multipleTrendChart"></div>
</body>
</html>
但它显示此错误“错误:类型不匹配。值22,08,17与列索引1中的类型timeofday不匹配”。 有人请建议我是什么问题。感谢
答案 0 :(得分:0)
如果你改变了json的格式,它应该如下所示,
没有围绕'timeofday'
{
"Date": "Dec 17,2017",
"start_time": [22,08,17],
"selected_time": [00,00,00]
},
否则,您可以使用以下内容转换为数组...
$.each(json, function (key, value) {
data.addRow([
value.Date,
value.start_time.split(','),
JSON.parse(value.selected_time)
]);
});