dtrz17917 2016-07-14 18:43
浏览 54
已采纳

最简单的方法来自动刷新页面中嵌入的Google图表

I have two files, one outputting data from a MySQL database and one drawing a Google Graph within a page with other metrics:

grab_twitter_stats.php Output:

[15, 32], [14, 55], [13, 45], [12, 52], [11, 57], [10, 55], [9, 58], [8, 42], [7, 44], [6, 40], [5, 54], [4, 52], [3, 60], [2, 71], [1, 43],

index.php Output:

<div id="curve_chart" style="width: 900px; height: 500px">

<script type="text/javascript">

google.charts.load('current', {packages: ['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['Minutes', 'Tweets'],

      <?php require('grab_twitter_stats.php');?>

    ]);

    var options = {
      title: 'Tweets in last 15 Minutes',
      curveType: 'function',
      hAxis: {
      title: 'Last 15 Minutes',
      direction: '-1'
      },
      legend: { position: 'bottom' }

    };

    var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

    chart.draw(data, options);

}

</script>   
</div>

This draws a graph that shows tweets in the last 15 minutes. I can get the graph to appear once, but upon trying to load a SetInterval, it does not refresh the Google Graph on the interval. I have tried wrapping the entire drawChart() function in it, and it doesn't seem to be working. I tried using AJAX but it is not formatted in JSON so ajax doesn't like it. Any suggestions on the easiest way to make this graph auto refresh?

  • 写回答

1条回答 默认 最新

  • dqybeh2884 2016-07-14 19:22
    关注

    although not JSON, you can still use ajax, even with plain text

    something like this should get you close...

    google.charts.load('current', {
      callback: function () {
        drawChart();
        setInterval(drawChart, (15 * 60 * 1000));
    
        function drawChart() {
          $.ajax({
            url: 'grab_twitter_stats.php',
            type: 'get',
            success: function (txt) {
              // check for trailing comma
              if (txt.slice(-1) === ',') {
                txt = txt.substring(0, txt.length - 1);
              }
              var txtData = JSON.parse('[["Minutes", "Tweets"],' + txt + ']');
    
              var data = google.visualization.arrayToDataTable(txtData);
    
              var options = {
                title: 'Tweets in last 15 Minutes',
                curveType: 'function',
                hAxis: {
                  title: 'Last 15 Minutes',
                  direction: '-1'
                },
                legend: { position: 'bottom' }
              };
    
              var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
              chart.draw(data, options);
            }
          });
        }
      },
      packages: ['corechart']
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源