所以我从sails.js获得了这个模型:
var sql = 'SELECT TO_DATE(CAST(LAST_UPDATE AS TEXT),\'YYYY-MM-DD\') AS VIEW_DATE, COUNT(STATISTIK_ID) AS VIEW_COUNT \
FROM STATISTIK \
GROUP BY VIEW_DATE \
ORDER BY VIEW_DATE ASC';
sails.log.debug('Query : ' + sql);
AdsStatistics.query(sql, function(err, data){
if(err){
response({"status": "ERR", "message": err['message']});
}
else{
console.log(data.rows);
response({"status": "OK", "message": "", "data": data.rows});
}
});
以及view.ejs
上的 javascriptvar data = response.data;
var iLen = data.length;
var lineData = [];
var line1=[];
var tempDate = new Date();
for (var i = 0; i < iLen; i++){
tempDate = data[i].view_date;
lineData[i] = "'"+ tempDate.substring(0,10) + "'," + data[i].view_count;
}
line1 = [lineData];
alert(line1); //just to check the data i parsed
console.log(line1); //just to check the data i parsed
var plot2 = $.jqplot('chart1', [line1] , {
title:'Views Statistics',
axes: {
xaxis: {
label: 'Date',
labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
renderer: $.jqplot.DateAxisRenderer,
tickOptions: {formatString:'%#d %b %Y'}
},
yaxis:{
label: 'Views',
labelRenderer: $.jqplot.CanvasAxisLabelRenderer
}
},
series:[{lineWidth:2, markerOptions:{style:'filledCircle'}}]
});
我已经解析了数据,这是来自控制台日志:
这来自Web控制台:
但为什么我仍然没有得到正确的图表?而且我似乎jqplot无法识别我在xaxis上的日期。请帮助我做错了什么?
答案 0 :(得分:0)
我发现我只需要为图轴数据制作一个二维数组。所以我编辑了这样的javascript代码,它完美无缺:
var data = response.data;
var iLen = data.length;
var lineData = [];
for (var i = 0; i < iLen; i++) {
lineData[i] = [];
for(var j = 0; j < 2; j++) {
if(j == 0)
lineData[i][j] = data[i].view_date.substring(0,10);
else
lineData[i][j] = parseInt(data[i].view_count);
}
}
var plot2 = $.jqplot('chart1', [lineData] , {
title:'Views Statistics',
axes: {
xaxis: {
label: 'Date',
labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
renderer: $.jqplot.DateAxisRenderer,
tickOptions: {formatString:'%#d %b %Y'}
},
yaxis:{
label: 'Views',
labelRenderer: $.jqplot.CanvasAxisLabelRenderer
}
}, series:[{lineWidth:2, markerOptions:{style:'filledCircle'}}]
});