我正在实行行使flot绘制一些从MySQL数据库提取的数据.我是登任命户的登录会见,我有一个SQL函数,它将检索给定月份天天的会见次数,即getStats($day).我已经在网上阅读了一些示例,以相识怎样正确执行此操纵,可是因为某些缘故起因,当我实行对JavaScript文件中的数组数据举办图形绘制时,它会酿成空-> ‘数据:’.下面是我的代码.我正在行使CI框架,因此,假如对我的代码有任何疑问,很也许是由于这个题目.我已经对数据举办了硬编码,而且可以正常事变.在此题目上的任何辅佐将不胜谢谢,今朝关于在数据库中行使flot的内容并不多.
型号-> metrics.php
function showUsers() {
//get the number of days in the current month and the first day
$num_days = cal_days_in_month(CAL_GREGORIAN,date(m),date(Y));
$first_day = strtotime(date('Y').'-'.date('m').'-01');
//iterate through all of the days
while( $i < $num_days ) {
//get the user visits for each day of the month
$log = $this->db->getStats($first_day);
//add +1 day to the current day
$first_day += 86400;
$flot_visits[] = '['.($first_day*1000).','.$log->fields['total'].']';
$i++;
}
//format in acceptable format for flot
$content['visits'] = '['.implode(',',$flot_visits).']';
//load the view and pass the array
$this->load->vars($content);
$this->load->view('metrics/user_metrics',$content);
}
查察—> user_metrics.php
<div id ="visits_graph" style="width:600px; height:300px"></div>
Javascript —> user_metrics.js
function metrics() {
$.ajax({
type: "POST",url: pathurl + "metrics/showUsers",data: "",success: function(data) {
graph_visits();
}
});
}
function graph_visits() {
$.plot($('#visits_graph'),[
{ label: 'Visits',data: <?php echo $visits ?> }
],{
xaxis: {
mode: "time",timeformat: "%m/%d/%y"
},lines: { show: true },points: { show: true },grid: { backgroundColor: #fffaff' }
});
}
最佳谜底
我以为您的题目出在指标成果之内. (我也猜你在用JQuery)
今朝,您的指标函数在后端哀求一个页面,但不执行任何操纵.
>将后端要领showUsers()变动为:
function showUsers() {
//get the number of days in the current month and the first day
$num_days = cal_days_in_month(CAL_GREGORIAN,date(Y));
$first_day = strtotime(date('Y').'-'.date('m').'-01');
//iterate through all of the days
while( $i db->getStats($first_day);
//add +1 day to the current day
$first_day += 86400;
//change this to be a nested array
$flot_visits[] = array( ($first_day*1000),$log->fields['total'] );
$i++;
}
//output javascript array
echo json_encode( $flot_visits );
}
>将user_metrics.js变动为:
function metrics() {
$.getJSON({
pathurl + "metrics/showUsers",function(data) {
//use the returned data
graph_visits(data);
}
});
}
function graph_visits( graphData ) {
$.plot($('#visits_graph'),[
{ label: 'Visits',data: graphData }
],{
xaxis: {
mode: "time",timeformat: "%m/%d/%y"
},grid: { backgroundColor: #fffaff' }
});
}
(编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|