2015-06-17 46 views
0

我想在highchart中顯示mysql數據庫。我找到了一個從文檔中獲取數據的例子,但我想使用函數。當我使用功能程序不起作用。函數的輸出與更改前的文檔相同。也許在函數或者我使用它的方式上有一些錯誤。PHP + mysql + highcharts

這是我的代碼:

<? $data = new MysqlDatabase($db) ?> 
<script type="text/javascript"> 
    var chart; 
    $(document).ready(function() { 
     var options = { 
      chart: { 
       renderTo: 'container', 
       defaultSeriesType: 'line', 
       marginRight: 130, 
       marginBottom: 25 
      }, 
      title: { 
       text: 'Date', 
       x: -20 //center 
      }, 
      subtitle: { 
       text: '', 
       x: -20 
      }, 
      xAxis: { 
       type: 'datetime', 
       tickInterval: 3600 * 1000, // one hour 
       tickWidth: 0, 
       gridLineWidth: 1, 
       labels: { 
        align: 'center', 
        x: -3, 
        y: 20, 
        formatter: function() { 
         return Highcharts.dateFormat('%l%p', this.value); 
        } 
       } 
      }, 
      yAxis: { 
       title: { 
        text: 'Humidity' 
       }, 
       plotLines: [{ 
        value: 0, 
        width: 1, 
        color: '#808080' 
       }] 
      }, 
      tooltip: { 
       formatter: function() { 
        return Highcharts.dateFormat('%l%p', this.x-(1000*3600)) +'-'+ Highcharts.dateFormat('%l%p', this.x) +': <b>'+ this.y + '</b>'; 
       } 
      }, 
      legend: { 
       layout: 'vertical', 
       align: 'right', 
       verticalAlign: 'top', 
       x: -10, 
       y: 100, 
       borderWidth: 0 
      }, 
      series: [{ 
       name: 'Count' 
      }] 
     } 
     jQuery.get(<?php $data->find_all_for_charts($db);?>, null, function(tsv) { 
      var lines = []; 
      traffic = []; 
      try { 
       // split the data return into lines and parse them 
       tsv = tsv.split(/\n/g); 
       jQuery.each(tsv, function(i, line) { 
        line = line.split(/\t/); 
        date = Date.parse(line[0] +' UTC'); 
        traffic.push([ 
         date, 
         parseInt(line[1].replace(',', ''), 10) 
        ]); 
       }); 
      } catch (e) { } 
      options.series[0].data = traffic; 
      chart = new Highcharts.Chart(options); 
     }); 
    }); 
</script> 

功能代碼:

<?php 
class MysqlDatabase 
{ 

    public function find_all_for_charts($db) 
    { 
     $stmt = $db->query('SELECT * FROM temperature'); 

     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
      echo $row['timestamp'] . "\t" . $row['humidity'] . "\n"; 
     } 
    } 
} 
?> 

不是函數此示例中使用的文件的名稱:我怎樣才能解決

jQuery.get(<?php $data->find_all_for_charts($db);?>, null, function(tsv) { 

你的問題?提前致謝。 :)

回答

2

查找到jQuery.get()的文檔:此功能的https://api.jquery.com/jquery.get/

第一個參數是GET請求URL。

在你的代碼是從方法的數據:

MysqlDatabase->find_all_for_charts() 

在HighCharts包,你可以找到例子來說明如何使用它。只需從http://www.highcharts.com/download下載它,並尋找:「Ajax加載的數據,可點擊的點」演示。