php - 使用 mysql 和 ajax 用 FLOT 绘制图形

标签 php mysql ajax codeigniter flot

我正在尝试使用 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)

目前,您的指标函数在后端请求一个页面,但未对其执行任何操作。

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

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

关于php - 使用 mysql 和 ajax 用 FLOT 绘制图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1820023/

相关文章:

javascript - HTML 表单 : display a text in an input (textarea)

php - PHP 获取第 n 层多维数组中 key 的值

MySQL - 如何处理历史数据?

php - 哪种方式加载MySQL数据库比较好?

php - Mysqli 多查询不更新所有字段

php - 如何使用 Laravel 获取银行中的 BMP 图像并将其显示在 View 中?

php - 如何查看Mysql表有没有变化?

jquery - Bootstrap 弹出窗口 : reload content with ajax

javascript - 在 JavaScript 中向 Microsoft Emotion API 发出 ajax 请求

javascript - 如何从 php 变量数组执行 jquery 自动完成功能