2017-08-24 88 views
1

我创建了一个图表,我想实时更新,这就是为什么我使用ajax。我的问题是,如何显示ajax响应?以下是我的图表示例脚本。请帮帮我。非常感谢。如何显示ajax响应

Chart.php -

$(document).ready(function() { 
    Highcharts.chart('container', { 
     chart: { 
      type: 'bar' 
     }, 
     title: { 
      text: "TITLE" 
     }, 
     series: [{ 
      name: 'Present', 
      data: [*//must display the ajax response here//*] 
     }] 
    }); 
}); 

ajax.php

<script> 
function fanc_no(){ 
    $.ajax({ 
     url: "test.php", 
     success: function(result){ 
      $("#container").html(result); 
     } 
    }); 
} 
window.setInterval(function(){ 
    func_no(); 
}, 1000); 
</script> 
+1

[使用Ajax将数据加载到Highcharts中]可能的副本(https://stackoverflow.com/questions/12223972/load-data-into-highcharts-with-ajax) –

回答

0

看看在load功能:现在

chart: { 
    events: { 
     load: function() { 

      // set up the updating of the chart each second 
      var series = this.series[0]; 
      setInterval(function() { 
       var x = (new Date()).getTime(), // current time 
        y = Math.round(Math.random() * 100); 
       series.addPoint([x, y], true, true); 
      }, 1000); 
     } 
    } 
} 

,该setInterval函数内部,你必须调用你的ajax并传递结果如图所示。

看看它the official docs

+0

Hello!有没有其他的条形图选项?这显示为线图。 – COCECS

+0

我想这适用于线条和条形图。如果不尝试下面的答案。 – treolandix

+0

这不起作用。它总是说,这个网页正在放缓网站。 – COCECS