我有以下代码显示过去30天内文件的下载次数:
所有数据均来自" ajax / downloads.php"通过对我的数据库进行简单查询。
var pastDays = 30; //this value shows how many days back to show statistics
$.ajax({
type: "POST",
cache: false,
url: "ajax/downloads.php",
dataType: "json",
data: "days="+pastDays,
success : function(res) {
var obj = [];
for(var x = 0; x <= pastDays; x++){
var currObj = res[x];
var objCombo = currObj.split("|");
var objItem = "{date: '"+objCombo[0]+"', downloads: '"+objCombo[1]+"'}";
objItem = JSON.stringify(eval("(" + objItem + ")"));
obj.push(objItem);
}
console.log(obj);
}
这是我已经得到的ajax响应(res):
console.log(res);
["07Apr|1", "06Apr|3", "05Apr|12", "04Apr|11", "03Apr|0", "02Apr|0", "01Apr|6", "31Mar|0", "30Mar|7", "29Mar|16", "28Mar|5", "27Mar|5", "26Mar|12", "25Mar|9", "24Mar|4", "23Mar|10", "22Mar|16", "21Mar|2", "20Mar|19", "19Mar|22", "18Mar|10", "17Mar|11", "16Mar|10", "15Mar|19", "14Mar|0", "13Mar|4", "12Mar|14", "11Mar|5", "10Mar|26", "09Mar|7", "08Mar|5"]
推送后得到的控制台输出是:
console.log(obj);
["{"date":"07Apr","downloads":"1"}", "{"date":"06Apr","downloads":"3"}", "{"date":"05Apr","downloads":"12"}", "{"date":"04Apr","downloads":"11"}", "{"date":"03Apr","downloads":"0"}", "{"date":"02Apr","downloads":"0"}", "{"date":"01Apr","downloads":"6"}", "{"date":"31Mar","downloads":"0"}", "{"date":"30Mar","downloads":"7"}", "{"date":"29Mar","downloads":"16"}", "{"date":"28Mar","downloads":"5"}", "{"date":"27Mar","downloads":"5"}", "{"date":"26Mar","downloads":"12"}", "{"date":"25Mar","downloads":"9"}", "{"date":"24Mar","downloads":"4"}", "{"date":"23Mar","downloads":"10"}", "{"date":"22Mar","downloads":"16"}", "{"date":"21Mar","downloads":"2"}", "{"date":"20Mar","downloads":"19"}", "{"date":"19Mar","downloads":"22"}", "{"date":"18Mar","downloads":"10"}", "{"date":"17Mar","downloads":"11"}", "{"date":"16Mar","downloads":"10"}", "{"date":"15Mar","downloads":"19"}", "{"date":"14Mar","downloads":"0"}", "{"date":"13Mar","downloads":"4"}", "{"date":"12Mar","downloads":"14"}", "{"date":"11Mar","downloads":"5"}", "{"date":"10Mar","downloads":"26"}", "{"date":"09Mar","downloads":"7"}", "{"date":"08Mar","downloads":"5"}"]
然而,当我把它送到Morris.bar时,我得到了#34; undefined&#34;值。
如果我指定&#34; var obj&#34; Morris.js工作正常。在我的代码中手动并省略了ajax:
var obj = [ {"date":"07Apr","downloads":"1"}, {"date":"06Apr","downloads":"3"}, {"date":"05Apr","downloads":"12"}, {"date":"04Apr","downloads":"11"}, {"date":"03Apr","downloads":"0"}, {"date":"02Apr","downloads":"0"}, {"date":"01Apr","downloads":"6"}, {"date":"31Mar","downloads":"0"}, {"date":"30Mar","downloads":"7"}, {"date":"29Mar","downloads":"16"}, {"date":"28Mar","downloads":"5"}, {"date":"27Mar","downloads":"5"}, {"date":"26Mar","downloads":"12"}, {"date":"25Mar","downloads":"9"}, {"date":"24Mar","downloads":"4"}, {"date":"23Mar","downloads":"10"}, {"date":"22Mar","downloads":"16"}, {"date":"21Mar","downloads":"2"}, {"date":"20Mar","downloads":"19"}, {"date":"19Mar","downloads":"22"}, {"date":"18Mar","downloads":"10"}, {"date":"17Mar","downloads":"11"}, {"date":"16Mar","downloads":"10"}, {"date":"15Mar","downloads":"19"}, {"date":"14Mar","downloads":"0"}, {"date":"13Mar","downloads":"4"}, {"date":"12Mar","downloads":"14"}, {"date":"11Mar","downloads":"5"}, {"date":"10Mar","downloads":"26"}, {"date":"09Mar","downloads":"7"}, {"date":"08Mar","downloads":"5"} ];
注意两个数组之间的区别:
从ajax我得到:
["{"date":"07Apr","downloads":"1"}", "{"date":"06Apr","downloads":"3"}", ...]
虽然有效的是:
[ {"date":"07Apr","downloads":"1"}, {"date":"06Apr","downloads":"3"}, ...]
最后,这就是我发起莫里斯的方式:
Morris.Bar({
element: 'bar-requests',
data: obj,
xkey: ['date'],
ykeys: ['downloads'],
labels: ['downloads']
});
关于如何将我的ajax返回数据提供给Morris的任何想法?
答案 0 :(得分:0)
也许我首先没有正确表达问题,因为我没有得到任何答案,我想简化它并将其作为一个新问题发布(希望我没有违反任何问题)网站rulez)...无论如何,问题解决了!!!
以下是适合我的解决方案: answer by Touhid Alam