3

flot を使用して、MySQL データベースから取得したデータをプロットしようとしています。私はユーザーのログイン訪問をログに記録しており、特定の月の 1 日あたりの訪問数を取得する 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 ---> 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' }
         });
}
4

2 に答える 2

3

あなたの問題はメトリック関数内にあると思います。(また、あなたは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' }
             });
    }
于 2009-12-02T01:11:28.800 に答える
0

私はそれを機能させましたが、私が望む方法ではありません。私は答えを投稿すると思ったので、他の誰かがこの問題に遭遇した場合、少なくともそれを機能させることができます.

したがって、何らかの理由で、配列がjavacscriptファイルに表示されないというフレームワークのために、javascriptファイルと実際のview(html)ファイルが分離されているためだと思います。問題は関数内にあります

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 ファイルの上部にある 2 つのタグの間の関数の内容をコピーして貼り付けるだけでした。このように見えるはずです。

<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>

フレームワークから離れているため、このソリューションはあまり好きではありませんが、これまでのところ、私が思いついた唯一のソリューションです。

于 2009-12-01T14:49:46.103 に答える