2009-11-30 27 views
3

我想用flot來繪製一些從MySQL數據庫中提取的數據。我是日誌用戶登錄訪問,並且我有一個SQL函數,它將檢索給定月份每天的訪問次數getStats($ day)。我已經在線閱讀了一些例子,如何正確地做到這一點,但出於某種原因,當我嘗試在我的javascript文件中繪製數組數據時,它會出現空白 - >'data:'。以下是我的代碼。我使用CI框架,所以如果有關於我的代碼的一些問題,很可能是因爲這個。我有硬編碼數據,它工作正常。對於這個問題的任何幫助,將不勝感激現在沒有太多關於使用數據庫的flot。用FLOT繪圖使用mysql和ajax

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

回答

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> 

我真的不喜歡這個解決方案,因爲它是從框架離斷,但到目前爲止,唯一的解決辦法我已經能夠拿出。

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

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