简体   繁体   English

使用PHP / JS自动刷新Highcharts

[英]Auto Refreshing Highcharts with PHP/JS

Im attempting to get a highcharts chart to refresh every minute with new data. 我正在尝试获取高图图表,以每分钟刷新一次新数据。 So far I have the chart being created using a php outputting on a header as javascript. 到目前为止,我已经使用php在标头上以javascript输出的方式创建了图表。

I have this at the top of the page: 我在页面顶部有这个:

<?php
    //set the output header as JS
    Header("content-type: application/x-javascript");
    //output start of javascript chart
?>
   var chart;
   chart = new Highcharts.Chart({

Then some PHP to get the data from the database, and output in the highcharts format. 然后一些PHP从数据库中获取数据,并以highcharts格式输出。

I Now have this on my index page calling the graph, it displays, then does destroy the graph, and redisplays the graph but it doesn't seem to re-run the php code: 我现在在索引页面上调用图形,将其显示出来,然后销毁图形,然后重新显示图形,但似乎没有重新运行php代码:

<script>
$(document).ready(function(){
    $.ajaxSetup({
        cache: false,
    });
    $.get("api_dashchart.php?uID=<?php echo $userInfo_ID; ?>");
    var refreshId = setInterval(function(){
        if(chart) chart.destroy();
        $.get("api_dashchart.php?uID=<?php echo $userInfo_ID; ?>");
    }, 45000);
});

Continued help much appreciated. 持续的帮助深表感谢。

I think you are mixing server and client code here, You PHP code will only be executed once on the server, while the page is fetched. 我认为您在这里混合服务器和客户端代码,而在获取页面时 ,PHP代码将仅在服务器上执行一次。

window.setInterval(function(
    displaygraph();
}, 60000);

Cannot update data, it will only re-execute displaygraph(), but the data inside this method is not updated from server, but the data that was fetched during page load will be used, you will need to use AJAX, and make an ajax request to get updated data and then use it to redraw the chart with newly fetched data. 无法更新数据,它将仅重新执行displaygraph(),但此方法中的数据不会从服务器更新,但是将使用在页面加载期间获取的数据,您将需要使用AJAX并制作一个ajax请求获取更新的数据,然后使用它来重新绘制包含新获取的数据的图表。 Try debugging you javascript using Firebug . 尝试使用Firebug调试javascript。
As for the first load issue, you can easily do it with an if statement inside display graph. 至于第一个负载问题,您可以在显示图形中使用if语句轻松完成。

if(chart) chart.destroy();

To sum it up, 把它们加起来,

charting.js charting.js

var chart;
function displaygraph() {
 $.getJSON("getjson.php", function(json) {
  if(chart) chart.destroy();
  //get data from json, and use in chart
  chart = new Highcharts.Chart({....});
});
setInterval(displaygraph, 60000);

getjson.php getjson.php

<?php
  $data = ...
  echo json_encode($data);
?>

More on $.getJSON @ http://api.jquery.com/jQuery.getJSON/ , $.ajax @ http://api.jquery.com/jQuery.ajax/ json-encoding in php @ http://php.net/manual/en/function.json-encode.php 有关$ .getJSON @ http://api.jquery.com/jQuery.getJSON/的更多信息,$ .ajax @ http://api.jquery.com/jQuery.ajax/ php中的json编码@ http:// php .net / manual / en / function.json-encode.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM