简体   繁体   English

在Highcharts中自动刷新Json数据

[英]Auto-refresh Json data in Highcharts

I've been working this for past 4 days but still can figure out how to solve this issue: 我已经工作了4天,但仍然可以弄清楚如何解决此问题:

  1. The data in the database changes every 5 minutes. 数据库中的数据每5分钟更改一次。
  2. I want to display the data in a new chart without refreshing the whole page. 我想在不刷新整个页面的情况下在新图表中显示数据。

CODE: 码:

$(function () {
    $(document).ready(function() {
        $.getJSON("test2.php", function(json) {
            Highcharts.setOptions({
                global: {
                    useUTC: false
                }
            });

            $('#container').highcharts({
                renderTo: 'container',
                defaultSeriesType: 'spline',
                chart: {
                    type: 'area',
                },
                title: {
                    text: 'Transaction Count'
                },
                subtitle: {
                    text: '5 Minutes Count'
                },
                exporting: { 
                    enabled: false 
                },
                xAxis: {
                    categories:json,
                    title:{
                        text: 'Time Of The Day',
                        style: {
                            color: '#666666',
                                fontSize: '12px',
                                fontWeight: 'normal'
                            }
                        },
                        tickInterval: 4,
                        type: 'datetime',
                        labels: {
                            style: {
                                fontFamily: 'Tahoma'
                            },
                        }
                    },
                    yAxis: {
                        title: {
                            text: 'Number Of Transaction'
                        },
                        labels: {
                            formatter: function () {
                                return this.value;
                            }
                        }
                    },
                    tooltip: {
                        pointFormat: '{series.name}: <b>{point.y:,.0f}</b>'
                    },
                    plotOptions: {
                        column: {colorByPoint: true},
                        area: {
                            marker: {
                            enabled: false,
                            symbol: 'circle',
                            radius: 5,
                            states: {
                                hover: {
                                    enabled: true
                                }
                            }
                        }
                    }
                },
                colors: [
                    'green',  //Buy
                    '#4572A7' //Paid
                ],
                series: json
            });
        });
    });
});

You can change the dataset, like this : 您可以更改数据集,如下所示:

$('#container').highcharts().series[0].setData([129.2,144.0,...], false);

You can redraw, like this : 您可以像这样重画:

$('#container').highcharts().redraw();

So, what you need to do, is create a function that (1) first gets the data from the server, (2) then updates the data and (3) then redraws, like this : 因此,您需要做的是创建一个函数,该函数(1)首先从服务器获取数据,(2)然后更新数据,(3)然后重绘,如下所示:

var updateChart = function() {
    $.getJSON('test2.json', function(data) {
        var chart = $('#container').highcharts();
        $.each(data, function(pos, serie) {
            chart.series[pos].setData(serie, false);
        });
        chart.redraw();
    });
}

To repeat this every 5 minutes, you can use setInterval , like this : 要每5分钟重复一次,您可以使用setInterval ,如下所示:

setInterval(function() {
    $.getJSON('test2.json', function(data) {
        var chart = $('#container').highcharts();
        $.each(data, function(pos, serie) {
            chart.series[pos].setData(serie, false);
        });
        chart.redraw();
    });
}, 300000);

Instead of putting your Highchart script in the document.ready function, you can create a funcion getHighchart and put the code about into it. 无需将Highchart脚本放入document.ready函数中,而是可以创建一个功能getHighchart并将代码放入其中。 and depend on how many seconds you want the code to readload as below, but you have to be referencing the jquery js. 并取决于您希望代码重新加载多少秒,如下所示,但是您必须引用jquery js。

$(function () {
    setInterval(getHighChart, 30000); //30 seconds onload="getHighChart()" 
});

function getHighChart() {
    //to containe you script for loading the chart
}

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

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