Php live谷歌图表

ruyhziif  于 2023-01-16  发布在  PHP
关注(0)|答案(2)|浏览(124)

我正在从MySQL的图表工作,我想做它的生活或更新自己每10秒.
我的数据库是关于天气和收集数据通过一个arduino天气盾...我可以显示图表,当我点击该页面,但我想更新数据库中记录的每5秒这是我的代码,希望有人能帮助:

<?php
function tempf($input,$name){
global $conn;

echo ' <script type="text/javascript" src="https://www.google.com/jsapi"></script>
              <script type="text/javascript">
                google.load("visualization", "1", {packages:["corechart"]});
                google.setOnLoadCallback(drawChart);
                function drawChart() {
                  var data = google.visualization.arrayToDataTable([
                    ["Year", "'.$name.'"],';
    $resultValue = mysqli_query($conn, "SELECT * FROM daily ORDER BY timeStamp DESC LIMIT 200 ");
    if(mysqli_num_rows($resultValue)>0){
        while($rowValue = mysqli_fetch_array($resultValue)){
             echo '["'.$rowValue['timeStamp'].'", '.$rowValue[''.$input.''].'],';
        }
    }
     echo ']);

                  var options = {
                    title: "Graph"
                  };

                  var chart = new google.visualization.LineChart(document.getElementById("chart_div"));
                  chart.draw(data, options);
                }
              </script>
              <div id="chart_div" style="width: 900px; height: 500px; margin-left:4.5cm; "></div>';

}

?>

我用这个页面来获取图表的每一列在我的mysql数据库谢谢

ocebsuys

ocebsuys1#

这是非常简单的做到这一点与Javascript无需刷新您的页面,取代您的折线图.draw(数据,选项)如下:

function update(){

  timerId = setTimeout(function () {
    chart.draw(data, options);
  }, 5000);

}

update();

如果你想了解更多关于javascript、jquery和php的网页开发教程,请看:http://www.newvibe.ca/92weblessons

mjqavswn

mjqavswn2#

我创建了数据库,并复制了这个例子。当我追加记录到数据库表,图表不刷新...需要手动刷新浏览器。也许有人可以帮助解决这个问题。任何帮助我很感激。谢谢
'

<?php

tempf("temp", "gra");

?>

    <?php
    function tempf($input, $name){
    //global $conn;
    include('con.php');
    
    echo ' <script type="text/javascript" src="https://www.google.com/jsapi"></script>
                  <script type="text/javascript">
                    google.load("visualization", "1", {packages:["corechart"]});
                    google.setOnLoadCallback(drawChart);
                    function drawChart() {
                      var data = google.visualization.arrayToDataTable([
                        ["Year", "'.$name.'"],';
        $resultValue = mysqli_query($conn, "SELECT * FROM `daily` ORDER BY `timeStamp` DESC LIMIT 200 ");
        if(mysqli_num_rows($resultValue)>0){
            while($rowValue = mysqli_fetch_array($resultValue)){
                 echo '["'.$rowValue['timeStamp'].'", '.$rowValue[''.$input.''].'],';
            }
        }
         echo ']);
    
                      var options = {
                        title: "Graph"
                      };
    
                      var chart = new google.visualization.LineChart(document.getElementById("chart_div"));
                      
                      function update(){
                      console.log("Timer event");
                      timerId = setTimeout(function () {
                      chart.draw(data, options);
                      }, 5000);
                          
                      }
                      
                      update();
                    }
                  </script>
                  <div id="chart_div" style="width: 900px; height: 500px; margin-left:4.5cm; "></div>';
    }
    
    ?>

'

相关问题