首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Gauge图表实时更新

Google Gauge图表实时更新
EN

Stack Overflow用户
提问于 2013-10-01 17:21:18
回答 1查看 5.5K关注 0票数 1

我一直在尝试实时更新我的谷歌仪表图表。

我的代码如下。

代码语言:javascript
复制
<script type='text/javascript'>
      google.load('visualization', '1', {packages:['gauge']});
      google.setOnLoadCallback(drawChart);
      function drawChart() {

      var json = $.ajax({
                    url: 'graph.php', // make this url point to the data file
                    dataType: 'json',
                    async: false
                }).responseText;
                //alert(json);
        var data = google.visualization.arrayToDataTable(json);
        var options = {
          width: 400, height: 120,
          redFrom: 0, redTo: 3,
          greenFrom:<?php echo $inactivecount['inactive_count']-3;?>, greenTo: <?php echo $inactivecount['inactive_count'];?>,
          minorTicks: 0,
          min:0,
          max:<?php echo $inactivecount['inactive_count'];?>,
          'majorTicks': ["",""],
          'animation.duration':100
        };

        var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
        //setInterval(drawChart(12,10),1000);
        chart.draw(data, options);

        setInterval(drawChart, 1000);
      }
    </script>

Ajax文件如下所示。

代码语言:javascript
复制
$table = array();
$table=array(0=>array('Label','Value'),1=>array('Likes',$like));
// encode the table as JSON
$jsonTable = json_encode($table);

// set up header; first two prevent IE from caching queries
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Oct 2013 05:00:00 GMT');
header('Content-type: application/json');

// return the JSON data
echo $jsonTable;

如果在数据中对json进行硬编码,那么它可以很好地工作,但是当我以相同的json格式从ajax返回json时,它不会显示仪表。

EN

回答 1

Stack Overflow用户

发布于 2013-10-01 22:37:11

首先,在绘图函数结束时调用setInterval(drawChart, 1000);并不是您想要做的事情--这会在每次调用时产生一个新的间隔(在现有间隔之上),因此间隔会呈指数级增长,每秒增加一倍(粗略地说,考虑到AJAX调用和代码的执行时间,这会稍微长一点)。这将很快锁定您的浏览器和/或用传入的请求淹没您的服务器。试着这样做:

代码语言:javascript
复制
function drawChart() {
    var data;
    var options = {
        width: 400,
        height: 120,
        redFrom: 0,
        redTo: 3,
        greenFrom: <?php echo $inactivecount['inactive_count']-3;?>,
        greenTo: <?php echo $inactivecount['inactive_count'];?>,
        minorTicks: 0,
        min: 0,
        max: <?php echo $inactivecount['inactive_count'];?>,
        majorTicks: ["",""],
        animation: {
            duration: 100
        }
    };

    var chart = new google.visualization.Gauge(document.getElementById('chart_div'));

    function refreshData () {
        var json = $.ajax({
            url: 'graph.php', // make this url point to the data file
            dataType: 'json',
            async: false
        }).responseText;

        data = google.visualization.arrayToDataTable(json);

        chart.draw(data, options);
    }

    refreshData();
    setInterval(refreshData, 1000);
}

如果这不起作用,那么在浏览器中转到graph.php并发布它的输出,这样我就可以测试它了。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19112437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档