使用mysql和ajax绘制FLOT图表

3
我正在尝试使用flot绘制从MySQL数据库中提取的一些数据。我记录用户登录访问量,我有一个SQL函数可以检索给定月份每天的访问次数,getStats($day)。我已经在网上阅读了一些正确操作此类操作的示例,但出现问题的是当我在JavaScript文件中尝试以数组形式呈现数据时,“它为空--> 'data:'”。以下是我的代码。我使用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 ---> 用户指标.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' }
         });
}
2个回答

3

我认为你的问题在于metrics函数。(我猜你正在使用JQuery)

目前,你的metrics函数请求后端页面,但什么也不做。

  1. 将后端方法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(m), 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"
                          },
                   lines: { show: true },
                   points: { show: true },
                   grid: { backgroundColor: #fffaff' }
             });
    }

0

我让它工作了,但不是我想要的方式。我想我会发布答案,以便如果其他人遇到此问题,他们至少可以让它工作。

因此,由于JavaScript文件和实际视图(HTML)文件由于框架而分离,所以某种原因导致数组对于javacscript文件不可见。问题在于函数内部。

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' }
     });
}

因为它们是分开的

          <?php echo $visits ?> 

没有返回任何内容。我解决的方法是进入视图,将函数内容在 HTML 文件顶部的两个标签之间复制并粘贴。它应该看起来像这样:

<script language="javascript">


   $.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' }
     });

</script>

我并不是很喜欢这个解决方案,因为它偏离了框架,但到目前为止,这是我能想出的唯一解决方案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接