Highcharts - load and refresh csv

2019-09-14 01:12发布

问题:

I have 2 Charts (using Highcharts) with different data on one page. The data to visualize comes from CSV files. My Code works so far, and the Charts show the CVS content visualized. But: I need to reload the CSV every 5 Minutes, cause new data gets written in these CSV files every 5 Minutes. How do I perform that reload in the best way? My code to generate the 2 Charts:

$.get('chart_1.csv', function(csv) {
 $('#chart_1').highcharts({
    chart: {
        type: 'column'
    },
    data: {
        csv: csv
    }
 });
});

$.get('chart_2.csv', function(csv) {
 $('#chart_2').highcharts({
    chart: {
        type: 'column'
    },
    data: {
        csv: csv
    }
 });
});

回答1:

Use setInterval, put your ajax inside the function.

function function1() {
    //do your AJAX stuff here
}
setInterval(function1, 300000); //300000 MS == 5 minutes

If you want to show the graph on load use this:

$(window).load(function() {
//ajax function here
}

Reference.



回答2:

There is a way to read updated csv files every time. Default, ajax requests use cache. You can change that by using: $.ajaxSetup({ cache: false }); This is the way to do it when using implicit ajax requests like $.get. Otherwise, just add "cache: false" in your ajax statements list.